use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class SQLDeleteEdgeTest method testDeleteVertexWithReturn.
public void testDeleteVertexWithReturn() {
OIdentifiable v1 = database.command(new OCommandSQL("create vertex V set returning = true")).execute();
List<OIdentifiable> v2s = database.command(new OCommandSQL("delete vertex V return before where returning = true")).execute();
Assert.assertEquals(v2s.size(), 1);
Assert.assertTrue(v2s.contains(v1));
}
use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class OMatchStatement method addResult.
private boolean addResult(MatchContext matchContext, OSQLAsynchQuery<ODocument> request, OCommandContext ctx) {
ODocument doc = null;
if (returnsElements()) {
for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
if (isExplicitAlias(entry.getKey()) && entry.getValue() != null) {
ORecord record = entry.getValue().getRecord();
if (request.getResultListener() != null && record != null) {
if (!addSingleResult(request, (OBasicCommandContext) ctx, record))
return false;
}
}
}
} else if (returnsPathElements()) {
for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
if (entry.getValue() != null) {
ORecord record = entry.getValue().getRecord();
if (request.getResultListener() != null && record != null) {
if (!addSingleResult(request, (OBasicCommandContext) ctx, record))
return false;
}
}
}
} else if (returnsPatterns()) {
doc = getDatabase().newInstance();
doc.setTrackingChanges(false);
for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
if (isExplicitAlias(entry.getKey())) {
doc.field(entry.getKey(), entry.getValue());
}
}
} else if (returnsPaths()) {
doc = getDatabase().newInstance();
doc.setTrackingChanges(false);
for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
doc.field(entry.getKey(), entry.getValue());
}
} else if (returnsJson()) {
doc = jsonToDoc(matchContext, ctx);
} else {
doc = getDatabase().newInstance();
doc.setTrackingChanges(false);
int i = 0;
ODocument mapDoc = new ODocument();
mapDoc.setTrackingChanges(false);
mapDoc.fromMap((Map) matchContext.matched);
ctx.setVariable("$current", mapDoc);
for (OExpression item : returnItems) {
OIdentifier returnAliasIdentifier = returnAliases.get(i);
OIdentifier returnAlias;
if (returnAliasIdentifier == null) {
returnAlias = item.getDefaultAlias();
} else {
returnAlias = returnAliasIdentifier;
}
doc.field(returnAlias.getStringValue(), item.execute(mapDoc, ctx));
i++;
}
doc.setTrackingChanges(true);
}
if (request.getResultListener() != null && doc != null) {
if (!addSingleResult(request, (OBasicCommandContext) ctx, doc))
return false;
}
return true;
}
use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class OMatchStatement method expandCartesianProduct.
private boolean expandCartesianProduct(Pattern pattern, MatchContext matchContext, Map<String, String> aliasClasses, Map<String, OWhereClause> aliasFilters, OCommandContext iCommandContext, OSQLAsynchQuery<ODocument> request) {
for (String alias : pattern.aliasToNode.keySet()) {
if (!matchContext.matched.containsKey(alias)) {
String target = aliasClasses.get(alias);
if (target == null) {
throw new OCommandExecutionException("Cannot execute MATCH statement on alias " + alias + ": class not defined");
}
Iterable<OIdentifiable> values = fetchAliasCandidates(alias, aliasFilters, iCommandContext, aliasClasses);
for (OIdentifiable id : values) {
MatchContext childContext = matchContext.copy(alias, id);
if (allNodesCalculated(childContext, pattern)) {
// false if limit reached
boolean added = addResult(childContext, request, iCommandContext);
if (!added) {
return false;
}
} else {
// false if limit reached
boolean added = expandCartesianProduct(pattern, childContext, aliasClasses, aliasFilters, iCommandContext, request);
if (!added) {
return false;
}
}
}
break;
}
}
return true;
}
use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class OMatchStatement method processContext.
private boolean processContext(Pattern pattern, MatchExecutionPlan executionPlan, MatchContext matchContext, Map<String, String> aliasClasses, Map<String, OWhereClause> aliasFilters, OCommandContext iCommandContext, OSQLAsynchQuery<ODocument> request) {
iCommandContext.setVariable("$matched", matchContext.matched);
if (pattern.getNumOfEdges() == matchContext.matchedEdges.size() && allNodesCalculated(matchContext, pattern)) {
// false if limit reached
return addResult(matchContext, request, iCommandContext);
}
if (executionPlan.sortedEdges.size() == matchContext.currentEdgeNumber) {
// false if limit reached
return expandCartesianProduct(pattern, matchContext, aliasClasses, aliasFilters, iCommandContext, request);
}
EdgeTraversal currentEdge = executionPlan.sortedEdges.get(matchContext.currentEdgeNumber);
PatternNode rootNode = currentEdge.out ? currentEdge.edge.out : currentEdge.edge.in;
if (currentEdge.out) {
PatternEdge outEdge = currentEdge.edge;
if (!matchContext.matchedEdges.containsKey(outEdge)) {
OIdentifiable startingPoint = matchContext.matched.get(outEdge.out.alias);
if (startingPoint == null) {
//restart from candidates (disjoint patterns? optional? just could not proceed from last node?)
Iterable rightCandidates = matchContext.candidates.get(outEdge.out.alias);
if (rightCandidates != null) {
if (!processContextFromCandidates(pattern, executionPlan, matchContext, aliasClasses, aliasFilters, iCommandContext, request, rightCandidates, outEdge.out.alias, matchContext.currentEdgeNumber)) {
return false;
}
}
return true;
}
Object rightValues = outEdge.executeTraversal(matchContext, iCommandContext, startingPoint, 0);
if (outEdge.in.isOptionalNode() && (isEmptyResult(rightValues) || !contains(rightValues, matchContext.matched.get(outEdge.in.alias)))) {
MatchContext childContext = matchContext.copy(outEdge.in.alias, null);
childContext.matched.put(outEdge.in.alias, null);
//TODO testOptional 3 match passa con +1
childContext.currentEdgeNumber = matchContext.currentEdgeNumber + 1;
childContext.matchedEdges.put(outEdge, true);
if (!processContext(pattern, executionPlan, childContext, aliasClasses, aliasFilters, iCommandContext, request)) {
return false;
}
}
if (!(rightValues instanceof Iterable)) {
rightValues = Collections.singleton(rightValues);
}
String rightClassName = aliasClasses.get(outEdge.in.alias);
OClass rightClass = getDatabase().getMetadata().getSchema().getClass(rightClassName);
for (OIdentifiable rightValue : (Iterable<OIdentifiable>) rightValues) {
if (rightValue == null) {
//broken graph?, null reference
continue;
}
if (rightClass != null && !matchesClass(rightValue, rightClass)) {
continue;
}
Iterable<OIdentifiable> prevMatchedRightValues = matchContext.candidates.get(outEdge.in.alias);
if (matchContext.matched.containsKey(outEdge.in.alias)) {
if (matchContext.matched.get(outEdge.in.alias).getIdentity().equals(rightValue.getIdentity())) {
MatchContext childContext = matchContext.copy(outEdge.in.alias, rightValue.getIdentity());
childContext.currentEdgeNumber = matchContext.currentEdgeNumber + 1;
childContext.matchedEdges.put(outEdge, true);
if (!processContext(pattern, executionPlan, childContext, aliasClasses, aliasFilters, iCommandContext, request)) {
return false;
}
break;
}
} else if (prevMatchedRightValues != null && prevMatchedRightValues.iterator().hasNext()) {
// values
for (OIdentifiable id : prevMatchedRightValues) {
if (id.getIdentity().equals(rightValue.getIdentity())) {
MatchContext childContext = matchContext.copy(outEdge.in.alias, id);
childContext.currentEdgeNumber = matchContext.currentEdgeNumber + 1;
childContext.matchedEdges.put(outEdge, true);
if (!processContext(pattern, executionPlan, childContext, aliasClasses, aliasFilters, iCommandContext, request)) {
return false;
}
}
}
} else {
// searching for neighbors
MatchContext childContext = matchContext.copy(outEdge.in.alias, rightValue.getIdentity());
childContext.currentEdgeNumber = matchContext.currentEdgeNumber + 1;
childContext.matchedEdges.put(outEdge, true);
if (!processContext(pattern, executionPlan, childContext, aliasClasses, aliasFilters, iCommandContext, request)) {
return false;
}
}
}
}
} else {
PatternEdge inEdge = currentEdge.edge;
if (!matchContext.matchedEdges.containsKey(inEdge)) {
if (!inEdge.item.isBidirectional()) {
throw new RuntimeException("Invalid pattern to match!");
}
if (!matchContext.matchedEdges.containsKey(inEdge)) {
Object leftValues = inEdge.item.method.executeReverse(matchContext.matched.get(inEdge.in.alias), iCommandContext);
if (inEdge.out.isOptionalNode() && (isEmptyResult(leftValues) || !contains(leftValues, matchContext.matched.get(inEdge.out.alias)))) {
MatchContext childContext = matchContext.copy(inEdge.out.alias, null);
childContext.matched.put(inEdge.out.alias, null);
childContext.currentEdgeNumber = matchContext.currentEdgeNumber + 1;
childContext.matchedEdges.put(inEdge, true);
if (!processContext(pattern, executionPlan, childContext, aliasClasses, aliasFilters, iCommandContext, request)) {
return false;
}
}
if (!(leftValues instanceof Iterable)) {
leftValues = Collections.singleton(leftValues);
}
String leftClassName = aliasClasses.get(inEdge.out.alias);
OClass leftClass = getDatabase().getMetadata().getSchema().getClass(leftClassName);
for (OIdentifiable leftValue : (Iterable<OIdentifiable>) leftValues) {
if (leftValue == null) {
//broken graph? null reference
continue;
}
if (leftClass != null && !matchesClass(leftValue, leftClass)) {
continue;
}
Iterable<OIdentifiable> prevMatchedRightValues = matchContext.candidates.get(inEdge.out.alias);
if (matchContext.matched.containsKey(inEdge.out.alias)) {
if (matchContext.matched.get(inEdge.out.alias).getIdentity().equals(leftValue.getIdentity())) {
MatchContext childContext = matchContext.copy(inEdge.out.alias, leftValue.getIdentity());
childContext.currentEdgeNumber = matchContext.currentEdgeNumber + 1;
childContext.matchedEdges.put(inEdge, true);
if (!processContext(pattern, executionPlan, childContext, aliasClasses, aliasFilters, iCommandContext, request)) {
return false;
}
break;
}
} else if (prevMatchedRightValues != null && prevMatchedRightValues.iterator().hasNext()) {
// values
for (OIdentifiable id : prevMatchedRightValues) {
if (id.getIdentity().equals(leftValue.getIdentity())) {
MatchContext childContext = matchContext.copy(inEdge.out.alias, id);
childContext.currentEdgeNumber = matchContext.currentEdgeNumber + 1;
childContext.matchedEdges.put(inEdge, true);
if (!processContext(pattern, executionPlan, childContext, aliasClasses, aliasFilters, iCommandContext, request)) {
return false;
}
}
}
} else {
// searching for neighbors
OWhereClause where = aliasFilters.get(inEdge.out.alias);
String className = aliasClasses.get(inEdge.out.alias);
OClass oClass = getDatabase().getMetadata().getSchema().getClass(className);
if ((oClass == null || matchesClass(leftValue, oClass)) && (where == null || where.matchesFilters(leftValue, iCommandContext))) {
MatchContext childContext = matchContext.copy(inEdge.out.alias, leftValue.getIdentity());
childContext.currentEdgeNumber = matchContext.currentEdgeNumber + 1;
childContext.matchedEdges.put(inEdge, true);
if (!processContext(pattern, executionPlan, childContext, aliasClasses, aliasFilters, iCommandContext, request)) {
return false;
}
}
}
}
}
}
}
return true;
}
use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.
the class OSQLEngine method parseRIDTarget.
public Set<OIdentifiable> parseRIDTarget(final ODatabaseDocument database, String iTarget, final OCommandContext iContext, Map<Object, Object> iArgs) {
final Set<OIdentifiable> ids;
if (iTarget.startsWith("(")) {
// SUB-QUERY
final OSQLSynchQuery<Object> query = new OSQLSynchQuery<Object>(iTarget.substring(1, iTarget.length() - 1));
query.setContext(iContext);
final List<OIdentifiable> result = database.query(query, iArgs);
if (result == null || result.isEmpty())
ids = Collections.emptySet();
else {
ids = new HashSet<OIdentifiable>((int) (result.size() * 1.3));
for (OIdentifiable aResult : result) ids.add(aResult.getIdentity());
}
} else if (iTarget.startsWith("[")) {
// COLLECTION OF RIDS
final String[] idsAsStrings = iTarget.substring(1, iTarget.length() - 1).split(",");
ids = new HashSet<OIdentifiable>((int) (idsAsStrings.length * 1.3));
for (String idsAsString : idsAsStrings) {
if (idsAsString.startsWith("$")) {
Object r = iContext.getVariable(idsAsString);
if (r instanceof OIdentifiable)
ids.add((OIdentifiable) r);
else
OMultiValue.add(ids, r);
} else
ids.add(new ORecordId(idsAsString));
}
} else {
// SINGLE RID
if (iTarget.startsWith("$")) {
Object r = iContext.getVariable(iTarget);
if (r instanceof OIdentifiable)
ids = Collections.<OIdentifiable>singleton((OIdentifiable) r);
else
ids = (Set<OIdentifiable>) OMultiValue.add(new HashSet<OIdentifiable>(OMultiValue.getSize(r)), r);
} else
ids = Collections.<OIdentifiable>singleton(new ORecordId(iTarget));
}
return ids;
}
Aggregations