Search in sources :

Example 11 with ORecordSerializer

use of com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer in project orientdb by orientechnologies.

the class OTransactionOptimisticProxy method begin.

@Override
public void begin() {
    super.begin();
    // Needed for keep the exception and insure that all data is read from the socket.
    OException toThrow = null;
    try {
        setUsingLog(channel.readByte() == 1);
        Map<ORecord, byte[]> lazyDeserialize = new IdentityHashMap<ORecord, byte[]>();
        byte lastTxStatus;
        for (lastTxStatus = channel.readByte(); lastTxStatus == 1; lastTxStatus = channel.readByte()) {
            final byte recordStatus = channel.readByte();
            final ORecordId rid = channel.readRID();
            final byte recordType = channel.readByte();
            final ORecordOperation entry = new OTransactionEntryProxy(recordType);
            entry.type = recordStatus;
            switch(recordStatus) {
                case ORecordOperation.CREATED:
                case ORecordOperation.RECYCLED:
                    byte[] content = channel.readBytes();
                    ORecordInternal.fill(entry.getRecord(), rid, 0, null, true);
                    lazyDeserialize.put(entry.getRecord(), content);
                    if (recordStatus == ORecordOperation.CREATED)
                        // SAVE THE RECORD TO RETRIEVE THEM FOR THE NEW RID TO SEND BACK TO THE REQUESTER
                        createdRecords.put(rid.copy(), entry.getRecord());
                    break;
                case ORecordOperation.UPDATED:
                    int version = channel.readVersion();
                    byte[] bytes = channel.readBytes();
                    ORecordInternal.fill(entry.getRecord(), rid, version, null, true);
                    lazyDeserialize.put(entry.getRecord(), bytes);
                    if (protocolVersion >= 23)
                        ORecordInternal.setContentChanged(entry.getRecord(), channel.readBoolean());
                    break;
                case ORecordOperation.DELETED:
                    // LOAD RECORD TO BE SURE IT HASN'T BEEN DELETED BEFORE + PROVIDE CONTENT FOR ANY HOOK
                    final ORecord rec = rid.getRecord();
                    int deleteVersion = channel.readVersion();
                    if (rec == null)
                        toThrow = new ORecordNotFoundException(rid);
                    else {
                        ORecordInternal.setVersion(rec, deleteVersion);
                        entry.setRecord(rec);
                    }
                    break;
                default:
                    throw new OTransactionException("Unrecognized tx command: " + recordStatus);
            }
            // PUT IN TEMPORARY LIST TO GET FETCHED AFTER ALL FOR CACHE
            tempEntries.put(entry.getRecord().getIdentity(), entry);
        }
        String dbSerializerName = "";
        if (database != null)
            dbSerializerName = database.getSerializer().toString();
        String name = oNetworkProtocolBinary.getRecordSerializerName(connection);
        for (Map.Entry<ORecord, byte[]> entry : lazyDeserialize.entrySet()) {
            ORecord record = entry.getKey();
            final boolean contentChanged = ORecordInternal.isContentChanged(record);
            if (ORecordInternal.getRecordType(record) == ODocument.RECORD_TYPE && !dbSerializerName.equals(name)) {
                ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(name);
                ser.fromStream(entry.getValue(), record, null);
                record.setDirty();
                ORecordInternal.setContentChanged(record, contentChanged);
            } else {
                record.fromStream(entry.getValue());
                record.setDirty();
                ORecordInternal.setContentChanged(record, contentChanged);
            }
        }
        if (toThrow != null)
            throw toThrow;
        if (lastTxStatus == -1)
            // ABORT TX
            throw new OTransactionAbortedException("Transaction aborted by the client");
        final ODocument remoteIndexEntries = new ODocument(channel.readBytes());
        fillIndexOperations(remoteIndexEntries);
        // FIRE THE TRIGGERS ONLY AFTER HAVING PARSED THE REQUEST
        for (Entry<ORID, ORecordOperation> entry : tempEntries.entrySet()) {
            if (entry.getValue().type == ORecordOperation.UPDATED) {
                // SPECIAL CASE FOR UPDATE: WE NEED TO LOAD THE RECORD AND APPLY CHANGES TO GET WORKING HOOKS (LIKE INDEXES)
                final ORecord record = entry.getValue().record.getRecord();
                final boolean contentChanged = ORecordInternal.isContentChanged(record);
                final ORecord loadedRecord = record.getIdentity().copy().getRecord();
                if (loadedRecord == null)
                    throw new ORecordNotFoundException(record.getIdentity());
                if (ORecordInternal.getRecordType(loadedRecord) == ODocument.RECORD_TYPE && ORecordInternal.getRecordType(loadedRecord) == ORecordInternal.getRecordType(record)) {
                    ((ODocument) loadedRecord).merge((ODocument) record, false, false);
                    loadedRecord.setDirty();
                    ORecordInternal.setContentChanged(loadedRecord, contentChanged);
                    ORecordInternal.setVersion(loadedRecord, record.getVersion());
                    entry.getValue().record = loadedRecord;
                    // SAVE THE RECORD TO RETRIEVE THEM FOR THE NEW VERSIONS TO SEND BACK TO THE REQUESTER
                    updatedRecords.put((ORecordId) entry.getKey(), entry.getValue().getRecord());
                }
            }
            addRecord(entry.getValue().getRecord(), entry.getValue().type, null);
        }
        tempEntries.clear();
        // UNMARSHALL ALL THE RECORD AT THE END TO BE SURE ALL THE RECORD ARE LOADED IN LOCAL TX
        for (ORecord record : createdRecords.values()) {
            unmarshallRecord(record);
            if (record instanceof ODocument) {
                // Force conversion of value to class for trigger default values.
                ODocumentInternal.autoConvertValueToClass(connection.getDatabase(), (ODocument) record);
            }
        }
        for (ORecord record : updatedRecords.values()) unmarshallRecord(record);
    } catch (IOException e) {
        rollback();
        throw OException.wrapException(new OSerializationException("Cannot read transaction record from the network. Transaction aborted"), e);
    }
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) IOException(java.io.IOException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OTransactionAbortedException(com.orientechnologies.orient.core.exception.OTransactionAbortedException) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 12 with ORecordSerializer

use of com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer in project orientdb by orientechnologies.

the class ODocumentTest method testKeepSchemafullFieldTypeSerialization.

@Test
public void testKeepSchemafullFieldTypeSerialization() throws Exception {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:" + ODocumentTest.class.getSimpleName());
    db.create();
    try {
        OClass clazz = db.getMetadata().getSchema().createClass("Test");
        clazz.createProperty("integer", OType.INTEGER);
        clazz.createProperty("link", OType.LINK);
        clazz.createProperty("string", OType.STRING);
        clazz.createProperty("binary", OType.BINARY);
        ODocument doc = new ODocument(clazz);
        doc.field("integer", 10);
        doc.field("link", new ORecordId(1, 2));
        doc.field("string", "string");
        doc.field("binary", new byte[] { 30 });
        // the types are from the schema.
        assertEquals(doc.fieldType("integer"), OType.INTEGER);
        assertEquals(doc.fieldType("link"), OType.LINK);
        assertEquals(doc.fieldType("string"), OType.STRING);
        assertEquals(doc.fieldType("binary"), OType.BINARY);
        ORecordSerializer ser = ODatabaseDocumentTx.getDefaultSerializer();
        byte[] bytes = ser.toStream(doc, false);
        doc = new ODocument();
        ser.fromStream(bytes, doc, null);
        assertEquals(doc.fieldType("integer"), OType.INTEGER);
        assertEquals(doc.fieldType("string"), OType.STRING);
        assertEquals(doc.fieldType("binary"), OType.BINARY);
        assertEquals(doc.fieldType("link"), OType.LINK);
    } finally {
        db.drop();
    }
}
Also used : OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.testng.annotations.Test)

Example 13 with ORecordSerializer

use of com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer in project orientdb by orientechnologies.

the class ODocumentTest method testKeepAutoFieldTypeSerialization.

@Test
public void testKeepAutoFieldTypeSerialization() throws Exception {
    ODocument doc = new ODocument();
    doc.field("integer", 10);
    doc.field("link", new ORecordId(1, 2));
    doc.field("string", "string");
    doc.field("binary", new byte[] { 30 });
    // this is null because is not set on value set.
    assertNull(doc.fieldType("integer"));
    assertNull(doc.fieldType("link"));
    assertNull(doc.fieldType("string"));
    assertNull(doc.fieldType("binary"));
    ORecordSerializer ser = ODatabaseDocumentTx.getDefaultSerializer();
    byte[] bytes = ser.toStream(doc, false);
    doc = new ODocument();
    ser.fromStream(bytes, doc, null);
    assertEquals(doc.fieldType("integer"), OType.INTEGER);
    assertEquals(doc.fieldType("string"), OType.STRING);
    assertEquals(doc.fieldType("binary"), OType.BINARY);
    assertEquals(doc.fieldType("link"), OType.LINK);
}
Also used : ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.testng.annotations.Test)

Example 14 with ORecordSerializer

use of com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer in project orientdb by orientechnologies.

the class TestNetworkSerializerIndipendency method createCsvDatabaseConnectBinary.

@Test
public void createCsvDatabaseConnectBinary() throws IOException {
    ORecordSerializer prev = ODatabaseDocumentTx.getDefaultSerializer();
    ODatabaseDocumentTx.setDefaultSerializer(ORecordSerializerSchemaAware2CSV.INSTANCE);
    createDatabase();
    ODatabaseDocumentTx dbTx = null;
    try {
        ODatabaseDocumentTx.setDefaultSerializer(ORecordSerializerBinary.INSTANCE);
        dbTx = new ODatabaseDocumentTx("remote:localhost/test");
        dbTx.open("admin", "admin");
        ODocument document = new ODocument();
        document.field("name", "something");
        document.field("surname", "something-else");
        document = dbTx.save(document);
        dbTx.commit();
        ODocument doc = dbTx.load(document.getIdentity());
        assertEquals(doc.fields(), document.fields());
        assertEquals(doc.field("name"), document.field("name"));
        assertEquals(doc.field("surname"), document.field("surname"));
    } finally {
        if (dbTx != null) {
            dbTx.close();
            dbTx.getStorage().close();
        }
        dropDatabase();
        ODatabaseDocumentTx.setDefaultSerializer(prev);
    }
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 15 with ORecordSerializer

use of com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer in project orientdb by orientechnologies.

the class TestNetworkSerializerIndipendency method createBinaryDatabaseConnectCsv.

@Test
public void createBinaryDatabaseConnectCsv() throws IOException {
    ORecordSerializer prev = ODatabaseDocumentTx.getDefaultSerializer();
    ODatabaseDocumentTx.setDefaultSerializer(ORecordSerializerBinary.INSTANCE);
    createDatabase();
    ODatabaseDocumentTx dbTx = null;
    try {
        ODatabaseDocumentTx.setDefaultSerializer(ORecordSerializerSchemaAware2CSV.INSTANCE);
        dbTx = new ODatabaseDocumentTx("remote:localhost/test");
        dbTx.open("admin", "admin");
        ODocument document = new ODocument();
        document.field("name", "something");
        document.field("surname", "something-else");
        document = dbTx.save(document);
        dbTx.commit();
        ODocument doc = dbTx.load(document.getIdentity());
        assertEquals(doc.fields(), document.fields());
        assertEquals(doc.field("name"), document.field("name"));
        assertEquals(doc.field("surname"), document.field("surname"));
    } finally {
        if (dbTx != null) {
            dbTx.close();
            dbTx.getStorage().close();
        }
        dropDatabase();
        ODatabaseDocumentTx.setDefaultSerializer(prev);
    }
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Aggregations

ORecordSerializer (com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer)16 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 ORecordId (com.orientechnologies.orient.core.id.ORecordId)5 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)4 Test (org.testng.annotations.Test)4 IOException (java.io.IOException)3 OException (com.orientechnologies.common.exception.OException)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 Test (org.junit.Test)2 ConsoleCommand (com.orientechnologies.common.console.annotation.ConsoleCommand)1 OIOException (com.orientechnologies.common.io.OIOException)1 OCommandCache (com.orientechnologies.orient.core.cache.OCommandCache)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)1 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)1 OTransactionAbortedException (com.orientechnologies.orient.core.exception.OTransactionAbortedException)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 ORID (com.orientechnologies.orient.core.id.ORID)1