Search in sources :

Example 16 with ORecord

use of com.orientechnologies.orient.core.record.ORecord 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 17 with ORecord

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

the class OStreamSerializerAnyRecord method toStream.

public byte[] toStream(Object iObject) throws IOException {
    if (iObject == null)
        return null;
    if (((ORecord) iObject).getIdentity() == null)
        throw new OSerializationException("Cannot serialize record without identity. Store it before serialization.");
    final StringBuilder buffer = OStreamSerializerHelper.writeRecordType(iObject.getClass(), new StringBuilder(1024));
    buffer.append(((ORecord) iObject).getIdentity().toString());
    return buffer.toString().getBytes("UTF-8");
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 18 with ORecord

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

the class OStreamSerializerAnyRecord method fromStream.

/**
 * Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
 */
public Object fromStream(byte[] iStream) throws IOException {
    if (iStream == null || iStream.length == 0)
        // NULL VALUE
        return null;
    final String stream = new String(iStream, "UTF-8");
    Class<?> cls = null;
    try {
        final StringBuilder content = new StringBuilder(1024);
        cls = OStreamSerializerHelper.readRecordType(stream, content);
        // TRY WITH THE DATABASE CONSTRUCTOR
        for (Constructor<?> c : cls.getDeclaredConstructors()) {
            Class<?>[] params = c.getParameterTypes();
            if (params.length == 2 && params[1].equals(ORID.class)) {
                ORecord rec = (ORecord) c.newInstance(new ORecordId(content.toString()));
                // rec.load();
                return rec;
            }
        }
    } catch (Exception e) {
        throw OException.wrapException(new OSerializationException("Error on unmarshalling content. Class " + (cls != null ? cls.getName() : "?")), e);
    }
    throw new OSerializationException("Cannot unmarshall the record since the serialized object of class " + (cls != null ? cls.getSimpleName() : "?") + " has no constructor with suitable parameters: (ORID)");
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecord(com.orientechnologies.orient.core.record.ORecord) ORID(com.orientechnologies.orient.core.id.ORID) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Example 19 with ORecord

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

the class OObjectDatabaseTx method delete.

@Override
public ODatabaseObject delete(final ORID iRID) {
    checkOpeness();
    if (iRID == null)
        return this;
    final ORecord record = iRID.getRecord();
    if (record instanceof ODocument) {
        Object iPojo = getUserObjectByRecord(record, null);
        deleteCascade((ODocument) record);
        underlying.delete(record);
        if (getTransaction() instanceof OTransactionNoTx)
            unregisterPojo(iPojo, (ODocument) record);
    }
    return this;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) OMetadataObject(com.orientechnologies.orient.object.metadata.OMetadataObject) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OTransactionNoTx(com.orientechnologies.orient.core.tx.OTransactionNoTx)

Example 20 with ORecord

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

the class OObjectLazySet method convertAllInternal.

protected void convertAllInternal() {
    if (converted || !convertToRecord)
        return;
    final Set<Object> copy = new HashSet<Object>(underlying);
    super.clear();
    final ODatabasePojoAbstract<TYPE> database = getDatabase();
    for (Object e : copy) {
        if (e != null) {
            if (e instanceof ORID)
                super.add(database.getUserObjectByRecord(((ODatabaseDocument) getDatabase().getUnderlying()).load((ORID) e, fetchPlan), fetchPlan));
            else if (e instanceof ODocument)
                super.add(database.getUserObjectByRecord((ORecord) e, fetchPlan));
            else
                super.add((TYPE) e);
        }
    }
    converted = true;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ProxyObject(javassist.util.proxy.ProxyObject) ORID(com.orientechnologies.orient.core.id.ORID) HashSet(java.util.HashSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecord (com.orientechnologies.orient.core.record.ORecord)194 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)91 ORecordId (com.orientechnologies.orient.core.id.ORecordId)40 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)36 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)27 ORID (com.orientechnologies.orient.core.id.ORID)27 IOException (java.io.IOException)21 OException (com.orientechnologies.common.exception.OException)17 Test (org.junit.Test)16 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)15 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)15 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)15 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)14 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)8 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)8 Test (org.testng.annotations.Test)8 OIOException (com.orientechnologies.common.io.OIOException)7