Search in sources :

Example 6 with ORecordLazyMultiValue

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

the class OrientJdbcResultSet method getObject.

public Object getObject(String columnLabel) throws SQLException {
    if ("@rid".equals(columnLabel) || "rid".equals(columnLabel)) {
        return ((ODocument) document.field("rid")).getIdentity().toString();
    }
    if ("@class".equals(columnLabel) || "class".equals(columnLabel))
        return ((ODocument) document.field("rid")).getClassName();
    try {
        Object value = document.field(columnLabel);
        if (value == null) {
            return null;
        } else {
            // of ODocument
            if (value instanceof ORecordLazyMultiValue) {
                ORecordLazyMultiValue lazyRecord = (ORecordLazyMultiValue) value;
                lazyRecord.convertLinks2Records();
                return lazyRecord;
            } else {
                return value;
            }
        }
    } catch (Exception e) {
        throw new SQLException("An error occurred during the retrieval of the Java Object at column '" + columnLabel + "'", e);
    }
}
Also used : ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)

Example 7 with ORecordLazyMultiValue

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

the class ODatabaseRepair method removeBrokenLinks.

protected long removeBrokenLinks() {
    long fixedLinks = 0l;
    long modifiedDocuments = 0l;
    long errors = 0l;
    message("\n- Removing broken links...");
    for (String clusterName : database.getClusterNames()) {
        for (ORecord rec : database.browseCluster(clusterName)) {
            try {
                if (rec instanceof ODocument) {
                    boolean changed = false;
                    final ODocument doc = (ODocument) rec;
                    for (String fieldName : doc.fieldNames()) {
                        final Object fieldValue = doc.rawField(fieldName);
                        if (fieldValue instanceof OIdentifiable) {
                            if (fixLink(fieldValue)) {
                                doc.field(fieldName, (OIdentifiable) null);
                                fixedLinks++;
                                changed = true;
                                if (verbose)
                                    message("\n--- reset link " + ((OIdentifiable) fieldValue).getIdentity() + " in field '" + fieldName + "' (rid=" + doc.getIdentity() + ")");
                            }
                        } else if (fieldValue instanceof Iterable<?>) {
                            if (fieldValue instanceof ORecordLazyMultiValue)
                                ((ORecordLazyMultiValue) fieldValue).setAutoConvertToRecord(false);
                            final Iterator<Object> it = ((Iterable) fieldValue).iterator();
                            for (int i = 0; it.hasNext(); ++i) {
                                final Object v = it.next();
                                if (fixLink(v)) {
                                    it.remove();
                                    fixedLinks++;
                                    changed = true;
                                    if (verbose)
                                        message("\n--- reset link " + ((OIdentifiable) v).getIdentity() + " as item " + i + " in collection of field '" + fieldName + "' (rid=" + doc.getIdentity() + ")");
                                }
                            }
                        }
                    }
                    if (changed) {
                        modifiedDocuments++;
                        doc.save();
                        if (verbose)
                            message("\n-- updated document " + doc.getIdentity());
                    }
                }
            } catch (Exception e) {
                errors++;
            }
        }
    }
    message("\n-- Done! Fixed links: " + fixedLinks + ", modified documents: " + modifiedDocuments);
    return errors;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) Iterator(java.util.Iterator) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)

Example 8 with ORecordLazyMultiValue

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

the class OFetchHelper method fetchCollection.

@SuppressWarnings("unchecked")
private static void fetchCollection(final ODocument iRootRecord, final Object iUserObject, final OFetchPlan iFetchPlan, final Object fieldValue, final String fieldName, final int iCurrentLevel, final int iLevelFromRoot, final int iFieldDepthLevel, final Map<ORID, Integer> parsedRecords, final String iFieldPathFromRoot, final OFetchListener iListener, final OFetchContext iContext) throws IOException {
    final Iterable<?> linked;
    if (fieldValue instanceof Iterable<?> || fieldValue instanceof ORidBag) {
        linked = (Iterable<OIdentifiable>) fieldValue;
        iContext.onBeforeCollection(iRootRecord, fieldName, iUserObject, (Iterable) linked);
    } else if (fieldValue.getClass().isArray()) {
        linked = OMultiValue.getMultiValueIterable(fieldValue, false);
        iContext.onBeforeCollection(iRootRecord, fieldName, iUserObject, (Iterable) linked);
    } else if (fieldValue instanceof Map<?, ?>) {
        linked = (Collection<?>) ((Map<?, ?>) fieldValue).values();
        iContext.onBeforeMap(iRootRecord, fieldName, iUserObject);
    } else
        throw new IllegalStateException("Unrecognized type: " + fieldValue.getClass());
    final Iterator<?> iter;
    if (linked instanceof ORecordLazyMultiValue)
        iter = ((ORecordLazyMultiValue) linked).rawIterator();
    else
        iter = linked.iterator();
    try {
        while (iter.hasNext()) {
            final Object o = iter.next();
            if (o == null)
                continue;
            if (o instanceof OIdentifiable) {
                OIdentifiable d = (OIdentifiable) o;
                // GO RECURSIVELY
                final Integer fieldDepthLevel = parsedRecords.get(d.getIdentity());
                if (!d.getIdentity().isPersistent() || (fieldDepthLevel != null && fieldDepthLevel.intValue() == iLevelFromRoot)) {
                    removeParsedFromMap(parsedRecords, d);
                    d = d.getRecord();
                    if (d == null)
                        iListener.processStandardField(null, d, null, iContext, iUserObject, "", null);
                    else if (!(d instanceof ODocument)) {
                        iListener.processStandardField(null, d, fieldName, iContext, iUserObject, "", null);
                    } else {
                        iContext.onBeforeDocument(iRootRecord, (ODocument) d, fieldName, iUserObject);
                        final Object userObject = iListener.fetchLinkedCollectionValue(iRootRecord, iUserObject, fieldName, (ODocument) d, iContext);
                        processRecord((ODocument) d, userObject, iFetchPlan, iCurrentLevel, iLevelFromRoot, iFieldDepthLevel, parsedRecords, iFieldPathFromRoot, iListener, iContext, "");
                        iContext.onAfterDocument(iRootRecord, (ODocument) d, fieldName, iUserObject);
                    }
                } else {
                    iListener.parseLinkedCollectionValue(iRootRecord, d, iUserObject, fieldName, iContext);
                }
            } else if (o instanceof Map<?, ?>) {
                fetchMap(iRootRecord, iUserObject, iFetchPlan, o, null, iCurrentLevel + 1, iLevelFromRoot, iFieldDepthLevel, parsedRecords, iFieldPathFromRoot, iListener, iContext);
            } else if (OMultiValue.isMultiValue(o)) {
                fetchCollection(iRootRecord, iUserObject, iFetchPlan, o, null, iCurrentLevel + 1, iLevelFromRoot, iFieldDepthLevel, parsedRecords, iFieldPathFromRoot, iListener, iContext);
            } else if (o instanceof String || o instanceof Number || o instanceof Boolean) {
                ((OJSONFetchContext) iContext).getJsonWriter().writeValue(0, false, o);
            }
        }
    } finally {
        if (fieldValue instanceof Iterable<?> || fieldValue instanceof ORidBag)
            iContext.onAfterCollection(iRootRecord, fieldName, iUserObject);
        else if (fieldValue.getClass().isArray())
            iContext.onAfterCollection(iRootRecord, fieldName, iUserObject);
        else if (fieldValue instanceof Map<?, ?>)
            iContext.onAfterMap(iRootRecord, fieldName, iUserObject);
    }
}
Also used : OJSONFetchContext(com.orientechnologies.orient.core.fetch.json.OJSONFetchContext) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 9 with ORecordLazyMultiValue

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

the class OFetchHelper method processRecord.

private static void processRecord(final ODocument record, final Object iUserObject, final OFetchPlan iFetchPlan, final int iCurrentLevel, final int iLevelFromRoot, final int iFieldDepthLevel, final Map<ORID, Integer> parsedRecords, final String iFieldPathFromRoot, final OFetchListener iListener, final OFetchContext iContext, final String iFormat) throws IOException {
    if (record == null)
        return;
    if (!iListener.requireFieldProcessing() && iFetchPlan == OFetchHelper.DEFAULT_FETCHPLAN)
        return;
    Object fieldValue;
    iContext.onBeforeFetch(record);
    Set<String> toRemove = new HashSet<String>();
    for (String fieldName : record.fieldNames()) {
        String fieldPath = !iFieldPathFromRoot.isEmpty() ? iFieldPathFromRoot + "." + fieldName : fieldName;
        int depthLevel;
        depthLevel = getDepthLevel(iFetchPlan, fieldPath, iCurrentLevel);
        if (depthLevel == -2) {
            toRemove.add(fieldName);
            continue;
        }
        if (iFieldDepthLevel > -1)
            depthLevel = iFieldDepthLevel;
        fieldValue = record.rawField(fieldName);
        OType fieldType = record.fieldType(fieldName);
        boolean fetch = !iFormat.contains("shallow") && (!(fieldValue instanceof OIdentifiable) || depthLevel == -1 || iCurrentLevel <= depthLevel || (iFetchPlan != null && iFetchPlan.has(fieldPath, iCurrentLevel)));
        final boolean isEmbedded = isEmbedded(fieldValue);
        if (!fetch && isEmbedded && iContext.fetchEmbeddedDocuments())
            // EMBEDDED, GO DEEPER
            fetch = true;
        if (iFormat.contains("shallow") || fieldValue == null || (!fetch && fieldValue instanceof OIdentifiable) || !(fieldValue instanceof OIdentifiable) && (!(fieldValue instanceof ORecordLazyMultiValue) || !((ORecordLazyMultiValue) fieldValue).rawIterator().hasNext() || !(((ORecordLazyMultiValue) fieldValue).rawIterator().next() instanceof OIdentifiable)) && (!(fieldValue.getClass().isArray()) || Array.getLength(fieldValue) == 0 || !(Array.get(fieldValue, 0) instanceof OIdentifiable)) && !containsIdentifiers(fieldValue)) {
            iContext.onBeforeStandardField(fieldValue, fieldName, iUserObject, fieldType);
            iListener.processStandardField(record, fieldValue, fieldName, iContext, iUserObject, iFormat, fieldType);
            iContext.onAfterStandardField(fieldValue, fieldName, iUserObject, fieldType);
        } else {
            try {
                if (fetch) {
                    final int nextLevel = isEmbedded ? iLevelFromRoot : iLevelFromRoot + 1;
                    fetch(record, iUserObject, iFetchPlan, fieldValue, fieldName, iCurrentLevel, nextLevel, iFieldDepthLevel, parsedRecords, depthLevel, fieldPath, iListener, iContext);
                }
            } catch (Exception e) {
                OLogManager.instance().error(null, "Fetching error on record %s", e, record.getIdentity());
            }
        }
    }
    for (String fieldName : toRemove) {
        iListener.skipStandardField(record, fieldName, iContext, iUserObject, iFormat);
    }
    iContext.onAfterFetch(record);
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) IOException(java.io.IOException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)

Example 10 with ORecordLazyMultiValue

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

the class ORecordSerializerJSON method fromString.

public ORecord fromString(String iSource, ORecord iRecord, final String[] iFields, final String iOptions, boolean needReload) {
    iSource = unwrapSource(iSource);
    String className = null;
    boolean noMap = false;
    if (iOptions != null) {
        final String[] format = iOptions.split(",");
        for (String f : format) if (f.equalsIgnoreCase("noMap"))
            noMap = true;
    }
    if (iRecord != null)
        // RESET ALL THE FIELDS
        iRecord.clear();
    final List<String> fields = OStringSerializerHelper.smartSplit(iSource, PARAMETER_SEPARATOR, 0, -1, true, true, false, false, ' ', '\n', '\r', '\t');
    if (fields.size() % 2 != 0)
        throw new OSerializationException("Error on unmarshalling JSON content: wrong format \"" + iSource + "\". Use <field> : <value>");
    Map<String, Character> fieldTypes = null;
    if (fields != null && fields.size() > 0) {
        // SEARCH FOR FIELD TYPES IF ANY
        for (int i = 0; i < fields.size(); i += 2) {
            final String fieldName = OIOUtils.getStringContent(fields.get(i));
            final String fieldValue = fields.get(i + 1);
            final String fieldValueAsString = OIOUtils.getStringContent(fieldValue);
            if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
                fieldTypes = loadFieldTypes(fieldTypes, fieldValueAsString);
            } else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
                if (iRecord == null || ORecordInternal.getRecordType(iRecord) != fieldValueAsString.charAt(0)) {
                    // CREATE THE RIGHT RECORD INSTANCE
                    iRecord = Orient.instance().getRecordFactoryManager().newInstance((byte) fieldValueAsString.charAt(0));
                }
            } else if (needReload && fieldName.equals(ODocumentHelper.ATTRIBUTE_RID) && iRecord instanceof ODocument) {
                if (fieldValue != null && fieldValue.length() > 0) {
                    ORecord localRecord = ODatabaseRecordThreadLocal.INSTANCE.get().load(new ORecordId(fieldValueAsString));
                    if (localRecord != null)
                        iRecord = localRecord;
                }
            } else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_CLASS) && iRecord instanceof ODocument) {
                className = "null".equals(fieldValueAsString) ? null : fieldValueAsString;
                ODocumentInternal.fillClassNameIfNeeded(((ODocument) iRecord), className);
            }
        }
        if (iRecord == null)
            iRecord = new ODocument();
        try {
            for (int i = 0; i < fields.size(); i += 2) {
                final String fieldName = OIOUtils.getStringContent(fields.get(i));
                final String fieldValue = fields.get(i + 1);
                final String fieldValueAsString = OIOUtils.getStringContent(fieldValue);
                // RECORD ATTRIBUTES
                if (fieldName.equals(ODocumentHelper.ATTRIBUTE_RID))
                    ORecordInternal.setIdentity(iRecord, new ORecordId(fieldValueAsString));
                else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_VERSION))
                    ORecordInternal.setVersion(iRecord, Integer.parseInt(fieldValue));
                else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_CLASS)) {
                    continue;
                } else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
                    continue;
                } else if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
                    continue;
                } else if (fieldName.equals("value") && !(iRecord instanceof ODocument)) {
                    // RECORD VALUE(S)
                    if ("null".equals(fieldValue))
                        iRecord.fromStream(OCommonConst.EMPTY_BYTE_ARRAY);
                    else if (iRecord instanceof OBlob) {
                        // BYTES
                        iRecord.fromStream(OBase64Utils.decode(fieldValueAsString));
                    } else if (iRecord instanceof ORecordStringable) {
                        ((ORecordStringable) iRecord).value(fieldValueAsString);
                    } else
                        throw new IllegalArgumentException("unsupported type of record");
                } else if (iRecord instanceof ODocument) {
                    final ODocument doc = ((ODocument) iRecord);
                    // DETERMINE THE TYPE FROM THE SCHEMA
                    OType type = determineType(doc, fieldName);
                    final Object v = getValue(doc, fieldName, fieldValue, fieldValueAsString, type, null, fieldTypes, noMap, iOptions);
                    if (v != null)
                        if (v instanceof Collection<?> && !((Collection<?>) v).isEmpty()) {
                            if (v instanceof ORecordLazyMultiValue)
                                ((ORecordLazyMultiValue) v).setAutoConvertToRecord(false);
                            // CHECK IF THE COLLECTION IS EMBEDDED
                            if (type == null) {
                                // TRY TO UNDERSTAND BY FIRST ITEM
                                Object first = ((Collection<?>) v).iterator().next();
                                if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid())
                                    type = v instanceof Set<?> ? OType.EMBEDDEDSET : OType.EMBEDDEDLIST;
                            }
                            if (type != null) {
                                // TREAT IT AS EMBEDDED
                                doc.field(fieldName, v, type);
                                continue;
                            }
                        } else if (v instanceof Map<?, ?> && !((Map<?, ?>) v).isEmpty()) {
                            // CHECK IF THE MAP IS EMBEDDED
                            Object first = ((Map<?, ?>) v).values().iterator().next();
                            if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid()) {
                                doc.field(fieldName, v, OType.EMBEDDEDMAP);
                                continue;
                            }
                        } else if (v instanceof ODocument && type != null && type.isLink()) {
                            String className1 = ((ODocument) v).getClassName();
                            if (className1 != null && className1.length() > 0)
                                ((ODocument) v).save();
                        }
                    if (type == null && fieldTypes != null && fieldTypes.containsKey(fieldName))
                        type = ORecordSerializerStringAbstract.getType(fieldValue, fieldTypes.get(fieldName));
                    if (v instanceof OTrackedSet<?>) {
                        if (OMultiValue.getFirstValue((Set<?>) v) instanceof OIdentifiable)
                            type = OType.LINKSET;
                    } else if (v instanceof OTrackedList<?>) {
                        if (OMultiValue.getFirstValue((List<?>) v) instanceof OIdentifiable)
                            type = OType.LINKLIST;
                    }
                    if (type != null)
                        doc.field(fieldName, v, type);
                    else
                        doc.field(fieldName, v);
                }
            }
            if (className != null) {
                // Trigger the default value
                ((ODocument) iRecord).setClassName(className);
            }
        } catch (Exception e) {
            if (iRecord.getIdentity().isValid())
                throw OException.wrapException(new OSerializationException("Error on unmarshalling JSON content for record " + iRecord.getIdentity()), e);
            else
                throw OException.wrapException(new OSerializationException("Error on unmarshalling JSON content for record: " + iSource), e);
        }
    }
    return iRecord;
}
Also used : OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) ORecordStringable(com.orientechnologies.orient.core.record.ORecordStringable) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) ParseException(java.text.ParseException) IOException(java.io.IOException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue) ORecord(com.orientechnologies.orient.core.record.ORecord) Collection(java.util.Collection) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 OType (com.orientechnologies.orient.core.metadata.schema.OType)5 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)4 ORecord (com.orientechnologies.orient.core.record.ORecord)3 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)2 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Set (java.util.Set)2 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)1 OException (com.orientechnologies.common.exception.OException)1 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)1 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)1