diff --git a/leveldb-api/src/main/java/org/iq80/leveldb/WriteBatch.java b/leveldb-api/src/main/java/org/iq80/leveldb/WriteBatch.java index eb462ced..8c984c57 100644 --- a/leveldb-api/src/main/java/org/iq80/leveldb/WriteBatch.java +++ b/leveldb-api/src/main/java/org/iq80/leveldb/WriteBatch.java @@ -59,6 +59,7 @@ public interface WriteBatch * The size of the database changes caused by this batch. * Note: This number is tied to implementation details, and may change across * releases. It is intended for LevelDB usage metrics. + * WriteBatch header has an 8-byte sequence number followed by a 4-byte count. * @return size */ long approximateSize(); diff --git a/leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java b/leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java index 72df8258..ac9b0209 100644 --- a/leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java +++ b/leveldb/src/test/java/org/iq80/leveldb/impl/DbImplTest.java @@ -126,8 +126,20 @@ public void testEmptyBatch() // write an empty batch WriteBatch batch = db.createWriteBatch(); - batch.close(); + batch.put("foobar".getBytes(UTF_8), "tgz".getBytes(UTF_8)); + batch.put("bob".getBytes(UTF_8), "tar".getBytes(UTF_8)); + WriteBatch source = db.createWriteBatch(); + source.put("foobar".getBytes(UTF_8), "tgz".getBytes(UTF_8)); + source.put("bob".getBytes(UTF_8), "tar".getBytes(UTF_8)); + batch.clear(); + assertEquals(12, batch.approximateSize()); + batch.append(source); + batch.append(source); + assertEquals(source.approximateSize() * 2 -12, batch.approximateSize()); + batch.clear(); + assertEquals(12, batch.approximateSize()); db.write(batch); + batch.close(); // close the db db.close();