Search in sources :

Example 1 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class ORecordSerializerBinaryV0 method serialize.

@SuppressWarnings("unchecked")
@Override
public void serialize(final ODocument document, final BytesContainer bytes, final boolean iClassOnly) {
    final OClass clazz = serializeClass(document, bytes);
    if (iClassOnly) {
        writeEmptyString(bytes);
        return;
    }
    final Map<String, OProperty> props = clazz != null ? clazz.propertiesMap() : null;
    final Set<Entry<String, ODocumentEntry>> fields = ODocumentInternal.rawEntries(document);
    final int[] pos = new int[fields.size()];
    int i = 0;
    final Entry<String, ODocumentEntry>[] values = new Entry[fields.size()];
    for (Entry<String, ODocumentEntry> entry : fields) {
        ODocumentEntry docEntry = entry.getValue();
        if (!docEntry.exist())
            continue;
        if (docEntry.property == null && props != null) {
            OProperty prop = props.get(entry.getKey());
            if (prop != null && docEntry.type == prop.getType())
                docEntry.property = prop;
        }
        if (docEntry.property != null) {
            OVarIntSerializer.write(bytes, (docEntry.property.getId() + 1) * -1);
            if (docEntry.property.getType() != OType.ANY)
                pos[i] = bytes.alloc(OIntegerSerializer.INT_SIZE);
            else
                pos[i] = bytes.alloc(OIntegerSerializer.INT_SIZE + 1);
        } else {
            writeString(bytes, entry.getKey());
            pos[i] = bytes.alloc(OIntegerSerializer.INT_SIZE + 1);
        }
        values[i] = entry;
        i++;
    }
    writeEmptyString(bytes);
    int size = i;
    for (i = 0; i < size; i++) {
        int pointer = 0;
        final Object value = values[i].getValue().value;
        if (value != null) {
            final OType type = getFieldType(values[i].getValue());
            if (type == null) {
                throw new OSerializationException("Impossible serialize value of type " + value.getClass() + " with the ODocument binary serializer");
            }
            pointer = serializeValue(bytes, value, type, getLinkedType(document, type, values[i].getKey()));
            OIntegerSerializer.INSTANCE.serializeLiteral(pointer, bytes.bytes, pos[i]);
            if (values[i].getValue().property == null || values[i].getValue().property.getType() == OType.ANY)
                writeOType(bytes, (pos[i] + OIntegerSerializer.INT_SIZE), type);
        }
    }
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ODocumentEntry(com.orientechnologies.orient.core.record.impl.ODocumentEntry) ODocumentEntry(com.orientechnologies.orient.core.record.impl.ODocumentEntry) Entry(java.util.Map.Entry)

Example 2 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class ORecordSerializerBinaryV0 method getFieldNames.

@Override
public String[] getFieldNames(ODocument reference, final BytesContainer bytes) {
    // SKIP CLASS NAME
    final int classNameLen = OVarIntSerializer.readAsInteger(bytes);
    bytes.skip(classNameLen);
    final List<String> result = new ArrayList<String>();
    String fieldName;
    while (true) {
        OGlobalProperty prop = null;
        final int len = OVarIntSerializer.readAsInteger(bytes);
        if (len == 0) {
            // SCAN COMPLETED
            break;
        } else if (len > 0) {
            // PARSE FIELD NAME
            fieldName = stringFromBytes(bytes.bytes, bytes.offset, len).intern();
            result.add(fieldName);
            // SKIP THE REST
            bytes.skip(len + OIntegerSerializer.INT_SIZE + 1);
        } else {
            // LOAD GLOBAL PROPERTY BY ID
            final int id = (len * -1) - 1;
            prop = ODocumentInternal.getGlobalPropertyById(reference, id);
            if (prop == null) {
                throw new OSerializationException("Missing property definition for property id '" + id + "'");
            }
            result.add(prop.getName());
            // SKIP THE REST
            bytes.skip(OIntegerSerializer.INT_SIZE + (prop.getType() != OType.ANY ? 0 : 1));
        }
    }
    return result.toArray(new String[result.size()]);
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Example 3 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class ORecordSerializerNetworkV0 method writeEmbeddedCollection.

private int writeEmbeddedCollection(final BytesContainer bytes, final Collection<?> value, final OType linkedType) {
    final int pos = OVarIntSerializer.write(bytes, value.size());
    // TODO manage embedded type from schema and auto-determined.
    writeOType(bytes, bytes.alloc(1), OType.ANY);
    for (Object itemValue : value) {
        // TODO:manage in a better way null entry
        if (itemValue == null) {
            writeOType(bytes, bytes.alloc(1), OType.ANY);
            continue;
        }
        OType type;
        if (linkedType == null)
            type = getTypeFromValueEmbedded(itemValue);
        else
            type = linkedType;
        if (type != null) {
            writeOType(bytes, bytes.alloc(1), type);
            serializeValue(bytes, itemValue, type, null);
        } else {
            throw new OSerializationException("Impossible serialize value of type " + value.getClass() + " with the ODocument binary serializer");
        }
    }
    return pos;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OType(com.orientechnologies.orient.core.metadata.schema.OType)

Example 4 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class ORecordSerializerNetworkV0 method writeEmbeddedMap.

@SuppressWarnings("unchecked")
private int writeEmbeddedMap(BytesContainer bytes, Map<Object, Object> map) {
    final int[] pos = new int[map.size()];
    int i = 0;
    Entry<Object, Object>[] values = new Entry[map.size()];
    final int fullPos = OVarIntSerializer.write(bytes, map.size());
    for (Entry<Object, Object> entry : map.entrySet()) {
        // TODO:check skip of complex types
        // FIXME: changed to support only string key on map
        OType type = OType.STRING;
        writeOType(bytes, bytes.alloc(1), type);
        writeString(bytes, entry.getKey().toString());
        pos[i] = bytes.alloc(OIntegerSerializer.INT_SIZE + 1);
        values[i] = entry;
        i++;
    }
    for (i = 0; i < values.length; i++) {
        int pointer = 0;
        final Object value = values[i].getValue();
        if (value != null) {
            final OType type = getTypeFromValueEmbedded(value);
            if (type == null) {
                throw new OSerializationException("Impossible serialize value of type " + value.getClass() + " with the ODocument binary serializer");
            }
            pointer = serializeValue(bytes, value, type, null);
            OIntegerSerializer.INSTANCE.serializeLiteral(pointer, bytes.bytes, pos[i]);
            writeOType(bytes, (pos[i] + OIntegerSerializer.INT_SIZE), type);
        }
    }
    return fullPos;
}
Also used : ODocumentEntry(com.orientechnologies.orient.core.record.impl.ODocumentEntry) Entry(java.util.Map.Entry) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OType(com.orientechnologies.orient.core.metadata.schema.OType)

Example 5 with OSerializationException

use of com.orientechnologies.orient.core.exception.OSerializationException in project orientdb by orientechnologies.

the class ORecordSerializerCSVAbstract method embeddedMapFromStream.

public Map<String, Object> embeddedMapFromStream(final ODocument iSourceDocument, final OType iLinkedType, final String iValue, final String iName) {
    if (iValue.length() == 0)
        return null;
    // REMOVE BEGIN & END MAP CHARACTERS
    String value = iValue.substring(1, iValue.length() - 1);
    @SuppressWarnings("rawtypes") Map map;
    if (iLinkedType == OType.LINK || iLinkedType == OType.EMBEDDED)
        map = new ORecordLazyMap(iSourceDocument, ODocument.RECORD_TYPE);
    else
        map = new OTrackedMap<Object>(iSourceDocument);
    if (value.length() == 0)
        return map;
    final List<String> items = OStringSerializerHelper.smartSplit(value, OStringSerializerHelper.RECORD_SEPARATOR, true, false);
    if (map instanceof ORecordElement)
        ((ORecordElement) map).setInternalStatus(STATUS.UNMARSHALLING);
    for (String item : items) {
        if (item != null && !item.isEmpty()) {
            final List<String> entries = OStringSerializerHelper.smartSplit(item, OStringSerializerHelper.ENTRY_SEPARATOR, true, false);
            if (!entries.isEmpty()) {
                final Object mapValueObject;
                if (entries.size() > 1) {
                    String mapValue = entries.get(1);
                    final OType linkedType;
                    if (iLinkedType == null)
                        if (!mapValue.isEmpty()) {
                            linkedType = getType(mapValue);
                            if ((iName == null || iSourceDocument.fieldType(iName) == null || iSourceDocument.fieldType(iName) != OType.EMBEDDEDMAP) && isConvertToLinkedMap(map, linkedType)) {
                                // CONVERT IT TO A LAZY MAP
                                map = new ORecordLazyMap(iSourceDocument, ODocument.RECORD_TYPE);
                                ((ORecordElement) map).setInternalStatus(STATUS.UNMARSHALLING);
                            } else if (map instanceof ORecordLazyMap && linkedType != OType.LINK) {
                                map = new OTrackedMap<Object>(iSourceDocument, map, null);
                            }
                        } else
                            linkedType = OType.EMBEDDED;
                    else
                        linkedType = iLinkedType;
                    if (linkedType == OType.EMBEDDED && mapValue.length() >= 2)
                        mapValue = mapValue.substring(1, mapValue.length() - 1);
                    mapValueObject = fieldTypeFromStream(iSourceDocument, linkedType, mapValue);
                    if (mapValueObject != null && mapValueObject instanceof ODocument)
                        ODocumentInternal.addOwner((ODocument) mapValueObject, iSourceDocument);
                } else
                    mapValueObject = null;
                final Object key = fieldTypeFromStream(iSourceDocument, OType.STRING, entries.get(0));
                try {
                    map.put(key, mapValueObject);
                } catch (ClassCastException e) {
                    throw OException.wrapException(new OSerializationException("Cannot load map because the type was not the expected: key=" + key + "(type " + key.getClass().toString() + "), value=" + mapValueObject + "(type " + key.getClass() + ")"), e);
                }
            }
        }
    }
    if (map instanceof ORecordElement)
        ((ORecordElement) map).setInternalStatus(STATUS.LOADED);
    return map;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) ORecordElement(com.orientechnologies.orient.core.db.record.ORecordElement) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)34 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)11 OException (com.orientechnologies.common.exception.OException)10 IOException (java.io.IOException)9 OType (com.orientechnologies.orient.core.metadata.schema.OType)7 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)6 ORecord (com.orientechnologies.orient.core.record.ORecord)6 ORID (com.orientechnologies.orient.core.id.ORID)5 ORecordId (com.orientechnologies.orient.core.id.ORecordId)5 OSerializableStream (com.orientechnologies.orient.core.serialization.OSerializableStream)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Map (java.util.Map)4 Entry (java.util.Map.Entry)4 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)3 ODocumentSerializable (com.orientechnologies.orient.core.serialization.ODocumentSerializable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ParseException (java.text.ParseException)3