goleveldb Documentation

repository·master·Indexed 27 days ago

https://github.com/syndtr/goleveldb

A 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.

Tokens
1.2K
Snippets
8
Records
8
Agent score
42%

What's inside goleveldb

  1. Seek and iterate from a specific key

    master

    To 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()
    ...
  2. Iterate over database content

    master

    Use db.NewIterator to traverse the database.

    Important:

    • The slices returned by iter.Key() and iter.Value() are only valid until the next call to Next(). Do not modify them or store them without copying.
    • Always call iter.Release() to free resources.
    • Check 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()
    ...
  3. Iterate over a range or prefix

    master

    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()
    ...
  4. Open or create a LevelDB database

    master

    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()
    ...
  5. Configure Bloom Filters

    master

    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()
    ...
  6. Perform batch writes

    master

    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)
    ...
  7. Read, write, and delete keys

    master

    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)
    ...