Skip to content

Commit d6606df

Browse files
committed
Fixed issue about managing array of edges and edges taken from the context
1 parent 9139f39 commit d6606df

3 files changed

Lines changed: 88 additions & 69 deletions

File tree

core/src/main/java/com/orientechnologies/orient/core/sql/OSQLEngine.java

Lines changed: 76 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.orientechnologies.orient.core.command.OCommandContext;
2727
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
2828
import com.orientechnologies.orient.core.db.record.OIdentifiable;
29-
import com.orientechnologies.orient.core.id.ORID;
3029
import com.orientechnologies.orient.core.id.ORecordId;
3130
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
3231
import com.orientechnologies.orient.core.sql.filter.OSQLFilter;
@@ -61,6 +60,40 @@ public class OSQLEngine {
6160
private static OQueryOperator[] SORTED_OPERATORS = null;
6261
private static ClassLoader orientClassLoader = OSQLEngine.class.getClassLoader();
6362

63+
/**
64+
* internal use only, to sort operators.
65+
*/
66+
private static final class Pair {
67+
68+
final OQueryOperator before;
69+
final OQueryOperator after;
70+
71+
public Pair(final OQueryOperator before, final OQueryOperator after) {
72+
this.before = before;
73+
this.after = after;
74+
}
75+
76+
@Override
77+
public boolean equals(final Object obj) {
78+
if (obj instanceof Pair) {
79+
final Pair that = (Pair) obj;
80+
return before == that.before && after == that.after;
81+
}
82+
return false;
83+
}
84+
85+
@Override
86+
public int hashCode() {
87+
return System.identityHashCode(before) + 31 * System.identityHashCode(after);
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return before + " > " + after;
93+
}
94+
95+
}
96+
6497
protected OSQLEngine() {
6598
}
6699

@@ -263,6 +296,20 @@ public static OCollate getCollate(final String name) {
263296
return null;
264297
}
265298

299+
public static OSQLMethod getMethod(String iMethodName) {
300+
iMethodName = iMethodName.toLowerCase(Locale.ENGLISH);
301+
302+
final Iterator<OSQLMethodFactory> ite = getMethodFactories();
303+
while (ite.hasNext()) {
304+
final OSQLMethodFactory factory = ite.next();
305+
if (factory.hasMethod(iMethodName)) {
306+
return factory.createMethod(iMethodName);
307+
}
308+
}
309+
310+
return null;
311+
}
312+
266313
public synchronized OQueryOperator[] getRecordOperators() {
267314
if (SORTED_OPERATORS != null) {
268315
return SORTED_OPERATORS;
@@ -396,75 +443,48 @@ public OSQLTarget parseTarget(final String iText, final OCommandContext iContext
396443
return new OSQLTarget(iText, iContext, iFilterKeyword);
397444
}
398445

399-
public Set<ORID> parseRIDTarget(final ODatabaseRecord database, final String iTarget) {
400-
final Set<ORID> ids;
446+
public Set<OIdentifiable> parseRIDTarget(final ODatabaseRecord database, String iTarget, final OCommandContext iContext) {
447+
final Set<OIdentifiable> ids;
401448
if (iTarget.startsWith("(")) {
402449
// SUB-QUERY
403-
final List<OIdentifiable> result = database.query(new OSQLSynchQuery<Object>(iTarget.substring(1, iTarget.length() - 1)));
450+
final OSQLSynchQuery<Object> query = new OSQLSynchQuery<Object>(iTarget.substring(1, iTarget.length() - 1));
451+
query.setContext(iContext);
452+
453+
final List<OIdentifiable> result = database.query(query);
404454
if (result == null || result.isEmpty())
405455
ids = Collections.emptySet();
406456
else {
407-
ids = new HashSet<ORID>((int) (result.size() * 1.3));
457+
ids = new HashSet<OIdentifiable>((int) (result.size() * 1.3));
408458
for (OIdentifiable aResult : result)
409459
ids.add(aResult.getIdentity());
410460
}
411461
} else if (iTarget.startsWith("[")) {
412462
// COLLECTION OF RIDS
413463
final String[] idsAsStrings = iTarget.substring(1, iTarget.length() - 1).split(",");
414-
ids = new HashSet<ORID>((int) (idsAsStrings.length * 1.3));
415-
for (String idsAsString : idsAsStrings)
416-
ids.add(new ORecordId(idsAsString));
417-
} else
418-
// SINGLE RID
419-
ids = Collections.<ORID> singleton(new ORecordId(iTarget));
420-
return ids;
421-
}
422-
423-
public static OSQLMethod getMethod(String iMethodName) {
424-
iMethodName = iMethodName.toLowerCase(Locale.ENGLISH);
425-
426-
final Iterator<OSQLMethodFactory> ite = getMethodFactories();
427-
while (ite.hasNext()) {
428-
final OSQLMethodFactory factory = ite.next();
429-
if (factory.hasMethod(iMethodName)) {
430-
return factory.createMethod(iMethodName);
431-
}
432-
}
433-
434-
return null;
435-
}
436-
437-
/**
438-
* internal use only, to sort operators.
439-
*/
440-
private static final class Pair {
441-
442-
final OQueryOperator before;
443-
final OQueryOperator after;
444-
445-
public Pair(final OQueryOperator before, final OQueryOperator after) {
446-
this.before = before;
447-
this.after = after;
448-
}
449-
450-
@Override
451-
public boolean equals(final Object obj) {
452-
if (obj instanceof Pair) {
453-
final Pair that = (Pair) obj;
454-
return before == that.before && after == that.after;
464+
ids = new HashSet<OIdentifiable>((int) (idsAsStrings.length * 1.3));
465+
for (String idsAsString : idsAsStrings) {
466+
if (idsAsString.startsWith("$")) {
467+
Object r = iContext.getVariable(idsAsString);
468+
if (r instanceof OIdentifiable)
469+
ids.add((OIdentifiable) r);
470+
else
471+
OMultiValue.add(ids, r);
472+
} else
473+
ids.add(new ORecordId(idsAsString));
455474
}
456-
return false;
457-
}
475+
} else {
476+
// SINGLE RID
477+
if (iTarget.startsWith("$")) {
478+
Object r = iContext.getVariable(iTarget);
479+
if (r instanceof OIdentifiable)
480+
ids = Collections.<OIdentifiable> singleton((OIdentifiable) r);
481+
else
482+
ids = (Set<OIdentifiable>) OMultiValue.add(new HashSet<OIdentifiable>(OMultiValue.getSize(r)), r);
458483

459-
@Override
460-
public int hashCode() {
461-
return System.identityHashCode(before) + 31 * System.identityHashCode(after);
462-
}
484+
} else
485+
ids = Collections.<OIdentifiable> singleton(new ORecordId(iTarget));
463486

464-
@Override
465-
public String toString() {
466-
return before + " > " + after;
467487
}
468-
488+
return ids;
469489
}
470490
}

graphdb/src/main/java/com/orientechnologies/orient/graph/sql/OCommandExecutorSQLCreateEdge.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import com.orientechnologies.orient.core.command.OCommandRequest;
1919
import com.orientechnologies.orient.core.command.OCommandRequestText;
2020
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
21+
import com.orientechnologies.orient.core.db.record.OIdentifiable;
2122
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
2223
import com.orientechnologies.orient.core.exception.OConcurrentModificationException;
23-
import com.orientechnologies.orient.core.id.ORID;
2424
import com.orientechnologies.orient.core.metadata.schema.OClass;
2525
import com.orientechnologies.orient.core.sql.OCommandExecutorSQLSetAware;
2626
import com.orientechnologies.orient.core.sql.OCommandParameters;
@@ -119,17 +119,17 @@ public Object execute(final Map<Object, Object> iArgs) {
119119
return OGraphCommandExecutorSQLFactory.runInTx(new OGraphCommandExecutorSQLFactory.GraphCallBack<List<Object>>() {
120120
@Override
121121
public List<Object> call(OrientBaseGraph graph) {
122-
final Set<ORID> fromIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), from);
123-
final Set<ORID> toIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), to);
122+
final Set<OIdentifiable> fromIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), from, context);
123+
final Set<OIdentifiable> toIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), to, context);
124124

125125
// CREATE EDGES
126126
final List<Object> edges = new ArrayList<Object>();
127-
for (ORID from : fromIds) {
127+
for (OIdentifiable from : fromIds) {
128128
final OrientVertex fromVertex = graph.getVertex(from);
129129
if (fromVertex == null)
130130
throw new OCommandExecutionException("Source vertex '" + from + "' not exists");
131131

132-
for (ORID to : toIds) {
132+
for (OIdentifiable to : toIds) {
133133
final OrientVertex toVertex;
134134
if (from.equals(to)) {
135135
toVertex = fromVertex;

graphdb/src/main/java/com/orientechnologies/orient/graph/sql/OCommandExecutorSQLDeleteEdge.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.orientechnologies.orient.core.command.OCommandResultListener;
2121
import com.orientechnologies.orient.core.db.record.OIdentifiable;
2222
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
23-
import com.orientechnologies.orient.core.id.ORID;
2423
import com.orientechnologies.orient.core.id.ORecordId;
2524
import com.orientechnologies.orient.core.metadata.schema.OClass;
2625
import com.orientechnologies.orient.core.record.impl.ODocument;
@@ -152,26 +151,26 @@ public Object call(OrientBaseGraph graph) {
152151
OGraphCommandExecutorSQLFactory.runInTx(new OGraphCommandExecutorSQLFactory.GraphCallBack<Object>() {
153152
@Override
154153
public Object call(OrientBaseGraph graph) {
155-
Set<ORID> fromIds = null;
154+
Set<OIdentifiable> fromIds = null;
156155
if (fromExpr != null)
157-
fromIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), fromExpr);
158-
Set<ORID> toIds = null;
156+
fromIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), fromExpr, context);
157+
Set<OIdentifiable> toIds = null;
159158
if (toExpr != null)
160-
toIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), toExpr);
159+
toIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), toExpr, context);
161160

162161
if (fromIds != null && toIds != null) {
163162
// REMOVE ALL THE EDGES BETWEEN VERTICES
164-
for (ORID fromId : fromIds)
163+
for (OIdentifiable fromId : fromIds)
165164
for (Edge e : graph.getVertex(fromId).getEdges(Direction.OUT))
166165
if (toIds.contains(((OrientEdge) e).getInVertex().getIdentity()))
167166
edges.add((OrientEdge) e);
168167
} else if (fromIds != null)
169168
// REMOVE ALL THE EDGES THAT START FROM A VERTEXES
170-
for (ORID fromId : fromIds)
169+
for (OIdentifiable fromId : fromIds)
171170
edges.add((OrientEdge) graph.getVertex(fromId).getEdges(Direction.OUT));
172171
else if (toIds != null)
173172
// REMOVE ALL THE EDGES THAT ARRIVE TO A VERTEXES
174-
for (ORID toId : toIds)
173+
for (OIdentifiable toId : toIds)
175174
edges.add((OrientEdge) graph.getVertex(toId).getEdges(Direction.IN));
176175
else
177176
throw new OCommandExecutionException("Invalid target");

0 commit comments

Comments
 (0)