Search in sources :

Example 1 with ORecordSerializationContext

use of com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext in project orientdb by orientechnologies.

the class OSBTreeRidBag method serialize.

@Override
public int serialize(byte[] stream, int offset, UUID ownerUuid) {
    for (Map.Entry<OIdentifiable, OModifiableInteger> entry : newEntries.entrySet()) {
        OIdentifiable identifiable = entry.getKey();
        assert identifiable instanceof ORecord;
        Change c = changes.get(identifiable);
        final int delta = entry.getValue().intValue();
        if (c == null)
            changes.put(identifiable, new DiffChange(delta));
        else
            c.applyDiff(delta);
    }
    newEntries.clear();
    final ORecordSerializationContext context;
    boolean remoteMode = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage() instanceof OStorageProxy;
    if (remoteMode) {
        context = null;
    } else
        context = ORecordSerializationContext.getContext();
    // make sure that we really save underlying record.
    if (collectionPointer == null) {
        if (context != null) {
            final int clusterId = getHighLevelDocClusterId();
            assert clusterId > -1;
            collectionPointer = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager().createSBTree(clusterId, ownerUuid);
        }
    }
    OBonsaiCollectionPointer collectionPointer;
    if (this.collectionPointer != null)
        collectionPointer = this.collectionPointer;
    else {
        collectionPointer = OBonsaiCollectionPointer.INVALID;
    }
    OLongSerializer.INSTANCE.serializeLiteral(collectionPointer.getFileId(), stream, offset);
    offset += OLongSerializer.LONG_SIZE;
    OBonsaiBucketPointer rootPointer = collectionPointer.getRootPointer();
    OLongSerializer.INSTANCE.serializeLiteral(rootPointer.getPageIndex(), stream, offset);
    offset += OLongSerializer.LONG_SIZE;
    OIntegerSerializer.INSTANCE.serializeLiteral(rootPointer.getPageOffset(), stream, offset);
    offset += OIntegerSerializer.INT_SIZE;
    // Keep this section for binary compatibility with versions older then 1.7.5
    OIntegerSerializer.INSTANCE.serializeLiteral(size, stream, offset);
    offset += OIntegerSerializer.INT_SIZE;
    if (context == null) {
        ChangeSerializationHelper.INSTANCE.serializeChanges(changes, OLinkSerializer.INSTANCE, stream, offset);
    } else {
        ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
        for (Entry<OIdentifiable, Change> change : this.changes.entrySet()) {
            OIdentifiable key = change.getKey();
            if (db != null && db.getTransaction().isActive()) {
                if (!key.getIdentity().isPersistent()) {
                    OIdentifiable newKey = db.getTransaction().getRecord(key.getIdentity());
                    if (newKey != null) {
                        changes.remove(key);
                        changes.put(newKey, change.getValue());
                    }
                }
            }
        }
        this.collectionPointer = collectionPointer;
        context.push(new ORidBagUpdateSerializationOperation(changes, collectionPointer));
        // 0-length serialized list of changes
        OIntegerSerializer.INSTANCE.serializeLiteral(0, stream, offset);
        offset += OIntegerSerializer.INT_SIZE;
    }
    return offset;
}
Also used : OStorageProxy(com.orientechnologies.orient.core.storage.OStorageProxy) OModifiableInteger(com.orientechnologies.common.types.OModifiableInteger) ORecordSerializationContext(com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext) Change(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag.Change) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OBonsaiBucketPointer(com.orientechnologies.orient.core.index.sbtreebonsai.local.OBonsaiBucketPointer) ORidBagUpdateSerializationOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.ORidBagUpdateSerializationOperation) ORecord(com.orientechnologies.orient.core.record.ORecord) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 2 with ORecordSerializationContext

use of com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext 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)

Aggregations

ORecord (com.orientechnologies.orient.core.record.ORecord)2 ORecordSerializationContext (com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext)2 OModifiableInteger (com.orientechnologies.common.types.OModifiableInteger)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OEmbeddedRidBag (com.orientechnologies.orient.core.db.record.ridbag.embedded.OEmbeddedRidBag)1 OSBTreeCollectionManager (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)1 OSBTreeRidBag (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag)1 Change (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag.Change)1 OBonsaiBucketPointer (com.orientechnologies.orient.core.index.sbtreebonsai.local.OBonsaiBucketPointer)1 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)1 ORidBagUpdateSerializationOperation (com.orientechnologies.orient.core.storage.impl.local.paginated.ORidBagUpdateSerializationOperation)1 UUID (java.util.UUID)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1