Search in sources :

Example 6 with OMultiCollectionIterator

use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.

the class ORecordSerializerCSVAbstract method fieldToStream.

public void fieldToStream(final ODocument iRecord, final StringBuilder iOutput, OUserObject2RecordHandler iObjHandler, final OType iType, final OClass iLinkedClass, final OType iLinkedType, final String iName, final Object iValue, final boolean iSaveOnlyDirty) {
    if (iValue == null)
        return;
    final long timer = PROFILER.startChrono();
    switch(iType) {
        case LINK:
            {
                if (!(iValue instanceof OIdentifiable))
                    throw new OSerializationException("Found an unexpected type during marshalling of a LINK where a OIdentifiable (ORID or any Record) was expected. The string representation of the object is: " + iValue);
                if (!((OIdentifiable) iValue).getIdentity().isValid() && iValue instanceof ODocument && ((ODocument) iValue).isEmbedded()) {
                    // WRONG: IT'S EMBEDDED!
                    fieldToStream(iRecord, iOutput, iObjHandler, OType.EMBEDDED, iLinkedClass, iLinkedType, iName, iValue, iSaveOnlyDirty);
                } else {
                    final Object link = linkToStream(iOutput, iRecord, iValue);
                    if (link != null)
                        // OVERWRITE CONTENT
                        iRecord.field(iName, link);
                    PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.link2string"), "Serialize link to string", timer);
                }
                break;
            }
        case LINKLIST:
            {
                iOutput.append(OStringSerializerHelper.LIST_BEGIN);
                if (iValue instanceof ORecordLazyList && ((ORecordLazyList) iValue).getStreamedContent() != null) {
                    iOutput.append(((ORecordLazyList) iValue).getStreamedContent());
                    PROFILER.updateCounter(PROFILER.getProcessMetric("serializer.record.string.linkList2string.cached"), "Serialize linklist to string in stream mode", +1);
                } else {
                    final ORecordLazyList coll;
                    final Iterator<OIdentifiable> it;
                    if (iValue instanceof OMultiCollectionIterator<?>) {
                        final OMultiCollectionIterator<OIdentifiable> iterator = (OMultiCollectionIterator<OIdentifiable>) iValue;
                        iterator.reset();
                        it = iterator;
                        coll = null;
                    } else if (!(iValue instanceof ORecordLazyList)) {
                        // FIRST TIME: CONVERT THE ENTIRE COLLECTION
                        coll = new ORecordLazyList(iRecord);
                        if (iValue.getClass().isArray()) {
                            Iterable<Object> iterab = OMultiValue.getMultiValueIterable(iValue, false);
                            for (Object i : iterab) {
                                coll.add((OIdentifiable) i);
                            }
                        } else {
                            coll.addAll((Collection<? extends OIdentifiable>) iValue);
                            ((Collection<? extends OIdentifiable>) iValue).clear();
                        }
                        iRecord.field(iName, coll);
                        it = coll.rawIterator();
                    } else {
                        // LAZY LIST
                        coll = (ORecordLazyList) iValue;
                        if (coll.getStreamedContent() != null) {
                            // APPEND STREAMED CONTENT
                            iOutput.append(coll.getStreamedContent());
                            PROFILER.updateCounter(PROFILER.getProcessMetric("serializer.record.string.linkList2string.cached"), "Serialize linklist to string in stream mode", +1);
                            it = coll.newItemsIterator();
                        } else
                            it = coll.rawIterator();
                    }
                    if (it != null && it.hasNext()) {
                        final StringBuilder buffer = new StringBuilder(128);
                        for (int items = 0; it.hasNext(); items++) {
                            if (items > 0)
                                buffer.append(OStringSerializerHelper.RECORD_SEPARATOR);
                            final OIdentifiable item = it.next();
                            final OIdentifiable newRid = linkToStream(buffer, iRecord, item);
                            if (newRid != null)
                                ((OLazyIterator<OIdentifiable>) it).update(newRid);
                        }
                        if (coll != null)
                            coll.convertRecords2Links();
                        iOutput.append(buffer);
                        // UPDATE THE STREAM
                        if (coll != null)
                            coll.setStreamedContent(buffer);
                    }
                }
                iOutput.append(OStringSerializerHelper.LIST_END);
                PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkList2string"), "Serialize linklist to string", timer);
                break;
            }
        case LINKSET:
            {
                if (!(iValue instanceof OStringBuilderSerializable)) {
                    if (iValue instanceof OAutoConvertToRecord)
                        ((OAutoConvertToRecord) iValue).setAutoConvertToRecord(false);
                    final Collection<OIdentifiable> coll;
                    // FIRST TIME: CONVERT THE ENTIRE COLLECTION
                    if (!(iValue instanceof ORecordLazySet)) {
                        final ORecordLazySet set = new ORecordLazySet(iRecord);
                        set.addAll((Collection<OIdentifiable>) iValue);
                        iRecord.field(iName, set);
                        coll = set;
                    } else
                        coll = (Collection<OIdentifiable>) iValue;
                    serializeSet(coll, iOutput);
                } else {
                    // LAZY SET
                    final OStringBuilderSerializable coll = (OStringBuilderSerializable) iValue;
                    coll.toStream(iOutput);
                }
                PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkSet2string"), "Serialize linkset to string", timer);
                break;
            }
        case LINKMAP:
            {
                iOutput.append(OStringSerializerHelper.MAP_BEGIN);
                Map<Object, Object> map = (Map<Object, Object>) iValue;
                // LINKED MAP
                if (map instanceof OLazyObjectMapInterface<?>)
                    ((OLazyObjectMapInterface<?>) map).setConvertToRecord(false);
                boolean invalidMap = false;
                try {
                    int items = 0;
                    for (Map.Entry<Object, Object> entry : map.entrySet()) {
                        if (items++ > 0)
                            iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
                        fieldTypeToString(iOutput, OType.STRING, entry.getKey());
                        iOutput.append(OStringSerializerHelper.ENTRY_SEPARATOR);
                        final Object link = linkToStream(iOutput, iRecord, entry.getValue());
                        if (link != null && !invalidMap)
                            // IDENTITY IS CHANGED, RE-SET INTO THE COLLECTION TO RECOMPUTE THE HASH
                            invalidMap = true;
                    }
                } finally {
                    if (map instanceof OLazyObjectMapInterface<?>) {
                        ((OLazyObjectMapInterface<?>) map).setConvertToRecord(true);
                    }
                }
                if (invalidMap) {
                    final ORecordLazyMap newMap = new ORecordLazyMap(iRecord, ODocument.RECORD_TYPE);
                    // REPLACE ALL CHANGED ITEMS
                    for (Map.Entry<Object, Object> entry : map.entrySet()) {
                        newMap.put(entry.getKey(), (OIdentifiable) entry.getValue());
                    }
                    map.clear();
                    iRecord.field(iName, newMap);
                }
                iOutput.append(OStringSerializerHelper.MAP_END);
                PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkMap2string"), "Serialize linkmap to string", timer);
                break;
            }
        case EMBEDDED:
            if (iValue instanceof ORecord) {
                iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
                toString((ORecord) iValue, iOutput, null, iObjHandler, false, true);
                iOutput.append(OStringSerializerHelper.EMBEDDED_END);
            } else if (iValue instanceof ODocumentSerializable) {
                final ODocument doc = ((ODocumentSerializable) iValue).toDocument();
                doc.field(ODocumentSerializable.CLASS_NAME, iValue.getClass().getName());
                iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
                toString(doc, iOutput, null, iObjHandler, false, true);
                iOutput.append(OStringSerializerHelper.EMBEDDED_END);
            } else if (iValue != null)
                iOutput.append(iValue.toString());
            PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embed2string"), "Serialize embedded to string", timer);
            break;
        case EMBEDDEDLIST:
            embeddedCollectionToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty, false);
            PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedList2string"), "Serialize embeddedlist to string", timer);
            break;
        case EMBEDDEDSET:
            embeddedCollectionToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty, true);
            PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedSet2string"), "Serialize embeddedset to string", timer);
            break;
        case EMBEDDEDMAP:
            {
                embeddedMapToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty);
                PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedMap2string"), "Serialize embeddedmap to string", timer);
                break;
            }
        case LINKBAG:
            {
                iOutput.append(OStringSerializerHelper.BAG_BEGIN);
                ((ORidBag) iValue).toStream(iOutput);
                iOutput.append(OStringSerializerHelper.BAG_END);
                break;
            }
        default:
            fieldTypeToString(iOutput, iType, iValue);
    }
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OStringBuilderSerializable(com.orientechnologies.orient.core.serialization.serializer.string.OStringBuilderSerializable) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) OAutoConvertToRecord(com.orientechnologies.orient.core.db.record.OAutoConvertToRecord) Entry(java.util.Map.Entry) OLazyIterator(com.orientechnologies.common.collection.OLazyIterator) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) ORecord(com.orientechnologies.orient.core.record.ORecord) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OLazyIterator(com.orientechnologies.common.collection.OLazyIterator) Iterator(java.util.Iterator) Collection(java.util.Collection) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OLazyObjectMapInterface(com.orientechnologies.orient.core.db.object.OLazyObjectMapInterface) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 7 with OMultiCollectionIterator

use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.

the class OTransactionIndexChangesPerKey method interpretAsNonUnique.

private Iterable<OTransactionIndexEntry> interpretAsNonUnique() {
    if (entries.size() < 2)
        return new ArrayList<OTransactionIndexEntry>(entries);
    if (entries.size() == 2) {
        final OTransactionIndexEntry entryA = entries.get(0);
        final OTransactionIndexEntry entryB = entries.get(1);
        final ORID ridA = entryA.value == null ? null : entryA.value.getIdentity();
        final ORID ridB = entryB.value == null ? null : entryB.value.getIdentity();
        if (ridA == null) {
            assert entryA.operation == OPERATION.REMOVE;
            if (entryB.operation == OPERATION.REMOVE)
                // both are removals
                return Collections.singletonList(entryA);
            // second operation is a put
            return new ArrayList<OTransactionIndexEntry>(entries);
        }
        if (ridB == null) {
            assert entryB.operation == OPERATION.REMOVE;
            // the only observable change is a key removal
            return Collections.singletonList(entryB);
        }
        if (ridA.equals(ridB)) {
            if (// both operations do the same on the same RID
            entryA.operation == entryB.operation)
                return Collections.singletonList(entryA);
            if (// remove operation cancels put
            entryB.operation == OPERATION.REMOVE)
                return Collections.emptyList();
            // put wins
            return Collections.singletonList(entryB);
        }
        // it's put-put on different RIDs
        return new ArrayList<OTransactionIndexEntry>(entries);
    }
    // 2. Calculate observable changes to index.
    final Set<OTransactionIndexEntry> changes = new HashSet<OTransactionIndexEntry>(entries.size());
    final Set<OTransactionIndexEntry> interpretation = new HashSet<OTransactionIndexEntry>(entries.size());
    boolean seenKeyRemoval = false;
    for (OTransactionIndexEntry entry : entries) {
        final OIdentifiable value = entry.value;
        final ORID rid = value == null ? null : value.getIdentity();
        switch(entry.operation) {
            case PUT:
                assert rid != null;
                interpretation.add(entry);
                break;
            case REMOVE:
                if (rid == null) {
                    interpretation.clear();
                    changes.clear();
                    // save key removal as it affects the key regardless of the RID
                    changes.add(entry);
                    seenKeyRemoval = true;
                } else {
                    if (!interpretation.remove(entry))
                        if (// no point in removing a RID from the removed key
                        !seenKeyRemoval)
                            // save that operation to remove the RID potentially created outside of a transaction
                            changes.add(entry);
                }
                break;
            case CLEAR:
            default:
                assert false;
                break;
        }
    }
    // 3. Build resulting equivalent operation sequence.
    // remove any removal which has a corresponding put, put is enough
    changes.removeAll(interpretation);
    if (// no observable changes except maybe some removals
    interpretation.isEmpty())
        // it's either single key removal or one or more RID removals
        return changes;
    if (!seenKeyRemoval && /* since key removal is an ordered operation */
    interpretation.size() < SET_ADD_THRESHOLD) {
        changes.addAll(interpretation);
        return changes;
    } else {
        final OMultiCollectionIterator<OTransactionIndexEntry> result = new OMultiCollectionIterator<OTransactionIndexEntry>();
        result.setAutoConvertToRecord(false);
        result.add(changes);
        result.add(interpretation);
        return result;
    }
}
Also used : OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 8 with OMultiCollectionIterator

use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.

the class OSQLFunctionOut 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("out", "in");
    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 9 with OMultiCollectionIterator

use of com.orientechnologies.common.collection.OMultiCollectionIterator in project orientdb by orientechnologies.

the class OrientVertex method getVertices.

/**
 * Returns a lazy iterable instance against vertices.
 *
 * @param iDirection The direction between OUT, IN or BOTH
 * @param iLabels    Optional varargs of Strings representing edge label to consider
 */
@Override
public Iterable<Vertex> getVertices(final Direction iDirection, final String... iLabels) {
    setCurrentGraphInThreadLocal();
    OrientBaseGraph.getEdgeClassNames(getGraph(), iLabels);
    OrientBaseGraph.encodeClassNames(iLabels);
    final ODocument doc = getRecord();
    final OMultiCollectionIterator<Vertex> iterable = new OMultiCollectionIterator<Vertex>();
    for (OTriple<String, Direction, String> connectionField : getConnectionFields(iDirection, iLabels)) {
        String fieldName = connectionField.getKey();
        OPair<Direction, String> connection = connectionField.getValue();
        final Object fieldValue = doc.rawField(fieldName);
        if (fieldValue != null)
            if (fieldValue instanceof OIdentifiable) {
                addSingleVertex(doc, iterable, fieldName, connection, fieldValue, iLabels);
            } else if (fieldValue instanceof Collection<?>) {
                Collection<?> coll = (Collection<?>) fieldValue;
                if (coll.size() == 1) {
                    // SINGLE ITEM: AVOID CALLING ITERATOR
                    if (coll instanceof ORecordLazyMultiValue)
                        addSingleVertex(doc, iterable, fieldName, connection, ((ORecordLazyMultiValue) coll).rawIterator().next(), iLabels);
                    else if (coll instanceof List<?>)
                        addSingleVertex(doc, iterable, fieldName, connection, ((List<?>) coll).get(0), iLabels);
                    else
                        addSingleVertex(doc, iterable, fieldName, connection, coll.iterator().next(), iLabels);
                } else {
                    // CREATE LAZY Iterable AGAINST COLLECTION FIELD
                    if (coll instanceof ORecordLazyMultiValue)
                        iterable.add(new OrientVertexIterator(this, coll, ((ORecordLazyMultiValue) coll).rawIterator(), connection, iLabels, coll.size()));
                    else
                        iterable.add(new OrientVertexIterator(this, coll, coll.iterator(), connection, iLabels, -1));
                }
            } else if (fieldValue instanceof ORidBag) {
                iterable.add(new OrientVertexIterator(this, fieldValue, ((ORidBag) fieldValue).rawIterator(), connection, iLabels, -1));
            }
    }
    return iterable;
}
Also used : PartitionVertex(com.tinkerpop.blueprints.util.wrappers.partition.PartitionVertex) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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