Search in sources :

Example 61 with PageCursor

use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.

the class CommonAbstractStore method rebuildIdGenerator.

/**
     * Should rebuild the id generator from scratch.
     * <p>
     * Note: This method may be called both while the store has the store file mapped in the
     * page cache, and while the store file is not mapped. Implementers must therefore
     * map their own temporary PagedFile for the store file, and do their file IO through that,
     * if they need to access the data in the store file.
     */
final void rebuildIdGenerator() {
    int blockSize = getRecordSize();
    if (blockSize <= 0) {
        throw new InvalidRecordException("Illegal blockSize: " + blockSize);
    }
    log.info("Rebuilding id generator for[" + getStorageFileName() + "] ...");
    closeIdGenerator();
    createIdGenerator(getIdFileName());
    openIdGenerator();
    long defraggedCount = 0;
    boolean fastRebuild = isOnlyFastIdGeneratorRebuildEnabled(configuration);
    try {
        long foundHighId = scanForHighId();
        setHighId(foundHighId);
        if (!fastRebuild) {
            try (PageCursor cursor = storeFile.io(0, PF_SHARED_WRITE_LOCK | PF_READ_AHEAD)) {
                defraggedCount = rebuildIdGeneratorSlow(cursor, getRecordsPerPage(), blockSize, foundHighId);
            }
        }
    } catch (IOException e) {
        throw new UnderlyingStorageException("Unable to rebuild id generator " + getStorageFileName(), e);
    }
    log.info("[" + getStorageFileName() + "] high id=" + getHighId() + " (defragged=" + defraggedCount + ")");
    log.info(getStorageFileName() + " rebuild id generator, highId=" + getHighId() + " defragged count=" + defraggedCount);
    if (!fastRebuild) {
        closeIdGenerator();
        openIdGenerator();
    }
}
Also used : IOException(java.io.IOException) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 62 with PageCursor

use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.

the class MetaDataStore method setRecord.

/**
     * Writes a record in a neostore file.
     * This method only works for neostore files of the current version.
     *
     * @param pageCache {@link PageCache} the {@code neostore} file lives in.
     * @param neoStore {@link File} pointing to the neostore.
     * @param position record {@link Position}.
     * @param value value to write in that record.
     * @return the previous value before writing.
     * @throws IOException if any I/O related error occurs.
     */
public static long setRecord(PageCache pageCache, File neoStore, Position position, long value) throws IOException {
    long previousValue = FIELD_NOT_INITIALIZED;
    int pageSize = getPageSize(pageCache);
    try (PagedFile pagedFile = pageCache.map(neoStore, pageSize)) {
        int offset = offset(position);
        try (PageCursor cursor = pagedFile.io(0, PagedFile.PF_SHARED_WRITE_LOCK)) {
            if (cursor.next()) {
                // We're overwriting a record, get the previous value
                cursor.setOffset(offset);
                byte inUse = cursor.getByte();
                long record = cursor.getLong();
                if (inUse == Record.IN_USE.byteValue()) {
                    previousValue = record;
                }
                // Write the value
                cursor.setOffset(offset);
                cursor.putByte(Record.IN_USE.byteValue());
                cursor.putLong(value);
                if (cursor.checkAndClearBoundsFlag()) {
                    MetaDataRecord neoStoreRecord = new MetaDataRecord();
                    neoStoreRecord.setId(position.id);
                    throw new UnderlyingStorageException(buildOutOfBoundsExceptionMessage(neoStoreRecord, 0, offset, RECORD_SIZE, pageSize, neoStore.getAbsolutePath()));
                }
            }
        }
    }
    return previousValue;
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 63 with PageCursor

use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.

the class MetaDataStore method setUpgradeTransaction.

public void setUpgradeTransaction(long id, long checksum, long timestamp) {
    long pageId = pageIdForRecord(Position.UPGRADE_TRANSACTION_ID.id);
    assert pageId == pageIdForRecord(Position.UPGRADE_TRANSACTION_CHECKSUM.id);
    synchronized (upgradeTransactionLock) {
        try (PageCursor cursor = storeFile.io(pageId, PF_SHARED_WRITE_LOCK)) {
            if (!cursor.next()) {
                throw new UnderlyingStorageException("Could not access MetaDataStore page " + pageId);
            }
            setRecord(cursor, Position.UPGRADE_TRANSACTION_ID, id);
            setRecord(cursor, Position.UPGRADE_TRANSACTION_CHECKSUM, checksum);
            setRecord(cursor, Position.UPGRADE_TRANSACTION_COMMIT_TIMESTAMP, timestamp);
            upgradeTxIdField = id;
            upgradeTxChecksumField = checksum;
            upgradeCommitTimestampField = timestamp;
            upgradeTransaction = new TransactionId(id, checksum, timestamp);
        } catch (IOException e) {
            throw new UnderlyingStorageException(e);
        }
    }
}
Also used : IOException(java.io.IOException) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 64 with PageCursor

use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.

the class ConfigurableStandalonePageCacheFactoryTest method mustAutomaticallyStartEvictionThread.

@Test(timeout = 10000)
public void mustAutomaticallyStartEvictionThread() throws IOException {
    try (FileSystemAbstraction fs = new DelegateFileSystemAbstraction(Jimfs.newFileSystem(jimConfig()))) {
        File file = new File("/a").getCanonicalFile();
        fs.create(file).close();
        try (PageCache cache = ConfigurableStandalonePageCacheFactory.createPageCache(fs);
            PagedFile pf = cache.map(file, 4096);
            PageCursor cursor = pf.io(0, PagedFile.PF_SHARED_WRITE_LOCK)) {
            // If the eviction thread has not been started, then this test will block forever.
            for (int i = 0; i < 10_000; i++) {
                assertTrue(cursor.next());
                cursor.putInt(42);
            }
        }
    }
}
Also used : DelegateFileSystemAbstraction(org.neo4j.io.fs.DelegateFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) PagedFile(org.neo4j.io.pagecache.PagedFile) DelegateFileSystemAbstraction(org.neo4j.io.fs.DelegateFileSystemAbstraction) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) PageCursor(org.neo4j.io.pagecache.PageCursor) Test(org.junit.Test)

Example 65 with PageCursor

use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.

the class CompositePageCursor method putByte.

@Override
public void putByte(byte value) {
    PageCursor cursor = cursor(Byte.BYTES);
    cursor.putByte(value);
    offset++;
}
Also used : PageCursor(org.neo4j.io.pagecache.PageCursor)

Aggregations

PageCursor (org.neo4j.io.pagecache.PageCursor)184 Test (org.junit.Test)124 StubPageCursor (org.neo4j.io.pagecache.StubPageCursor)106 PagedFile (org.neo4j.io.pagecache.PagedFile)19 IOException (java.io.IOException)12 File (java.io.File)8 CursorException (org.neo4j.io.pagecache.CursorException)6 PageCacheTest (org.neo4j.io.pagecache.PageCacheTest)6 CompositePageCursor (org.neo4j.io.pagecache.impl.CompositePageCursor)6 DelegatingPageCursor (org.neo4j.io.pagecache.impl.DelegatingPageCursor)6 ByteBuffer (java.nio.ByteBuffer)5 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)5 StoreChannel (org.neo4j.io.fs.StoreChannel)4 DelegatingPagedFile (org.neo4j.io.pagecache.DelegatingPagedFile)4 ConfigurablePageCursorTracerSupplier (org.neo4j.io.pagecache.tracing.ConfigurablePageCursorTracerSupplier)4 RecordingPageCacheTracer (org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer)4 Evict (org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer.Evict)4 RecordingPageCursorTracer (org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer)4 Fault (org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer.Fault)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)3