Search in sources :

Example 46 with OIdentifiable

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

the class OCommandExecutorSQLDelete method execute.

public Object execute(final Map<Object, Object> iArgs) {
    if (query == null && indexName == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    if (!returning.equalsIgnoreCase("COUNT"))
        allDeletedRecords = new ArrayList<ORecord>();
    if (query != null) {
        // AGAINST CLUSTERS AND CLASSES
        query.setContext(getContext());
        Object prevLockValue = query.getContext().getVariable("$locking");
        if (lockStrategy.equals("RECORD"))
            query.getContext().setVariable("$locking", OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK);
        query.execute(iArgs);
        query.getContext().setVariable("$locking", prevLockValue);
        if (returning.equalsIgnoreCase("COUNT"))
            // RETURNS ONLY THE COUNT
            return recordCount;
        else
            // RETURNS ALL THE DELETED RECORDS
            return allDeletedRecords;
    } else {
        // AGAINST INDEXES
        if (compiledFilter != null)
            compiledFilter.bindParameters(iArgs);
        final OIndex index = getDatabase().getMetadata().getIndexManager().getIndex(indexName);
        if (index == null)
            throw new OCommandExecutionException("Target index '" + indexName + "' not found");
        Object key = null;
        Object value = VALUE_NOT_FOUND;
        if (compiledFilter == null || compiledFilter.getRootCondition() == null) {
            if (returning.equalsIgnoreCase("COUNT")) {
                // RETURNS ONLY THE COUNT
                final long total = index.getSize();
                index.clear();
                return total;
            } else {
                // RETURNS ALL THE DELETED RECORDS
                OIndexCursor cursor = index.cursor();
                Map.Entry<Object, OIdentifiable> entry;
                while ((entry = cursor.nextEntry()) != null) {
                    OIdentifiable rec = entry.getValue();
                    rec = rec.getRecord();
                    if (rec != null)
                        allDeletedRecords.add((ORecord) rec);
                }
                index.clear();
                return allDeletedRecords;
            }
        } else {
            if (KEYWORD_KEY.equalsIgnoreCase(compiledFilter.getRootCondition().getLeft().toString()))
                // FOUND KEY ONLY
                key = getIndexKey(index.getDefinition(), compiledFilter.getRootCondition().getRight());
            else if (KEYWORD_RID.equalsIgnoreCase(compiledFilter.getRootCondition().getLeft().toString())) {
                // BY RID
                value = OSQLHelper.getValue(compiledFilter.getRootCondition().getRight());
            } else if (compiledFilter.getRootCondition().getLeft() instanceof OSQLFilterCondition) {
                // KEY AND VALUE
                final OSQLFilterCondition leftCondition = (OSQLFilterCondition) compiledFilter.getRootCondition().getLeft();
                if (KEYWORD_KEY.equalsIgnoreCase(leftCondition.getLeft().toString()))
                    key = getIndexKey(index.getDefinition(), leftCondition.getRight());
                final OSQLFilterCondition rightCondition = (OSQLFilterCondition) compiledFilter.getRootCondition().getRight();
                if (KEYWORD_RID.equalsIgnoreCase(rightCondition.getLeft().toString()))
                    value = OSQLHelper.getValue(rightCondition.getRight());
            }
            final boolean result;
            if (value != VALUE_NOT_FOUND) {
                assert key != null;
                result = index.remove(key, (OIdentifiable) value);
            } else
                result = index.remove(key);
            if (returning.equalsIgnoreCase("COUNT"))
                return result ? 1 : 0;
            else
                // TODO: REFACTOR INDEX TO RETURN DELETED ITEMS
                throw new UnsupportedOperationException();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) ORecord(com.orientechnologies.orient.core.record.ORecord) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) Map(java.util.Map)

Example 47 with OIdentifiable

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

the class ORecordsReturnHandler method evaluateExpression.

private Object evaluateExpression(final ODocument record) {
    if (returnExpression == null) {
        return record;
    } else {
        final Object itemResult;
        final ODocument wrappingDoc;
        context.setVariable("current", record);
        itemResult = OSQLHelper.getValue(returnExpression, (ODocument) ((OIdentifiable) record).getRecord(), context);
        if (itemResult instanceof OIdentifiable)
            return itemResult;
        // WRAP WITH ODOCUMENT TO BE TRANSFERRED THROUGH BINARY DRIVER
        return new ODocument("value", itemResult);
    }
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 48 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable 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 49 with OIdentifiable

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

the class OQueryOperatorTraverse method traverse.

@SuppressWarnings("unchecked")
private boolean traverse(Object iTarget, final OSQLFilterCondition iCondition, final int iLevel, final Set<ORID> iEvaluatedRecords, final OCommandContext iContext) {
    if (endDeepLevel > -1 && iLevel > endDeepLevel)
        return false;
    if (iTarget instanceof OIdentifiable) {
        if (iEvaluatedRecords.contains(((OIdentifiable) iTarget).getIdentity()))
            // ALREADY EVALUATED
            return false;
        // TRANSFORM THE ORID IN ODOCUMENT
        iTarget = ((OIdentifiable) iTarget).getRecord();
    }
    if (iTarget instanceof ODocument) {
        final ODocument target = (ODocument) iTarget;
        iEvaluatedRecords.add(target.getIdentity());
        if (target.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED)
            try {
                target.load();
            } catch (final ORecordNotFoundException e) {
                // INVALID RID
                return false;
            }
        if (iLevel >= startDeepLevel && (Boolean) iCondition.evaluate(target, null, iContext) == Boolean.TRUE)
            return true;
        // TRAVERSE THE DOCUMENT ITSELF
        if (cfgFields != null)
            for (final String cfgField : cfgFields) {
                if (cfgField.equalsIgnoreCase(OSQLFilterItemFieldAny.FULL_NAME)) {
                    // ANY
                    for (final String fieldName : target.fieldNames()) if (traverse(target.rawField(fieldName), iCondition, iLevel + 1, iEvaluatedRecords, iContext))
                        return true;
                } else if (cfgField.equalsIgnoreCase(OSQLFilterItemFieldAny.FULL_NAME)) {
                    // ALL
                    for (final String fieldName : target.fieldNames()) if (!traverse(target.rawField(fieldName), iCondition, iLevel + 1, iEvaluatedRecords, iContext))
                        return false;
                    return true;
                } else {
                    if (traverse(target.rawField(cfgField), iCondition, iLevel + 1, iEvaluatedRecords, iContext))
                        return true;
                }
            }
    } else if (iTarget instanceof OQueryRuntimeValueMulti) {
        final OQueryRuntimeValueMulti multi = (OQueryRuntimeValueMulti) iTarget;
        for (final Object o : multi.getValues()) {
            if (traverse(o, iCondition, iLevel + 1, iEvaluatedRecords, iContext) == Boolean.TRUE)
                return true;
        }
    } else if (iTarget instanceof Map<?, ?>) {
        final Map<Object, Object> map = (Map<Object, Object>) iTarget;
        for (final Object o : map.values()) {
            if (traverse(o, iCondition, iLevel + 1, iEvaluatedRecords, iContext) == Boolean.TRUE)
                return true;
        }
    } else if (OMultiValue.isMultiValue(iTarget)) {
        final Iterable<Object> collection = OMultiValue.getMultiValueIterable(iTarget, false);
        for (final Object o : collection) {
            if (traverse(o, iCondition, iLevel + 1, iEvaluatedRecords, iContext) == Boolean.TRUE)
                return true;
        }
    } else if (iTarget instanceof Iterator) {
        final Iterator iterator = (Iterator) iTarget;
        while (iterator.hasNext()) {
            if (traverse(iterator.next(), iCondition, iLevel + 1, iEvaluatedRecords, iContext) == Boolean.TRUE)
                return true;
        }
    }
    return false;
}
Also used : Iterator(java.util.Iterator) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Map(java.util.Map) OQueryRuntimeValueMulti(com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 50 with OIdentifiable

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

the class OFunctionCall method execute.

private Object execute(Object targetObjects, OCommandContext ctx, String name) {
    List<Object> paramValues = new ArrayList<Object>();
    OIdentifiable record = ctx == null ? null : (OIdentifiable) ctx.getVariable("$current");
    if (record == null && targetObjects instanceof OIdentifiable) {
        record = (OIdentifiable) targetObjects;
    }
    for (OExpression expr : this.params) {
        paramValues.add(expr.execute(record, ctx));
    }
    OSQLFunction function = OSQLEngine.getInstance().getFunction(name);
    if (function != null) {
        return function.execute(targetObjects, record, null, paramValues.toArray(), ctx);
    }
    throw new UnsupportedOperationException("This expression is not currently supported: " + toString());
}
Also used : ArrayList(java.util.ArrayList) OSQLFunction(com.orientechnologies.orient.core.sql.functions.OSQLFunction) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Aggregations

OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)536 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)278 ORecordId (com.orientechnologies.orient.core.id.ORecordId)120 Test (org.testng.annotations.Test)104 HashSet (java.util.HashSet)89 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)79 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)70 ORID (com.orientechnologies.orient.core.id.ORID)56 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)47 Test (org.junit.Test)43 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)42 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)41 ArrayList (java.util.ArrayList)39 ORecord (com.orientechnologies.orient.core.record.ORecord)35 Map (java.util.Map)31 ByteBuffer (java.nio.ByteBuffer)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)22 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)22 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)21