Search in sources :

Example 11 with OSerializableStream

use of com.orientechnologies.orient.core.serialization.OSerializableStream in project orientdb by orientechnologies.

the class OTransactionRealAbstract method serializeIndexChangeEntry.

protected ODocument serializeIndexChangeEntry(OTransactionIndexChangesPerKey entry, final ODocument indexDoc) {
    // SERIALIZE KEY
    ODocument keyContainer = new ODocument();
    keyContainer.setTrackingChanges(false);
    try {
        if (entry.key != null) {
            if (entry.key instanceof OCompositeKey) {
                final List<Object> keys = ((OCompositeKey) entry.key).getKeys();
                keyContainer.field("key", keys, OType.EMBEDDEDLIST);
                keyContainer.field("binary", false);
            } else if (!(entry.key instanceof ORecordElement) && (entry.key instanceof OSerializableStream)) {
                keyContainer.field("key", OStreamSerializerAnyStreamable.INSTANCE.toStream(entry.key), OType.BINARY);
                keyContainer.field("binary", true);
            } else {
                keyContainer.field("key", entry.key);
                keyContainer.field("binary", false);
            }
        } else
            keyContainer = null;
    } catch (IOException ioe) {
        throw OException.wrapException(new OTransactionException("Error during index changes serialization. "), ioe);
    }
    final List<ODocument> operations = new ArrayList<ODocument>();
    // SERIALIZE VALUES
    if (entry.entries != null && !entry.entries.isEmpty()) {
        for (OTransactionIndexEntry e : entry.entries) {
            final ODocument changeDoc = new ODocument().setAllowChainedAccess(false);
            ODocumentInternal.addOwner((ODocument) changeDoc, indexDoc);
            // SERIALIZE OPERATION
            changeDoc.field("o", e.operation.ordinal());
            if (e.value instanceof ORecord && e.value.getIdentity().isNew()) {
                final ORecord saved = getRecord(e.value.getIdentity());
                if (saved != null)
                    e.value = saved;
                else
                    ((ORecord) e.value).save();
            }
            changeDoc.field("v", e.value != null ? e.value.getIdentity() : null);
            operations.add(changeDoc);
        }
    }
    ODocument res = new ODocument();
    res.setTrackingChanges(false);
    ODocumentInternal.addOwner(res, indexDoc);
    return res.setAllowChainedAccess(false).field("k", keyContainer, OType.EMBEDDED).field("ops", operations, OType.EMBEDDEDLIST);
}
Also used : OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecord(com.orientechnologies.orient.core.record.ORecord) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) IOException(java.io.IOException) OTransactionIndexEntry(com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordElement(com.orientechnologies.orient.core.db.record.ORecordElement)

Aggregations

OSerializableStream (com.orientechnologies.orient.core.serialization.OSerializableStream)11 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 ODocumentSerializable (com.orientechnologies.orient.core.serialization.ODocumentSerializable)5 OException (com.orientechnologies.common.exception.OException)4 OValidationException (com.orientechnologies.orient.core.exception.OValidationException)4 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)2 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 OLiveQuery (com.orientechnologies.orient.core.sql.query.OLiveQuery)2 BigDecimal (java.math.BigDecimal)2 OIOException (com.orientechnologies.common.io.OIOException)1 OChannelBinaryAsynchClient (com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)1 OCommandRequestAsynch (com.orientechnologies.orient.core.command.OCommandRequestAsynch)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1