Search in sources :

Example 16 with ORidBag

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

the class ODocumentTest method testChangeTypeOnValueSet.

@Test
public void testChangeTypeOnValueSet() throws Exception {
    ODocument doc = new ODocument();
    doc.field("link", new ORecordId(1, 2));
    ORecordSerializer ser = ODatabaseDocumentTx.getDefaultSerializer();
    byte[] bytes = ser.toStream(doc, false);
    doc = new ODocument();
    ser.fromStream(bytes, doc, null);
    assertEquals(doc.fieldType("link"), OType.LINK);
    doc.field("link", new ORidBag());
    assertNotEquals(doc.fieldType("link"), OType.LINK);
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.testng.annotations.Test)

Example 17 with ORidBag

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

the class ODocumentValidationTest method testValidLinkCollectionsUpdate.

@Test
public void testValidLinkCollectionsUpdate() {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + ODocumentValidationTest.class.getSimpleName());
    db.create();
    try {
        OClass clazz = db.getMetadata().getSchema().createClass("Validation");
        OClass clazz1 = db.getMetadata().getSchema().createClass("Validation1");
        clazz.createProperty("linkList", OType.LINKLIST).setLinkedClass(clazz1);
        clazz.createProperty("linkSet", OType.LINKSET).setLinkedClass(clazz1);
        clazz.createProperty("linkMap", OType.LINKMAP).setLinkedClass(clazz1);
        clazz.createProperty("linkBag", OType.LINKBAG).setLinkedClass(clazz1);
        ODocument d = new ODocument(clazz);
        d.field("link", new ODocument(clazz1));
        d.field("embedded", new ODocument(clazz1));
        List<ODocument> list = Arrays.asList(new ODocument(clazz1));
        d.field("linkList", list);
        Set<ODocument> set = new HashSet<ODocument>(list);
        d.field("linkSet", set);
        d.field("linkBag", new ORidBag());
        Map<String, ODocument> map = new HashMap<String, ODocument>();
        map.put("a", new ODocument(clazz1));
        d.field("linkMap", map);
        db.save(d);
        try {
            ODocument newD = d.copy();
            ((Collection) newD.field("linkList")).add(new ODocument(clazz));
            newD.validate();
            AssertJUnit.fail();
        } catch (OValidationException v) {
        }
        try {
            ODocument newD = d.copy();
            ((Collection) newD.field("linkSet")).add(new ODocument(clazz));
            newD.validate();
            AssertJUnit.fail();
        } catch (OValidationException v) {
        }
        try {
            ODocument newD = d.copy();
            ((ORidBag) newD.field("linkBag")).add(new ODocument(clazz));
            newD.validate();
            AssertJUnit.fail();
        } catch (OValidationException v) {
        }
        try {
            ODocument newD = d.copy();
            ((Map<String, ODocument>) newD.field("linkMap")).put("a", new ODocument(clazz));
            newD.validate();
            AssertJUnit.fail();
        } catch (OValidationException v) {
        }
    } finally {
        db.drop();
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Test(org.testng.annotations.Test)

Example 18 with ORidBag

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

the class OBonsaiTreeRepair method repairDatabaseRidbags.

public void repairDatabaseRidbags(ODatabaseDocument db, OCommandOutputListener outputListener) {
    message(outputListener, "Repair of ridbags is started ...\n");
    final OMetadata metadata = db.getMetadata();
    final OSchema schema = metadata.getSchema();
    final OClass edgeClass = schema.getClass(OrientEdgeType.CLASS_NAME);
    if (edgeClass != null) {
        final HashMap<String, Set<ORID>> processedVertexes = new HashMap<String, Set<ORID>>();
        final long countEdges = db.countClass(edgeClass.getName());
        message(outputListener, countEdges + " will be processed.");
        long counter = 0;
        for (ODocument edge : db.browseClass(edgeClass.getName())) {
            try {
                final String label;
                if (edge.field(OrientElement.LABEL_FIELD_NAME) != null) {
                    label = edge.field(OrientElement.LABEL_FIELD_NAME);
                } else if (!edge.getClassName().equals(edgeClass.getName())) {
                    label = edge.getClassName();
                } else {
                    counter++;
                    continue;
                }
                final ODocument inVertex = edge.<OIdentifiable>field(OrientBaseGraph.CONNECTION_IN).getRecord();
                final ODocument outVertex = edge.<OIdentifiable>field(OrientBaseGraph.CONNECTION_OUT).getRecord();
                final String inVertexName = OrientVertex.getConnectionFieldName(Direction.IN, label, true);
                final String outVertexName = OrientVertex.getConnectionFieldName(Direction.OUT, label, true);
                Set<ORID> inVertexes = processedVertexes.get(inVertexName);
                if (inVertexes == null) {
                    inVertexes = new HashSet<ORID>();
                    processedVertexes.put(inVertexName, inVertexes);
                }
                Set<ORID> outVertexes = processedVertexes.get(outVertexName);
                if (outVertexes == null) {
                    outVertexes = new HashSet<ORID>();
                    processedVertexes.put(outVertexName, outVertexes);
                }
                if (inVertex.field(inVertexName) instanceof ORidBag) {
                    if (inVertexes.add(inVertex.getIdentity())) {
                        inVertex.field(inVertexName, new ORidBag());
                    }
                    final ORidBag inRidBag = inVertex.field(inVertexName);
                    inRidBag.add(edge.getIdentity());
                    inVertex.save();
                }
                if (outVertex.field(outVertexName) instanceof ORidBag) {
                    if (outVertexes.add(outVertex.getIdentity())) {
                        outVertex.field(outVertexName, new ORidBag());
                    }
                    final ORidBag outRidBag = outVertex.field(outVertexName);
                    outRidBag.add(edge.getIdentity());
                    outVertex.save();
                }
                counter++;
                if (counter > 0 && counter % 1000 == 0)
                    message(outputListener, counter + " edges were processed out of " + countEdges + " \n.");
            } catch (Exception e) {
                e.printStackTrace();
                message(outputListener, "Error during processing of edge with id " + edge.getIdentity() + "\n");
            }
        }
        message(outputListener, "Processed " + counter + " from " + countEdges + ".");
    }
    message(outputListener, "repair of ridbags is completed\n");
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OMetadata(com.orientechnologies.orient.core.metadata.OMetadata) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 19 with ORidBag

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

the class OGraphRepair method isEdgeBroken.

private boolean isEdgeBroken(final OIdentifiable vertex, final String fieldName, final Direction direction, final OIdentifiable edgeRID, final ORepairStats stats, final boolean useVertexFieldsForEdgeLabels) {
    onScannedLink(stats, edgeRID);
    boolean broken = false;
    if (edgeRID == null)
        // RID NULL
        broken = true;
    else {
        ODocument record = null;
        try {
            record = edgeRID.getIdentity().getRecord();
        } catch (ORecordNotFoundException e) {
            broken = true;
        }
        if (record == null)
            // RECORD DELETED
            broken = true;
        else {
            final OImmutableClass immutableClass = ODocumentInternal.getImmutableSchemaClass(record);
            if (immutableClass == null || (!immutableClass.isVertexType() && !immutableClass.isEdgeType()))
                // INVALID RECORD TYPE: NULL OR NOT GRAPH TYPE
                broken = true;
            else {
                if (immutableClass.isVertexType()) {
                    // VERTEX -> LIGHTWEIGHT EDGE
                    final String inverseFieldName = OrientVertex.getInverseConnectionFieldName(fieldName, useVertexFieldsForEdgeLabels);
                    // CHECK THE VERTEX IS IN INVERSE EDGE CONTAINS
                    final Object inverseEdgeContainer = record.field(inverseFieldName);
                    if (inverseEdgeContainer == null)
                        // NULL CONTAINER
                        broken = true;
                    else {
                        if (inverseEdgeContainer instanceof OIdentifiable) {
                            if (!inverseEdgeContainer.equals(vertex))
                                // NOT THE SAME
                                broken = true;
                        } else if (inverseEdgeContainer instanceof Collection<?>) {
                            if (!((Collection) inverseEdgeContainer).contains(vertex))
                                // NOT IN COLLECTION
                                broken = true;
                        } else if (inverseEdgeContainer instanceof ORidBag) {
                            if (!((ORidBag) inverseEdgeContainer).contains(vertex))
                                // NOT IN RIDBAG
                                broken = true;
                        }
                    }
                } else {
                    // EDGE -> REGULAR EDGE, OK
                    final OIdentifiable backRID = OrientEdge.getConnection(record, direction);
                    if (backRID == null || !backRID.equals(vertex))
                        // BACK RID POINTS TO ANOTHER VERTEX
                        broken = true;
                }
            }
        }
    }
    if (broken) {
        onRemovedLink(stats, edgeRID);
        return true;
    }
    return false;
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) Collection(java.util.Collection) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 20 with ORidBag

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

the class OrientEdge method dropEdgeFromVertex.

protected boolean dropEdgeFromVertex(final OIdentifiable iEdge, final ODocument iVertex, final String iFieldName, final Object iFieldValue) {
    if (iFieldValue == null) {
        // NO EDGE? WARN
        OLogManager.instance().debug(this, "Edge not found in vertex's property %s.%s while removing the edge %s", iVertex.getIdentity(), iFieldName, iEdge.getIdentity());
        return false;
    } else if (iFieldValue instanceof OIdentifiable) {
        if (iFieldValue.equals(iEdge))
            iVertex.removeField(iFieldName);
        else {
            // NO EDGE? WARN
            OLogManager.instance().warn(this, "Edge not found in vertex's property %s.%s link while removing the edge %s", iVertex.getIdentity(), iFieldName, iEdge.getIdentity());
            return false;
        }
    } else if (iFieldValue instanceof ORidBag) {
        // ALREADY A SET: JUST REMOVE THE NEW EDGE
        final ORidBag bag = (ORidBag) iFieldValue;
        bag.remove(iEdge);
    } else if (iFieldValue instanceof Collection<?>) {
        // CONVERT COLLECTION IN TREE-SET AND REMOVE THE EDGE
        final Collection<Object> coll = (Collection<Object>) iFieldValue;
        if (!coll.remove(iEdge)) {
            OLogManager.instance().warn(this, "Edge not found in vertex's property %s.%s set while removing the edge %s", iVertex.getIdentity(), iFieldName, iEdge.getIdentity());
            return false;
        }
        if (coll.size() == 1)
            iVertex.field(iFieldName, coll.iterator().next());
        else if (coll.size() == 0)
            iVertex.removeField(iFieldName);
    } else
        throw new IllegalStateException("Wrong type found in the field '" + iFieldName + "': " + iFieldValue.getClass());
    return true;
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

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