Search in sources :

Example 11 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.

the class OrientVertex method countEdges.

/**
 * (Blueprints Extension) Returns the number of edges connected to the current Vertex.
 *
 * @param iDirection The direction between OUT, IN or BOTH
 * @param iLabels    Optional labels as Strings to consider
 *
 * @return A long with the total edges found
 */
public long countEdges(final Direction iDirection, final String... iLabels) {
    checkIfAttached();
    long counter = 0;
    OrientBaseGraph.getEdgeClassNames(getGraph(), iLabels);
    OrientBaseGraph.encodeClassNames(iLabels);
    if (settings.isUseVertexFieldsForEdgeLabels() || iLabels == null || iLabels.length == 0) {
        // VERY FAST
        final ODocument doc = getRecord();
        for (String fieldName : doc.fieldNames()) {
            final OPair<Direction, String> connection = getConnection(iDirection, fieldName, iLabels);
            if (connection == null)
                // SKIP THIS FIELD
                continue;
            final Object fieldValue = doc.field(fieldName);
            if (fieldValue != null)
                if (fieldValue instanceof Collection<?>)
                    counter += ((Collection<?>) fieldValue).size();
                else if (fieldValue instanceof Map<?, ?>)
                    counter += ((Map<?, ?>) fieldValue).size();
                else if (fieldValue instanceof ORidBag) {
                    counter += ((ORidBag) fieldValue).size();
                } else {
                    counter++;
                }
        }
    } else {
        // SLOWER: BROWSE & FILTER
        for (Edge e : getEdges(iDirection, iLabels)) if (e != null)
            counter++;
    }
    return counter;
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 12 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.

the class OrientVertex method remove.

/**
 * Removes the current Vertex from the Graph. all the incoming and outgoing edges are automatically removed too.
 */
@Override
public void remove() {
    checkClass();
    final OrientBaseGraph graph = checkIfAttached();
    graph.setCurrentGraphInThreadLocal();
    graph.autoStartTransaction();
    final ODocument doc = getRecord();
    if (doc == null)
        throw ExceptionFactory.vertexWithIdDoesNotExist(this.getId());
    Map<String, List<ODocument>> treeRidbagEdgesToRemove = new HashMap<String, List<ODocument>>();
    if (!graph.getRawGraph().getTransaction().isActive()) {
        for (String fieldName : doc.fieldNames()) {
            final OPair<Direction, String> connection = getConnection(Direction.BOTH, fieldName);
            if (connection == null)
                // SKIP THIS FIELD
                continue;
            Object fv = doc.field(fieldName);
            if (fv instanceof ORidBag && !((ORidBag) fv).isEmbedded()) {
                List<ODocument> docs = new ArrayList<ODocument>();
                for (OIdentifiable id : (ORidBag) fv) docs.add(OrientBaseGraph.getDocument(id, true));
                treeRidbagEdgesToRemove.put(fieldName, docs);
            }
        }
    }
    // REMOVE THE VERTEX RECORD FIRST TO CATCH CME BEFORE EDGES ARE REMOVED
    super.removeRecord();
    // REMOVE THE VERTEX FROM MANUAL INDEXES
    final Iterator<Index<? extends Element>> it = graph.getIndices().iterator();
    if (it.hasNext()) {
        final Set<Edge> allEdges = new HashSet<Edge>();
        for (Edge e : getEdges(Direction.BOTH)) allEdges.add(e);
        while (it.hasNext()) {
            final Index<? extends Element> index = it.next();
            if (Vertex.class.isAssignableFrom(index.getIndexClass())) {
                OrientIndex<OrientVertex> idx = (OrientIndex<OrientVertex>) index;
                idx.removeElement(this);
            }
            if (Edge.class.isAssignableFrom(index.getIndexClass())) {
                OrientIndex<OrientEdge> idx = (OrientIndex<OrientEdge>) index;
                for (Edge e : allEdges) idx.removeElement((OrientEdge) e);
            }
        }
    }
    for (Map.Entry<String, List<ODocument>> entry : treeRidbagEdgesToRemove.entrySet()) {
        doc.removeField(entry.getKey());
        Iterator<ODocument> iter = entry.getValue().iterator();
        while (iter.hasNext()) {
            ODocument docEdge = iter.next();
            OrientBaseGraph.deleteEdgeIfAny(docEdge, false);
        }
    }
    graph.removeEdgesInternal(this, doc, null, true, settings.isUseVertexFieldsForEdgeLabels(), settings.isAutoScaleEdgeType());
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 13 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.

the class ORecordSerializerJSON method getValueAsCollection.

private Object getValueAsCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
    // remove square brackets
    iFieldValue = iFieldValue.substring(1, iFieldValue.length() - 1);
    if (iType == OType.LINKBAG) {
        final ORidBag bag = new ORidBag();
        parseCollection(iRecord, iFieldValue, iType, OType.LINK, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {

            @Override
            public void visitItem(Object item) {
                bag.add((OIdentifiable) item);
            }
        });
        return bag;
    } else if (iType == OType.LINKSET) {
        return getValueAsLinkedCollection(new ORecordLazySet(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else if (iType == OType.LINKLIST) {
        return getValueAsLinkedCollection(new ORecordLazyList(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else if (iType == OType.EMBEDDEDSET) {
        return getValueAsEmbeddedCollection(new OTrackedSet<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else {
        return getValueAsEmbeddedCollection(new OTrackedList<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    }
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 14 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.

the class ORecordSerializerBinaryV0 method deserializeValue.

@Override
public Object deserializeValue(final BytesContainer bytes, final OType type, final ODocument ownerDocument) {
    Object value = null;
    switch(type) {
        case INTEGER:
            value = OVarIntSerializer.readAsInteger(bytes);
            break;
        case LONG:
            value = OVarIntSerializer.readAsLong(bytes);
            break;
        case SHORT:
            value = OVarIntSerializer.readAsShort(bytes);
            break;
        case STRING:
            value = readString(bytes);
            break;
        case DOUBLE:
            value = Double.longBitsToDouble(readLong(bytes));
            break;
        case FLOAT:
            value = Float.intBitsToFloat(readInteger(bytes));
            break;
        case BYTE:
            value = readByte(bytes);
            break;
        case BOOLEAN:
            value = readByte(bytes) == 1;
            break;
        case DATETIME:
            value = new Date(OVarIntSerializer.readAsLong(bytes));
            break;
        case DATE:
            long savedTime = OVarIntSerializer.readAsLong(bytes) * MILLISEC_PER_DAY;
            savedTime = convertDayToTimezone(TimeZone.getTimeZone("GMT"), ODateHelper.getDatabaseTimeZone(), savedTime);
            value = new Date(savedTime);
            break;
        case EMBEDDED:
            value = new ODocument();
            deserialize((ODocument) value, bytes);
            if (((ODocument) value).containsField(ODocumentSerializable.CLASS_NAME)) {
                String className = ((ODocument) value).field(ODocumentSerializable.CLASS_NAME);
                try {
                    Class<?> clazz = Class.forName(className);
                    ODocumentSerializable newValue = (ODocumentSerializable) clazz.newInstance();
                    newValue.fromDocument((ODocument) value);
                    value = newValue;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else
                ODocumentInternal.addOwner((ODocument) value, ownerDocument);
            break;
        case EMBEDDEDSET:
            value = readEmbeddedSet(bytes, ownerDocument);
            break;
        case EMBEDDEDLIST:
            value = readEmbeddedList(bytes, ownerDocument);
            break;
        case LINKSET:
            value = readLinkCollection(bytes, new ORecordLazySet(ownerDocument));
            break;
        case LINKLIST:
            value = readLinkCollection(bytes, new ORecordLazyList(ownerDocument));
            break;
        case BINARY:
            value = readBinary(bytes);
            break;
        case LINK:
            value = readOptimizedLink(bytes);
            break;
        case LINKMAP:
            value = readLinkMap(bytes, ownerDocument);
            break;
        case EMBEDDEDMAP:
            value = readEmbeddedMap(bytes, ownerDocument);
            break;
        case DECIMAL:
            value = ODecimalSerializer.INSTANCE.deserialize(bytes.bytes, bytes.offset);
            bytes.skip(ODecimalSerializer.INSTANCE.getObjectSize(bytes.bytes, bytes.offset));
            break;
        case LINKBAG:
            ORidBag bag = new ORidBag();
            bag.fromStream(bytes);
            bag.setOwner(ownerDocument);
            value = bag;
            break;
        case TRANSIENT:
            break;
        case CUSTOM:
            try {
                String className = readString(bytes);
                Class<?> clazz = Class.forName(className);
                OSerializableStream stream = (OSerializableStream) clazz.newInstance();
                stream.fromStream(readBinary(bytes));
                if (stream instanceof OSerializableWrapper)
                    value = ((OSerializableWrapper) stream).getSerializable();
                else
                    value = stream;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        case ANY:
            break;
    }
    return value;
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OException(com.orientechnologies.common.exception.OException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) OValidationException(com.orientechnologies.orient.core.exception.OValidationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 15 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.

the class ORecordSerializerNetworkV0 method deserializeValue.

public Object deserializeValue(BytesContainer bytes, OType type, ODocument document) {
    Object value = null;
    switch(type) {
        case INTEGER:
            value = OVarIntSerializer.readAsInteger(bytes);
            break;
        case LONG:
            value = OVarIntSerializer.readAsLong(bytes);
            break;
        case SHORT:
            value = OVarIntSerializer.readAsShort(bytes);
            break;
        case STRING:
            value = readString(bytes);
            break;
        case DOUBLE:
            value = Double.longBitsToDouble(readLong(bytes));
            break;
        case FLOAT:
            value = Float.intBitsToFloat(readInteger(bytes));
            break;
        case BYTE:
            value = readByte(bytes);
            break;
        case BOOLEAN:
            value = readByte(bytes) == 1;
            break;
        case DATETIME:
            value = new Date(OVarIntSerializer.readAsLong(bytes));
            break;
        case DATE:
            long savedTime = OVarIntSerializer.readAsLong(bytes) * MILLISEC_PER_DAY;
            savedTime = convertDayToTimezone(TimeZone.getTimeZone("GMT"), ODateHelper.getDatabaseTimeZone(), savedTime);
            value = new Date(savedTime);
            break;
        case EMBEDDED:
            value = new ODocument();
            deserialize((ODocument) value, bytes);
            if (((ODocument) value).containsField(ODocumentSerializable.CLASS_NAME)) {
                String className = ((ODocument) value).field(ODocumentSerializable.CLASS_NAME);
                try {
                    Class<?> clazz = Class.forName(className);
                    ODocumentSerializable newValue = (ODocumentSerializable) clazz.newInstance();
                    newValue.fromDocument((ODocument) value);
                    value = newValue;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else
                ODocumentInternal.addOwner((ODocument) value, document);
            break;
        case EMBEDDEDSET:
            value = readEmbeddedCollection(bytes, new OTrackedSet<Object>(document), document);
            break;
        case EMBEDDEDLIST:
            value = readEmbeddedCollection(bytes, new OTrackedList<Object>(document), document);
            break;
        case LINKSET:
            value = readLinkCollection(bytes, new ORecordLazySet(document));
            break;
        case LINKLIST:
            value = readLinkCollection(bytes, new ORecordLazyList(document));
            break;
        case BINARY:
            value = readBinary(bytes);
            break;
        case LINK:
            value = readOptimizedLink(bytes);
            break;
        case LINKMAP:
            value = readLinkMap(bytes, document);
            break;
        case EMBEDDEDMAP:
            value = readEmbeddedMap(bytes, document);
            break;
        case DECIMAL:
            value = ODecimalSerializer.INSTANCE.deserialize(bytes.bytes, bytes.offset);
            bytes.skip(ODecimalSerializer.INSTANCE.getObjectSize(bytes.bytes, bytes.offset));
            break;
        case LINKBAG:
            ORidBag bag = new ORidBag();
            bag.fromStream(bytes);
            bag.setOwner(document);
            value = bag;
            break;
        case TRANSIENT:
            break;
        case CUSTOM:
            try {
                String className = readString(bytes);
                Class<?> clazz = Class.forName(className);
                OSerializableStream stream = (OSerializableStream) clazz.newInstance();
                stream.fromStream(readBinary(bytes));
                if (stream instanceof OSerializableWrapper)
                    value = ((OSerializableWrapper) stream).getSerializable();
                else
                    value = stream;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        case ANY:
            break;
    }
    return value;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OException(com.orientechnologies.common.exception.OException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OValidationException(com.orientechnologies.orient.core.exception.OValidationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)135 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)103 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)68 ORecordId (com.orientechnologies.orient.core.id.ORecordId)37 ArrayList (java.util.ArrayList)27 Test (org.testng.annotations.Test)24 ORID (com.orientechnologies.orient.core.id.ORID)21 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)15 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)13 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)11 OStorage (com.orientechnologies.orient.core.storage.OStorage)7 HashSet (java.util.HashSet)7 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)6 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)6 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)4 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)3