dain / leveldb

Port of LevelDB to Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NPE after writting an empty batch, closing and re-opening

badlogic opened this issue · comments

Hi,

in a simple set of integration tests i came across this issue:

  1. open a store in a specific directory, using options to create it if it's missing
  2. create a WriteBatch, leave it empty, and write it to the db
  3. close the WriteBatch and the store
  4. re-open the store, using the same options
  5. crashes with an NPE, stack trace as below
java.lang.NullPointerException
    at org.iq80.leveldb.impl.DbImpl.writeLevel0Table(DbImpl.java:900)
at org.iq80.leveldb.impl.DbImpl.recoverLogFile(DbImpl.java:522)
at org.iq80.leveldb.impl.DbImpl.<init>(DbImpl.java:184)
at org.iq80.leveldb.impl.Iq80DBFactory.open(Iq80DBFactory.java:60)
at at.knowcenter.storage.leveldb.LeveldbStorage.<init>(LeveldbStorage.java:86)

Code to reproduce the issue:

Options options = new Options();
options.createIfMissing(true);                  
db = new Iq80DBFactory().open(directory, options);
WriteBatch batch = db.createWriteBatch();
batch.close();
db.write(batch);
db.close();
db = new Iq80DBFactory().open(directory, options);

Writting an empty batch to the db is of course a bit bollocks, however, i vote for db.write(WriteBatch batch) to check if there's actually something to be written. I can of course work around this issue easily, but it would lower the amount of surprises new users of this lib encounter :)

I didn't have time to look into the leveldb code itself yet, but i'd assume it's an easy to fix issue.

Thanks for your work!
Mario