Skip to content

Commit

Permalink
fix idnex entry offset (apache#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaijack authored and merlimat committed May 7, 2018
1 parent 97829ef commit 8f705be
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class OffloadIndexBlockBuilderImpl implements OffloadIndexBlockBuilder {

private LedgerMetadata ledgerMetadata;
private List<OffloadIndexEntryImpl> entries;
private int lastBlockSize;

public OffloadIndexBlockBuilderImpl() {
this.entries = Lists.newArrayList();
Expand All @@ -50,13 +51,14 @@ public OffloadIndexBlockBuilder withMetadata(LedgerMetadata metadata) {
public OffloadIndexBlockBuilder addBlock(long firstEntryId, int partId, int blockSize) {
// we should added one by one.
long offset;
if(firstEntryId == 0) {
if (firstEntryId == 0) {
checkState(entries.size() == 0);
offset = 0;
} else {
checkState(entries.size() > 0);
offset = entries.get(entries.size() - 1).getOffset() + blockSize;
offset = entries.get(entries.size() - 1).getOffset() + lastBlockSize;
}
lastBlockSize = blockSize;

this.entries.add(OffloadIndexEntryImpl.of(firstEntryId, partId, offset));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void recycle() {

@Override
public OffloadIndexEntry getIndexEntryForEntry(long messageEntryId) throws IOException {
if(messageEntryId > segmentMetadata.getLastEntryId()) {
if (messageEntryId > segmentMetadata.getLastEntryId()) {
log.warn("Try to get entry: {}, which beyond lastEntryId {}, return null",
messageEntryId, segmentMetadata.getLastEntryId());
throw new IndexOutOfBoundsException("Entry index: " + messageEntryId +
Expand Down Expand Up @@ -302,7 +302,8 @@ private OffloadIndexBlock fromStream(InputStream stream) throws IOException {
DataInputStream dis = new DataInputStream(stream);
int magic = dis.readInt();
if (magic != this.INDEX_MAGIC_WORD) {
throw new IOException("Invalid MagicWord. read: " + magic + " expected: " + INDEX_MAGIC_WORD);
throw new IOException("Invalid MagicWord. read: " + Integer.toHexString(magic)
+ " expected: 0x" + Integer.toHexString(INDEX_MAGIC_WORD));
}
int indexBlockLength = dis.readInt();
int segmentMetadataLength = dis.readInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void offloadIndexBlockImplTest() throws Exception {

blockBuilder.withMetadata(metadata);

blockBuilder.addBlock(0, 2, 0);
blockBuilder.addBlock(0, 2, 64 * 1024 * 1024);
blockBuilder.addBlock(1000, 3, 64 * 1024 * 1024);
blockBuilder.addBlock(2000, 4, 64 * 1024 * 1024);
OffloadIndexBlock indexBlock = blockBuilder.build();
Expand Down

0 comments on commit 8f705be

Please sign in to comment.