/** * Open reader on the reftable. * <p> * The returned reader is not thread safe. * * @param ctx * reader to access the DFS storage. * @return cursor to read the table; caller must close. * @throws java.io.IOException * table cannot be opened. */ public ReftableReader open(DfsReader ctx) throws IOException { return new ReftableReader(new CacheSource(this, cache, ctx)); }
private BlockReader seek(byte blockType, byte[] key, BlockReader idx, long startPos, long endPos) throws IOException { if (idx != null) { // Walk through a possibly multi-level index to a leaf block. BlockReader block = idx; do { if (block.seekKey(key) > 0) { return null; } long pos = block.readPositionFromIndex(); block = readBlock(pos, endPos); } while (block.type() == INDEX_BLOCK_TYPE); block.seekKey(key); return block; } return binarySearch(blockType, key, startPos, endPos); }
private BlockReader binarySearch(byte blockType, byte[] key, long startPos, long endPos) throws IOException { if (blockSize == 0) { BlockReader b = readBlock(startPos, endPos); if (blockType != b.type()) { return null; } b.seekKey(key); return b; } int low = (int) (startPos / blockSize); int end = blocksIn(startPos, endPos); BlockReader block = null; do { int mid = (low + end) >>> 1; block = readBlock(((long) mid) * blockSize, endPos); if (blockType != block.type()) { return null; } int cmp = block.seekKey(key); if (cmp < 0) { end = mid; } else if (cmp == 0) { break; } else /* if (cmp > 0) */ { low = mid + 1; } } while (low < end); return block; }
private void adjustUpdateIndexes(ReftableReader reader) throws IOException { if (minUpdateIndex == -1) { minUpdateIndex = reader.minUpdateIndex(); } else { minUpdateIndex = Math.min(minUpdateIndex, reader.minUpdateIndex()); } maxUpdateIndex = Math.max(maxUpdateIndex, reader.maxUpdateIndex()); }
/** {@inheritDoc} */ @Override public RefCursor seekRef(String refName) throws IOException { initRefIndex(); byte[] key = refName.getBytes(UTF_8); RefCursorImpl i = new RefCursorImpl(refEnd, key, false); i.block = seek(REF_BLOCK_TYPE, key, refIndex, 0, refEnd); return i; }
/** {@inheritDoc} */ @Override public RefCursor allRefs() throws IOException { if (blockSize == -1) { readFileHeader(); } long end = refEnd > 0 ? refEnd : (src.size() - FILE_FOOTER_LEN); src.adviseSequentialRead(0, end); RefCursorImpl i = new RefCursorImpl(end, null, false); i.block = readBlock(0, end); return i; }
/** {@inheritDoc} */ @Override public LogCursor allLogs() throws IOException { initLogIndex(); if (logPosition > 0) { src.adviseSequentialRead(logPosition, logEnd); LogCursorImpl i = new LogCursorImpl(logEnd, null); i.block = readBlock(logPosition, logEnd); return i; } return new EmptyLogCursor(); }
/** {@inheritDoc} */ @Override public LogCursor seekLog(String refName, long updateIndex) throws IOException { initLogIndex(); if (logPosition > 0) { byte[] key = LogEntry.key(refName, updateIndex); byte[] match = refName.getBytes(UTF_8); LogCursorImpl i = new LogCursorImpl(logEnd, match); i.block = seek(LOG_BLOCK_TYPE, key, logIndex, logPosition, logEnd); return i; } return new EmptyLogCursor(); }
private BlockReader readIndex(long pos) throws IOException { int sz = readBlockLen(pos); BlockReader i = new BlockReader(); i.readBlock(src, pos, sz); i.verifyIndex(); return i; }
private long nextUpdateIndex() throws IOException { long updateIndex = 0; for (Reftable r : refdb.stack().readers()) { if (r instanceof ReftableReader) { updateIndex = Math.max(updateIndex, ((ReftableReader) r).maxUpdateIndex()); } } return updateIndex + 1; }
/** {@inheritDoc} */ @Override public RefCursor byObjectId(AnyObjectId id) throws IOException { initObjIndex(); ObjCursorImpl i = new ObjCursorImpl(refEnd, id); if (objIndex != null) { i.initSeek(); } else { i.initScan(); } return i; }
/** {@inheritDoc} */ @Override public RefCursor seekRefsWithPrefix(String prefix) throws IOException { initRefIndex(); byte[] key = prefix.getBytes(UTF_8); RefCursorImpl i = new RefCursorImpl(refEnd, key, true); i.block = seek(REF_BLOCK_TYPE, key, refIndex, 0, refEnd); return i; }
/** {@inheritDoc} */ @Override public RefCursor allRefs() throws IOException { if (blockSize == -1) { readFileHeader(); } long end = refEnd > 0 ? refEnd : (src.size() - FILE_FOOTER_LEN); src.adviseSequentialRead(0, end); RefCursorImpl i = new RefCursorImpl(end, null, false); i.block = readBlock(0, end); return i; }
/** {@inheritDoc} */ @Override public LogCursor allLogs() throws IOException { initLogIndex(); if (logPosition > 0) { src.adviseSequentialRead(logPosition, logEnd); LogCursorImpl i = new LogCursorImpl(logEnd, null); i.block = readBlock(logPosition, logEnd); return i; } return new EmptyLogCursor(); }
private void adjustUpdateIndexes(ReftableReader reader) throws IOException { if (minUpdateIndex == -1) { minUpdateIndex = reader.minUpdateIndex(); } else { minUpdateIndex = Math.min(minUpdateIndex, reader.minUpdateIndex()); } maxUpdateIndex = Math.max(maxUpdateIndex, reader.maxUpdateIndex()); }
/** {@inheritDoc} */ @Override public LogCursor seekLog(String refName, long updateIndex) throws IOException { initLogIndex(); if (logPosition > 0) { byte[] key = LogEntry.key(refName, updateIndex); byte[] match = refName.getBytes(UTF_8); LogCursorImpl i = new LogCursorImpl(logEnd, match); i.block = seek(LOG_BLOCK_TYPE, key, logIndex, logPosition, logEnd); return i; } return new EmptyLogCursor(); }
private BlockReader readBlock(long pos, long end) throws IOException { if (indexCache != null) { BlockReader b = indexCache.get(pos); if (b != null) { return b; } } int sz = blockSize; if (sz == 0) { sz = readBlockLen(pos); } else if (pos + sz > end) { sz = (int) (end - pos); // last block may omit padding. } BlockReader b = new BlockReader(); b.readBlock(src, pos, sz); if (b.type() == INDEX_BLOCK_TYPE && !b.truncated()) { if (indexCache == null) { indexCache = new LongMap<>(); } indexCache.put(pos, b); } return b; }
private long nextUpdateIndex() throws IOException { long updateIndex = 0; for (Reftable r : refdb.stack().readers()) { if (r instanceof ReftableReader) { updateIndex = Math.max(updateIndex, ((ReftableReader) r).maxUpdateIndex()); } } return updateIndex + 1; }
/** {@inheritDoc} */ @Override public RefCursor byObjectId(AnyObjectId id) throws IOException { initObjIndex(); ObjCursorImpl i = new ObjCursorImpl(refEnd, id); if (objIndex != null) { i.initSeek(); } else { i.initScan(); } return i; }
/** {@inheritDoc} */ @Override public RefCursor seekRef(String refName) throws IOException { initRefIndex(); byte[] key = refName.getBytes(UTF_8); RefCursorImpl i = new RefCursorImpl(refEnd, key, false); i.block = seek(REF_BLOCK_TYPE, key, refIndex, 0, refEnd); return i; }