Search in sources :

Example 51 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class ORecordSerializerNetworkV0 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 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;
        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]);
            writeOType(bytes, (pos[i] + OIntegerSerializer.INT_SIZE), type);
        }
    }
}
Also used : ODocumentEntry(com.orientechnologies.orient.core.record.impl.ODocumentEntry) Entry(java.util.Map.Entry) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OType(com.orientechnologies.orient.core.metadata.schema.OType) ODocumentEntry(com.orientechnologies.orient.core.record.impl.ODocumentEntry)

Example 52 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class ORecordSerializerNetworkV0 method readLinkMap.

private Map<Object, OIdentifiable> readLinkMap(final BytesContainer bytes, final ODocument document) {
    int size = OVarIntSerializer.readAsInteger(bytes);
    Map<Object, OIdentifiable> result = new ORecordLazyMap(document);
    while ((size--) > 0) {
        OType keyType = readOType(bytes);
        Object key = deserializeValue(bytes, keyType, document);
        ORecordId value = readOptimizedLink(bytes);
        if (value.equals(NULL_RECORD_ID))
            result.put(key, null);
        else
            result.put(key, value);
    }
    return result;
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 53 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType 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 54 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OSQLFilterCondition method evaluate.

public Object evaluate(final OIdentifiable iCurrentRecord, final ODocument iCurrentResult, final OCommandContext iContext) {
    boolean binaryEvaluation = operator != null && operator.isSupportingBinaryEvaluate() && iCurrentRecord != null && iCurrentRecord.getIdentity().isPersistent();
    if (left instanceof OSQLQuery<?>)
        // EXECUTE SUB QUERIES ONLY ONCE
        left = ((OSQLQuery<?>) left).setContext(iContext).execute();
    Object l = evaluate(iCurrentRecord, iCurrentResult, left, iContext, binaryEvaluation);
    if (operator == null || operator.canShortCircuit(l))
        return l;
    if (right instanceof OSQLQuery<?>)
        // EXECUTE SUB QUERIES ONLY ONCE
        right = ((OSQLQuery<?>) right).setContext(iContext).execute();
    Object r = evaluate(iCurrentRecord, iCurrentResult, right, iContext, binaryEvaluation);
    if (binaryEvaluation && l instanceof OBinaryField) {
        if (r != null && !(r instanceof OBinaryField)) {
            final OType type = OType.getTypeByValue(r);
            if (ORecordSerializerBinary.INSTANCE.getCurrentSerializer().getComparator().isBinaryComparable(type)) {
                final BytesContainer bytes = new BytesContainer();
                ORecordSerializerBinary.INSTANCE.getCurrentSerializer().serializeValue(bytes, r, type, null);
                bytes.offset = 0;
                final OCollate collate = r instanceof OSQLFilterItemField ? ((OSQLFilterItemField) r).getCollate(iCurrentRecord) : null;
                r = new OBinaryField(null, type, bytes, collate);
                if (!(right instanceof OSQLFilterItem || right instanceof OSQLFilterCondition))
                    // FIXED VALUE, REPLACE IT
                    right = r;
            }
        } else if (r instanceof OBinaryField)
            // GET THE COPY OR MT REASONS
            r = ((OBinaryField) r).copy();
    }
    if (binaryEvaluation && r instanceof OBinaryField) {
        if (l != null && !(l instanceof OBinaryField)) {
            final OType type = OType.getTypeByValue(l);
            if (ORecordSerializerBinary.INSTANCE.getCurrentSerializer().getComparator().isBinaryComparable(type)) {
                final BytesContainer bytes = new BytesContainer();
                ORecordSerializerBinary.INSTANCE.getCurrentSerializer().serializeValue(bytes, l, type, null);
                bytes.offset = 0;
                final OCollate collate = l instanceof OSQLFilterItemField ? ((OSQLFilterItemField) l).getCollate(iCurrentRecord) : null;
                l = new OBinaryField(null, type, bytes, collate);
                if (!(left instanceof OSQLFilterItem || left instanceof OSQLFilterCondition))
                    // FIXED VALUE, REPLACE IT
                    left = l;
            }
        } else if (l instanceof OBinaryField)
            // GET THE COPY OR MT REASONS
            l = ((OBinaryField) l).copy();
    }
    if (binaryEvaluation)
        binaryEvaluation = l instanceof OBinaryField && r instanceof OBinaryField;
    if (!binaryEvaluation) {
        // no collate for regular expressions, otherwise quotes will result in no match
        final OCollate collate = operator instanceof OQueryOperatorMatches ? null : getCollate(iCurrentRecord);
        final Object[] convertedValues = checkForConversion(iCurrentRecord, l, r, collate);
        if (convertedValues != null) {
            l = convertedValues[0];
            r = convertedValues[1];
        }
    }
    Object result;
    try {
        result = operator.evaluateRecord(iCurrentRecord, iCurrentResult, this, l, r, iContext);
    } catch (OCommandExecutionException e) {
        throw e;
    } catch (Exception e) {
        if (OLogManager.instance().isDebugEnabled())
            OLogManager.instance().debug(this, "Error on evaluating expression (%s)", e, toString());
        result = Boolean.FALSE;
    }
    return result;
}
Also used : OBinaryField(com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField) OType(com.orientechnologies.orient.core.metadata.schema.OType) OException(com.orientechnologies.common.exception.OException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ParseException(java.text.ParseException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) BytesContainer(com.orientechnologies.orient.core.serialization.serializer.record.binary.BytesContainer) OQueryOperatorMatches(com.orientechnologies.orient.core.sql.operator.OQueryOperatorMatches) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OCollate(com.orientechnologies.orient.core.collate.OCollate) OSQLQuery(com.orientechnologies.orient.core.sql.query.OSQLQuery)

Example 55 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType 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

OType (com.orientechnologies.orient.core.metadata.schema.OType)65 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)21 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)15 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)14 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)12 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)11 Map (java.util.Map)10 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)5 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)5 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)5 OBinarySerializerFactory (com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory)5 Collection (java.util.Collection)5 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)4 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)4 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)4 ORID (com.orientechnologies.orient.core.id.ORID)4