Search in sources :

Example 1 with ORecordLazyList

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

the class ORecordSerializerNetworkV0 method deserializeValue.

public Object deserializeValue(BytesContainer bytes, OType type, ODocument document) {
    Object value = null;
    switch(type) {
        case INTEGER:
            value = OVarIntSerializer.readAsInteger(bytes);
            break;
        case LONG:
            value = OVarIntSerializer.readAsLong(bytes);
            break;
        case SHORT:
            value = OVarIntSerializer.readAsShort(bytes);
            break;
        case STRING:
            value = readString(bytes);
            break;
        case DOUBLE:
            value = Double.longBitsToDouble(readLong(bytes));
            break;
        case FLOAT:
            value = Float.intBitsToFloat(readInteger(bytes));
            break;
        case BYTE:
            value = readByte(bytes);
            break;
        case BOOLEAN:
            value = readByte(bytes) == 1;
            break;
        case DATETIME:
            value = new Date(OVarIntSerializer.readAsLong(bytes));
            break;
        case DATE:
            long savedTime = OVarIntSerializer.readAsLong(bytes) * MILLISEC_PER_DAY;
            savedTime = convertDayToTimezone(TimeZone.getTimeZone("GMT"), ODateHelper.getDatabaseTimeZone(), savedTime);
            value = new Date(savedTime);
            break;
        case EMBEDDED:
            value = new ODocument();
            deserialize((ODocument) value, bytes);
            if (((ODocument) value).containsField(ODocumentSerializable.CLASS_NAME)) {
                String className = ((ODocument) value).field(ODocumentSerializable.CLASS_NAME);
                try {
                    Class<?> clazz = Class.forName(className);
                    ODocumentSerializable newValue = (ODocumentSerializable) clazz.newInstance();
                    newValue.fromDocument((ODocument) value);
                    value = newValue;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else
                ODocumentInternal.addOwner((ODocument) value, document);
            break;
        case EMBEDDEDSET:
            value = readEmbeddedCollection(bytes, new OTrackedSet<Object>(document), document);
            break;
        case EMBEDDEDLIST:
            value = readEmbeddedCollection(bytes, new OTrackedList<Object>(document), document);
            break;
        case LINKSET:
            value = readLinkCollection(bytes, new ORecordLazySet(document));
            break;
        case LINKLIST:
            value = readLinkCollection(bytes, new ORecordLazyList(document));
            break;
        case BINARY:
            value = readBinary(bytes);
            break;
        case LINK:
            value = readOptimizedLink(bytes);
            break;
        case LINKMAP:
            value = readLinkMap(bytes, document);
            break;
        case EMBEDDEDMAP:
            value = readEmbeddedMap(bytes, document);
            break;
        case DECIMAL:
            value = ODecimalSerializer.INSTANCE.deserialize(bytes.bytes, bytes.offset);
            bytes.skip(ODecimalSerializer.INSTANCE.getObjectSize(bytes.bytes, bytes.offset));
            break;
        case LINKBAG:
            ORidBag bag = new ORidBag();
            bag.fromStream(bytes);
            bag.setOwner(document);
            value = bag;
            break;
        case TRANSIENT:
            break;
        case CUSTOM:
            try {
                String className = readString(bytes);
                Class<?> clazz = Class.forName(className);
                OSerializableStream stream = (OSerializableStream) clazz.newInstance();
                stream.fromStream(readBinary(bytes));
                if (stream instanceof OSerializableWrapper)
                    value = ((OSerializableWrapper) stream).getSerializable();
                else
                    value = stream;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        case ANY:
            break;
    }
    return value;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OException(com.orientechnologies.common.exception.OException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OValidationException(com.orientechnologies.orient.core.exception.OValidationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with ORecordLazyList

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

the class ORecordSerializerCSVAbstract method embeddedCollectionFromStream.

public Object embeddedCollectionFromStream(final ODocument iDocument, final OType iType, OClass iLinkedClass, final OType iLinkedType, final String iValue) {
    if (iValue.length() == 0)
        return null;
    // REMOVE BEGIN & END COLLECTIONS CHARACTERS IF IT'S A COLLECTION
    final String value = iValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN || iValue.charAt(0) == OStringSerializerHelper.SET_BEGIN ? iValue.substring(1, iValue.length() - 1) : iValue;
    Collection<?> coll;
    if (iLinkedType == OType.LINK) {
        if (iDocument != null)
            coll = (Collection<?>) (iType == OType.EMBEDDEDLIST ? new ORecordLazyList(iDocument).setStreamedContent(new StringBuilder(value)) : unserializeSet(iDocument, value));
        else {
            if (iType == OType.EMBEDDEDLIST)
                coll = (Collection<?>) new ORecordLazyList().setStreamedContent(new StringBuilder(value));
            else {
                return unserializeSet(iDocument, value);
            }
        }
    } else
        coll = iType == OType.EMBEDDEDLIST ? new OTrackedList<Object>(iDocument) : new OTrackedSet<Object>(iDocument);
    if (value.length() == 0)
        return coll;
    OType linkedType;
    if (coll instanceof ORecordElement)
        ((ORecordElement) coll).setInternalStatus(STATUS.UNMARSHALLING);
    final List<String> items = OStringSerializerHelper.smartSplit(value, OStringSerializerHelper.RECORD_SEPARATOR, true, false);
    for (String item : items) {
        Object objectToAdd = null;
        linkedType = null;
        if (item.equals("null"))
            // NULL VALUE
            objectToAdd = null;
        else if (item.length() > 2 && item.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN) {
            // REMOVE EMBEDDED BEGIN/END CHARS
            item = item.substring(1, item.length() - 1);
            if (!item.isEmpty()) {
                // EMBEDDED RECORD, EXTRACT THE CLASS NAME IF DIFFERENT BY THE PASSED (SUB-CLASS OR IT WAS PASSED NULL)
                iLinkedClass = OStringSerializerHelper.getRecordClassName(item, iLinkedClass);
                if (iLinkedClass != null) {
                    ODocument doc = new ODocument();
                    objectToAdd = fromString(item, doc, null);
                    ODocumentInternal.fillClassNameIfNeeded(doc, iLinkedClass.getName());
                } else
                    // EMBEDDED OBJECT
                    objectToAdd = fieldTypeFromStream(iDocument, OType.EMBEDDED, item);
            }
        } else {
            if (linkedType == null) {
                final char begin = item.length() > 0 ? item.charAt(0) : OStringSerializerHelper.LINK;
                // AUTO-DETERMINE LINKED TYPE
                if (begin == OStringSerializerHelper.LINK)
                    linkedType = OType.LINK;
                else
                    linkedType = getType(item);
                if (linkedType == null)
                    throw new IllegalArgumentException("Linked type cannot be null. Probably the serialized type has not stored the type along with data");
            }
            if (iLinkedType == OType.CUSTOM)
                item = item.substring(1, item.length() - 1);
            objectToAdd = fieldTypeFromStream(iDocument, linkedType, item);
        }
        if (objectToAdd != null && objectToAdd instanceof ODocument && coll instanceof ORecordElement)
            ODocumentInternal.addOwner((ODocument) objectToAdd, (ORecordElement) coll);
        ((Collection<Object>) coll).add(objectToAdd);
    }
    if (coll instanceof ORecordElement)
        ((ORecordElement) coll).setInternalStatus(STATUS.LOADED);
    return coll;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordElement(com.orientechnologies.orient.core.db.record.ORecordElement) Collection(java.util.Collection) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with ORecordLazyList

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

the class ORecordSerializerJSON method getValueAsCollection.

private Object getValueAsCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
    // remove square brackets
    iFieldValue = iFieldValue.substring(1, iFieldValue.length() - 1);
    if (iType == OType.LINKBAG) {
        final ORidBag bag = new ORidBag();
        parseCollection(iRecord, iFieldValue, iType, OType.LINK, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {

            @Override
            public void visitItem(Object item) {
                bag.add((OIdentifiable) item);
            }
        });
        return bag;
    } else if (iType == OType.LINKSET) {
        return getValueAsLinkedCollection(new ORecordLazySet(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else if (iType == OType.LINKLIST) {
        return getValueAsLinkedCollection(new ORecordLazyList(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else if (iType == OType.EMBEDDEDSET) {
        return getValueAsEmbeddedCollection(new OTrackedSet<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else {
        return getValueAsEmbeddedCollection(new OTrackedList<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    }
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 4 with ORecordLazyList

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

the class OObjectProxyMethodHandler method manageCollectionSave.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object manageCollectionSave(final Object self, final Field f, Collection<?> value, final boolean customSerialization, final boolean isFieldUpdate) {
    final Class genericType = OReflectionHelper.getGenericMultivalueType(f);
    if (customSerialization) {
        if (value instanceof List<?>) {
            final List<Object> list = new ArrayList<Object>();
            setDocFieldValue(f.getName(), list, OType.EMBEDDEDLIST);
            value = new OObjectCustomSerializerList(OObjectEntitySerializer.getSerializedType(f), doc, new ArrayList<Object>(), (List<Object>) value);
        } else {
            final Set<Object> set = new HashSet<Object>();
            setDocFieldValue(f.getName(), set, OType.EMBEDDEDSET);
            value = new OObjectCustomSerializerSet(OObjectEntitySerializer.getSerializedType(f), doc, set, (Set<Object>) value);
        }
    } else if (genericType != null && genericType.isEnum()) {
        if (value instanceof List<?>) {
            final List<Object> list = new ArrayList<Object>();
            setDocFieldValue(f.getName(), list, OType.EMBEDDEDLIST);
            value = new OObjectEnumLazyList(genericType, doc, list, (List<Object>) value);
        } else {
            final Set<Object> set = new HashSet<Object>();
            setDocFieldValue(f.getName(), set, OType.EMBEDDEDSET);
            value = new OObjectEnumLazySet(genericType, doc, set, (Set<Object>) value);
        }
    } else if (!(value instanceof OObjectLazyMultivalueElement)) {
        boolean embedded = OObjectEntitySerializer.isEmbeddedField(self.getClass(), f.getName());
        if (value instanceof List) {
            OType type = embedded ? OType.EMBEDDEDLIST : OType.LINKLIST;
            List<OIdentifiable> docList = doc.field(f.getName(), type);
            if (docList == null) {
                if (embedded)
                    docList = new OTrackedList<OIdentifiable>(doc);
                else
                    docList = new ORecordLazyList(doc);
                setDocFieldValue(f.getName(), docList, type);
            } else if (isFieldUpdate) {
                docList.clear();
            }
            value = new OObjectLazyList(self, docList, value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
        } else if (value instanceof Set) {
            OType type = embedded ? OType.EMBEDDEDSET : OType.LINKSET;
            Set<OIdentifiable> docSet = doc.field(f.getName(), type);
            if (docSet == null) {
                if (embedded)
                    docSet = new OTrackedSet<OIdentifiable>(doc);
                else
                    docSet = new ORecordLazySet(doc);
                setDocFieldValue(f.getName(), docSet, type);
            } else if (isFieldUpdate) {
                docSet.clear();
            }
            value = new OObjectLazySet(self, docSet, (Set<?>) value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
        }
    }
    if (!((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).isLazyLoading())
        ((OObjectLazyMultivalueElement) value).detach(false);
    return value;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OObjectCustomSerializerSet(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerSet) HashSet(java.util.HashSet) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OObjectEnumLazySet(com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OObjectEnumLazyList(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyList) ArrayList(java.util.ArrayList) OObjectEnumLazySet(com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ArrayList(java.util.ArrayList) OObjectEnumLazyList(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyList) OObjectCustomSerializerList(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerList) List(java.util.List) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OObjectCustomSerializerSet(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerSet) HashSet(java.util.HashSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) OObjectCustomSerializerList(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerList) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject)

Example 5 with ORecordLazyList

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

the class OrientJdbcResultSetMetaData method getColumnType.

public int getColumnType(final int column) throws SQLException {
    final ODocument currentRecord = getCurrentRecord();
    final String[] fieldNames = currentRecord.fieldNames();
    if (column > fieldNames.length)
        return Types.NULL;
    String fieldName = fieldNames[column - 1];
    // The OClass is not available so attempting to retrieve the OType from
    // the schema class
    // results in a NullPointerException
    // OClass oclass = currentRecord.getSchemaClass();
    OType otype = currentRecord.fieldType(fieldName);
    if (otype == null) {
        Object value = currentRecord.field(fieldName);
        if (value == null) {
            return Types.NULL;
        } else if (value instanceof OBlob) {
            // records
            return Types.BINARY;
        } else if (value instanceof ORecordLazyList) {
            ORecordLazyList list = (ORecordLazyList) value;
            // check if all the list items are instances of ORecordBytes
            ListIterator<OIdentifiable> iterator = list.listIterator();
            OIdentifiable listElement;
            boolean stop = false;
            while (iterator.hasNext() && !stop) {
                listElement = iterator.next();
                if (!(listElement instanceof OBlob))
                    stop = true;
            }
            if (!stop) {
                return Types.BLOB;
            }
        }
        return this.getSQLTypeFromJavaClass(value);
    } else {
        if (otype == OType.EMBEDDED || otype == OType.LINK) {
            Object value = currentRecord.field(fieldName);
            if (value == null) {
                return Types.NULL;
            }
            // 1. Check if the type is another record or a collection of records
            if (value instanceof OBlob) {
                return Types.BINARY;
            } else {
                // the default type
                return typesSqlTypes.get(otype);
            }
        } else {
            if (otype == OType.EMBEDDEDLIST || otype == OType.LINKLIST) {
                Object value = currentRecord.field(fieldName);
                if (value == null) {
                    return Types.NULL;
                }
                if (value instanceof ORecordLazyList) {
                    ORecordLazyList list = (ORecordLazyList) value;
                    // check if all the list items are instances of ORecordBytes
                    ListIterator<OIdentifiable> iterator = list.listIterator();
                    OIdentifiable listElement;
                    boolean stop = false;
                    while (iterator.hasNext() && !stop) {
                        listElement = iterator.next();
                        if (!(listElement instanceof OBlob))
                            stop = true;
                    }
                    if (stop) {
                        return typesSqlTypes.get(otype);
                    } else {
                        return Types.BLOB;
                    }
                } else {
                    return typesSqlTypes.get(otype);
                //            return Types.JAVA_OBJECT;
                }
            } else {
                return typesSqlTypes.get(otype);
            }
        }
    }
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OBlob(com.orientechnologies.orient.core.record.impl.OBlob) OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)12 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)6 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)6 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)5 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)4 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)4 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)4 OType (com.orientechnologies.orient.core.metadata.schema.OType)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 OException (com.orientechnologies.common.exception.OException)2 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2