When using the following code below I expect to have only one entry in the db. I use the syndtr/goleveldb LevelDB implementation for Go.
for count := 0; count < 5; count++ {
err := db.Put([]byte("key"), []byte("value"))
if err != nil {
t.Error(err)
}
}
Instead, a hexdump reveals that there are 5 entries:
00000000 a8 ef d2 d4 17 00 01 01 00 00 00 00 00 00 00 01 |................|
00000010 00 00 00 01 03 6b 65 79 05 76 61 6c 75 65 10 23 |.....key.value.#|
00000020 44 b5 17 00 01 02 00 00 00 00 00 00 00 01 00 00 |D...............|
00000030 00 01 03 6b 65 79 05 76 61 6c 75 65 77 be 34 95 |...key.valuew.4.|
00000040 17 00 01 03 00 00 00 00 00 00 00 01 00 00 00 01 |................|
00000050 03 6b 65 79 05 76 61 6c 75 65 08 35 86 60 17 00 |.key.value.5.`..|
00000060 01 04 00 00 00 00 00 00 00 01 00 00 00 01 03 6b |...............k|
00000070 65 79 05 76 61 6c 75 65 6f 8c f6 00 17 00 01 05 |ey.valueo.......|
00000080 00 00 00 00 00 00 00 01 00 00 00 01 03 6b 65 79 |.............key|
00000090 05 76 61 6c 75 65 |.value|
00000096
From my understanding LevelDB
should by default overwrite any duplicate key or at least throw an error.
How can I control when the value gets overwritten or how can I get an error (other than checking db.Has(key)
every time that I db.Put(key)
?
Does compaction
have anything to do with this?