Search in sources :

Example 31 with OCacheEntry

use of com.orientechnologies.orient.core.storage.cache.OCacheEntry in project orientdb by orientechnologies.

the class OLruPageCache method loadPage.

@Override
public OCacheEntry loadPage(long fileId, long pageIndex, boolean checkPinnedPages, OWriteCache writeCache, int pageCount) throws IOException {
    final Key key = new Key(fileId, pageIndex);
    final Record record = pages.get(key);
    if (record != null) {
        ++record.usages;
        return record.cacheEntry;
    }
    final OCacheEntry cacheEntry = readCache.load(fileId, pageIndex, checkPinnedPages, writeCache, pageCount);
    if (cacheEntry == null)
        return null;
    if (pages.size() - maximumSize < evictionBatchSize)
        pages.put(key, new Record(cacheEntry));
    else {
        for (final Iterator<Record> i = pages.values().iterator(); i.hasNext(); ) {
            final Record r = i.next();
            if (r.usages <= 0) {
                for (long j = r.usages; j <= 0; ++j) readCache.release(r.cacheEntry, writeCache);
                i.remove();
                if (pages.size() < maximumSize) {
                    pages.put(key, new Record(cacheEntry));
                    break;
                }
            }
        }
    }
    return cacheEntry;
}
Also used : OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Example 32 with OCacheEntry

use of com.orientechnologies.orient.core.storage.cache.OCacheEntry in project orientdb by orientechnologies.

the class OTinyPageCache method loadPage.

@Override
public OCacheEntry loadPage(long fileId, long pageIndex, boolean checkPinnedPages, OWriteCache writeCache, int pageCount) throws IOException {
    long victimHits = Long.MAX_VALUE;
    int victimIndex = -1;
    for (int i = 0; i < pages.length; ++i) {
        final OCacheEntry entry = pages[i];
        if (entry == null) {
            // free slot found
            victimIndex = i;
            // mark as free
            victimHits = 0;
            continue;
        }
        final int countersBase = i * 2;
        assert counters[countersBase + 1] >= 1;
        if (pageIndex == entry.getPageIndex() && fileId == entry.getFileId()) {
            // found in cache
            ++counters[countersBase];
            ++counters[countersBase + 1];
            return entry;
        }
        if (victimHits != 0 && /* still no free slot found */
        counters[countersBase] <= 0) /* no usages */
        {
            final long hits = counters[countersBase + 1];
            if (hits < victimHits) {
                victimIndex = i;
                victimHits = hits;
            }
        }
    }
    final OCacheEntry entry = readCache.load(fileId, pageIndex, checkPinnedPages, writeCache, pageCount);
    if (entry == null)
        return null;
    if (victimIndex != -1) {
        // slot found
        final int countersBase = victimIndex * 2;
        if (victimHits != 0) {
            // slot is not free
            final OCacheEntry victim = pages[victimIndex];
            for (long i = counters[countersBase]; i <= 0; ++i) readCache.release(victim, writeCache);
        }
        pages[victimIndex] = entry;
        counters[countersBase] = 1;
        counters[countersBase + 1] = 1;
    }
    return entry;
}
Also used : OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Example 33 with OCacheEntry

use of com.orientechnologies.orient.core.storage.cache.OCacheEntry in project orientdb by orientechnologies.

the class OClusterPositionMap method resurrect.

public void resurrect(final long clusterPosition, final OClusterPositionMapBucket.PositionEntry entry) throws IOException {
    startOperation();
    try {
        OAtomicOperation atomicOperation = startAtomicOperation(true);
        acquireExclusiveLock();
        try {
            final long pageIndex = clusterPosition / OClusterPositionMapBucket.MAX_ENTRIES;
            final int index = (int) (clusterPosition % OClusterPositionMapBucket.MAX_ENTRIES);
            if (pageIndex >= getFilledUpTo(atomicOperation, fileId))
                throw new OClusterPositionMapException("Passed in cluster position " + clusterPosition + " is outside of range of cluster-position map", this);
            final OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false, 1);
            cacheEntry.acquireExclusiveLock();
            try {
                final OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChanges(atomicOperation, cacheEntry));
                bucket.resurrect(index, entry);
            } finally {
                cacheEntry.releaseExclusiveLock();
                releasePage(atomicOperation, cacheEntry);
            }
            endAtomicOperation(false, null);
        } catch (IOException e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OClusterPositionMapException("Error of resurrecting mapping between logical and physical record position", this), e);
        } catch (RuntimeException e) {
            endAtomicOperation(true, e);
            throw OException.wrapException(new OClusterPositionMapException("Error of resurrecting mapping between logical and physical record position", this), e);
        } finally {
            releaseExclusiveLock();
        }
    } finally {
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OClusterPositionMapException(com.orientechnologies.orient.core.exception.OClusterPositionMapException) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) IOException(java.io.IOException)

Example 34 with OCacheEntry

use of com.orientechnologies.orient.core.storage.cache.OCacheEntry in project orientdb by orientechnologies.

the class OClusterPositionMap method getStatus.

public byte getStatus(final long clusterPosition) throws IOException {
    startOperation();
    try {
        atomicOperationsManager.acquireReadLock(this);
        try {
            acquireSharedLock();
            try {
                final long pageIndex = clusterPosition / OClusterPositionMapBucket.MAX_ENTRIES;
                final int index = (int) (clusterPosition % OClusterPositionMapBucket.MAX_ENTRIES);
                final OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                if (pageIndex >= getFilledUpTo(atomicOperation, fileId))
                    return OClusterPositionMapBucket.NOT_EXISTENT;
                final OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false, 1);
                cacheEntry.acquireSharedLock();
                try {
                    final OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChanges(atomicOperation, cacheEntry));
                    return bucket.getStatus(index);
                } finally {
                    cacheEntry.releaseSharedLock();
                    releasePage(atomicOperation, cacheEntry);
                }
            } finally {
                releaseSharedLock();
            }
        } finally {
            atomicOperationsManager.releaseReadLock(this);
        }
    } finally {
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Example 35 with OCacheEntry

use of com.orientechnologies.orient.core.storage.cache.OCacheEntry in project orientdb by orientechnologies.

the class OClusterPositionMap method ceilingPositions.

public long[] ceilingPositions(long clusterPosition) throws IOException {
    startOperation();
    try {
        atomicOperationsManager.acquireReadLock(this);
        try {
            acquireSharedLock();
            try {
                if (clusterPosition < 0)
                    clusterPosition = 0;
                long pageIndex = clusterPosition / OClusterPositionMapBucket.MAX_ENTRIES;
                int index = (int) (clusterPosition % OClusterPositionMapBucket.MAX_ENTRIES);
                OAtomicOperation atomicOperation = atomicOperationsManager.getCurrentOperation();
                final long filledUpTo = getFilledUpTo(atomicOperation, fileId);
                if (pageIndex >= filledUpTo)
                    return OCommonConst.EMPTY_LONG_ARRAY;
                long[] result = null;
                do {
                    OCacheEntry cacheEntry = loadPage(atomicOperation, fileId, pageIndex, false, 1);
                    cacheEntry.acquireSharedLock();
                    OClusterPositionMapBucket bucket = new OClusterPositionMapBucket(cacheEntry, getChanges(atomicOperation, cacheEntry));
                    int resultSize = bucket.getSize() - index;
                    if (resultSize <= 0) {
                        cacheEntry.releaseSharedLock();
                        releasePage(atomicOperation, cacheEntry);
                        pageIndex++;
                        index = 0;
                    } else {
                        int entriesCount = 0;
                        long startIndex = cacheEntry.getPageIndex() * OClusterPositionMapBucket.MAX_ENTRIES + index;
                        result = new long[resultSize];
                        for (int i = 0; i < resultSize; i++) {
                            if (bucket.exists(i + index)) {
                                result[entriesCount] = startIndex + i;
                                entriesCount++;
                            }
                        }
                        if (entriesCount == 0) {
                            result = null;
                            pageIndex++;
                            index = 0;
                        } else
                            result = Arrays.copyOf(result, entriesCount);
                        cacheEntry.releaseSharedLock();
                        releasePage(atomicOperation, cacheEntry);
                    }
                } while (result == null && pageIndex < filledUpTo);
                if (result == null)
                    result = OCommonConst.EMPTY_LONG_ARRAY;
                return result;
            } finally {
                releaseSharedLock();
            }
        } finally {
            atomicOperationsManager.releaseReadLock(this);
        }
    } finally {
        completeOperation();
    }
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Aggregations

OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)209 ByteBuffer (java.nio.ByteBuffer)77 OAtomicOperation (com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation)76 OLogSequenceNumber (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber)74 OByteBufferPool (com.orientechnologies.common.directmemory.OByteBufferPool)69 OCachePointer (com.orientechnologies.orient.core.storage.cache.OCachePointer)65 IOException (java.io.IOException)61 OIndexException (com.orientechnologies.orient.core.index.OIndexException)23 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)20 OSessionStoragePerformanceStatistic (com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)20 OWALChangesTree (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OWALChangesTree)19 OException (com.orientechnologies.common.exception.OException)17 Test (org.testng.annotations.Test)17 ORecordId (com.orientechnologies.orient.core.id.ORecordId)14 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)13 OLocalHashTableException (com.orientechnologies.orient.core.exception.OLocalHashTableException)12 OSBTreeBonsaiLocalException (com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException)11 Lock (java.util.concurrent.locks.Lock)11 Random (java.util.Random)9 HashMap (java.util.HashMap)8