Skip to content

Commit

Permalink
move batchMode and failAtAnd at memeber level
Browse files Browse the repository at this point in the history
  • Loading branch information
robfrank committed Dec 19, 2024
1 parent 545573f commit a1902b9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
85 changes: 47 additions & 38 deletions console/src/main/java/com/arcadedb/console/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,20 @@ public class Console {
private long transactionBatchSize = 0L;
protected long currentOperationsInBatch = 0L;
private RemoteServer remoteServer;
private boolean batchMode = false;
private boolean failAtEnd = false;

public Console(final DatabaseInternal database) throws IOException {
this();
this.databaseProxy = database;
}

public Console(boolean batchMode, boolean failAtEnd) throws IOException {
this();
this.batchMode = batchMode;
this.failAtEnd = failAtEnd;
}

public Console() throws IOException {
IntegrationUtils.setRootPath(configuration);
databaseDirectory = configuration.getValueAsString(GlobalConfiguration.SERVER_DATABASE_DIRECTORY);
Expand Down Expand Up @@ -185,11 +193,11 @@ public static void execute(final String[] args) throws IOException {
}
}

final Console console = new Console();
final Console console = new Console(batchMode, failAtEnd);

try {
if (batchMode) {
console.parse(commands.toString(), true, batchMode, failAtEnd);
console.parse(commands.toString(), true);
console.parse("exit", true);
} else {
// INTERACTIVE MODE
Expand Down Expand Up @@ -298,10 +306,12 @@ private void executeSet(final String line) {
final String key = parts[0].trim();
final String value = parts[1].trim();

if ("limit".equalsIgnoreCase(key)) {
switch (key.toLowerCase()) {
case "limit" -> {
limit = Integer.parseInt(value);
outputLine(3, "Set new limit to %d", limit);
} else if ("asyncMode".equalsIgnoreCase(key)) {
}
case "asyncmode" -> {
asyncMode = Boolean.parseBoolean(value);
if (asyncMode) {
// ENABLE ASYNCHRONOUS PARALLEL MODE
Expand All @@ -314,39 +324,47 @@ private void executeSet(final String line) {
});
}
outputLine(3, "Set asyncMode to %s", asyncMode);
} else if ("transactionBatchSize".equalsIgnoreCase(key)) {
}
case "transactionbatchsize" -> {
transactionBatchSize = Integer.parseInt(value);
outputLine(3, "Set new transactionBatch to %d", transactionBatchSize);
} else if ("language".equalsIgnoreCase(key)) {
}
case "language" -> {
language = value;
outputLine(3, "Set language to %s", language);
} else if ("expandResultSet".equalsIgnoreCase(key)) {
}
case "expandresultset" -> {
expandResultSet = value.equalsIgnoreCase("true");
outputLine(3, "Set expanded result set to %s", expandResultSet);
} else if ("maxMultiValueEntries".equalsIgnoreCase(key)) {
}
case "maxmultivalueentries" -> {
maxMultiValueEntries = Integer.parseInt(value);
outputLine(3, "Set maximum multi value entries to %d", maxMultiValueEntries);
} else if ("verbose".equalsIgnoreCase(key)) {
}
case "verbose" -> {
verboseLevel = Integer.parseInt(value);
outputLine(3, "Set verbose level to %d", verboseLevel);
} else if ("maxWidth".equalsIgnoreCase(key)) {
}
case "maxwidth" -> {
maxWidth = Integer.parseInt(value);
outputLine(3, "Set maximum width to %d", maxWidth);
} else {
}
default -> {
if (!setGlobalConfiguration(key, value, false))
outputLine(3, "Setting '%s' is not supported by the console", key);
}
}

flushOutput();
}

private void executeTransactionStatus() {
checkDatabaseIsOpen();

if (databaseProxy instanceof DatabaseInternal) {
final TransactionContext tx = ((DatabaseInternal) databaseProxy).getTransaction();
if (databaseProxy instanceof DatabaseInternal db) {
final TransactionContext tx = db.getTransaction();
if (tx.isActive()) {
final ResultInternal row = new ResultInternal((Database) databaseProxy);
final ResultInternal row = new ResultInternal(db);
row.setPropertiesFromMap(tx.getStats());
printRecord(row);

Expand Down Expand Up @@ -561,15 +579,14 @@ else if (rec != null)

final List<TableFormatter.TableRow> resultSet = new ArrayList<>();

Object value;
for (final String fieldName : currentRecord.getPropertyNames()) {
value = currentRecord.getProperty(fieldName);
if (value instanceof byte[])
value = "byte[" + ((byte[]) value).length + "]";
else if (value instanceof Iterator<?>) {
Object value = currentRecord.getProperty(fieldName);
if (value instanceof byte[] bytes)
value = "byte[" + bytes.length + "]";
else if (value instanceof Iterator<?> iterator) {
final List<Object> coll = new ArrayList<>();
while (((Iterator<?>) value).hasNext())
coll.add(((Iterator<?>) value).next());
while (iterator.hasNext())
coll.add(iterator.next());
value = coll;
} else if (MultiValue.isMultiValue(value)) {
value = TableFormatter.getPrettyFieldMultiValue(MultiValue.getMultiValueIterator(value), maxMultiValueEntries);
Expand Down Expand Up @@ -608,14 +625,17 @@ public void onError(Exception exception) {
outputError(exception);
}
});
} else
} else {
try {
resultSet = databaseProxy.command(language, line);
} catch (Exception e) {
// outputError(e);
// return;
throw e;
if (batchMode && !failAtEnd)
throw e;
else
outputError(e);
return;
}
}

if (transactionBatchSize > 0) {
++currentOperationsInBatch;
Expand Down Expand Up @@ -736,16 +756,11 @@ private void executeLoad(final String fileName) throws IOException {
flushOutput();
}

public boolean parse(final String line) throws IOException, RuntimeException {
public boolean parse(final String line) throws IOException {
return parse(line, false);
}

public boolean parse(final String line, final boolean printCommand) throws IOException, RuntimeException {
return parse(line, printCommand, false, false);
}

public boolean parse(final String line, final boolean printCommand, final boolean batchMode, boolean failAtEnd)
throws IOException, RuntimeException {
public boolean parse(final String line, final boolean printCommand) throws IOException {

final ParsedLine parsedLine = parser.parse(line, 0);

Expand All @@ -765,14 +780,8 @@ public boolean parse(final String line, final boolean printCommand, final boolea
throw e;
}
} else {
try {
if (!execute(w))
return false;
} catch (final Exception e) {
return true;

// throw e;
}

}
}
Expand Down
20 changes: 19 additions & 1 deletion console/src/test/java/com/arcadedb/console/ConsoleBatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,37 @@ public void batchMode() throws IOException {

@Test
public void batchModeWithError() throws IOException {

// This should fail
assertThatThrownBy(() -> Console.execute(
new String[] { "-b", """
create database console;
create vertex table WRONG_STATEMENT;
create vertex type ConsoleOnlyVertex;
""" }))
.isInstanceOf(CommandSQLParsingException.class);

final Database db = new DatabaseFactory("./target/databases/console").open();
// the ConsoleOnlyVertex should not be created
assertThat(db.getSchema().existsType("ConsoleOnlyVertex")).isFalse();
db.drop();
}

@Test
public void batchModeWithFailAtEnd() throws IOException {
// Error is only printed out
Console.execute(
new String[] { "-b", "-fae", """
create database console;
create vertex table WRONG_STATEMENT;
create vertex type ConsoleOnlyVertex;
""" });

final Database db = new DatabaseFactory("./target/databases/console").open();
// the ConsoleOnlyVertex is created
assertThat(db.getSchema().existsType("ConsoleOnlyVertex")).isTrue();
db.drop();
}

@Test
public void interactiveMode() throws IOException {
Console.execute(new String[] { "create database console; create vertex type ConsoleOnlyVertex;exit" });
Expand Down

0 comments on commit a1902b9

Please sign in to comment.