Install goleveldb
masterInstall the leveldb package using go get:
go get github.com/syndtr/goleveldb/leveldbrepository·master·Indexed 27 days ago
https://github.com/syndtr/goleveldbA pure Go implementation of the LevelDB key/value database. It provides a high-performance, concurrent-safe storage engine supporting basic operations like Get, Put, and Delete, as well as batch writes, range and prefix scans, and Bloom filter configuration.
Install the leveldb package using go get:
go get github.com/syndtr/goleveldb/leveldbTo start an iteration from a specific key, use iter.Seek(key). This is useful for range scans starting at a known point.
iter := db.NewIterator(nil, nil)
for ok := iter.Seek(key); ok; ok = iter.Next() {
// Use key/value.
...
}
iter.Release()
err = iter.Error()
...Use db.NewIterator to traverse the database.
Important:
iter.Key() and iter.Value() are only valid until the next call to Next(). Do not modify them or store them without copying.iter.Release() to free resources.iter.Error() after the loop to ensure no errors occurred during iteration.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()
...You can limit the scope of an iterator using util.Range or util.BytesPrefix.
Range Scan: Use &util.Range{Start: []byte("..."), Limit: []byte("...")} to iterate between two keys.
Prefix Scan: Use util.BytesPrefix([]byte("...")) to iterate over all keys starting with a specific prefix.
// 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()
...Use leveldb.OpenFile to open an existing database or create a new one at the specified path. The returned *leveldb.DB instance is safe for concurrent use by multiple goroutines. Always ensure you call db.Close() when finished.
// The returned DB instance is safe for concurrent use. Which mean that all
// DB's methods may be called concurrently from multiple goroutine.
db, err := leveldb.OpenFile("path/to/db", nil)
...
defer db.Close()
...To improve read performance, you can enable a Bloom filter by passing an opt.Options struct to leveldb.OpenFile.
o := &opt.Options{
Filter: filter.NewBloomFilter(10),
}
db, err := leveldb.OpenFile("path/to/db", o)
...
defer db.Close()
...Use leveldb.Batch to group multiple Put and Delete operations into a single atomic write using db.Write(batch, nil). This is more efficient than multiple individual 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)
...Perform basic key-value operations using Get, Put, and Delete.
Note: The byte slice returned by Get should not be modified, as it points to internal buffers.
// 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)
...