Search in sources :

Example 1 with OTrackedMap

use of com.orientechnologies.orient.core.db.record.OTrackedMap 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)

Example 2 with OTrackedMap

use of com.orientechnologies.orient.core.db.record.OTrackedMap in project orientdb by orientechnologies.

the class OObjectSerializerHelper method multiValueToStream.

private static Object multiValueToStream(final Object iMultiValue, OType iType, final OEntityManager iEntityManager, final OUserObject2RecordHandler iObj2RecHandler, final ODatabaseObject db, final ODocument iRecord, final boolean iSaveOnlyDirty) {
    if (iMultiValue == null)
        return null;
    final Collection<Object> sourceValues;
    if (iMultiValue instanceof Collection<?>) {
        sourceValues = (Collection<Object>) iMultiValue;
    } else {
        sourceValues = (Collection<Object>) ((Map<?, ?>) iMultiValue).values();
    }
    if (iType == null) {
        if (sourceValues.size() == 0)
            return iMultiValue;
        // TRY TO UNDERSTAND THE COLLECTION TYPE BY ITS CONTENT
        final Object firstValue = sourceValues.iterator().next();
        if (firstValue == null)
            return iMultiValue;
        // DETERMINE THE RIGHT TYPE BASED ON SOURCE MULTI VALUE OBJECT
        if (OType.isSimpleType(firstValue)) {
            if (iMultiValue instanceof List)
                iType = OType.EMBEDDEDLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.EMBEDDEDSET;
            else
                iType = OType.EMBEDDEDMAP;
        } else {
            if (iMultiValue instanceof List)
                iType = OType.LINKLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.LINKSET;
            else
                iType = OType.LINKMAP;
        }
    }
    Object result = iMultiValue;
    final OType linkedType;
    // CREATE THE RETURN MULTI VALUE OBJECT BASED ON DISCOVERED TYPE
    if (iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.LINKSET)) {
        if (iRecord != null && iType.equals(OType.EMBEDDEDSET))
            result = new OTrackedSet<Object>(iRecord);
        else
            result = new ORecordLazySet(iRecord);
    } else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
        if (iRecord != null && iType.equals(OType.EMBEDDEDLIST))
            result = new OTrackedList<Object>(iRecord);
        else
            result = new ArrayList<Object>();
    }
    if (iType.equals(OType.LINKLIST) || iType.equals(OType.LINKSET) || iType.equals(OType.LINKMAP))
        linkedType = OType.LINK;
    else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.EMBEDDEDMAP))
        linkedType = OType.EMBEDDED;
    else
        throw new IllegalArgumentException("Type " + iType + " must be a multi value type (collection or map)");
    if (iMultiValue instanceof Set<?>) {
        for (Object o : sourceValues) {
            ((Collection<Object>) result).add(typeToStream(o, linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
        }
    } else if (iMultiValue instanceof List<?>) {
        for (int i = 0; i < sourceValues.size(); i++) {
            ((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
        }
    } else {
        if (iMultiValue instanceof OObjectLazyMap<?>) {
            result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
        } else {
            if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
                result = new OTrackedMap<Object>(iRecord);
            else
                result = new HashMap<Object, Object>();
            for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
                ((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
            }
        }
    }
    return result;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Entry(java.util.Map.Entry) Collection(java.util.Collection) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) List(java.util.List) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) ArrayList(java.util.ArrayList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) Map(java.util.Map) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap)

Example 3 with OTrackedMap

use of com.orientechnologies.orient.core.db.record.OTrackedMap in project orientdb by orientechnologies.

the class ORecordSerializerNetworkV0 method readEmbeddedMap.

private Object readEmbeddedMap(final BytesContainer bytes, final ODocument document) {
    int size = OVarIntSerializer.readAsInteger(bytes);
    final Map<Object, Object> result = new OTrackedMap<Object>(document);
    int last = 0;
    while ((size--) > 0) {
        OType keyType = readOType(bytes);
        Object key = deserializeValue(bytes, keyType, document);
        final int valuePos = readInteger(bytes);
        final OType type = readOType(bytes);
        if (valuePos != 0) {
            int headerCursor = bytes.offset;
            bytes.offset = valuePos;
            Object value = deserializeValue(bytes, type, document);
            if (bytes.offset > last)
                last = bytes.offset;
            bytes.offset = headerCursor;
            result.put(key, value);
        } else
            result.put(key, null);
    }
    if (last > bytes.offset)
        bytes.offset = last;
    return result;
}
Also used : OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OType(com.orientechnologies.orient.core.metadata.schema.OType)

Example 4 with OTrackedMap

use of com.orientechnologies.orient.core.db.record.OTrackedMap in project orientdb by orientechnologies.

the class OCommandExecutorSQLUpdate method handlePutEntries.

@SuppressWarnings({ "unchecked", "rawtypes" })
private boolean handlePutEntries(ODocument record) {
    boolean updated = false;
    if (!putEntries.isEmpty()) {
        // BIND VALUES TO PUT (AS MAP)
        for (OTriple<String, String, Object> entry : putEntries) {
            Object fieldValue = record.field(entry.getKey());
            if (fieldValue == null) {
                if (ODocumentInternal.getImmutableSchemaClass(record) != null) {
                    final OProperty property = ODocumentInternal.getImmutableSchemaClass(record).getProperty(entry.getKey());
                    if (property != null && (property.getType() != null && (!property.getType().equals(OType.EMBEDDEDMAP) && !property.getType().equals(OType.LINKMAP)))) {
                        throw new OCommandExecutionException("field " + entry.getKey() + " is not defined as a map");
                    }
                }
                fieldValue = new HashMap<String, Object>();
                record.field(entry.getKey(), fieldValue);
            }
            if (fieldValue instanceof Map<?, ?>) {
                Map<String, Object> map = (Map<String, Object>) fieldValue;
                OPair<String, Object> pair = entry.getValue();
                Object value = extractValue(record, pair);
                if (record.getSchemaClass() != null) {
                    final OProperty property = record.getSchemaClass().getProperty(entry.getKey());
                    if (property != null && property.getType().equals(OType.LINKMAP) && !(value instanceof OIdentifiable)) {
                        throw new OCommandExecutionException("field " + entry.getKey() + " defined of type LINKMAP accept only link values");
                    }
                }
                if (OType.LINKMAP.equals(OType.getTypeByValue(fieldValue)) && !(value instanceof OIdentifiable)) {
                    map = new OTrackedMap(record, map, Object.class);
                    record.field(entry.getKey(), map, OType.EMBEDDEDMAP);
                }
                map.put(pair.getKey(), value);
                updated = true;
            }
        }
    }
    return updated;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 5 with OTrackedMap

use of com.orientechnologies.orient.core.db.record.OTrackedMap in project orientdb by orientechnologies.

the class OObjectEntitySerializer method multiValueToStream.

@SuppressWarnings("unchecked")
private static Object multiValueToStream(final Object iMultiValue, OType iType, final ODatabaseObject db, final ODocument iRecord) {
    if (iMultiValue == null)
        return null;
    final Collection<Object> sourceValues;
    if (iMultiValue instanceof Collection<?>) {
        sourceValues = (Collection<Object>) iMultiValue;
    } else {
        sourceValues = (Collection<Object>) ((Map<?, ?>) iMultiValue).values();
    }
    if (sourceValues.size() == 0)
        return iMultiValue;
    // TRY TO UNDERSTAND THE COLLECTION TYPE BY ITS CONTENT
    final Object firstValue = sourceValues.iterator().next();
    if (firstValue == null)
        return iMultiValue;
    if (iType == null) {
        // DETERMINE THE RIGHT TYPE BASED ON SOURCE MULTI VALUE OBJECT
        if (OType.isSimpleType(firstValue)) {
            if (iMultiValue instanceof List)
                iType = OType.EMBEDDEDLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.EMBEDDEDSET;
            else
                iType = OType.EMBEDDEDMAP;
        } else {
            if (iMultiValue instanceof List)
                iType = OType.LINKLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.LINKSET;
            else
                iType = OType.LINKMAP;
        }
    }
    Object result = iMultiValue;
    final OType linkedType;
    // CREATE THE RETURN MULTI VALUE OBJECT BASED ON DISCOVERED TYPE
    if (iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.LINKSET)) {
        if (isToSerialize(firstValue.getClass()))
            result = new HashSet<Object>();
        else if ((iRecord != null && iType.equals(OType.EMBEDDEDSET)) || OType.isSimpleType(firstValue))
            result = new OTrackedSet<Object>(iRecord);
        else
            result = new ORecordLazySet(iRecord);
    } else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
        if (isToSerialize(firstValue.getClass()))
            result = new ArrayList<Object>();
        else if ((iRecord != null && iType.equals(OType.EMBEDDEDLIST)) || OType.isSimpleType(firstValue))
            result = new OTrackedList<Object>(iRecord);
        else
            result = new ORecordLazyList(iRecord);
    }
    if (iType.equals(OType.LINKLIST) || iType.equals(OType.LINKSET) || iType.equals(OType.LINKMAP))
        linkedType = OType.LINK;
    else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.EMBEDDEDMAP))
        if (firstValue instanceof List)
            linkedType = OType.EMBEDDEDLIST;
        else if (firstValue instanceof Set)
            linkedType = OType.EMBEDDEDSET;
        else if (firstValue instanceof Map)
            linkedType = OType.EMBEDDEDMAP;
        else
            linkedType = OType.EMBEDDED;
    else
        throw new IllegalArgumentException("Type " + iType + " must be a multi value type (collection or map)");
    if (iMultiValue instanceof Set<?>) {
        for (Object o : sourceValues) {
            ((Set<Object>) result).add(typeToStream(o, linkedType, db, null));
        }
    } else if (iMultiValue instanceof List<?>) {
        for (int i = 0; i < sourceValues.size(); i++) {
            ((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, db, null));
        }
    } else {
        if (iMultiValue instanceof OObjectLazyMap<?>) {
            result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
        } else {
            if (isToSerialize(firstValue.getClass()))
                result = new HashMap<Object, Object>();
            else if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
                result = new OTrackedMap<Object>(iRecord);
            else
                result = new ORecordLazyMap(iRecord);
            for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
                ((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, db, null));
            }
        }
    }
    return result;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) HashSet(java.util.HashSet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Entry(java.util.Map.Entry) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) Collection(java.util.Collection) ProxyObject(javassist.util.proxy.ProxyObject) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OSchemaProxyObject(com.orientechnologies.orient.object.metadata.schema.OSchemaProxyObject) List(java.util.List) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ArrayList(java.util.ArrayList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) HashMap(java.util.HashMap) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) HashSet(java.util.HashSet)

Aggregations

OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)7 OType (com.orientechnologies.orient.core.metadata.schema.OType)5 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)3 OObjectLazyMap (com.orientechnologies.orient.object.db.OObjectLazyMap)3 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)2 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)2 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 ProxyObject (javassist.util.proxy.ProxyObject)2 OObjectLazyMultivalueElement (com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement)1 OMultiValueChangeTimeLine (com.orientechnologies.orient.core.db.record.OMultiValueChangeTimeLine)1