Skip to content

Commit

Permalink
refactor:make sure that when a command context is created it receive …
Browse files Browse the repository at this point in the history
…a database
  • Loading branch information
tglman committed Jul 11, 2024
1 parent 2d06f8d commit d36e284
Show file tree
Hide file tree
Showing 53 changed files with 121 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public String getFetchPlan() {
}

public OCommandContext getContext() {
if (context == null) context = new OBasicCommandContext();
if (context == null) {
context = new OBasicCommandContext(ODatabaseRecordThreadLocal.instance().getIfDefined());
}
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.orientechnologies.common.listener.OProgressListener;
import com.orientechnologies.orient.core.command.OCommandContext.TIMEOUT_STRATEGY;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.replication.OAsyncReplicationError;
import com.orientechnologies.orient.core.replication.OAsyncReplicationOk;
Expand Down Expand Up @@ -183,7 +184,8 @@ public void setCacheableResult(final boolean iValue) {

@Override
public OCommandContext getContext() {
if (context == null) context = new OBasicCommandContext();
if (context == null)
context = new OBasicCommandContext(ODatabaseRecordThreadLocal.instance().getIfDefined());
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public OResultSet execute(ODatabaseDocumentInternal database, String script, Obj
}
List<OStatement> statements = OSQLEngine.parseScript(script, database);

OCommandContext scriptContext = new OBasicCommandContext();
((OBasicCommandContext) scriptContext).setDatabase(database);
OCommandContext scriptContext = new OBasicCommandContext(database);
Map<Object, Object> params = new HashMap<>();
if (args != null) {
for (int i = 0; i < args.length; i++) {
Expand All @@ -60,8 +59,7 @@ public OResultSet execute(ODatabaseDocumentInternal database, String script, Map
}
List<OStatement> statements = OSQLEngine.parseScript(script, database);

OCommandContext scriptContext = new OBasicCommandContext();
((OBasicCommandContext) scriptContext).setDatabase(database);
OCommandContext scriptContext = new OBasicCommandContext(database);

scriptContext.setInputParameters(params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public OCommandExecutorScript parse(final OCommandRequest iRequest) {
}

public Object execute(final Map<Object, Object> iArgs) {
if (context == null) context = new OBasicCommandContext();
if (context == null) context = new OBasicCommandContext(getDatabase());
return executeInContext(context, iArgs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,7 @@ public OLocalResultSetLifecycleDecorator query(OExecutionPlan plan, Map<Object,
getSharedContext().getOrientDB().startCommand(Optional.empty());
try {
preQueryStart();
OBasicCommandContext ctx = new OBasicCommandContext();
ctx.setDatabase(this);
OBasicCommandContext ctx = new OBasicCommandContext(this);
ctx.setInputParameters(params);

OLocalResultSet result = new OLocalResultSet((OInternalExecutionPlan) plan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ private void updateViewRow(
}
} else {
for (OProjectionItem oProjectionItem : projection.getItems()) {
Object value = oProjectionItem.execute(origin, new OBasicCommandContext());
Object value = oProjectionItem.execute(origin, new OBasicCommandContext(db));
viewRow.setProperty(oProjectionItem.getProjectionAliasAsString(), value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,7 @@ static boolean evaluateSecuirtyPolicyPredicate(
.executeNoAuthorization(
session.getName(),
(db -> {
OBasicCommandContext ctx = new OBasicCommandContext();
ctx.setDatabase(db);
OBasicCommandContext ctx = new OBasicCommandContext(db);
ctx.setDynamicVariable(
"$currentUser",
(inContext) -> {
Expand Down Expand Up @@ -458,8 +457,7 @@ static boolean evaluateSecuirtyPolicyPredicate(
.executeNoAuthorization(
session.getName(),
(db -> {
OBasicCommandContext ctx = new OBasicCommandContext();
ctx.setDatabase(db);
OBasicCommandContext ctx = new OBasicCommandContext(db);
ctx.setDynamicVariable(
"$currentUser",
(inContext) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ private OSQLFilterCondition convertToBetweenClause(final OSQLFilterCondition con

public void initContext() {
if (context == null) {
context = new OBasicCommandContext();
context = new OBasicCommandContext(getDatabase());
}
if (context.getDatabase() == null) {
((OBasicCommandContext) context).setDatabase(getDatabase());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected Iterator<Object> init(OCommandContext ctx) {
}

public OScriptExecutionPlan initPlan(OCommandContext ctx) {
OBasicCommandContext subCtx1 = new OBasicCommandContext();
OBasicCommandContext subCtx1 = new OBasicCommandContext(ctx.getDatabase());
subCtx1.setParent(ctx);
OScriptExecutionPlan plan = new OScriptExecutionPlan(subCtx1);
for (OStatement stm : body) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public GlobalLetQueryStep(
super(ctx, profilingEnabled);
this.varName = varName;

OBasicCommandContext subCtx = new OBasicCommandContext();
OBasicCommandContext subCtx = new OBasicCommandContext(ctx.getDatabase());
if (scriptVars != null) {
scriptVars.forEach(x -> subCtx.declareScriptVariable(x));
}
subCtx.setDatabase(ctx.getDatabase());
subCtx.setParent(ctx);
if (query.toString().contains("?")) {
// with positional parameters, you cannot know if a parameter has the same ordinal as the one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public OScriptExecutionPlan producePlan(OCommandContext ctx) {
}

public OScriptExecutionPlan initPositivePlan(OCommandContext ctx) {
OBasicCommandContext subCtx1 = new OBasicCommandContext();
OBasicCommandContext subCtx1 = new OBasicCommandContext(ctx.getDatabase());
subCtx1.setParent(ctx);
OScriptExecutionPlan positivePlan = new OScriptExecutionPlan(subCtx1);
for (OStatement stm : positiveStatements) {
Expand All @@ -56,7 +56,7 @@ public OScriptExecutionPlan initPositivePlan(OCommandContext ctx) {
public OScriptExecutionPlan initNegativePlan(OCommandContext ctx) {
if (negativeStatements != null) {
if (negativeStatements.size() > 0) {
OBasicCommandContext subCtx2 = new OBasicCommandContext();
OBasicCommandContext subCtx2 = new OBasicCommandContext(ctx.getDatabase());
subCtx2.setParent(ctx);
OScriptExecutionPlan negativePlan = new OScriptExecutionPlan(subCtx2);
for (OStatement stm : negativeStatements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public LetQueryStep(
}

private OResultInternal calculate(OResultInternal result, OCommandContext ctx) {
OBasicCommandContext subCtx = new OBasicCommandContext();
subCtx.setDatabase(ctx.getDatabase());
OBasicCommandContext subCtx = new OBasicCommandContext(ctx.getDatabase());
subCtx.setParentWithoutOverridingChild(ctx);
OInternalExecutionPlan subExecutionPlan;
if (query.toString().contains("?")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public LiveQueryListenerImpl(
} else if (statement.getTarget().getItem().getRids() != null) {
this.rids =
statement.getTarget().getItem().getRids().stream()
.map(x -> x.toRecordId(new OResultInternal(), new OBasicCommandContext()))
.map(
x ->
x.toRecordId(
new OResultInternal(), new OBasicCommandContext((ODatabaseSession) db)))
.collect(Collectors.toList());
}
execInSeparateDatabase(
Expand Down Expand Up @@ -146,16 +149,19 @@ record = copy(iRecord.before);
switch (iRecord.type) {
case ORecordOperation.DELETED:
record.setMetadata(BEFORE_METADATA_KEY, null);
clientListener.onDelete(execDb, applyProjections(record));
clientListener.onDelete(execDb, applyProjections((ODatabaseSession) execDb, record));
break;
case ORecordOperation.UPDATED:
OResult before =
applyProjections((OResultInternal) record.getMetadata(BEFORE_METADATA_KEY));
applyProjections(
(ODatabaseSession) execDb,
(OResultInternal) record.getMetadata(BEFORE_METADATA_KEY));
record.setMetadata(BEFORE_METADATA_KEY, null);
clientListener.onUpdate(execDb, before, applyProjections(record));
clientListener.onUpdate(
execDb, before, applyProjections((ODatabaseSession) execDb, record));
break;
case ORecordOperation.CREATED:
clientListener.onCreate(execDb, applyProjections(record));
clientListener.onCreate(execDb, applyProjections((ODatabaseSession) execDb, record));
break;
}
}
Expand All @@ -165,11 +171,11 @@ record = copy(iRecord.before);
}
}

private OResultInternal applyProjections(OResultInternal record) {
private OResultInternal applyProjections(ODatabaseSession session, OResultInternal record) {
if (statement.getProjection() != null) {
OResultInternal result =
(OResultInternal)
statement.getProjection().calculateSingle(new OBasicCommandContext(), record);
statement.getProjection().calculateSingle(new OBasicCommandContext(session), record);
return result;
}
return record;
Expand Down Expand Up @@ -218,7 +224,7 @@ private boolean filter(OResult record) {
if (where == null) {
return true;
}
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext((ODatabaseSession) this.execDb);
ctx.setInputParameters(params);
return where.matchesFilters(record, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3043,8 +3043,7 @@ private void handleSubqueryAsTarget(
OStatement subQuery,
OCommandContext ctx,
boolean profilingEnabled) {
OBasicCommandContext subCtx = new OBasicCommandContext();
subCtx.setDatabase(ctx.getDatabase());
OBasicCommandContext subCtx = new OBasicCommandContext(ctx.getDatabase());
subCtx.setParent(ctx);
OInternalExecutionPlan subExecutionPlan =
subQuery.createExecutionPlan(subCtx, profilingEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ private void handleSubqueryAsTarget(
OStatement subQuery,
OCommandContext ctx,
boolean profilingEnabled) {
OBasicCommandContext subCtx = new OBasicCommandContext();
subCtx.setDatabase(ctx.getDatabase());
OBasicCommandContext subCtx = new OBasicCommandContext(ctx.getDatabase());
subCtx.setParent(ctx);
OInternalExecutionPlan subExecutionPlan =
subQuery.createExecutionPlan(subCtx, profilingEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public OExecutionStream internalStart(OCommandContext ctx) throws OTimeoutExcept
}

public OScriptExecutionPlan initPlan(List<OStatement> body, OCommandContext ctx) {
OBasicCommandContext subCtx1 = new OBasicCommandContext();
OBasicCommandContext subCtx1 = new OBasicCommandContext(ctx.getDatabase());
subCtx1.setParent(ctx);
OScriptExecutionPlan plan = new OScriptExecutionPlan(subCtx1);
for (OStatement stm : body) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ public OCreateEdgeStatement(OrientSql p, int id) {
@Override
public OResultSet execute(
ODatabaseSession db, Object[] args, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
Map<Object, Object> params = new HashMap<>();
if (args != null) {
for (int i = 0; i < args.length; i++) {
Expand All @@ -63,11 +62,10 @@ public OResultSet execute(
@Override
public OResultSet execute(
ODatabaseSession db, Map params, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
ctx.setInputParameters(params);
OInsertExecutionPlan executionPlan;
if (usePlanCache) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ public OCreateVertexStatement(OrientSql p, int id) {
@Override
public OResultSet execute(
ODatabaseSession db, Map params, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
ctx.setInputParameters(params);
OInsertExecutionPlan executionPlan;
if (usePlanCache) {
Expand All @@ -50,11 +49,10 @@ public OResultSet execute(
@Override
public OResultSet execute(
ODatabaseSession db, Object[] args, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
Map<Object, Object> params = new HashMap<>();
if (args != null) {
for (int i = 0; i < args.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public ODDLStatement(OrientSql p, int id) {

public OResultSet execute(
ODatabaseSession db, Object[] args, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
Map<Object, Object> params = new HashMap<>();
if (args != null) {
for (int i = 0; i < args.length; i++) {
Expand All @@ -44,11 +43,10 @@ public OResultSet execute(

public OResultSet execute(
ODatabaseSession db, Map params, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
ctx.setInputParameters(params);
ODDLExecutionPlan executionPlan = (ODDLExecutionPlan) createExecutionPlan(ctx, false);
return new OExecutionResultSet(executionPlan.executeInternal(ctx), ctx, executionPlan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ public ODatabaseUserData copy() {
}

public void executeCreate(ODatabaseDocumentInternal db, OCommandContext parentCtx) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
ctx.setInputParameters(parentCtx.getInputParameters());
ctx.setDatabase(db);
OCreateUserStatement stm = new OCreateUserStatement(-1);
if (name != null) {
stm.name = name.copy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ public ODeleteEdgeStatement(OrientSql p, int id) {
@Override
public OResultSet execute(
ODatabaseSession db, Map params, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
ctx.setInputParameters(params);
ODeleteExecutionPlan executionPlan;
if (usePlanCache) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ public int hashCode() {
@Override
public OResultSet execute(
ODatabaseSession db, Map params, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
ctx.setInputParameters(params);
ODeleteExecutionPlan executionPlan;
if (usePlanCache) {
Expand All @@ -124,11 +123,10 @@ public OResultSet execute(
@Override
public OResultSet execute(
ODatabaseSession db, Object[] args, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
Map<Object, Object> params = new HashMap<>();
if (args != null) {
for (int i = 0; i < args.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ public ODeleteVertexStatement(OrientSql p, int id) {
@Override
public OResultSet execute(
ODatabaseSession db, Map params, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
ctx.setInputParameters(params);
ODeleteExecutionPlan executionPlan;
if (usePlanCache) {
Expand All @@ -50,11 +49,10 @@ public OResultSet execute(
@Override
public OResultSet execute(
ODatabaseSession db, Object[] args, OCommandContext parentCtx, boolean usePlanCache) {
OBasicCommandContext ctx = new OBasicCommandContext();
OBasicCommandContext ctx = new OBasicCommandContext(db);
if (parentCtx != null) {
ctx.setParentWithoutOverridingChild(parentCtx);
}
ctx.setDatabase(db);
Map<Object, Object> params = new HashMap<>();
if (args != null) {
for (int i = 0; i < args.length; i++) {
Expand Down
Loading

0 comments on commit d36e284

Please sign in to comment.