if (readBlock == null || !readBlock.contains(packKey, readPos)) { long start = toBlockStart(readPos); readBlock = blockCache.get(packKey, start); if (read(start, buf, 0, size) != size) throw new EOFException(); readBlock = new DfsBlock(packKey, start, buf); blockCache.put(readBlock); int n = readBlock.copy(readPos, dst, pos, cnt); readPos += n; return n;
private int setInput(long pos, Inflater inf) throws IOException, DataFormatException { if (pos < currPos) return getOrLoadBlock(pos).setInput(pos, inf); if (pos < currPos + currPtr) { int s = (int) (pos - currPos); int n = currPtr - s; inf.setInput(currBuf, s, n); return n; } throw new EOFException(JGitText.get().unexpectedEofInPack); }
@Override public ByteBuffer read(long pos, int cnt) throws IOException { if (ch == null && readAhead > 0 && notInCache(pos)) { open().setReadAheadBytes(readAhead); } DfsBlock block = cache.getOrLoad(file, pos, ctx, ch); if (block.start == pos && block.size() >= cnt) { return block.zeroCopyByteBuffer(cnt); } byte[] dst = new byte[cnt]; ByteBuffer buf = ByteBuffer.wrap(dst); buf.position(ctx.copy(file, pos, dst, 0, cnt)); return buf; }
private DfsBlock flushBlock() throws IOException { if (isEmptyPack) throw new IOException(DfsText.get().willNotStoreEmptyPack); out.write(currBuf, 0, currEnd); byte[] buf; if (currEnd == currBuf.length) { buf = currBuf; } else { buf = new byte[currEnd]; System.arraycopy(currBuf, 0, buf, 0, currEnd); } DfsBlock v = new DfsBlock(packKey, currPos, buf); readBlock = v; blockCache.put(v); return v; }
HashEntry e1 = table.get(slot); DfsBlock v = scan(e1, key, position); if (v != null && v.contains(key, requestedPosition)) { ctx.stats.blockCacheHit++; getStat(statHit, key).incrementAndGet(); Ref<DfsBlock> ref = new Ref<>(key, position, v.size(), v); ref.hot = true; for (;;) { e2 = table.get(slot); addToClock(ref, blockSize - v.size()); } finally { regionLock.unlock(); if (v.contains(file.key, requestedPosition)) return v; return getOrLoad(file, requestedPosition, ctx, fileChannel);
int copy(long pos, byte[] dstbuf, int dstoff, int cnt) { int ptr = (int) (pos - start); return copy(ptr, dstbuf, dstoff, cnt); }
DfsBlock quickCopy(DfsPackFile p, long pos, long cnt) throws IOException { pin(p, pos); if (block.contains(p.key, pos + (cnt - 1))) return block; return null; }
void put(DfsBlock v) { put(v.stream, v.start, v.size(), v); }
private void flushBlock() throws IOException { out.write(currBuf, 0, currPtr); byte[] buf; if (currPtr == currBuf.length) buf = currBuf; else buf = copyOf(currBuf, 0, currPtr); cache.put(new DfsBlock(packKey, currPos, buf)); currPos += currPtr; currPtr = 0; currBuf = null; }
key.cachedSize.addAndGet(v.size()); Ref<DfsBlock> ref = new Ref<DfsBlock>(key, position, v.size(), v); ref.hot = true; for (;;) { e2 = table.get(slot); addToClock(ref, blockSize - v.size()); } finally { regionLock.unlock(); if (v.contains(pack.key, requestedPosition)) return v; return getOrLoad(pack, requestedPosition, ctx);
int read(long pos, byte[] dst, int ptr, int cnt) throws IOException { int r = 0; while (pos < currPos && r < cnt) { DfsBlock b = getOrLoadBlock(pos); int n = b.copy(pos, dst, ptr + r, cnt - r); pos += n; r += n; } if (currPos <= pos && r < cnt) { int s = (int) (pos - currPos); int n = Math.min(currPtr - s, cnt - r); System.arraycopy(currBuf, s, dst, ptr + r, n); r += n; } return r; }
void pin(BlockBasedFile file, long position) throws IOException { if (block == null || !block.contains(file.key, position)) { // If memory is low, we may need what is in our window field to // be cleaned up by the GC during the get for the next window. // So we always clear it, even though we are just going to set // it again. block = null; block = file.getOrLoadBlock(position, this); } }
private void buffer(byte[] raw, int pos, int len) throws IOException { while (0 < len) { int n = Math.min(len, currBuf.length - currEnd); if (n == 0) { DfsBlock v = flushBlock(); currBuf = new byte[blockSize]; currEnd = 0; currPos += v.size(); continue; } System.arraycopy(raw, pos, currBuf, currEnd, n); pos += n; len -= n; currEnd += n; packEnd += n; } }
@Override public ByteBuffer read(long pos, int cnt) throws IOException { if (ch == null && readAhead > 0 && notInCache(pos)) { open().setReadAheadBytes(readAhead); } DfsBlock block = cache.getOrLoad(file, pos, ctx, ch); if (block.start == pos && block.size() >= cnt) { return block.zeroCopyByteBuffer(cnt); } byte[] dst = new byte[cnt]; ByteBuffer buf = ByteBuffer.wrap(dst); buf.position(ctx.copy(file, pos, dst, 0, cnt)); return buf; }
@Override protected int readDatabase(byte[] dst, int pos, int cnt) throws IOException { if (cnt == 0) return 0; if (currPos <= readPos) { // Requested read is still buffered. Copy direct from buffer. int p = (int) (readPos - currPos); int n = Math.min(cnt, currEnd - p); if (n == 0) throw new EOFException(); System.arraycopy(currBuf, p, dst, pos, n); readPos += n; return n; } if (readBlock == null || !readBlock.contains(packKey, readPos)) { long start = toBlockStart(readPos); readBlock = blockCache.get(packKey, start); if (readBlock == null) { int size = (int) Math.min(blockSize, packEnd - start); byte[] buf = new byte[size]; if (read(start, buf, 0, size) != size) throw new EOFException(); readBlock = new DfsBlock(packKey, start, buf); blockCache.put(readBlock); } } int n = readBlock.copy(readPos, dst, pos, cnt); readPos += n; return n; }