Search in sources :

Example 1 with ORidBag

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

the class ODocumentHelper method compareBags.

public static boolean compareBags(ODatabaseDocumentInternal iMyDb, ORidBag myFieldValue, ODatabaseDocumentInternal iOtherDb, ORidBag otherFieldValue, RIDMapper ridMapper) {
    final ORidBag myBag = myFieldValue;
    final ORidBag otherBag = otherFieldValue;
    final int mySize = makeDbCall(iMyDb, new ODbRelatedCall<Integer>() {

        public Integer call(ODatabaseDocumentInternal database) {
            return myBag.size();
        }
    });
    final int otherSize = makeDbCall(iOtherDb, new ODbRelatedCall<Integer>() {

        public Integer call(ODatabaseDocumentInternal database) {
            return otherBag.size();
        }
    });
    if (mySize != otherSize)
        return false;
    boolean oldMyAutoConvert;
    boolean oldOtherAutoConvert;
    oldMyAutoConvert = myBag.isAutoConvertToRecord();
    myBag.setAutoConvertToRecord(false);
    oldOtherAutoConvert = otherBag.isAutoConvertToRecord();
    otherBag.setAutoConvertToRecord(false);
    final ORidBag otherBagCopy = makeDbCall(iOtherDb, new ODbRelatedCall<ORidBag>() {

        @Override
        public ORidBag call(ODatabaseDocumentInternal database) {
            final ORidBag otherRidBag = new ORidBag();
            otherRidBag.setAutoConvertToRecord(false);
            for (OIdentifiable identifiable : otherBag) otherRidBag.add(identifiable);
            return otherRidBag;
        }
    });
    try {
        final Iterator<OIdentifiable> myIterator = makeDbCall(iMyDb, new ODbRelatedCall<Iterator<OIdentifiable>>() {

            public Iterator<OIdentifiable> call(ODatabaseDocumentInternal database) {
                return myBag.iterator();
            }
        });
        while (makeDbCall(iMyDb, new ODbRelatedCall<Boolean>() {

            public Boolean call(ODatabaseDocumentInternal database) {
                return myIterator.hasNext();
            }
        })) {
            final OIdentifiable myIdentifiable = makeDbCall(iMyDb, new ODbRelatedCall<OIdentifiable>() {

                @Override
                public OIdentifiable call(ODatabaseDocumentInternal database) {
                    return myIterator.next();
                }
            });
            final ORID otherRid;
            if (ridMapper != null) {
                ORID convertedRid = ridMapper.map(myIdentifiable.getIdentity());
                if (convertedRid != null)
                    otherRid = convertedRid;
                else
                    otherRid = myIdentifiable.getIdentity();
            } else
                otherRid = myIdentifiable.getIdentity();
            makeDbCall(iOtherDb, new ODbRelatedCall<Object>() {

                @Override
                public Object call(ODatabaseDocumentInternal database) {
                    otherBagCopy.remove(otherRid);
                    return null;
                }
            });
        }
        return makeDbCall(iOtherDb, new ODbRelatedCall<Boolean>() {

            @Override
            public Boolean call(ODatabaseDocumentInternal database) {
                return otherBagCopy.isEmpty();
            }
        });
    } finally {
        myBag.setAutoConvertToRecord(oldMyAutoConvert);
        otherBag.setAutoConvertToRecord(oldOtherAutoConvert);
    }
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ORID(com.orientechnologies.orient.core.id.ORID)

Example 2 with ORidBag

use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag 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 3 with ORidBag

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

the class OCommandExecutorSQLOptimizeDatabase method optimizeEdges.

private String optimizeEdges() {
    final ODatabaseDocumentInternal db = getDatabase();
    db.declareIntent(new OIntentMassiveInsert());
    try {
        long transformed = 0;
        if (db.getTransaction().isActive())
            db.commit();
        db.begin();
        try {
            final long totalEdges = db.countClass("E");
            long browsedEdges = 0;
            long lastLapBrowsed = 0;
            long lastLapTime = System.currentTimeMillis();
            for (ODocument doc : db.browseClass("E")) {
                if (Thread.currentThread().isInterrupted())
                    break;
                browsedEdges++;
                if (doc != null) {
                    if (doc.fields() == 2) {
                        final ORID edgeIdentity = doc.getIdentity();
                        final ODocument outV = doc.field("out");
                        final ODocument inV = doc.field("in");
                        // OUTGOING
                        final Object outField = outV.field("out_" + doc.getClassName());
                        if (outField instanceof ORidBag) {
                            final Iterator<OIdentifiable> it = ((ORidBag) outField).iterator();
                            while (it.hasNext()) {
                                OIdentifiable v = it.next();
                                if (edgeIdentity.equals(v)) {
                                    // REPLACE EDGE RID WITH IN-VERTEX RID
                                    it.remove();
                                    ((ORidBag) outField).add(inV.getIdentity());
                                    break;
                                }
                            }
                        }
                        outV.save();
                        // INCOMING
                        final Object inField = inV.field("in_" + doc.getClassName());
                        if (outField instanceof ORidBag) {
                            final Iterator<OIdentifiable> it = ((ORidBag) inField).iterator();
                            while (it.hasNext()) {
                                OIdentifiable v = it.next();
                                if (edgeIdentity.equals(v)) {
                                    // REPLACE EDGE RID WITH IN-VERTEX RID
                                    it.remove();
                                    ((ORidBag) inField).add(outV.getIdentity());
                                    break;
                                }
                            }
                        }
                        inV.save();
                        doc.delete();
                        if (++transformed % batch == 0) {
                            db.commit();
                            db.begin();
                        }
                        final long now = System.currentTimeMillis();
                        if (verbose && (now - lastLapTime > 2000)) {
                            final long elapsed = now - lastLapTime;
                            OLogManager.instance().info(this, "Browsed %,d of %,d edges, transformed %,d so far (%,d edges/sec)", browsedEdges, totalEdges, transformed, (((browsedEdges - lastLapBrowsed) * 1000 / elapsed)));
                            lastLapTime = System.currentTimeMillis();
                            lastLapBrowsed = browsedEdges;
                        }
                    }
                }
            }
            // LAST COMMIT
            db.commit();
        } finally {
            if (db.getTransaction().isActive())
                db.rollback();
        }
        return "Transformed " + transformed + " regular edges in lightweight edges";
    } finally {
        db.declareIntent(null);
    }
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OIntentMassiveInsert(com.orientechnologies.orient.core.intent.OIntentMassiveInsert) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with ORidBag

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

the class SBTreeBagDeleteTest method testDeleteRidbagTx.

@Test
public void testDeleteRidbagTx() {
    ODocument doc = new ODocument();
    ORidBag bag = new ORidBag();
    int size = OGlobalConfiguration.INDEX_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD.getValueAsInteger() * 2;
    for (int i = 0; i < size; i++) bag.add(new ORecordId(10, i));
    doc.field("bag", bag);
    ORID id = db.save(doc).getIdentity();
    bag = doc.field("bag");
    OBonsaiCollectionPointer pointer = bag.getPointer();
    db.begin();
    db.delete(doc);
    db.commit();
    doc = db.load(id);
    assertNull(doc);
    ((OSBTreeCollectionManagerShared) db.getSbTreeCollectionManager()).clear();
    OSBTreeBonsai<OIdentifiable, Integer> tree = db.getSbTreeCollectionManager().loadSBTree(pointer);
    assertNull(tree);
}
Also used : OBonsaiCollectionPointer(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OBonsaiCollectionPointer) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORID(com.orientechnologies.orient.core.id.ORID) OSBTreeCollectionManagerShared(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManagerShared) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 5 with ORidBag

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

the class OPropertyRidBagIndexDefinition method createValue.

@Override
public Object createValue(final List<?> params) {
    if (!(params.get(0) instanceof ORidBag))
        return null;
    final ORidBag ridBag = (ORidBag) params.get(0);
    final List<Object> values = new ArrayList<Object>();
    for (final OIdentifiable item : ridBag) {
        values.add(createSingleValue(item));
    }
    return values;
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ArrayList(java.util.ArrayList) 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