Search in sources :

Example 91 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 92 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 93 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 94 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)

Example 95 with ORecordId

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

the class OPaginatedCluster method deleteRecord.

public boolean deleteRecord(long clusterPosition) throws IOException {
    startOperation();
    OSessionStoragePerformanceStatistic statistic = performanceStatisticManager.getSessionPerformanceStatistic();
    if (statistic != null)
        statistic.startRecordDeletionTimer();
    try {
        OAtomicOperation atomicOperation = startAtomicOperation(true);
        acquireExclusiveLock();
        try {
            OClusterPositionMapBucket.PositionEntry positionEntry = clusterPositionMap.get(clusterPosition, 1);
            if (positionEntry == null) {
                endAtomicOperation(false, null);
                return false;
            }
            long pageIndex = positionEntry.getPageIndex();
            int recordPosition = positionEntry.getRecordPosition();
            if (getFilledUpTo(atomicOperation, fileId) <= pageIndex) {
                endAtomicOperation(false, null);
                return false;
            }
            long nextPagePointer;
            int removedContentSize = 0;
            do {
                boolean cacheEntryReleased = false;
                OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false);
                cacheEntry.acquireExclusiveLock();
                int initialFreePageIndex;
                try {
                    OClusterPage localPage = new OClusterPage(cacheEntry, false, getChanges(atomicOperation, cacheEntry));
                    initialFreePageIndex = calculateFreePageIndex(localPage);
                    if (localPage.isDeleted(recordPosition)) {
                        if (removedContentSize == 0) {
                            cacheEntryReleased = true;
                            try {
                                cacheEntry.releaseExclusiveLock();
                                releasePage(atomicOperation, cacheEntry);
                            } finally {
                                endAtomicOperation(false, null);
                            }
                            return false;
                        } else
                            throw new OPaginatedClusterException("Content of record " + new ORecordId(id, clusterPosition) + " was broken", this);
                    } else if (removedContentSize == 0) {
                        cacheEntry.releaseExclusiveLock();
                        releasePage(atomicOperation, cacheEntry);
                        cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false);
                        cacheEntry.acquireExclusiveLock();
                        localPage = new OClusterPage(cacheEntry, false, getChanges(atomicOperation, cacheEntry));
                    }
                    byte[] content = localPage.getRecordBinaryValue(recordPosition, 0, localPage.getRecordSize(recordPosition));
                    int initialFreeSpace = localPage.getFreeSpace();
                    localPage.deleteRecord(recordPosition);
                    removedContentSize += localPage.getFreeSpace() - initialFreeSpace;
                    nextPagePointer = OLongSerializer.INSTANCE.deserializeNative(content, content.length - OLongSerializer.LONG_SIZE);
                } finally {
                    if (!cacheEntryReleased) {
                        cacheEntry.releaseExclusiveLock();
                        releasePage(atomicOperation, cacheEntry);
                    }
                }
                updateFreePagesIndex(fileId, pinnedStateEntryIndex, initialFreePageIndex, pageIndex, atomicOperation);
                pageIndex = getPageIndex(nextPagePointer);
                recordPosition = getRecordPosition(nextPagePointer);
            } while (nextPagePointer >= 0);
            updateClusterState(fileId, pinnedStateEntryIndex, -1, -removedContentSize, atomicOperation);
            clusterPositionMap.remove(clusterPosition);
            addAtomicOperationMetadata(new ORecordId(id, clusterPosition), atomicOperation);
            endAtomicOperation(false, null);
            return true;
        } catch (IOException e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OPaginatedClusterException("Error during record deletion", this), e);
        } catch (RuntimeException e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OPaginatedClusterException("Error during record deletion", this), e);
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        if (statistic != null)
            statistic.stopRecordDeletionTimer();
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) IOException(java.io.IOException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)

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