Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.logging.*;
import java.util.stream.*;

public class EmbeddedDatabase extends RWLockContext implements DatabaseInternal {
public static final int EDGE_LIST_INITIAL_CHUNK_SIZE = 64;
Expand Down Expand Up @@ -1696,14 +1695,13 @@ private void setDefaultValues(Record record) {
private ResultSet executeInternal(final List<Statement> statements, final CommandContext scriptContext) {
ScriptExecutionPlan plan = new ScriptExecutionPlan(scriptContext);

plan.setStatement(statements.stream().map(Statement::toString).collect(Collectors.joining(";")));
plan.setStatements(statements);

List<Statement> lastRetryBlock = new ArrayList<>();
int nestedTxLevel = 0;

for (Statement stm : statements) {
if (stm.getOriginalStatement() == null)
stm.setOriginalStatement(stm.toString());
stm.setOriginalStatement(stm);

if (stm instanceof BeginStatement)
nestedTxLevel++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ public CommandContext setDatabase(final Database database) {
return this;
}

/**
* TODO OPTIMIZE THIS
*/
public static int getLowerIndexOf(final String iText, final int iBeginOffset, final String... iToSearch) {
int lowest = -1;
for (String toSearch : iToSearch) {
Expand Down Expand Up @@ -401,14 +404,17 @@ public static int getLowerIndexOf(final String iText, final int iBeginOffset, fi
return lowest;
}

/**
* TODO: optimize this
*/
public static int getHigherIndexOf(final String iText, final int iBeginOffset, final String... iToSearch) {
int lowest = -1;
int highest = -1;
for (String toSearch : iToSearch) {
int index = iText.indexOf(toSearch, iBeginOffset);
if (index > -1 && (lowest == -1 || index > lowest))
lowest = index;
if (index > -1 && (highest == -1 || index > highest))
highest = index;
}
return lowest;
return highest;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public Result next() {
Result result = nextItem;
nextItem = null;
fetched++;
ctx.setVariable("$current", result);
ctx.setVariable("current", result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Result next() {
if (served < size) {
served++;
ResultInternal result = new ResultInternal();
ctx.setVariable("$current", result);
ctx.setVariable("current", result);
return result;
}
throw new IllegalStateException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Result next() {
if (currentResultSet != null && currentResultSet.hasNext()) {
totDispatched++;
Result result = currentResultSet.next();
ctx.setVariable("$current", result);
ctx.setVariable("current", result);
return result;
} else {
if (currentStep >= getSubSteps().size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ record = iterator.next();
nFetched++;
ResultInternal result = new ResultInternal();
result.element = (Document) record;
ctx.setVariable("$current", result);
ctx.setVariable("current", result);
return result;
} finally {
if (profilingEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Result next() {
ResultInternal result = new ResultInternal();
result.setProperty("key", key);
result.setProperty("rid", value);
ctx.setVariable("$current", result);
ctx.setVariable("current", result);
return result;
} finally {
if (profilingEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
package com.arcadedb.query.sql.executor;

import com.arcadedb.query.sql.parser.Statement;

import java.util.*;

/**
* Created by luigidellaquila on 06/07/16.
*/
Expand Down Expand Up @@ -59,6 +63,6 @@ default String getStatement() {
return null;
}

default void setStatement(String stm) {
default void setStatements(List<Statement> stm) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,32 +175,32 @@ public Object next() {
}

public void fetchNext() {
Object previousMatch = iCommandContext.getVariable("$currentMatch");
Object previousMatch = iCommandContext.getVariable("currentMatch");
ResultInternal matched = (ResultInternal) iCommandContext.getVariable("matched");
if (matched != null) {
matched.setProperty(getStartingPointAlias(), sourceRecord.getProperty(getStartingPointAlias()));
}
while (iter.hasNext()) {
ResultInternal next = iter.next();
Document elem = next.toElement();
iCommandContext.setVariable("$currentMatch", elem);
iCommandContext.setVariable("currentMatch", elem);
if (matchesFilters(iCommandContext, theFilter, elem) && matchesClass(iCommandContext, theClassName, elem) && matchesCluster(iCommandContext,
theClusterId, elem) && matchesRid(iCommandContext, theTargetRid, elem)) {
nextElement = next;
break;
}
}
iCommandContext.setVariable("$currentMatch", previousMatch);
iCommandContext.setVariable("currentMatch", previousMatch);
}
};
};

} else { // in this case also zero level (starting point) is considered and traversal depth is
// given by the while condition
result = new ArrayList<>();
iCommandContext.setVariable("$depth", depth);
Object previousMatch = iCommandContext.getVariable("$currentMatch");
iCommandContext.setVariable("$currentMatch", startingPoint);
iCommandContext.setVariable("depth", depth);
Object previousMatch = iCommandContext.getVariable("currentMatch");
iCommandContext.setVariable("currentMatch", startingPoint);

if (matchesFilters(iCommandContext, filter, startingPoint) && matchesClass(iCommandContext, className, startingPoint) && matchesCluster(iCommandContext,
clusterId, startingPoint) && matchesRid(iCommandContext, targetRid, startingPoint)) {
Expand Down Expand Up @@ -241,7 +241,7 @@ public void fetchNext() {
}
}
}
iCommandContext.setVariable("$currentMatch", previousMatch);
iCommandContext.setVariable("currentMatch", previousMatch);
}
return result;
}
Expand Down Expand Up @@ -334,13 +334,13 @@ protected Iterable<ResultInternal> traversePatternEdge(Identifiable startingPoin
}
}

Object prevCurrent = iCommandContext.getVariable("$current");
iCommandContext.setVariable("$current", startingPoint);
Object prevCurrent = iCommandContext.getVariable("current");
iCommandContext.setVariable("current", startingPoint);
Object qR;
try {
qR = this.item.getMethod().execute(startingPoint, possibleResults, iCommandContext);
} finally {
iCommandContext.setVariable("$current", prevCurrent);
iCommandContext.setVariable("current", prevCurrent);
}

if (qR == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ protected Iterable<ResultInternal> traversePatternEdge(Identifiable startingPoin
}
}

Object prevCurrent = iCommandContext.getVariable("$current");
iCommandContext.setVariable("$current", startingPoint);
Object prevCurrent = iCommandContext.getVariable("current");
iCommandContext.setVariable("current", startingPoint);
Object qR;
try {
// TODO check possible results!
qR = ((FieldMatchPathItem) this.item).getExp().execute(startingPoint, iCommandContext);
} finally {
iCommandContext.setVariable("$current", prevCurrent);
iCommandContext.setVariable("current", prevCurrent);
}

if (qR == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public Result next() {
} else {
result.setProperty(getAlias(), subResultSet.next());
}
ctx.setVariable("$matched", result);
ctx.setVariable("matched", result);
currentCount++;
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected Iterable<ResultInternal> traversePatternEdge(Identifiable startingPoin
List<Object> nextStep = new ArrayList<>();
nextStep.add(startingPoint);

Object oldCurrent = iCommandContext.getVariable("$current");
Object oldCurrent = iCommandContext.getVariable("current");
for (MatchPathItem sub : item.getItems()) {
List<ResultInternal> rightSide = new ArrayList<>();
for (Object o : nextStep) {
Expand All @@ -78,7 +78,7 @@ protected Iterable<ResultInternal> traversePatternEdge(Identifiable startingPoin
subtraverser.executeTraversal(iCommandContext, sub, (Identifiable) current, 0, null).forEach(x -> rightSide.add(x));

} else {
iCommandContext.setVariable("$current", o);
iCommandContext.setVariable("current", o);
Object nextSteps = method.execute(o, possibleResults, iCommandContext);
if (nextSteps instanceof Collection) {
((Collection) nextSteps).stream().map(x -> toOResultInternal(x)).filter(Objects::nonNull)
Expand Down Expand Up @@ -109,7 +109,7 @@ protected Iterable<ResultInternal> traversePatternEdge(Identifiable startingPoin
result = rightSide;
}

iCommandContext.setVariable("$current", oldCurrent);
iCommandContext.setVariable("current", oldCurrent);
// return (qR instanceof Iterable) ? (Iterable) qR : Collections.singleton((PIdentifiable) qR);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Result next() {
Result result = nextResult;
fetchNext(ctx, nRecords);
localCount++;
ctx.setVariable("$matched", result);
ctx.setVariable("matched", result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public boolean hasNext() {
@Override
public Result next() {
Result item = parentRs.next();
Object oldCurrent = ctx.getVariable("$current");
ctx.setVariable("$current", item);
Object oldCurrent = ctx.getVariable("current");
ctx.setVariable("current", item);
Result result = calculateProjections(ctx, item);
ctx.setVariable("$current", oldCurrent);
ctx.setVariable("current", oldCurrent);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,6 @@ public void setMetadata(final String key, final Object value) {
metadata.put(key, value);
}

public void clearMetadata() {
metadata = null;
}

public void removeMetadata(final String key) {
if (key == null || metadata == null)
return;

metadata.remove(key);
}

public void addMetadata(final Map<String, Object> values) {
if (values == null)
return;

if (this.metadata == null)
this.metadata = new HashMap<>();

this.metadata.putAll(values);
}

@Override
public Set<String> getMetadataKeys() {
return metadata == null ? Collections.emptySet() : metadata.keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* Created by luigidellaquila on 08/08/16.
*/

import com.arcadedb.query.sql.parser.Statement;

import java.util.*;
import java.util.stream.*;

Expand All @@ -35,14 +37,15 @@ public class ScriptExecutionPlan implements InternalExecutionPlan {
protected List<ScriptLineStep> steps = new ArrayList<>();
private ExecutionStepInternal lastStep = null;
private ResultSet finalResult = null;
private String statement;
private List<Statement> statements;
private String statementAsString;

public ScriptExecutionPlan(CommandContext ctx) {
public ScriptExecutionPlan(final CommandContext ctx) {
this.ctx = ctx;
}

@Override
public void reset(CommandContext ctx) {
public void reset(final CommandContext ctx) {
// TODO
throw new UnsupportedOperationException();
}
Expand All @@ -53,7 +56,7 @@ public void close() {
}

@Override
public ResultSet fetchNext(int n) {
public ResultSet fetchNext(final int n) {
doExecute(n);
return new ResultSet() {

Expand Down Expand Up @@ -88,7 +91,7 @@ public Map<String, Long> getQueryStats() {
};
}

private void doExecute(int n) {
private void doExecute(final int n) {
if (!executed) {
executeUntilReturn();
executed = true;
Expand All @@ -109,10 +112,10 @@ private void doExecute(int n) {
}

@Override
public String prettyPrint(int depth, int indent) {
StringBuilder result = new StringBuilder();
public String prettyPrint(final int depth, final int indent) {
final StringBuilder result = new StringBuilder();
for (int i = 0; i < steps.size(); i++) {
ExecutionStepInternal step = steps.get(i);
final ExecutionStepInternal step = steps.get(i);
result.append(step.prettyPrint(depth, indent));
if (i < steps.size() - 1) {
result.append("\n");
Expand All @@ -121,9 +124,9 @@ public String prettyPrint(int depth, int indent) {
return result.toString();
}

public void chain(InternalExecutionPlan nextPlan, boolean profilingEnabled) {
ScriptLineStep lastStep = steps.size() == 0 ? null : steps.get(steps.size() - 1);
ScriptLineStep nextStep = new ScriptLineStep(nextPlan, ctx, profilingEnabled);
public void chain(final InternalExecutionPlan nextPlan, final boolean profilingEnabled) {
final ScriptLineStep lastStep = steps.size() == 0 ? null : steps.get(steps.size() - 1);
final ScriptLineStep nextStep = new ScriptLineStep(nextPlan, ctx, profilingEnabled);
if (lastStep != null) {
lastStep.setNext(nextStep);
nextStep.setPrevious(lastStep);
Expand All @@ -138,13 +141,13 @@ public List<ExecutionStep> getSteps() {
return (List) steps;
}

public void setSteps(List<ExecutionStepInternal> steps) {
public void setSteps(final List<ExecutionStepInternal> steps) {
this.steps = (List) steps;
}

@Override
public Result toResult() {
ResultInternal result = new ResultInternal();
final ResultInternal result = new ResultInternal();
result.setProperty("type", "ScriptExecutionPlan");
result.setProperty("javaType", getClass().getName());
result.setProperty("cost", getCost());
Expand Down Expand Up @@ -237,11 +240,13 @@ public ExecutionStepInternal executeFull() {

@Override
public String getStatement() {
return statement;
if (statementAsString == null)
statementAsString = statements.stream().map(Statement::toString).collect(Collectors.joining(";"));
return statementAsString;
}

@Override
public void setStatement(String statement) {
this.statement = statement;
public void setStatements(final List<Statement> statements) {
this.statements = statements;
}
}
Loading