Search in sources :

Example 1 with ORecordSerializer

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

the class ODocument method writeExternal.

@Override
public void writeExternal(final ObjectOutput stream) throws IOException {
    ORecordSerializer serializer = ORecordSerializerFactory.instance().getFormat(ORecordSerializerNetwork.NAME);
    final byte[] idBuffer = _recordId.toStream();
    stream.writeInt(-1);
    stream.writeInt(idBuffer.length);
    stream.write(idBuffer);
    stream.writeInt(_recordVersion);
    final byte[] content = serializer.toStream(this, false);
    stream.writeInt(content.length);
    stream.write(content);
    stream.writeBoolean(_dirty);
    stream.writeObject(serializer.toString());
}
Also used : ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer)

Example 2 with ORecordSerializer

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

the class ODocumentTest method testChangeTypeOnValueSet.

@Test
public void testChangeTypeOnValueSet() throws Exception {
    ODocument doc = new ODocument();
    doc.field("link", new ORecordId(1, 2));
    ORecordSerializer ser = ODatabaseDocumentTx.getDefaultSerializer();
    byte[] bytes = ser.toStream(doc, false);
    doc = new ODocument();
    ser.fromStream(bytes, doc, null);
    assertEquals(doc.fieldType("link"), OType.LINK);
    doc.field("link", new ORidBag());
    assertNotEquals(doc.fieldType("link"), OType.LINK);
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.testng.annotations.Test)

Example 3 with ORecordSerializer

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

the class ODocumentTest method testKeepFieldTypeSerialization.

@Test
public void testKeepFieldTypeSerialization() throws Exception {
    ODocument doc = new ODocument();
    doc.field("integer", 10, OType.INTEGER);
    doc.field("link", new ORecordId(1, 2), OType.LINK);
    doc.field("string", 20, OType.STRING);
    doc.field("binary", new byte[] { 30 }, OType.BINARY);
    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);
}
Also used : ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.testng.annotations.Test)

Example 4 with ORecordSerializer

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

the class ONetworkProtocolBinary method command.

protected void command(OClientConnection connection) throws IOException {
    setDataCommandInfo(connection, "Execute remote command");
    byte type = channel.readByte();
    final boolean live = type == 'l';
    final boolean asynch = type == 'a';
    if (connection == null && connection.getDatabase() == null)
        throw new IOException("Found invalid session");
    String dbSerializerName = connection.getDatabase().getSerializer().toString();
    String name = getRecordSerializerName(connection);
    if (!dbSerializerName.equals(name)) {
        ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(name);
        ONetworkThreadLocalSerializer.setNetworkSerializer(ser);
    }
    OCommandRequestText command = (OCommandRequestText) OStreamSerializerAnyStreamable.INSTANCE.fromStream(channel.readBytes());
    ONetworkThreadLocalSerializer.setNetworkSerializer(null);
    final Map<Object, Object> params = command.getParameters();
    if (asynch && command instanceof OSQLSynchQuery) {
        // CONVERT IT IN ASYNCHRONOUS QUERY
        final OSQLAsynchQuery asynchQuery = new OSQLAsynchQuery(command.getText());
        asynchQuery.setFetchPlan(command.getFetchPlan());
        asynchQuery.setLimit(command.getLimit());
        asynchQuery.setTimeout(command.getTimeoutTime(), command.getTimeoutStrategy());
        asynchQuery.setUseCache(((OSQLSynchQuery) command).isUseCache());
        command = asynchQuery;
    }
    connection.getData().commandDetail = command.getText();
    beginResponse();
    try {
        connection.getData().command = command;
        OAbstractCommandResultListener listener = null;
        OLiveCommandResultListener liveListener = null;
        OCommandResultListener cmdResultListener = command.getResultListener();
        if (live) {
            liveListener = new OLiveCommandResultListener(server, connection, clientTxId, cmdResultListener);
            listener = new OSyncCommandResultListener(null);
            command.setResultListener(liveListener);
        } else if (asynch) {
            // IF COMMAND CACHE IS ENABLED, RESULT MUST BE COLLECTED
            final OCommandCache cmdCache = connection.getDatabase().getMetadata().getCommandCache();
            if (cmdCache.isEnabled())
                // CREATE E COLLECTOR OF RESULT IN RAM TO CACHE THE RESULT
                cmdResultListener = new OCommandCacheRemoteResultListener(cmdResultListener, cmdCache);
            listener = new OAsyncCommandResultListener(connection, this, clientTxId, cmdResultListener);
            command.setResultListener(listener);
        } else {
            listener = new OSyncCommandResultListener(null);
        }
        final long serverTimeout = OGlobalConfiguration.COMMAND_TIMEOUT.getValueAsLong();
        if (serverTimeout > 0 && command.getTimeoutTime() > serverTimeout)
            // FORCE THE SERVER'S TIMEOUT
            command.setTimeout(serverTimeout, command.getTimeoutStrategy());
        if (!isConnectionAlive(connection))
            return;
        // REQUEST CAN'T MODIFY THE RESULT, SO IT'S CACHEABLE
        command.setCacheableResult(true);
        // ASSIGNED THE PARSED FETCHPLAN
        final OCommandRequest commandImpl = connection.getDatabase().command(command);
        listener.setFetchPlan(commandImpl.getFetchPlan());
        final Object result;
        if (params == null)
            result = commandImpl.execute();
        else
            result = commandImpl.execute(params);
        // FETCHPLAN HAS TO BE ASSIGNED AGAIN, because it can be changed by SQL statement
        listener.setFetchPlan(commandImpl.getFetchPlan());
        if (asynch) {
            // ASYNCHRONOUS
            if (listener.isEmpty())
                try {
                    sendOk(connection, clientTxId);
                } catch (IOException ignored) {
                }
            // NO MORE RECORDS
            channel.writeByte((byte) 0);
        } else {
            // SYNCHRONOUS
            sendOk(connection, clientTxId);
            boolean isRecordResultSet = true;
            if (command instanceof OCommandRequestInternal)
                isRecordResultSet = command.isRecordResultSet();
            serializeValue(connection, listener, result, false, isRecordResultSet);
            if (listener instanceof OSyncCommandResultListener) {
                // SEND FETCHED RECORDS TO LOAD IN CLIENT CACHE
                for (ORecord rec : ((OSyncCommandResultListener) listener).getFetchedRecordsToSend()) {
                    // CLIENT CACHE RECORD. IT
                    channel.writeByte((byte) 2);
                    // ISN'T PART OF THE
                    // RESULT SET
                    writeIdentifiable(connection, rec);
                }
                // NO MORE RECORDS
                channel.writeByte((byte) 0);
            }
        }
    } finally {
        connection.getData().command = null;
        endResponse(connection);
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OCommandCache(com.orientechnologies.orient.core.cache.OCommandCache) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) OSQLAsynchQuery(com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery)

Example 5 with ORecordSerializer

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

the class OClientConnectionManager method pushDistribCfg2Clients.

/**
 * Pushes the distributed configuration to all the connected clients.
 */
public void pushDistribCfg2Clients(final ODocument iConfig) {
    if (iConfig == null)
        return;
    final Set<String> pushed = new HashSet<String>();
    for (OClientConnection c : connections.values()) {
        if (!c.getData().supportsPushMessages)
            continue;
        try {
            final String remoteAddress = c.getRemoteAddress();
            if (pushed.contains(remoteAddress))
                // ALREADY SENT: JUMP IT
                continue;
        } catch (Exception e) {
            // SOCKET EXCEPTION SKIP IT
            continue;
        }
        if (!(c.getProtocol() instanceof ONetworkProtocolBinary) || c.getData().serializationImpl == null)
            // INVOLVE ONLY BINARY PROTOCOLS
            continue;
        final ONetworkProtocolBinary p = (ONetworkProtocolBinary) c.getProtocol();
        final OChannelBinary channel = p.getChannel();
        final ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(c.getData().serializationImpl);
        if (ser == null)
            return;
        final byte[] content = ser.toStream(iConfig, false);
        try {
            // TRY ACQUIRING THE LOCK FOR MAXIMUM 3 SECS TO AVOID TO FREEZE CURRENT THREAD
            if (channel.tryAcquireWriteLock(TIMEOUT_PUSH)) {
                try {
                    channel.writeByte(OChannelBinaryProtocol.PUSH_DATA);
                    channel.writeInt(Integer.MIN_VALUE);
                    channel.writeByte(OChannelBinaryProtocol.REQUEST_PUSH_DISTRIB_CONFIG);
                    channel.writeBytes(content);
                    channel.flush();
                    pushed.add(c.getRemoteAddress());
                    OLogManager.instance().debug(this, "Sent updated cluster configuration to the remote client %s", c.getRemoteAddress());
                } finally {
                    channel.releaseWriteLock();
                }
            } else {
                OLogManager.instance().info(this, "Timeout on sending updated cluster configuration to the remote client %s", c.getRemoteAddress());
            }
        } catch (Exception e) {
            OLogManager.instance().warn(this, "Cannot push cluster configuration to the client %s", e, c.getRemoteAddress());
        }
    }
}
Also used : OChannelBinary(com.orientechnologies.orient.enterprise.channel.binary.OChannelBinary) ONetworkProtocolBinary(com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) OException(com.orientechnologies.common.exception.OException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) IOException(java.io.IOException)

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