Search in sources :

Example 16 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class OCommandExecutorSQLSelect method unwind.

private Collection<OIdentifiable> unwind(final OIdentifiable iRecord, final List<String> unwindFields, final OCommandContext iContext) {
    final List<OIdentifiable> result = new ArrayList<OIdentifiable>();
    ODocument doc;
    if (iRecord instanceof ODocument) {
        doc = (ODocument) iRecord;
    } else {
        doc = iRecord.getRecord();
    }
    if (unwindFields.size() == 0) {
        ORecordInternal.setIdentity(doc, new ORecordId(-2, getTemporaryRIDCounter(iContext)));
        result.add(doc);
    } else {
        String firstField = unwindFields.get(0);
        final List<String> nextFields = unwindFields.subList(1, unwindFields.size());
        Object fieldValue = doc.field(firstField);
        if (fieldValue == null || !(fieldValue instanceof Iterable) || fieldValue instanceof ODocument) {
            result.addAll(unwind(doc, nextFields, iContext));
        } else {
            Iterator iterator = ((Iterable) fieldValue).iterator();
            if (!iterator.hasNext()) {
                ODocument unwindedDoc = new ODocument();
                doc.copyTo(unwindedDoc);
                unwindedDoc.field(firstField, (Object) null);
                result.addAll(unwind(unwindedDoc, nextFields, iContext));
            } else {
                do {
                    Object o = iterator.next();
                    ODocument unwindedDoc = new ODocument();
                    doc.copyTo(unwindedDoc);
                    unwindedDoc.field(firstField, o);
                    result.addAll(unwind(unwindedDoc, nextFields, iContext));
                } while (iterator.hasNext());
            }
        }
    }
    return result;
}
Also used : OIdentifiableIterator(com.orientechnologies.orient.core.iterator.OIdentifiableIterator) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OSortedMultiIterator(com.orientechnologies.common.collection.OSortedMultiIterator) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 17 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class ORecordSerializerStringAbstract method getTypeValue.

/**
   * Parses a string returning the value with the closer type. Numbers by default are INTEGER if haven't decimal separator,
   * otherwise FLOAT. To treat all the number types numbers are postponed with a character that tells the type: b=byte, s=short,
   * l=long, f=float, d=double, t=date. If starts with # it's a RecordID. Most of the code is equals to getType() but has been
   * copied to speed-up it.
   *
   * @param iValue
   *          Value to parse
   * @return The closest type recognized
   */
public static Object getTypeValue(final String iValue) {
    if (iValue == null || iValue.equalsIgnoreCase("NULL"))
        return null;
    if (iValue.length() == 0)
        return "";
    if (iValue.length() > 1)
        if (iValue.charAt(0) == '"' && iValue.charAt(iValue.length() - 1) == '"')
            // STRING
            return OStringSerializerHelper.decode(iValue.substring(1, iValue.length() - 1));
        else if (iValue.charAt(0) == OStringSerializerHelper.BINARY_BEGINEND && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.BINARY_BEGINEND)
            // STRING
            return OStringSerializerHelper.getBinaryContent(iValue);
        else if (iValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.LIST_END) {
            // LIST
            final ArrayList<String> coll = new ArrayList<String>();
            OStringSerializerHelper.getCollection(iValue, 0, coll, OStringSerializerHelper.LIST_BEGIN, OStringSerializerHelper.LIST_END, OStringSerializerHelper.COLLECTION_SEPARATOR);
            return coll;
        } else if (iValue.charAt(0) == OStringSerializerHelper.SET_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.SET_END) {
            // SET
            final Set<String> coll = new HashSet<String>();
            OStringSerializerHelper.getCollection(iValue, 0, coll, OStringSerializerHelper.SET_BEGIN, OStringSerializerHelper.SET_END, OStringSerializerHelper.COLLECTION_SEPARATOR);
            return coll;
        } else if (iValue.charAt(0) == OStringSerializerHelper.MAP_BEGIN && iValue.charAt(iValue.length() - 1) == OStringSerializerHelper.MAP_END) {
            // MAP
            return OStringSerializerHelper.getMap(iValue);
        }
    if (iValue.charAt(0) == ORID.PREFIX)
        // RID
        return new ORecordId(iValue);
    boolean integer = true;
    char c;
    boolean stringStarBySign = false;
    for (int index = 0; index < iValue.length(); ++index) {
        c = iValue.charAt(index);
        if (c < '0' || c > '9') {
            if ((index == 0 && (c == '+' || c == '-'))) {
                stringStarBySign = true;
                continue;
            } else if (c == DECIMAL_SEPARATOR)
                integer = false;
            else {
                if (index > 0) {
                    if (!integer && c == 'E') {
                        // CHECK FOR SCIENTIFIC NOTATION
                        if (index < iValue.length())
                            index++;
                        if (iValue.charAt(index) == '-')
                            continue;
                    }
                    final String v = iValue.substring(0, index);
                    if (c == 'f')
                        return new Float(v);
                    else if (c == 'c')
                        return new BigDecimal(v);
                    else if (c == 'l')
                        return new Long(v);
                    else if (c == 'd')
                        return new Double(v);
                    else if (c == 'b')
                        return new Byte(v);
                    else if (c == 'a' || c == 't')
                        return new Date(Long.parseLong(v));
                    else if (c == 's')
                        return new Short(v);
                }
                return iValue;
            }
        } else if (stringStarBySign) {
            stringStarBySign = false;
        }
    }
    if (stringStarBySign)
        return iValue;
    if (integer) {
        try {
            return new Integer(iValue);
        } catch (NumberFormatException e) {
            return new Long(iValue);
        }
    } else if ("NaN".equals(iValue) || "Infinity".equals(iValue))
        // NaN and Infinity CANNOT BE MANAGED BY BIG-DECIMAL TYPE
        return new Double(iValue);
    else
        return new BigDecimal(iValue);
}
Also used : ORecordId(com.orientechnologies.orient.core.id.ORecordId) BigDecimal(java.math.BigDecimal)

Example 18 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class OStreamSerializerAnyRecord method fromStream.

/**
   * Re-Create any object if the class has a public constructor that accepts a String as unique parameter.
   */
public Object fromStream(byte[] iStream) throws IOException {
    if (iStream == null || iStream.length == 0)
        // NULL VALUE
        return null;
    final String stream = new String(iStream, "UTF-8");
    Class<?> cls = null;
    try {
        final StringBuilder content = new StringBuilder(1024);
        cls = OStreamSerializerHelper.readRecordType(stream, content);
        // TRY WITH THE DATABASE CONSTRUCTOR
        for (Constructor<?> c : cls.getDeclaredConstructors()) {
            Class<?>[] params = c.getParameterTypes();
            if (params.length == 2 && params[1].equals(ORID.class)) {
                ORecord rec = (ORecord) c.newInstance(new ORecordId(content.toString()));
                // rec.load();
                return rec;
            }
        }
    } catch (Exception e) {
        throw OException.wrapException(new OSerializationException("Error on unmarshalling content. Class " + (cls != null ? cls.getName() : "?")), e);
    }
    throw new OSerializationException("Cannot unmarshall the record since the serialized object of class " + (cls != null ? cls.getSimpleName() : "?") + " has no constructor with suitable parameters: (ORID)");
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecord(com.orientechnologies.orient.core.record.ORecord) ORID(com.orientechnologies.orient.core.id.ORID) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException)

Example 19 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class OTransactionOptimistic method invokeCallbacks.

private void invokeCallbacks() {
    if (recordCreatedCallback != null || recordUpdatedCallback != null) {
        for (ORecordOperation operation : allEntries.values()) {
            final ORecord record = operation.getRecord();
            final ORID identity = record.getIdentity();
            if (operation.type == ORecordOperation.CREATED && recordCreatedCallback != null)
                recordCreatedCallback.call(new ORecordId(identity), identity.getClusterPosition());
            else if (operation.type == ORecordOperation.UPDATED && recordUpdatedCallback != null)
                recordUpdatedCallback.call(new ORecordId(identity), record.getVersion());
        }
    }
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORecord(com.orientechnologies.orient.core.record.ORecord) ORID(com.orientechnologies.orient.core.id.ORID) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 20 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class OTransactionRealAbstract method updateIdentityAfterCommit.

public void updateIdentityAfterCommit(final ORID oldRid, final ORID newRid) {
    if (oldRid.equals(newRid))
        // NO CHANGE, IGNORE IT
        return;
    // XXX: Identity update may mutate the index keys, so we have to identify and reinsert potentially affected index keys to keep
    // the OTransactionIndexChanges.changesPerKey in a consistent state.
    final List<KeyChangesUpdateRecord> keyRecordsToReinsert = new ArrayList<KeyChangesUpdateRecord>();
    final OIndexManager indexManager = getDatabase().getMetadata().getIndexManager();
    for (Entry<String, OTransactionIndexChanges> entry : indexEntries.entrySet()) {
        final OIndex<?> index = indexManager.getIndex(entry.getKey());
        if (index == null)
            throw new OTransactionException("Cannot find index '" + entry.getValue() + "' while committing transaction");
        final Dependency[] fieldRidDependencies = getIndexFieldRidDependencies(index);
        if (!isIndexMayDependOnRids(fieldRidDependencies))
            continue;
        final OTransactionIndexChanges indexChanges = entry.getValue();
        for (final Iterator<OTransactionIndexChangesPerKey> iterator = indexChanges.changesPerKey.values().iterator(); iterator.hasNext(); ) {
            final OTransactionIndexChangesPerKey keyChanges = iterator.next();
            if (isIndexKeyMayDependOnRid(keyChanges.key, oldRid, fieldRidDependencies)) {
                keyRecordsToReinsert.add(new KeyChangesUpdateRecord(keyChanges, indexChanges));
                iterator.remove();
            }
        }
    }
    // Update the identity.
    final ORecordOperation rec = getRecordEntry(oldRid);
    if (rec != null) {
        updatedRids.put(newRid.copy(), oldRid.copy());
        if (!rec.getRecord().getIdentity().equals(newRid)) {
            ORecordInternal.onBeforeIdentityChanged(rec.getRecord());
            final ORecordId recordId = (ORecordId) rec.getRecord().getIdentity();
            if (recordId == null) {
                ORecordInternal.setIdentity(rec.getRecord(), new ORecordId(newRid));
            } else {
                recordId.setClusterPosition(newRid.getClusterPosition());
                recordId.setClusterId(newRid.getClusterId());
            }
            ORecordInternal.onAfterIdentityChanged(rec.getRecord());
        }
    }
    for (KeyChangesUpdateRecord record : keyRecordsToReinsert) record.indexChanges.changesPerKey.put(record.keyChanges.key, record.keyChanges);
    // Update the indexes.
    final List<OTransactionRecordIndexOperation> transactionIndexOperations = recordIndexOperations.get(translateRid(oldRid));
    if (transactionIndexOperations != null) {
        for (final OTransactionRecordIndexOperation indexOperation : transactionIndexOperations) {
            OTransactionIndexChanges indexEntryChanges = indexEntries.get(indexOperation.index);
            if (indexEntryChanges == null)
                continue;
            final OTransactionIndexChangesPerKey keyChanges = indexEntryChanges.changesPerKey.get(indexOperation.key);
            if (keyChanges != null)
                updateChangesIdentity(oldRid, newRid, keyChanges);
        }
    }
}
Also used : ORecordId(com.orientechnologies.orient.core.id.ORecordId) OIndexManager(com.orientechnologies.orient.core.index.OIndexManager) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException)

Aggregations

ORecordId (com.orientechnologies.orient.core.id.ORecordId)431 Test (org.testng.annotations.Test)153 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)139 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)120 ORID (com.orientechnologies.orient.core.id.ORID)71 HashSet (java.util.HashSet)63 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)42 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)37 ORecord (com.orientechnologies.orient.core.record.ORecord)37 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)34 OIndexTxAwareMultiValue (com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue)30 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)30 HashMap (java.util.HashMap)29 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)28 IOException (java.io.IOException)25 Child (com.orientechnologies.orient.test.domain.business.Child)24 OException (com.orientechnologies.common.exception.OException)23 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)23 Map (java.util.Map)22 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)21