Search in sources :

Example 81 with ORecord

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

the class ORidBag method toStream.

public int toStream(BytesContainer bytesContainer) throws OSerializationException {
    final ORecordSerializationContext context = ORecordSerializationContext.getContext();
    if (context != null) {
        if (isEmbedded() && ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager() != null && delegate.size() >= topThreshold) {
            ORidBagDelegate oldDelegate = delegate;
            delegate = new OSBTreeRidBag();
            boolean oldAutoConvert = oldDelegate.isAutoConvertToRecord();
            oldDelegate.setAutoConvertToRecord(false);
            for (OIdentifiable identifiable : oldDelegate) delegate.add(identifiable);
            final ORecord owner = oldDelegate.getOwner();
            delegate.setOwner(owner);
            for (OMultiValueChangeListener<OIdentifiable, OIdentifiable> listener : oldDelegate.getChangeListeners()) delegate.addChangeListener(listener);
            owner.setDirty();
            oldDelegate.setAutoConvertToRecord(oldAutoConvert);
            oldDelegate.requestDelete();
        } else if (bottomThreshold >= 0 && !isEmbedded() && delegate.size() <= bottomThreshold) {
            ORidBagDelegate oldDelegate = delegate;
            boolean oldAutoConvert = oldDelegate.isAutoConvertToRecord();
            oldDelegate.setAutoConvertToRecord(false);
            delegate = new OEmbeddedRidBag();
            for (OIdentifiable identifiable : oldDelegate) delegate.add(identifiable);
            final ORecord owner = oldDelegate.getOwner();
            delegate.setOwner(owner);
            for (OMultiValueChangeListener<OIdentifiable, OIdentifiable> listener : oldDelegate.getChangeListeners()) delegate.addChangeListener(listener);
            owner.setDirty();
            oldDelegate.setAutoConvertToRecord(oldAutoConvert);
            oldDelegate.requestDelete();
        }
    }
    final UUID oldUuid = uuid;
    final OSBTreeCollectionManager sbTreeCollectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
    if (sbTreeCollectionManager != null)
        uuid = sbTreeCollectionManager.listenForChanges(this);
    else
        uuid = null;
    boolean hasUuid = uuid != null;
    final int serializedSize = OByteSerializer.BYTE_SIZE + delegate.getSerializedSize() + ((hasUuid) ? OUUIDSerializer.UUID_SIZE : 0);
    int pointer = bytesContainer.alloc(serializedSize);
    int offset = pointer;
    final byte[] stream = bytesContainer.bytes;
    byte configByte = 0;
    if (isEmbedded())
        configByte |= 1;
    if (hasUuid)
        configByte |= 2;
    stream[offset++] = configByte;
    if (hasUuid) {
        OUUIDSerializer.INSTANCE.serialize(uuid, stream, offset);
        offset += OUUIDSerializer.UUID_SIZE;
    }
    delegate.serialize(stream, offset, oldUuid);
    return pointer;
}
Also used : OEmbeddedRidBag(com.orientechnologies.orient.core.db.record.ridbag.embedded.OEmbeddedRidBag) ORecordSerializationContext(com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext) OSBTreeRidBag(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager) ORecord(com.orientechnologies.orient.core.record.ORecord) UUID(java.util.UUID)

Example 82 with ORecord

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

the class OEmbeddedRidBag method convertRecords2Links.

@Override
public boolean convertRecords2Links() {
    for (int i = 0; i < entriesLength; i++) {
        final Object entry = entries[i];
        if (entry instanceof OIdentifiable) {
            final OIdentifiable identifiable = (OIdentifiable) entry;
            if (identifiable instanceof ORecord) {
                final ORecord record = (ORecord) identifiable;
                entries[i] = record.getIdentity();
            }
        }
    }
    return true;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 83 with ORecord

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

the class OEmbeddedRidBag method convertLinks2Records.

@Override
public void convertLinks2Records() {
    for (int i = 0; i < entriesLength; i++) {
        final Object entry = entries[i];
        if (entry instanceof OIdentifiable) {
            final OIdentifiable identifiable = (OIdentifiable) entry;
            ORecord record = identifiable.getRecord();
            if (record != null) {
                if (this.owner != null) {
                    ORecordInternal.unTrack(this.owner, identifiable);
                    ORecordInternal.track(this.owner, record);
                }
                entries[i] = record;
            }
        }
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 84 with ORecord

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

the class ODatabaseDocumentTx method executeReadRecord.

/**
   * This method is internal, it can be subject to signature change or be removed, do not use.
   *
   * @Internal
   */
public <RET extends ORecord> RET executeReadRecord(final ORecordId rid, ORecord iRecord, final int recordVersion, final String fetchPlan, final boolean ignoreCache, final boolean iUpdateCache, final boolean loadTombstones, final OStorage.LOCKING_STRATEGY lockingStrategy, RecordReader recordReader) {
    checkOpeness();
    checkIfActive();
    getMetadata().makeThreadLocalSchemaSnapshot();
    ORecordSerializationContext.pushContext();
    try {
        checkSecurity(ORule.ResourceGeneric.CLUSTER, ORole.PERMISSION_READ, getClusterNameById(rid.getClusterId()));
        // SEARCH IN LOCAL TX
        ORecord record = getTransaction().getRecord(rid);
        if (record == OTransactionRealAbstract.DELETED_RECORD)
            // DELETED IN TX
            return null;
        if (record == null && !ignoreCache)
            // SEARCH INTO THE CACHE
            record = getLocalCache().findRecord(rid);
        if (record != null) {
            if (iRecord != null) {
                iRecord.fromStream(record.toStream());
                ORecordInternal.setVersion(iRecord, record.getVersion());
                record = iRecord;
            }
            OFetchHelper.checkFetchPlanValid(fetchPlan);
            if (callbackHooks(ORecordHook.TYPE.BEFORE_READ, record) == ORecordHook.RESULT.SKIP)
                return null;
            if (record.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED)
                record.reload();
            if (lockingStrategy == OStorage.LOCKING_STRATEGY.KEEP_SHARED_LOCK) {
                OLogManager.instance().warn(this, "You use deprecated record locking strategy: %s it may lead to deadlocks " + lockingStrategy);
                record.lock(false);
            } else if (lockingStrategy == OStorage.LOCKING_STRATEGY.KEEP_EXCLUSIVE_LOCK) {
                OLogManager.instance().warn(this, "You use deprecated record locking strategy: %s it may lead to deadlocks " + lockingStrategy);
                record.lock(true);
            }
            callbackHooks(ORecordHook.TYPE.AFTER_READ, record);
            if (record instanceof ODocument)
                ODocumentInternal.checkClass((ODocument) record, this);
            return (RET) record;
        }
        final ORawBuffer recordBuffer;
        if (!rid.isValid())
            recordBuffer = null;
        else {
            OFetchHelper.checkFetchPlanValid(fetchPlan);
            int version;
            if (iRecord != null)
                version = iRecord.getVersion();
            else
                version = recordVersion;
            recordBuffer = recordReader.readRecord(storage, rid, fetchPlan, ignoreCache, version);
        }
        if (recordBuffer == null)
            return null;
        if (iRecord == null || ORecordInternal.getRecordType(iRecord) != recordBuffer.recordType)
            // NO SAME RECORD TYPE: CAN'T REUSE OLD ONE BUT CREATE A NEW ONE FOR IT
            iRecord = Orient.instance().getRecordFactoryManager().newInstance(recordBuffer.recordType);
        ORecordInternal.fill(iRecord, rid, recordBuffer.version, recordBuffer.buffer, false);
        if (iRecord instanceof ODocument)
            ODocumentInternal.checkClass((ODocument) iRecord, this);
        if (ORecordVersionHelper.isTombstone(iRecord.getVersion()))
            return (RET) iRecord;
        if (callbackHooks(ORecordHook.TYPE.BEFORE_READ, iRecord) == ORecordHook.RESULT.SKIP)
            return null;
        iRecord.fromStream(recordBuffer.buffer);
        callbackHooks(ORecordHook.TYPE.AFTER_READ, iRecord);
        if (iUpdateCache)
            getLocalCache().updateRecord(iRecord);
        return (RET) iRecord;
    } catch (OOfflineClusterException t) {
        throw t;
    } catch (ORecordNotFoundException t) {
        throw t;
    } catch (Throwable t) {
        if (rid.isTemporary())
            throw OException.wrapException(new ODatabaseException("Error on retrieving record using temporary RID: " + rid), t);
        else
            throw OException.wrapException(new ODatabaseException("Error on retrieving record " + rid + " (cluster: " + storage.getPhysicalClusterNameById(rid.getClusterId()) + ")"), t);
    } finally {
        ORecordSerializationContext.pullContext();
        getMetadata().clearThreadLocalSchemaSnapshot();
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OOfflineClusterException(com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)

Example 85 with ORecord

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

the class OTraverseRecordSetProcess method process.

@SuppressWarnings("unchecked")
public OIdentifiable process() {
    while (target.hasNext()) {
        record = target.next();
        index++;
        final ORecord rec = record.getRecord();
        if (rec instanceof ODocument) {
            ODocument doc = (ODocument) rec;
            if (!doc.getIdentity().isPersistent() && doc.fields() == 1) {
                // EXTRACT THE FIELD CONTEXT
                Object fieldvalue = doc.field(doc.fieldNames()[0]);
                if (fieldvalue instanceof Collection<?>) {
                    command.getContext().push(new OTraverseRecordSetProcess(command, ((Collection<OIdentifiable>) fieldvalue).iterator(), getPath()));
                } else if (fieldvalue instanceof ODocument) {
                    command.getContext().push(new OTraverseRecordProcess(command, (ODocument) rec, getPath()));
                }
            } else {
                command.getContext().push(new OTraverseRecordProcess(command, (ODocument) rec, getPath()));
            }
            return null;
        }
    }
    return pop();
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) Collection(java.util.Collection) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecord (com.orientechnologies.orient.core.record.ORecord)177 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)80 ORecordId (com.orientechnologies.orient.core.id.ORecordId)37 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)35 ORID (com.orientechnologies.orient.core.id.ORID)24 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)19 IOException (java.io.IOException)18 Test (org.junit.Test)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)14 OException (com.orientechnologies.common.exception.OException)13 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)13 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)12 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)11 ArrayList (java.util.ArrayList)10 Test (org.testng.annotations.Test)8 OIOException (com.orientechnologies.common.io.OIOException)7 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)7 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)7 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)6 ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)6