use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.
the class OCommandExecutorSQLSelect method applyExpand.
/**
* Extract the content of collections and/or links and put it as result
*/
private void applyExpand() {
if (expandTarget == null) {
return;
}
final long startExpand = System.currentTimeMillis();
try {
if (tempResult == null) {
tempResult = new ArrayList<OIdentifiable>();
if (expandTarget instanceof OSQLFilterItemVariable) {
Object r = ((OSQLFilterItemVariable) expandTarget).getValue(null, null, context);
if (r != null) {
if (r instanceof OIdentifiable) {
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) r);
} else if (r instanceof Iterator || OMultiValue.isMultiValue(r)) {
for (Object o : OMultiValue.getMultiValueIterable(r)) {
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) o);
}
}
}
} else if (expandTarget instanceof OSQLFunctionRuntime && !hasFieldItemParams((OSQLFunctionRuntime) expandTarget)) {
if (((OSQLFunctionRuntime) expandTarget).aggregateResults()) {
throw new OCommandExecutionException("Unsupported operation: aggregate function in expand(" + expandTarget + ")");
} else {
Object r = ((OSQLFunctionRuntime) expandTarget).execute(null, null, null, context);
if (r instanceof OIdentifiable) {
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) r);
} else if (r instanceof Iterator || OMultiValue.isMultiValue(r)) {
int i = 0;
for (Object o : OMultiValue.getMultiValueIterable(r)) {
if ((++i) % 100 == 0 && !checkInterruption()) {
return;
}
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) o);
}
}
}
}
} else {
if (tempResult == null) {
tempResult = new ArrayList<OIdentifiable>();
}
final OMultiCollectionIterator<OIdentifiable> finalResult = new OMultiCollectionIterator<OIdentifiable>();
if (orderedFields == null || orderedFields.size() == 0) {
// expand is applied before sorting, so limiting the result set here would give wrong results
int iteratorLimit = 0;
if (limit < 0) {
iteratorLimit = -1;
} else {
iteratorLimit += limit;
}
finalResult.setLimit(iteratorLimit);
finalResult.setSkip(skip);
}
for (OIdentifiable id : tempResult) {
if (!checkInterruption()) {
return;
}
final Object fieldValue;
if (expandTarget instanceof OSQLFilterItem) {
fieldValue = ((OSQLFilterItem) expandTarget).getValue(id.getRecord(), null, context);
} else if (expandTarget instanceof OSQLFunctionRuntime) {
fieldValue = ((OSQLFunctionRuntime) expandTarget).getResult();
} else {
fieldValue = expandTarget.toString();
}
if (fieldValue != null) {
if (fieldValue instanceof ODocument) {
ArrayList<ODocument> partial = new ArrayList<ODocument>();
partial.add((ODocument) fieldValue);
finalResult.add(partial);
} else if (fieldValue instanceof Collection<?> || fieldValue.getClass().isArray() || fieldValue instanceof Iterator<?> || fieldValue instanceof OIdentifiable || fieldValue instanceof ORidBag) {
finalResult.add(fieldValue);
} else if (fieldValue instanceof Map<?, ?>) {
finalResult.add(((Map<?, OIdentifiable>) fieldValue).values());
}
}
}
tempResult = finalResult;
}
} finally {
context.setVariable("expandElapsed", (System.currentTimeMillis() - startExpand));
}
}
use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.
the class OCommandExecutorSQLSelect method applyOrderBy.
private void applyOrderBy(boolean clearOrderedFields) {
if (orderedFields.isEmpty() || fullySortedByIndex || isRidOnlySort()) {
return;
}
final long startOrderBy = System.currentTimeMillis();
try {
if (tempResult instanceof OMultiCollectionIterator) {
final List<OIdentifiable> list = new ArrayList<OIdentifiable>();
for (OIdentifiable o : tempResult) {
list.add(o);
}
tempResult = list;
}
tempResult = applySort((List<OIdentifiable>) tempResult, orderedFields, context);
if (clearOrderedFields) {
orderedFields.clear();
}
} finally {
metricRecorder.orderByElapsed(startOrderBy);
}
}
use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.
the class BlueprintsConcurrentGraphChangesTestNoTx method assertEdgesDeletedGraph.
private void assertEdgesDeletedGraph() {
OrientBaseGraph graph = getGraph();
graph.setUseLightweightEdges(false);
Assert.assertEquals(VERTEXES_COUNT, graph.countVertices("TestVertex"));
Assert.assertEquals(0, graph.countEdges("TestEdge"));
for (TestVertex vertex : vertexes) {
Iterable<Vertex> vertexes = graph.command(new OSQLSynchQuery<Vertex>("select from TestVertex where uuid = '" + vertex.uuid + "'")).execute();
Assert.assertTrue(vertexes.iterator().hasNext());
Vertex gVertex = vertexes.iterator().next();
OMultiCollectionIterator<Edge> outEdges = (OMultiCollectionIterator<Edge>) gVertex.getEdges(Direction.OUT);
Assert.assertEquals(outEdges.size(), 0);
OMultiCollectionIterator<Edge> inEdges = (OMultiCollectionIterator<Edge>) gVertex.getEdges(Direction.IN);
Assert.assertEquals(inEdges.size(), 0);
}
assertGraphIsConsistent(graph);
graph.shutdown();
}
use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.
the class OSQLFunctionIn method fetchFromIndex.
private Object fetchFromIndex(OrientBaseGraph graph, OIdentifiable iFrom, Iterable<OIdentifiable> iTo, String[] iEdgeTypes) {
String edgeClassName = null;
if (iEdgeTypes == null) {
edgeClassName = "E";
} else if (iEdgeTypes.length == 1) {
edgeClassName = iEdgeTypes[0];
} else {
return null;
}
OClass edgeClass = graph.getRawGraph().getMetadata().getSchema().getClass(edgeClassName);
if (edgeClass == null) {
return null;
}
Set<OIndex<?>> indexes = edgeClass.getInvolvedIndexes("in", "out");
if (indexes == null || indexes.size() == 0) {
return null;
}
OIndex index = indexes.iterator().next();
OMultiCollectionIterator<OrientVertex> result = new OMultiCollectionIterator<OrientVertex>();
for (OIdentifiable to : iTo) {
OCompositeKey key = new OCompositeKey(iFrom, to);
Object indexResult = index.get(key);
if (indexResult instanceof OIdentifiable) {
indexResult = Collections.singleton(indexResult);
}
Set<OIdentifiable> identities = new HashSet<OIdentifiable>();
for (OIdentifiable edge : ((Iterable<OrientEdge>) indexResult)) {
identities.add((OIdentifiable) ((ODocument) edge.getRecord()).rawField("in"));
}
result.add(identities);
}
return result;
}
use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.
the class OWhereClause method fetchFromIndexes.
public Iterable fetchFromIndexes(OClass oClass, OCommandContext ctx) {
List<OAndBlock> flattenedConditions = flatten();
if (flattenedConditions == null || flattenedConditions.size() == 0) {
return null;
}
Set<OIndex<?>> indexes = oClass.getIndexes();
List<OIndex> bestIndexes = new ArrayList<OIndex>();
List<Map<String, Object>> indexConditions = new ArrayList<Map<String, Object>>();
for (OAndBlock condition : flattenedConditions) {
Map<String, Object> conditions = getEqualityOperations(condition, ctx);
long conditionEstimation = Long.MAX_VALUE;
OIndex bestIndex = null;
Map<String, Object> bestCondition = null;
for (OIndex index : indexes) {
List<String> indexedFields = index.getDefinition().getFields();
int nMatchingKeys = 0;
for (String indexedField : indexedFields) {
if (conditions.containsKey(indexedField)) {
nMatchingKeys++;
} else {
break;
}
}
if (nMatchingKeys > 0) {
long newCount = estimateFromIndex(index, conditions, nMatchingKeys);
if (newCount >= 0 && newCount <= conditionEstimation) {
conditionEstimation = newCount;
bestIndex = index;
bestCondition = conditions;
}
}
}
if (bestIndex == null) {
return null;
}
bestIndexes.add(bestIndex);
indexConditions.add(bestCondition);
}
OMultiCollectionIterator result = new OMultiCollectionIterator();
for (int i = 0; i < bestIndexes.size(); i++) {
OIndex index = bestIndexes.get(i);
Map<String, Object> condition = indexConditions.get(i);
result.add(fetchFromIndex(index, indexConditions.get(i)));
}
return result;
}
Aggregations