tidwall / buntdb

BuntDB is an embeddable, in-memory key/value database for Go with custom indexing and geospatial support
MIT License
4.57k stars 289 forks source link

How to prevent the duplicated data with the same key? #28

Closed YamiOdymel closed 7 years ago

YamiOdymel commented 7 years ago

I'm playing around with BuntDB with this code (and executed it few times):

db.Update(func(tx *buntdb.Tx) error {
    _, _, err := tx.Set("token:ABCDEF", "Test", nil)
    return err
})

And I opened the data.db file I saw this.

*3
$3
set
$38
token:ABCDEF
$31
Test
*3
$3
set
$38
token:ABCDEF
$31
Test
*3
$3
set
$38
token:ABCDEF
$31
Test
*3
$3
set
$38
token:ABCDEF
$31
Test

Why would something like this happened? I thought BuntDB will overwrite the key, value but it just added another record.

Do I have to check the key does exist or not before I Set the key? If so, should I use Get to check it before I Set it?

tidwall commented 7 years ago

Why would something like this happened?

The db file is an append-only file. It's normal to find duplicates in the file. The db file is only used to load the database into memory when the database is opened, otherwise it's just a log. BuntDB will automatically keep the file small by periodically shrinking it, so you won't need to worry about it growing out of control.

Do I have to check the key does exist or not before I Set the key?

No. The Set operation will update an existing key or create a new key.

h3ku commented 6 years ago

Sorry for reopening this, but can you provide more information about the "shrink" process? I mean it deletes older values I suppose, but under what condition?

tidwall commented 6 years ago

The shrink process doesn’t delete anything. It creates a new file from the working in-memory data and overwrites the old file.