Search in sources :

Example 6 with ORecordSerializer

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

the class ODocument method readExternal.

@Override
public void readExternal(final ObjectInput stream) throws IOException, ClassNotFoundException {
    int i = stream.readInt();
    int size;
    if (i < 0)
        size = stream.readInt();
    else
        size = i;
    final byte[] idBuffer = new byte[size];
    stream.readFully(idBuffer);
    _recordId.fromStream(idBuffer);
    _recordVersion = stream.readInt();
    final int len = stream.readInt();
    final byte[] content = new byte[len];
    stream.readFully(content);
    _dirty = stream.readBoolean();
    ORecordSerializer serializer = _recordFormat;
    if (i < 0) {
        final String str = (String) stream.readObject();
        // TODO: WHEN TO USE THE SERIALIZER?
        serializer = ORecordSerializerFactory.instance().getFormat(str);
    }
    _status = ORecordElement.STATUS.UNMARSHALLING;
    try {
        serializer.fromStream(content, this, null);
    } finally {
        _status = ORecordElement.STATUS.LOADED;
    }
}
Also used : ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer)

Example 7 with ORecordSerializer

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

the class ONetworkProtocolBinary method fillRecord.

public void fillRecord(OClientConnection connection, final ORecordId rid, final byte[] buffer, final int version, final ORecord record) {
    String dbSerializerName = "";
    if (connection.getDatabase() != null)
        dbSerializerName = connection.getDatabase().getSerializer().toString();
    String name = getRecordSerializerName(connection);
    if (ORecordInternal.getRecordType(record) == ODocument.RECORD_TYPE && !dbSerializerName.equals(name)) {
        ORecordInternal.fill(record, rid, version, null, true);
        ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(name);
        ser.fromStream(buffer, record, null);
        record.setDirty();
    } else
        ORecordInternal.fill(record, rid, version, buffer, true);
}
Also used : ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer)

Example 8 with ORecordSerializer

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

the class ONetworkProtocolBinary method getRecordBytes.

public byte[] getRecordBytes(OClientConnection connection, final ORecord iRecord) {
    final byte[] stream;
    String dbSerializerName = null;
    if (ODatabaseRecordThreadLocal.INSTANCE.getIfDefined() != null)
        dbSerializerName = ((ODatabaseDocumentInternal) iRecord.getDatabase()).getSerializer().toString();
    String name = getRecordSerializerName(connection);
    if (ORecordInternal.getRecordType(iRecord) == ODocument.RECORD_TYPE && (dbSerializerName == null || !dbSerializerName.equals(name))) {
        ((ODocument) iRecord).deserializeFields();
        ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(name);
        stream = ser.toStream(iRecord, false);
    } else
        stream = iRecord.toStream();
    return stream;
}
Also used : ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 9 with ORecordSerializer

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

the class OConsoleDatabaseApp method exportRecord.

@ConsoleCommand(description = "Export the current record in the requested format", onlineHelp = "Console-Command-Export-Record")
public void exportRecord(@ConsoleParameter(name = "format", description = "Format, such as 'json'") final String iFormat, @ConsoleParameter(name = "options", description = "Options", optional = true) String iOptions) throws IOException {
    checkForDatabase();
    checkCurrentObject();
    final ORecordSerializer serializer = ORecordSerializerFactory.instance().getFormat(iFormat.toLowerCase(Locale.ENGLISH));
    if (serializer == null) {
        message("\nERROR: Format '" + iFormat + "' was not found.");
        printSupportedSerializerFormat();
        return;
    } else if (!(serializer instanceof ORecordSerializerStringAbstract)) {
        message("\nERROR: Format '" + iFormat + "' does not export as text.");
        printSupportedSerializerFormat();
        return;
    }
    if (iOptions == null || iOptions.length() <= 0) {
        iOptions = "rid,version,class,type,keepTypes,alwaysFetchEmbedded,fetchPlan:*:0,prettyPrint";
    }
    try {
        out.println(currentRecord.toJSON(iOptions));
    } catch (ODatabaseExportException e) {
        printError(e);
    }
}
Also used : ORecordSerializerStringAbstract(com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerStringAbstract) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ConsoleCommand(com.orientechnologies.common.console.annotation.ConsoleCommand)

Example 10 with ORecordSerializer

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

the class OCommandRequestTextAbstract method fromStream.

protected void fromStream(final OMemoryStream buffer) {
    text = buffer.getAsString();
    parameters = null;
    ORecordSerializer serializer = ONetworkThreadLocalSerializer.getNetworkSerializer();
    final boolean simpleParams = buffer.getAsBoolean();
    if (simpleParams) {
        final byte[] paramBuffer = buffer.getAsByteArray();
        final ODocument param = new ODocument();
        if (serializer != null)
            serializer.fromStream(paramBuffer, param, null);
        else
            param.fromStream(paramBuffer);
        Map<Object, Object> params = param.field("params");
        parameters = new HashMap<Object, Object>();
        if (params != null) {
            for (Entry<Object, Object> p : params.entrySet()) {
                final Object value;
                if (p.getValue() instanceof String)
                    value = ORecordSerializerStringAbstract.getTypeValue((String) p.getValue());
                else
                    value = p.getValue();
                if (p.getKey() instanceof String && Character.isDigit(((String) p.getKey()).charAt(0)))
                    parameters.put(Integer.parseInt((String) p.getKey()), value);
                else
                    parameters.put(p.getKey(), value);
            }
        } else {
            params = param.field("parameters");
            for (Entry<Object, Object> p : params.entrySet()) {
                if (p.getKey() instanceof String && Character.isDigit(((String) p.getKey()).charAt(0)))
                    parameters.put(Integer.parseInt((String) p.getKey()), p.getValue());
                else
                    parameters.put(p.getKey(), p.getValue());
            }
        }
    }
    final boolean compositeKeyParamsPresent = buffer.getAsBoolean();
    if (compositeKeyParamsPresent) {
        final byte[] paramBuffer = buffer.getAsByteArray();
        final ODocument param = new ODocument();
        if (serializer != null)
            serializer.fromStream(paramBuffer, param, null);
        else
            param.fromStream(paramBuffer);
        final Map<Object, Object> compositeKeyParams = param.field("compositeKeyParams");
        if (parameters == null)
            parameters = new HashMap<Object, Object>();
        for (final Entry<Object, Object> p : compositeKeyParams.entrySet()) {
            if (p.getValue() instanceof List) {
                final OCompositeKey compositeKey = new OCompositeKey((List<?>) p.getValue());
                if (p.getKey() instanceof String && Character.isDigit(((String) p.getKey()).charAt(0)))
                    parameters.put(Integer.parseInt((String) p.getKey()), compositeKey);
                else
                    parameters.put(p.getKey(), compositeKey);
            } else {
                final Object value = OCompositeKeySerializer.INSTANCE.deserialize(OStringSerializerHelper.getBinaryContent(p.getValue()), 0);
                if (p.getKey() instanceof String && Character.isDigit(((String) p.getKey()).charAt(0)))
                    parameters.put(Integer.parseInt((String) p.getKey()), value);
                else
                    parameters.put(p.getKey(), value);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) List(java.util.List) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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