Search in sources :

Example 6 with OSerializableStream

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

the class OStreamSerializerAnyStreamable method fromStream.

/**
   * Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
   */
public Object fromStream(final byte[] iStream) throws IOException {
    if (iStream == null || iStream.length == 0)
        // NULL VALUE
        return null;
    final int classNameSize = OBinaryProtocol.bytes2int(iStream);
    if (classNameSize <= 0) {
        final String message = "Class signature not found in ANY element: " + Arrays.toString(iStream);
        OLogManager.instance().error(this, message);
        throw new OSerializationException(message);
    }
    final String className = new String(iStream, 4, classNameSize, "UTF-8");
    try {
        final OSerializableStream stream;
        // CHECK FOR ALIASES
        if (className.equalsIgnoreCase("q"))
            // QUERY
            stream = new OSQLSynchQuery<Object>();
        else if (className.equalsIgnoreCase("c"))
            // SQL COMMAND
            stream = new OCommandSQL();
        else if (className.equalsIgnoreCase("s"))
            // SCRIPT COMMAND
            stream = new OCommandScript();
        else
            // CREATE THE OBJECT BY INVOKING THE EMPTY CONSTRUCTOR
            stream = (OSerializableStream) Class.forName(className).newInstance();
        return stream.fromStream(OArrays.copyOfRange(iStream, 4 + classNameSize, iStream.length));
    } catch (Exception e) {
        final String message = "Error on unmarshalling content. Class: " + className;
        OLogManager.instance().error(this, message, e);
        throw OException.wrapException(new OSerializationException(message), e);
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Example 7 with OSerializableStream

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

the class OStringSerializerAnyStreamable method toStream.

/**
   * Serialize the class name size + class name + object content
   * 
   * @param iValue
   */
public StringBuilder toStream(final StringBuilder iOutput, Object iValue) {
    if (iValue != null) {
        if (!(iValue instanceof OSerializableStream))
            throw new OSerializationException("Cannot serialize the object since it's not implements the OSerializableStream interface");
        OSerializableStream stream = (OSerializableStream) iValue;
        iOutput.append(iValue.getClass().getName());
        iOutput.append(OStreamSerializerHelper.SEPARATOR);
        iOutput.append(OBase64Utils.encodeBytes(stream.toStream()));
    }
    return iOutput;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream)

Example 8 with OSerializableStream

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

the class OStringSerializerAnyStreamable method fromStream.

/**
   * Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
   */
public Object fromStream(final String iStream) {
    if (iStream == null || iStream.length() == 0)
        // NULL VALUE
        return null;
    OSerializableStream instance = null;
    int propertyPos = iStream.indexOf(':');
    int pos = iStream.indexOf(OStreamSerializerHelper.SEPARATOR);
    if (pos < 0 || propertyPos > -1 && pos > propertyPos) {
        instance = new ODocument();
        pos = -1;
    } else {
        final String className = iStream.substring(0, pos);
        try {
            final Class<?> clazz = Class.forName(className);
            instance = (OSerializableStream) clazz.newInstance();
        } catch (Exception e) {
            final String message = "Error on unmarshalling content. Class: " + className;
            OLogManager.instance().error(this, message, e);
            throw OException.wrapException(new OSerializationException(message), e);
        }
    }
    instance.fromStream(OBase64Utils.decode(iStream.substring(pos + 1)));
    return instance;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) OException(com.orientechnologies.common.exception.OException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 9 with OSerializableStream

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

the class OStringSerializerEmbedded method toStream.

/**
   * Serialize the class name size + class name + object content
   * 
   * @param iValue
   */
public StringBuilder toStream(final StringBuilder iOutput, Object iValue) {
    if (iValue != null) {
        if (iValue instanceof ODocumentSerializable)
            iValue = ((ODocumentSerializable) iValue).toDocument();
        if (!(iValue instanceof OSerializableStream))
            throw new OSerializationException("Cannot serialize the object since it's not implements the OSerializableStream interface");
        OSerializableStream stream = (OSerializableStream) iValue;
        iOutput.append(iValue.getClass().getName());
        iOutput.append(OStreamSerializerHelper.SEPARATOR);
        try {
            iOutput.append(new String(stream.toStream(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw OException.wrapException(new OSerializationException("Error serializing embedded object"), e);
        }
    }
    return iOutput;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable)

Example 10 with OSerializableStream

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

the class ORecordSerializerBinaryV0 method serializeValue.

@SuppressWarnings("unchecked")
public int serializeValue(final BytesContainer bytes, Object value, final OType type, final OType linkedType) {
    int pointer = 0;
    switch(type) {
        case INTEGER:
        case LONG:
        case SHORT:
            pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
            break;
        case STRING:
            pointer = writeString(bytes, value.toString());
            break;
        case DOUBLE:
            long dg = Double.doubleToLongBits(((Number) value).doubleValue());
            pointer = bytes.alloc(OLongSerializer.LONG_SIZE);
            OLongSerializer.INSTANCE.serializeLiteral(dg, bytes.bytes, pointer);
            break;
        case FLOAT:
            int fg = Float.floatToIntBits(((Number) value).floatValue());
            pointer = bytes.alloc(OIntegerSerializer.INT_SIZE);
            OIntegerSerializer.INSTANCE.serializeLiteral(fg, bytes.bytes, pointer);
            break;
        case BYTE:
            pointer = bytes.alloc(1);
            bytes.bytes[pointer] = ((Number) value).byteValue();
            break;
        case BOOLEAN:
            pointer = bytes.alloc(1);
            bytes.bytes[pointer] = ((Boolean) value) ? (byte) 1 : (byte) 0;
            break;
        case DATETIME:
            if (value instanceof Number) {
                pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
            } else
                pointer = OVarIntSerializer.write(bytes, ((Date) value).getTime());
            break;
        case DATE:
            long dateValue;
            if (value instanceof Number) {
                dateValue = ((Number) value).longValue();
            } else
                dateValue = ((Date) value).getTime();
            dateValue = convertDayToTimezone(ODateHelper.getDatabaseTimeZone(), TimeZone.getTimeZone("GMT"), dateValue);
            pointer = OVarIntSerializer.write(bytes, dateValue / MILLISEC_PER_DAY);
            break;
        case EMBEDDED:
            pointer = bytes.offset;
            if (value instanceof ODocumentSerializable) {
                ODocument cur = ((ODocumentSerializable) value).toDocument();
                cur.field(ODocumentSerializable.CLASS_NAME, value.getClass().getName());
                serialize(cur, bytes, false);
            } else {
                serialize((ODocument) value, bytes, false);
            }
            break;
        case EMBEDDEDSET:
        case EMBEDDEDLIST:
            if (value.getClass().isArray())
                pointer = writeEmbeddedCollection(bytes, Arrays.asList(OMultiValue.array(value)), linkedType);
            else
                pointer = writeEmbeddedCollection(bytes, (Collection<?>) value, linkedType);
            break;
        case DECIMAL:
            BigDecimal decimalValue = (BigDecimal) value;
            pointer = bytes.alloc(ODecimalSerializer.INSTANCE.getObjectSize(decimalValue));
            ODecimalSerializer.INSTANCE.serialize(decimalValue, bytes.bytes, pointer);
            break;
        case BINARY:
            pointer = writeBinary(bytes, (byte[]) (value));
            break;
        case LINKSET:
        case LINKLIST:
            Collection<OIdentifiable> ridCollection = (Collection<OIdentifiable>) value;
            pointer = writeLinkCollection(bytes, ridCollection);
            break;
        case LINK:
            if (!(value instanceof OIdentifiable))
                throw new OValidationException("Value '" + value + "' is not a OIdentifiable");
            pointer = writeOptimizedLink(bytes, (OIdentifiable) value);
            break;
        case LINKMAP:
            pointer = writeLinkMap(bytes, (Map<Object, OIdentifiable>) value);
            break;
        case EMBEDDEDMAP:
            pointer = writeEmbeddedMap(bytes, (Map<Object, Object>) value);
            break;
        case LINKBAG:
            pointer = ((ORidBag) value).toStream(bytes);
            break;
        case CUSTOM:
            if (!(value instanceof OSerializableStream))
                value = new OSerializableWrapper((Serializable) value);
            pointer = writeString(bytes, value.getClass().getName());
            writeBinary(bytes, ((OSerializableStream) value).toStream());
            break;
        case TRANSIENT:
            break;
        case ANY:
            break;
    }
    return pointer;
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) BigDecimal(java.math.BigDecimal) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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