Search in sources :

Example 1 with OMultiCollectionIterator

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));
    }
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OIdentifiableIterator(com.orientechnologies.orient.core.iterator.OIdentifiableIterator) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OSortedMultiIterator(com.orientechnologies.common.collection.OSortedMultiIterator) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with OMultiCollectionIterator

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);
    }
}
Also used : OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 3 with OMultiCollectionIterator

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();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) Edge(com.tinkerpop.blueprints.Edge)

Example 4 with OMultiCollectionIterator

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;
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) HashSet(java.util.HashSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 5 with OMultiCollectionIterator

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;
}
Also used : OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator)

Aggregations

OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)9 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)5 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)2 OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)2 OIndex (com.orientechnologies.orient.core.index.OIndex)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)2 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)2 HashSet (java.util.HashSet)2 OLazyIterator (com.orientechnologies.common.collection.OLazyIterator)1 OSortedMultiIterator (com.orientechnologies.common.collection.OSortedMultiIterator)1 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)1 OLazyObjectMapInterface (com.orientechnologies.orient.core.db.object.OLazyObjectMapInterface)1 OAutoConvertToRecord (com.orientechnologies.orient.core.db.record.OAutoConvertToRecord)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)1 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)1 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)1 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)1