Skip to content

Commit

Permalink
fix: added database object into Result for a proper conversion from d…
Browse files Browse the repository at this point in the history
…ates using database's format
  • Loading branch information
lvca committed Apr 2, 2024
1 parent cdd7ccb commit 04b26cf
Show file tree
Hide file tree
Showing 64 changed files with 264 additions and 220 deletions.
10 changes: 5 additions & 5 deletions console/src/main/java/com/arcadedb/console/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private void executeTransactionStatus() {
if (databaseProxy instanceof DatabaseInternal) {
final TransactionContext tx = ((DatabaseInternal) databaseProxy).getTransaction();
if (tx.isActive()) {
final ResultInternal row = new ResultInternal();
final ResultInternal row = new ResultInternal((Database) databaseProxy);
row.setPropertiesFromMap(tx.getStats());
printRecord(row);

Expand Down Expand Up @@ -467,7 +467,7 @@ private void executeCreateUser(final String params) {
checkHasSpaces("User name", userName);

final String password;
Map<String,String> databases = new HashMap<String,String>();
Map<String, String> databases = new HashMap<String, String>();

if (databasesByPos > -1) {
password = params.substring(identifiedByPos + "IDENTIFIED BY".length() + 1, databasesByPos).trim();
Expand All @@ -477,11 +477,11 @@ private void executeCreateUser(final String params) {
for (final String db : databasesName) {
final int colonPos = db.indexOf(":");
if (colonPos > -1) {
final String dbname = db.substring(0,colonPos -1).trim();
final String dbname = db.substring(0, colonPos - 1).trim();
final String dbgroup = db.substring(colonPos + 1).trim();
databases.put(dbname,dbgroup);
databases.put(dbname, dbgroup);
} else {
databases.put(db,"admin");
databases.put(db, "admin");
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ protected PolyglotQueryEngine(final DatabaseInternal database, final String lang
this.language = language;
this.database = database;
this.allowedPackages = allowedPackages;
this.polyglotEngine = GraalPolyglotEngine.newBuilder(database, Engine.create()).setLanguage(language).setAllowedPackages(allowedPackages).build();
this.polyglotEngine = GraalPolyglotEngine.newBuilder(database, Engine.create()).setLanguage(language)
.setAllowedPackages(allowedPackages).build();
this.userCodeExecutorQueue = new ArrayBlockingQueue<>(10000);
this.userCodeExecutor = new ThreadPoolExecutor(8, 8, 30, TimeUnit.SECONDS, userCodeExecutorQueue, new ThreadPoolExecutor.CallerRunsPolicy());
this.userCodeExecutor = new ThreadPoolExecutor(8, 8, 30, TimeUnit.SECONDS, userCodeExecutorQueue,
new ThreadPoolExecutor.CallerRunsPolicy());
this.timeout = database.getConfiguration().getValueAsLong(GlobalConfiguration.POLYGLOT_COMMAND_TIMEOUT);
}

Expand All @@ -99,7 +101,8 @@ public String getLanguage() {
public ResultSet command(final String query, ContextConfiguration configuration, final Object... parameters) {
if (parameters == null || parameters.length == 0)
return command(query, configuration, (Map) null);
throw new UnsupportedOperationException("Execution of a command with positional parameter is not supported for polyglot engine");
throw new UnsupportedOperationException(
"Execution of a command with positional parameter is not supported for polyglot engine");
}

@Override
Expand Down Expand Up @@ -153,7 +156,7 @@ else if (result.fitsInFloat())
// UNKNOWN OR NOT SUPPORTED
value = null;

resultSet.add(new ResultInternal().setProperty("value", value));
resultSet.add(new ResultInternal(database).setProperty("value", value));
return resultSet;
}

Expand Down Expand Up @@ -185,7 +188,8 @@ public QueryEngine registerFunctions(final String function) {

@Override
public QueryEngine unregisterFunctions() {
this.polyglotEngine = GraalPolyglotEngine.newBuilder(database, Engine.create()).setLanguage(language).setAllowedPackages(allowedPackages).build();
this.polyglotEngine = GraalPolyglotEngine.newBuilder(database, Engine.create()).setLanguage(language)
.setAllowedPackages(allowedPackages).build();
return this;
}

Expand All @@ -212,12 +216,14 @@ public AnalyzedQuery analyze(final String query) {

@Override
public ResultSet query(final String query, ContextConfiguration configuration, final Map<String, Object> parameters) {
throw new UnsupportedOperationException("Execution of a query (idempotent) is not supported for polyglot engine. Use command instead");
throw new UnsupportedOperationException(
"Execution of a query (idempotent) is not supported for polyglot engine. Use command instead");
}

@Override
public ResultSet query(final String query, ContextConfiguration configuration, final Object... parameters) {
throw new UnsupportedOperationException("Execution of a query (idempotent) is not supported for polyglot engine. Use command instead");
throw new UnsupportedOperationException(
"Execution of a query (idempotent) is not supported for polyglot engine. Use command instead");
}

@Override
Expand Down Expand Up @@ -254,7 +260,7 @@ else if (o instanceof Identifiable)
else if (o instanceof Map)
return new ResultInternal((Map) o);

return new ResultInternal().setProperty("value", o);
return new ResultInternal(database).setProperty("value", o);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,15 @@ private void aggregate(final Result next, final CommandContext context) {
}
ResultInternal preAggr = aggregateResults.get(key);
if (preAggr == null) {
if (limit > 0 && aggregateResults.size() > limit) {
if (limit > 0 && aggregateResults.size() > limit)
return;
}
preAggr = new ResultInternal();

preAggr = new ResultInternal(context.getDatabase());

for (final ProjectionItem proj : this.projection.getItems()) {
final String alias = proj.getProjectionAlias().getStringValue();
if (!proj.isAggregate(context)) {
if (!proj.isAggregate(context))
preAggr.setProperty(alias, proj.execute(next, context));
}
}
aggregateResults.put(key, preAggr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void buildNextRecord() {
nextRecord = null;
return;
}
nextRecord = new ResultInternal();
nextRecord = new ResultInternal(context.getDatabase());

for (int i = 0; i < this.currentTuple.size(); i++) {
final Result res = this.currentTuple.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ public Result next() {
if (result instanceof ResultInternal) {
((ResultInternal) result).setElement(result.getElement().get());
} else {
final ResultInternal r = new ResultInternal();
final ResultInternal r = new ResultInternal(context.getDatabase());
r.setElement(result.getElement().get());
result = r;
}
} else {
} else
throw new CommandExecutionException("Current element is not a " + clsName + ": " + result);
}

return result;
} finally {
if( context.isProfiling() ) {
if (context.isProfiling())
cost += (System.nanoTime() - begin);
}
}
}

Expand All @@ -85,7 +84,7 @@ public void close() {
@Override
public String prettyPrint(final int depth, final int indent) {
String result = ExecutionStepInternal.getIndent(depth, indent) + "+ CAST TO " + clsName.toUpperCase(Locale.ENGLISH);
if( context.isProfiling() ) {
if (context.isProfiling()) {
result += " (" + getCostFormatted() + ")";
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ public Result next() {
try {

if (result instanceof UpdatableResult) {
final ResultInternal prevValue = new ResultInternal();
final ResultInternal prevValue = new ResultInternal(context.getDatabase());
final Record rec = result.getElement().get().getRecord();
prevValue.setProperty("@rid", rec.getIdentity());
if (rec instanceof Document) {
if (rec instanceof Document)
prevValue.setProperty("@type", ((Document) rec).getTypeName());
}
for (final String propName : result.getPropertyNames()) {

for (final String propName : result.getPropertyNames())
prevValue.setProperty(propName, result.getProperty(propName));
}

((UpdatableResult) result).previousValue = prevValue;
} else {
throw new CommandExecutionException("Cannot fetch previous value: " + result);
}
return result;
} finally {
if( context.isProfiling() ) {
if (context.isProfiling()) {
cost += (System.nanoTime() - begin);
}
}
Expand All @@ -87,7 +87,7 @@ public String prettyPrint(final int depth, final int indent) {
final StringBuilder result = new StringBuilder();
result.append(spaces);
result.append("+ COPY RECORD CONTENT BEFORE UPDATE");
if( context.isProfiling() ) {
if (context.isProfiling()) {
result.append(" (").append(getCostFormatted()).append(")");
}
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public boolean hasNext() {

@Override
public Result next() {
if (executed) {
if (executed)
throw new NoSuchElementException();
}

final long begin = context.isProfiling() ? System.nanoTime() : 0;
try {
final Index idx = context.getDatabase().getSchema().getIndexByName(target.getIndexName());
final long size = idx.countEntries();
executed = true;
final ResultInternal result = new ResultInternal();
final ResultInternal result = new ResultInternal(context.getDatabase());
result.setProperty(alias, size);
return result;
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ else if (targetName.startsWith("`") && targetName.endsWith("`"))

final long size = context.getDatabase().countType(targetName, true);
executed = true;
final ResultInternal result = new ResultInternal();
final ResultInternal result = new ResultInternal(context.getDatabase());
result.setProperty(alias, size);
return result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ResultSet syncPull(final CommandContext context, final int nRecords) thro
if (executed)
return new InternalResultSet();

final ResultInternal resultRecord = new ResultInternal();
final ResultInternal resultRecord = new ResultInternal(context.getDatabase());
executed = true;
long count = 0;
while (true) {
Expand All @@ -57,7 +57,7 @@ public ResultSet syncPull(final CommandContext context, final int nRecords) thro
result.add(resultRecord);
return result;
} finally {
if( context.isProfiling() ) {
if (context.isProfiling()) {
cost += (System.nanoTime() - begin);
}
}
Expand All @@ -75,7 +75,7 @@ public String prettyPrint(final int depth, final int indent) {
final StringBuilder result = new StringBuilder();
result.append(spaces);
result.append("+ COUNT");
if( context.isProfiling() ) {
if (context.isProfiling()) {
result.append(" (").append(getCostFormatted()).append(")");
}
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public String prettyPrint(final int depth, final int indent) {

@Override
public Result toResult() {
final ResultInternal result = new ResultInternal();
final ResultInternal result = new ResultInternal(context.getDatabase());
result.setProperty("type", "DDLExecutionPlan");
result.setProperty(JAVA_TYPE, getClass().getName());
result.setProperty("stmText", statement.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ public class DeleteFromIndexStep extends AbstractExecutionStep {
private boolean inited = false;
private IndexCursor cursor;

public DeleteFromIndexStep(final RangeIndex index, final BooleanExpression condition, final BinaryCondition additionalRangeCondition,
public DeleteFromIndexStep(final RangeIndex index, final BooleanExpression condition,
final BinaryCondition additionalRangeCondition,
final BooleanExpression ridCondition, final CommandContext context) {
this(index, condition, additionalRangeCondition, ridCondition, true, context);
}

public DeleteFromIndexStep(final RangeIndex index, final BooleanExpression condition, final BinaryCondition additionalRangeCondition,
public DeleteFromIndexStep(final RangeIndex index, final BooleanExpression condition,
final BinaryCondition additionalRangeCondition,
final BooleanExpression ridCondition, final boolean orderAsc, final CommandContext context) {
super(context);
this.index = index;
Expand Down Expand Up @@ -89,21 +91,20 @@ public boolean hasNext() {
public Result next() {
final long begin = context.isProfiling() ? System.nanoTime() : 0;
try {
if (!hasNext()) {
if (!hasNext())
throw new NoSuchElementException();
}

final Pair<Object, Identifiable> entry = nextEntry;
final ResultInternal result = new ResultInternal();
final ResultInternal result = new ResultInternal(context.getDatabase());
final Identifiable value = entry.getSecond();

index.remove(new Object[] { entry.getFirst() }, value);
localCount++;
nextEntry = loadNextEntry(context);
return result;
} finally {
if( context.isProfiling() ) {
if (context.isProfiling())
cost += (System.nanoTime() - begin);
}
}
}

Expand All @@ -122,7 +123,7 @@ private synchronized void init() {
} catch (final IOException e) {
e.printStackTrace();
} finally {
if( context.isProfiling() ) {
if (context.isProfiling()) {
cost += (System.nanoTime() - begin);
}
}
Expand All @@ -133,14 +134,14 @@ private Pair<Object, Identifiable> loadNextEntry(final CommandContext commandCon
final Object value = cursor.next();

final Pair<Object, Identifiable> result = new Pair(cursor.getKeys(), value);
if (ridCondition == null) {
if (ridCondition == null)
return result;
}
final ResultInternal res = new ResultInternal();

final ResultInternal res = new ResultInternal(context.getDatabase());
res.setProperty("rid", result.getSecond());
if (ridCondition.evaluate(res, commandContext)) {
if (ridCondition.evaluate(res, commandContext))
return result;
}

}
return null;
}
Expand Down Expand Up @@ -174,7 +175,8 @@ private void processFlatIteration() {
cursor = index.iterator(isOrderAsc());
}

private void init(final PCollection fromKey, final boolean fromKeyIncluded, final PCollection toKey, final boolean toKeyIncluded) {
private void init(final PCollection fromKey, final boolean fromKeyIncluded, final PCollection toKey,
final boolean toKeyIncluded) {
final Object secondValue = fromKey.execute((Result) null, context);
final Object thirdValue = toKey.execute((Result) null, context);

Expand Down Expand Up @@ -347,12 +349,14 @@ private boolean indexKeyToIncluded(final AndBlock keyCondition, final BinaryCond
@Override
public String prettyPrint(final int depth, final int indent) {
String result = ExecutionStepInternal.getIndent(depth, indent) + "+ DELETE FROM INDEX " + index.getName();
if( context.isProfiling() ) {
if (context.isProfiling()) {
result += " (" + getCostFormatted() + ")";
}
result += (condition == null ?
"" :
("\n" + ExecutionStepInternal.getIndent(depth, indent) + " " + condition + (additional == null ? "" : " and " + additional)));
("\n" + ExecutionStepInternal.getIndent(depth, indent) + " " + condition + (additional == null ?
"" :
" and " + additional)));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public Result next() {

if (served < size) {
served++;
final ResultInternal result = new ResultInternal();
final ResultInternal result = new ResultInternal(context.getDatabase());
context.setVariable("current", result);
return result;
}
throw new NoSuchElementException();
} finally {
if( context.isProfiling() ) {
if (context.isProfiling()) {
cost += (System.nanoTime() - begin);
}
}
Expand All @@ -73,7 +73,7 @@ public void reset() {
public String prettyPrint(final int depth, final int indent) {
final String spaces = ExecutionStepInternal.getIndent(depth, indent);
String result = spaces + "+ GENERATE " + size + " EMPTY " + (size == 1 ? "RECORD" : "RECORDS");
if( context.isProfiling() ) {
if (context.isProfiling()) {
result += " (" + getCostFormatted() + ")";
}
return result;
Expand Down
Loading

0 comments on commit 04b26cf

Please sign in to comment.