Search in sources :

Example 1 with OResettable

use of com.orientechnologies.common.util.OResettable in project orientdb by orientechnologies.

the class OLazyRecordMultiIterator method getCurrentIterator.

@SuppressWarnings("unchecked")
private Iterator<OIdentifiable> getCurrentIterator() {
    if (iteratorIndex > underlyingIterators.length)
        throw new NoSuchElementException();
    Object next = underlyingIterators[iteratorIndex];
    if (next == null) {
        // GET THE ITERATOR
        if (underlyingSources[iteratorIndex] instanceof OResettable) {
            // REUSE IT
            ((OResettable) underlyingSources[iteratorIndex]).reset();
            underlyingIterators[iteratorIndex] = underlyingSources[iteratorIndex];
        } else if (underlyingSources[iteratorIndex] instanceof Iterable<?>) {
            // CREATE A NEW ONE FROM THE COLLECTION
            underlyingIterators[iteratorIndex] = ((Iterable<?>) underlyingSources[iteratorIndex]).iterator();
        } else if (underlyingSources[iteratorIndex] instanceof Iterator<?>) {
            // COPY IT
            underlyingIterators[iteratorIndex] = underlyingSources[iteratorIndex];
        } else
            throw new IllegalStateException("Unsupported iteration source: " + underlyingSources[iteratorIndex]);
        next = underlyingIterators[iteratorIndex];
    }
    if (next instanceof Iterator<?>)
        return (Iterator<OIdentifiable>) next;
    return ((Collection<OIdentifiable>) next).iterator();
}
Also used : OResettable(com.orientechnologies.common.util.OResettable) Iterator(java.util.Iterator) OLazyIterator(com.orientechnologies.common.collection.OLazyIterator) Collection(java.util.Collection) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with OResettable

use of com.orientechnologies.common.util.OResettable in project orientdb by orientechnologies.

the class ORuntimeResult method applyRecord.

@SuppressWarnings("unchecked")
public static ODocument applyRecord(final ODocument iValue, final Map<String, Object> iProjections, final OCommandContext iContext, final OIdentifiable iRecord) {
    // APPLY PROJECTIONS
    ORecord record = (iRecord != null ? iRecord.getRecord() : null);
    //MANAGE SPECIFIC CASES FOR RECORD BYTES
    if (ORecordBytes.RECORD_TYPE == ORecordInternal.getRecordType(record)) {
        for (Entry<String, Object> projection : iProjections.entrySet()) {
            if ("@rid".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getIdentity());
            } else if ("@size".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getSize());
            } else if ("@version".equalsIgnoreCase("" + projection.getValue())) {
                iValue.field(projection.getKey(), record.getVersion());
            } else {
                Object val = projection.getValue();
                if (val instanceof Number || val instanceof String || val instanceof Boolean) {
                    iValue.field(projection.getKey(), val);
                } else {
                    iValue.field(projection.getKey(), (Object) null);
                }
            }
        }
        return iValue;
    }
    final ODocument inputDocument = (ODocument) record;
    if (iProjections.isEmpty())
        // SELECT * CASE
        inputDocument.copyTo(iValue);
    else {
        for (Entry<String, Object> projection : iProjections.entrySet()) {
            final String prjName = projection.getKey();
            final Object v = projection.getValue();
            if (v == null && prjName != null) {
                iValue.field(prjName, (Object) null);
                continue;
            }
            final Object projectionValue;
            if (v != null && v.equals("*")) {
                // COPY ALL
                inputDocument.copyTo(iValue);
                // CONTINUE WITH NEXT ITEM
                continue;
            } else if (v instanceof OSQLFilterItemVariable || v instanceof OSQLFilterItemField) {
                final OSQLFilterItemAbstract var = (OSQLFilterItemAbstract) v;
                final OPair<OSQLMethodRuntime, Object[]> last = var.getLastChainOperator();
                if (last != null && last.getKey().getMethod() instanceof OSQLMethodField && last.getValue() != null && last.getValue().length == 1 && last.getValue()[0].equals("*")) {
                    final Object value = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
                    if (inputDocument != null && value != null && inputDocument instanceof ODocument && value instanceof ODocument) {
                        // COPY FIELDS WITH PROJECTION NAME AS PREFIX
                        for (String fieldName : ((ODocument) value).fieldNames()) {
                            iValue.field(prjName + fieldName, ((ODocument) value).field(fieldName));
                        }
                    }
                    projectionValue = null;
                } else
                    // RETURN A VARIABLE FROM THE CONTEXT
                    projectionValue = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
            } else if (v instanceof OSQLFunctionRuntime) {
                final OSQLFunctionRuntime f = (OSQLFunctionRuntime) v;
                projectionValue = f.execute(inputDocument, inputDocument, iValue, iContext);
            } else {
                if (v == null) {
                    // SIMPLE NULL VALUE: SET IT IN DOCUMENT
                    iValue.field(prjName, v);
                    continue;
                }
                projectionValue = v;
            }
            if (projectionValue != null)
                if (projectionValue instanceof ORidBag)
                    iValue.field(prjName, new ORidBag((ORidBag) projectionValue));
                else if (projectionValue instanceof OIdentifiable && !(projectionValue instanceof ORID) && !(projectionValue instanceof ORecord))
                    iValue.field(prjName, ((OIdentifiable) projectionValue).getRecord());
                else if (projectionValue instanceof Iterator) {
                    boolean link = true;
                    // make temporary value typical case graph database elemenet's iterator edges
                    if (projectionValue instanceof OResettable)
                        ((OResettable) projectionValue).reset();
                    final List<Object> iteratorValues = new ArrayList<Object>();
                    final Iterator projectionValueIterator = (Iterator) projectionValue;
                    while (projectionValueIterator.hasNext()) {
                        Object value = projectionValueIterator.next();
                        if (value instanceof OIdentifiable) {
                            value = ((OIdentifiable) value).getRecord();
                            if (value != null && !((OIdentifiable) value).getIdentity().isPersistent())
                                link = false;
                        }
                        if (value != null)
                            iteratorValues.add(value);
                    }
                    iValue.field(prjName, iteratorValues, link ? OType.LINKLIST : OType.EMBEDDEDLIST);
                } else if (projectionValue instanceof ODocument && !((ODocument) projectionValue).getIdentity().isPersistent()) {
                    iValue.field(prjName, projectionValue, OType.EMBEDDED);
                } else if (projectionValue instanceof Set<?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKSET && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
                        type = OType.EMBEDDEDSET;
                    iValue.field(prjName, projectionValue, type);
                } else if (projectionValue instanceof Map<?, ?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKMAP && !entriesPersistent(((Map<?, OIdentifiable>) projectionValue).values()))
                        type = OType.EMBEDDEDMAP;
                    iValue.field(prjName, projectionValue, type);
                } else if (projectionValue instanceof List<?>) {
                    OType type = OType.getTypeByValue(projectionValue);
                    if (type == OType.LINKLIST && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
                        type = OType.EMBEDDEDLIST;
                    iValue.field(prjName, projectionValue, type);
                } else
                    iValue.field(prjName, projectionValue);
        }
    }
    return iValue;
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OSQLFilterItemAbstract(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemAbstract) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OSQLMethodField(com.orientechnologies.orient.core.sql.method.misc.OSQLMethodField) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) OResettable(com.orientechnologies.common.util.OResettable) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OType(com.orientechnologies.orient.core.metadata.schema.OType) OSQLFilterItemVariable(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable) OPair(com.orientechnologies.common.util.OPair) ORecord(com.orientechnologies.orient.core.record.ORecord)

Aggregations

OResettable (com.orientechnologies.common.util.OResettable)2 OLazyIterator (com.orientechnologies.common.collection.OLazyIterator)1 OPair (com.orientechnologies.common.util.OPair)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 ORID (com.orientechnologies.orient.core.id.ORID)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OSQLFilterItemAbstract (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemAbstract)1 OSQLFilterItemField (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField)1 OSQLFilterItemVariable (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable)1 OSQLFunctionRuntime (com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)1 OSQLMethodField (com.orientechnologies.orient.core.sql.method.misc.OSQLMethodField)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1