Skip to content

Commit

Permalink
perf(s3stream): cache parsed DataBlockIndex for IndexBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
lifepuzzlefun committed Feb 2, 2024
1 parent 4dae71c commit 435e1c5
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion s3stream/src/main/java/com/automq/stream/s3/ObjectReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,14 @@ public static class IndexBlock {
private final ByteBuf buf;
private final int size;
private final int count;
private DataBlockIndex[] dataBlockIndices;

public IndexBlock(S3ObjectMetadata s3ObjectMetadata, ByteBuf buf) {
this.s3ObjectMetadata = s3ObjectMetadata;
this.buf = buf;
this.size = buf.readableBytes();
this.count = buf.readableBytes() / INDEX_BLOCK_UNIT_SIZE;
this.dataBlockIndices = new DataBlockIndex[count];
}

public Iterator<DataBlockIndex> iterator() {
Expand All @@ -227,14 +229,25 @@ public DataBlockIndex get(int index) {
if (index < 0 || index >= count) {
throw new IllegalArgumentException("index" + index + " is out of range [0, " + count + ")");
}

// the buf is readonly so just check if the entry have been parsed.
if (dataBlockIndices[index] != null) {
return dataBlockIndices[index];
}

int base = index * INDEX_BLOCK_UNIT_SIZE;
long streamId = buf.getLong(base);
long startOffset = buf.getLong(base + 8);
int endOffsetDelta = buf.getInt(base + 16);
int recordCount = buf.getInt(base + 20);
long blockPosition = buf.getLong(base + 24);
int blockSize = buf.getInt(base + 32);
return new DataBlockIndex(streamId, startOffset, endOffsetDelta, recordCount, blockPosition, blockSize);

DataBlockIndex idx =
new DataBlockIndex(streamId, startOffset, endOffsetDelta, recordCount, blockPosition, blockSize);
dataBlockIndices[index] = idx;

return idx;
}

public FindIndexResult find(long streamId, long startOffset, long endOffset) {
Expand Down

0 comments on commit 435e1c5

Please sign in to comment.