leveldb icon indicating copy to clipboard operation
leveldb copied to clipboard

Background compaction produces invalid file size

Open zb3 opened this issue 8 years ago • 0 comments

First of all note that I don't really know how LevelDB works internally, I just tried to find out what was causing cpp version of leveldb to be unable to load databases saved by this version...

So in DbImpl.java file, around line 1171 we have:

        long currentEntries = compactionState.builder.getEntryCount();
        compactionState.builder.finish();

        long currentBytes = compactionState.builder.getFileSize();
        compactionState.currentFileSize = currentBytes;

The problem is that getFileSize() in this case returns file size + 4. That function is:

        return position + dataBlockBuilder.currentSizeEstimate();

But in this case we've already called finish and the footer was already written. finish() called flush() which reset dataBlockBuilder. For empty BlockBuilder, currentSizeEstimate returns SIZE_OF_INT (4), but in this case it shouldn't even be called because the file was already finished

A quick but ugly fix I made was to change:

        long currentBytes = compactionState.builder.getFileSize();

to

        long currentBytes = compactionState.builder.getFileSize()-SIZE_OF_INT;

And now background compaction seems to work.

zb3 avatar Dec 14 '17 11:12 zb3