use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.
the class FreeListIdProvider method acquireNewIdFromFreelistOrEnd.
private long acquireNewIdFromFreelistOrEnd(long stableGeneration, long unstableGeneration, boolean allowTakeLastFromPage) throws IOException {
if ((readPageId != writePageId || readPos < writePos) && (allowTakeLastFromPage || readPos < freelistNode.maxEntries() - 1)) {
// or the read pos is < write pos so check if we can grab the next id (generation could still mismatch).
try (PageCursor cursor = pagedFile.io(readPageId, PagedFile.PF_SHARED_READ_LOCK)) {
if (!cursor.next()) {
throw new IOException("Couldn't go to free-list read page " + readPageId);
}
long resultPageId;
do {
resultPageId = freelistNode.read(cursor, stableGeneration, readPos);
} while (cursor.shouldRetry());
if (resultPageId != FreelistNode.NO_PAGE_ID) {
// FreelistNode compares generation and so this means that we have an available
// id in the free list which we can acquire from a stable generation. Increment readPos
readPos++;
if (readPos >= freelistNode.maxEntries()) {
// The current reader page is exhausted, go to the next free-list page.
readPos = 0;
do {
readPageId = FreelistNode.next(cursor);
} while (cursor.shouldRetry());
// Put the exhausted free-list page id itself on the free-list
long exhaustedFreelistPageId = cursor.getCurrentPageId();
releaseId(stableGeneration, unstableGeneration, exhaustedFreelistPageId);
monitor.releasedFreelistPageId(exhaustedFreelistPageId);
}
return resultPageId;
}
}
}
// Fall-back to acquiring at the end of the file
return nextLastId();
}
use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.
the class FreeListIdProvider method acquireNewId.
private long acquireNewId(long stableGeneration, long unstableGeneration, boolean allowTakeLastFromPage) throws IOException {
// Acquire id from free-list or end of store file
long acquiredId = acquireNewIdFromFreelistOrEnd(stableGeneration, unstableGeneration, allowTakeLastFromPage);
// Zap the page, i.e. set all bytes to zero
try (PageCursor cursor = pagedFile.io(acquiredId, PagedFile.PF_SHARED_WRITE_LOCK)) {
PageCursorUtil.goTo(cursor, "newly allocated free-list page", acquiredId);
cursor.zapPage();
// don't initialize node here since this acquisition can be used both for tree nodes
// as well as free-list nodes.
}
return acquiredId;
}
use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.
the class GBPTree method writerHeader.
private static void writerHeader(PagedFile pagedFile, Header.Writer headerWriter, TreeState otherState, PageCursor cursor) throws IOException {
// Write/carry over header
int headerOffset = cursor.getOffset();
// will contain length of written header data (below)
int headerDataOffset = headerOffset + Integer.BYTES;
if (otherState.isValid()) {
PageCursor previousCursor = pagedFile.io(otherState.pageId(), PagedFile.PF_SHARED_READ_LOCK);
PageCursorUtil.goTo(previousCursor, "previous state page", otherState.pageId());
do {
// Place the previous state cursor after state data
TreeState.read(previousCursor);
// Read length of previous header
int previousLength = previousCursor.getInt();
// Reserve space to store length
cursor.setOffset(headerDataOffset);
// Write
headerWriter.write(previousCursor, previousLength, cursor);
} while (previousCursor.shouldRetry());
checkOutOfBounds(previousCursor);
checkOutOfBounds(cursor);
int length = cursor.getOffset() - headerDataOffset;
cursor.putInt(headerOffset, length);
}
}
use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.
the class GBPTree method readMeta.
private void readMeta(File indexFile, Layout<KEY, VALUE> layout, PagedFile pagedFile) throws IOException {
// Read meta
int formatVersion;
long layoutIdentifier;
int majorVersion;
int minorVersion;
try (PageCursor metaCursor = openMetaPageCursor(pagedFile, PagedFile.PF_SHARED_READ_LOCK)) {
do {
formatVersion = metaCursor.getInt();
pageSize = metaCursor.getInt();
layoutIdentifier = metaCursor.getLong();
majorVersion = metaCursor.getInt();
minorVersion = metaCursor.getInt();
layout.readMetaData(metaCursor);
} while (metaCursor.shouldRetry());
checkOutOfBounds(metaCursor);
metaCursor.checkAndClearCursorException();
} catch (CursorException e) {
throw new MetadataMismatchException(format("Tried to open %s, but caught an error while reading meta data. " + "File is expected to be corrupt, try to rebuild.", indexFile), e);
}
if (formatVersion != FORMAT_VERSION) {
throw new MetadataMismatchException("Tried to open %s with a different format version than " + "what it was created with. Created with:%d, opened with %d", indexFile, formatVersion, FORMAT_VERSION);
}
if (layoutIdentifier != layout.identifier()) {
throw new MetadataMismatchException("Tried to open " + indexFile + " using different layout identifier " + "than what it was created with. Created with:" + layoutIdentifier + ", opened with " + layout.identifier());
}
if (majorVersion != layout.majorVersion() || minorVersion != layout.minorVersion()) {
throw new MetadataMismatchException("Tried to open " + indexFile + " using different layout version " + "than what it was created with. Created with:" + majorVersion + "." + minorVersion + ", opened with " + layout.majorVersion() + "." + layout.minorVersion());
}
}
use of org.neo4j.io.pagecache.PageCursor in project neo4j by neo4j.
the class MuninnPageCacheTest method mustFlushDirtyPagesOnEvictingFirstPage.
@Test
public void mustFlushDirtyPagesOnEvictingFirstPage() throws Exception {
writeInitialDataTo(file("a"));
RecordingPageCacheTracer tracer = new RecordingPageCacheTracer();
RecordingPageCursorTracer cursorTracer = new RecordingPageCursorTracer();
ConfigurablePageCursorTracerSupplier cursorTracerSupplier = new ConfigurablePageCursorTracerSupplier(cursorTracer);
MuninnPageCache pageCache = createPageCache(fs, 2, 8, blockCacheFlush(tracer), cursorTracerSupplier);
PagedFile pagedFile = pageCache.map(file("a"), 8);
try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
assertTrue(cursor.next());
cursor.putLong(0L);
}
cursorTracer.reportEvents();
assertNotNull(cursorTracer.observe(Fault.class));
assertEquals(1, cursorTracer.faults());
assertEquals(1, tracer.faults());
int clockArm = pageCache.evictPages(1, 0, tracer.beginPageEvictions(1));
assertThat(clockArm, is(1));
assertNotNull(tracer.observe(Evict.class));
ByteBuffer buf = ByteBuffer.allocate(16);
StoreChannel channel = fs.open(file("a"), "r");
channel.read(buf);
buf.flip();
assertThat(buf.getLong(), is(0L));
assertThat(buf.getLong(), is(y));
}
Aggregations