Search in sources :

Example 56 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 57 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)

Example 58 with OType

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

the class OQueryOperatorContains method evaluateExpression.

@Override
@SuppressWarnings("unchecked")
protected boolean evaluateExpression(final OIdentifiable iRecord, final OSQLFilterCondition iCondition, final Object iLeft, final Object iRight, OCommandContext iContext) {
    final OSQLFilterCondition condition;
    if (iCondition.getLeft() instanceof OSQLFilterCondition)
        condition = (OSQLFilterCondition) iCondition.getLeft();
    else if (iCondition.getRight() instanceof OSQLFilterCondition)
        condition = (OSQLFilterCondition) iCondition.getRight();
    else
        condition = null;
    if (iLeft instanceof Iterable<?>) {
        final Iterable<Object> iterable = (Iterable<Object>) iLeft;
        if (condition != null) {
            // CHECK AGAINST A CONDITION
            for (final Object o : iterable) {
                final OIdentifiable id;
                if (o instanceof OIdentifiable)
                    id = (OIdentifiable) o;
                else if (o instanceof Map<?, ?>) {
                    final Iterator<Object> iter = ((Map<?, Object>) o).values().iterator();
                    final Object v = iter.hasNext() ? iter.next() : null;
                    if (v instanceof OIdentifiable)
                        id = (OIdentifiable) v;
                    else
                        // TRANSFORM THE ENTIRE MAP IN A DOCUMENT. PROBABLY HAS BEEN IMPORTED FROM JSON
                        id = new ODocument((Map) o);
                } else if (o instanceof Iterable<?>) {
                    final Iterator<OIdentifiable> iter = ((Iterable<OIdentifiable>) o).iterator();
                    id = iter.hasNext() ? iter.next() : null;
                } else
                    continue;
                if ((Boolean) condition.evaluate(id, null, iContext) == Boolean.TRUE)
                    return true;
            }
        } else {
            // CHECK AGAINST A SINGLE VALUE
            OType type = null;
            if (iCondition.getLeft() instanceof OSQLFilterItemField && ((OSQLFilterItemField) iCondition.getLeft()).isFieldChain() && ((OSQLFilterItemField) iCondition.getLeft()).getFieldChain().getItemCount() == 1) {
                String fieldName = ((OSQLFilterItemField) iCondition.getLeft()).getFieldChain().getItemName(0);
                if (fieldName != null) {
                    Object record = iRecord.getRecord();
                    if (record instanceof ODocument) {
                        OProperty property = ((ODocument) record).getSchemaClass().getProperty(fieldName);
                        if (property != null && property.getType().isMultiValue()) {
                            type = property.getLinkedType();
                        }
                    }
                }
            }
            for (final Object o : iterable) {
                if (OQueryOperatorEquals.equals(iRight, o, type))
                    return true;
            }
        }
    } else if (iRight instanceof Iterable<?>) {
        // CHECK AGAINST A CONDITION
        final Iterable<OIdentifiable> iterable = (Iterable<OIdentifiable>) iRight;
        if (condition != null) {
            for (final OIdentifiable o : iterable) {
                if ((Boolean) condition.evaluate(o, null, iContext) == Boolean.TRUE)
                    return true;
            }
        } else {
            // CHECK AGAINST A SINGLE VALUE
            for (final Object o : iterable) {
                if (OQueryOperatorEquals.equals(iLeft, o))
                    return true;
            }
        }
    }
    return false;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) Iterator(java.util.Iterator) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 59 with OType

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

the class OAbstractPaginatedStorage method addIndexEngine.

public int addIndexEngine(String engineName, final String algorithm, final String indexType, final OIndexDefinition indexDefinition, final OBinarySerializer valueSerializer, final boolean isAutomatic, final Boolean durableInNonTxMode, final int version, final Map<String, String> engineProperties, final Set<String> clustersToIndex, final ODocument metadata) {
    checkOpeness();
    stateLock.acquireWriteLock();
    try {
        checkOpeness();
        checkLowDiskSpaceFullCheckpointRequestsAndBackgroundDataFlushExceptions();
        final String originalName = engineName;
        engineName = engineName.toLowerCase(configuration.getLocaleInstance());
        if (indexEngineNameMap.containsKey(engineName)) {
            // OLD INDEX FILE ARE PRESENT: THIS IS THE CASE OF PARTIAL/BROKEN INDEX
            OLogManager.instance().warn(this, "Index with name '%s' already exists, removing it and re-create the index", engineName);
            final OIndexEngine engine = indexEngineNameMap.remove(engineName);
            if (engine != null) {
                indexEngines.remove(engine);
                configuration.deleteIndexEngine(engineName);
                engine.delete();
            }
        }
        makeStorageDirty();
        final OBinarySerializer keySerializer = determineKeySerializer(indexDefinition);
        final int keySize = determineKeySize(indexDefinition);
        final OType[] keyTypes = indexDefinition != null ? indexDefinition.getTypes() : null;
        final boolean nullValuesSupport = indexDefinition != null && !indexDefinition.isNullValuesIgnored();
        final byte serializerId;
        if (valueSerializer != null)
            serializerId = valueSerializer.getId();
        else
            serializerId = -1;
        final OIndexEngine engine = OIndexes.createIndexEngine(originalName, algorithm, indexType, durableInNonTxMode, this, version, engineProperties, metadata);
        engine.create(valueSerializer, isAutomatic, keyTypes, nullValuesSupport, keySerializer, keySize, clustersToIndex, engineProperties, metadata);
        indexEngineNameMap.put(engineName, engine);
        indexEngines.add(engine);
        final OStorageConfiguration.IndexEngineData engineData = new OStorageConfiguration.IndexEngineData(originalName, algorithm, indexType, durableInNonTxMode, version, serializerId, keySerializer.getId(), isAutomatic, keyTypes, nullValuesSupport, keySize, engineProperties);
        configuration.addIndexEngine(engineName, engineData);
        return indexEngines.size() - 1;
    } catch (IOException e) {
        throw OException.wrapException(new OStorageException("Cannot add index engine " + engineName + " in storage."), e);
    } finally {
        stateLock.releaseWriteLock();
    }
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OStorageConfiguration(com.orientechnologies.orient.core.config.OStorageConfiguration) OBinarySerializer(com.orientechnologies.common.serialization.types.OBinarySerializer)

Example 60 with OType

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

the class BinaryComparatorCompareTest method testCompareNumber.

protected void testCompareNumber(OType sourceType, Number value10AsSourceType) {
    OType[] numberTypes = new OType[] { OType.BYTE, OType.DOUBLE, OType.FLOAT, OType.SHORT, OType.INTEGER, OType.LONG, OType.DATETIME };
    for (OType t : numberTypes) {
        if (sourceType == OType.DATETIME && t == OType.BYTE)
            // SKIP TEST
            continue;
        testCompare(sourceType, t);
    }
    for (OType t : numberTypes) {
        testCompare(t, sourceType);
    }
    // STRING
    if (sourceType != OType.DATETIME) {
        Assert.assertEquals(comparator.compare(field(sourceType, value10AsSourceType), field(OType.STRING, value10AsSourceType.toString())), 0);
        Assert.assertTrue(comparator.compare(field(sourceType, value10AsSourceType), field(OType.STRING, "9")) < 0);
        Assert.assertTrue(comparator.compare(field(sourceType, value10AsSourceType), field(OType.STRING, "11")) < 0);
        Assert.assertTrue(comparator.compare(field(sourceType, value10AsSourceType.intValue() * 2), field(OType.STRING, "11")) > 0);
        Assert.assertEquals(comparator.compare(field(OType.STRING, value10AsSourceType.toString()), field(sourceType, value10AsSourceType)), 0);
        Assert.assertTrue(comparator.compare(field(OType.STRING, value10AsSourceType.toString()), field(sourceType, value10AsSourceType.intValue() - 1)) < 0);
        Assert.assertTrue(comparator.compare(field(OType.STRING, value10AsSourceType.toString()), field(sourceType, value10AsSourceType.intValue() + 1)) < 0);
        Assert.assertTrue(comparator.compare(field(OType.STRING, "" + value10AsSourceType.intValue() * 2), field(sourceType, value10AsSourceType.intValue())) > 0);
    }
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType)

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