commit | 1996ac2d281f2ba6171499ac0de852e41e4d446e | [log] [tgz] |
---|---|---|
author | Suryandaru Triandana <syndtr@gmail.com> | Sun Oct 09 17:02:47 2016 +0700 |
committer | Suryandaru Triandana <syndtr@gmail.com> | Sun Oct 09 17:02:47 2016 +0700 |
tree | 566dbb0d94cc3a18bd12179e60e57943ed28f3ac | |
parent | cde1025996b58f92704db5cb60a487efa2640d99 [diff] |
leveldb: partially rewrite batch and db writer The new batch implementation is more cleaner by spliting batch header buffer. Also added index, replaying batch should be much more performant now. DB writer now guarantee that user batch content won't be changed, previous implementation would append batch directly into user batch during write merge, this plain wrong and can potentially cause issue. Also added options to disable write merge. There are internal corruption detected on issue #155, the batch apparently contains invalid batch header and record with invalid length. Also lots of batches with invalid sequence number present in the journal. Hopefully this change fixes some of that.
This is an implementation of the LevelDB key/value database in the Go programming language.
go get github.com/syndtr/goleveldb/leveldb
go1.4
or newer.Create or open a database:
db, err := leveldb.OpenFile("path/to/db", nil)
...
defer db.Close()
...
Read or modify the database content:
// Remember that the contents of the returned slice should not be modified. data, err := db.Get([]byte("key"), nil) ... err = db.Put([]byte("key"), []byte("value"), nil) ... err = db.Delete([]byte("key"), nil) ...
Iterate over database content:
iter := db.NewIterator(nil, nil) for iter.Next() { // Remember that the contents of the returned slice should not be modified, and // only valid until the next call to Next. key := iter.Key() value := iter.Value() ... } iter.Release() err = iter.Error() ...
Seek-then-Iterate:
iter := db.NewIterator(nil, nil) for ok := iter.Seek(key); ok; ok = iter.Next() { // Use key/value. ... } iter.Release() err = iter.Error() ...
Iterate over subset of database content:
iter := db.NewIterator(&util.Range{Start: []byte("foo"), Limit: []byte("xoo")}, nil) for iter.Next() { // Use key/value. ... } iter.Release() err = iter.Error() ...
Iterate over subset of database content with a particular prefix:
iter := db.NewIterator(util.BytesPrefix([]byte("foo-")), nil) for iter.Next() { // Use key/value. ... } iter.Release() err = iter.Error() ...
Batch writes:
batch := new(leveldb.Batch)
batch.Put([]byte("foo"), []byte("value"))
batch.Put([]byte("bar"), []byte("another value"))
batch.Delete([]byte("baz"))
err = db.Write(batch, nil)
...
Use bloom filter:
o := &opt.Options{
Filter: filter.NewBloomFilter(10),
}
db, err := leveldb.OpenFile("path/to/db", o)
...
defer db.Close()
...
You can read package documentation here.