Documentation ¶
Overview ¶
Package leveldb provides implementation of LevelDB key/value database.
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() ...
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() ...
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() ...
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() ...
Index ¶
- Variables
- type Batch
- type BatchReplay
- type DB
- func (db *DB) Close() error
- func (db *DB) CompactRange(r util.Range) error
- func (db *DB) Delete(key []byte, wo *opt.WriteOptions) error
- func (db *DB) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error)
- func (db *DB) GetProperty(name string) (value string, err error)
- func (db *DB) GetSnapshot() (*Snapshot, error)
- func (db *DB) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator
- func (db *DB) Put(key, value []byte, wo *opt.WriteOptions) error
- func (db *DB) SizeOf(ranges []util.Range) (Sizes, error)
- func (db *DB) Write(b *Batch, wo *opt.WriteOptions) (err error)
- type ErrBatchCorrupted
- type ErrIkeyCorrupted
- type ErrManifestCorrupted
- type Reader
- type Sizes
- type Snapshot
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.ErrNotFound ErrSnapshotReleased = errors.New("leveldb: snapshot released") ErrIterReleased = errors.New("leveldb: iterator released") ErrClosed = errors.New("leveldb: closed") )
Functions ¶
This section is empty.
Types ¶
type Batch ¶
type Batch struct {
// contains filtered or unexported fields
}
Batch is a write batch.
func (*Batch) Delete ¶
Delete appends 'delete operation' of the given key to the batch. It is safe to modify the contents of the argument after Delete returns.
func (*Batch) Dump ¶ added in v0.10.6
Dump dumps batch contents. The returned slice can be loaded into the batch using Load method. The returned slice is not its own copy, so the contents should not be modified.
func (*Batch) Load ¶ added in v0.10.6
Load loads given slice into the batch. Previous contents of the batch will be discarded. The given slice will not be copied and will be used as batch buffer, so it is not safe to modify the contents of the slice.
func (*Batch) Put ¶
Put appends 'put operation' of the given key/value pair to the batch. It is safe to modify the contents of the argument after Put returns.
func (*Batch) Replay ¶ added in v0.10.6
func (b *Batch) Replay(r BatchReplay) error
Replay replays batch contents.
type BatchReplay ¶ added in v0.10.6
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a LevelDB database.
func Open ¶
Open opens or creates a DB for the given storage. The DB will be created if not exist, unless ErrorIfMissing is true. Also, if ErrorIfExist is true and the DB exist Open will returns os.ErrExist error.
Open will return an error with type of ErrCorrupted if corruption detected in the DB. Corrupted DB can be recovered with Recover function.
The returned DB instance is goroutine-safe. The DB must be closed after use, by calling Close method.
func OpenFile ¶
OpenFile opens or creates a DB for the given path. The DB will be created if not exist, unless ErrorIfMissing is true. Also, if ErrorIfExist is true and the DB exist OpenFile will returns os.ErrExist error.
OpenFile uses standard file-system backed storage implementation as desribed in the leveldb/storage package.
OpenFile will return an error with type of ErrCorrupted if corruption detected in the DB. Corrupted DB can be recovered with Recover function.
The returned DB instance is goroutine-safe. The DB must be closed after use, by calling Close method.
func Recover ¶
Recover recovers and opens a DB with missing or corrupted manifest files for the given storage. It will ignore any manifest files, valid or not. The DB must already exist or it will returns an error. Also, Recover will ignore ErrorIfMissing and ErrorIfExist options.
The returned DB instance is goroutine-safe. The DB must be closed after use, by calling Close method.
func RecoverFile ¶
RecoverFile recovers and opens a DB with missing or corrupted manifest files for the given path. It will ignore any manifest files, valid or not. The DB must already exist or it will returns an error. Also, Recover will ignore ErrorIfMissing and ErrorIfExist options.
RecoverFile uses standard file-system backed storage implementation as desribed in the leveldb/storage package.
The returned DB instance is goroutine-safe. The DB must be closed after use, by calling Close method.
func (*DB) Close ¶
Close closes the DB. This will also releases any outstanding snapshot and abort any in-flight compaction.
It is not safe to close a DB until all outstanding iterators are released. It is valid to call Close multiple times. Other methods should not be called after the DB has been closed.
func (*DB) CompactRange ¶
CompactRange compacts the underlying DB for the given key range. In particular, deleted and overwritten versions are discarded, and the data is rearranged to reduce the cost of operations needed to access the data. This operation should typically only be invoked by users who understand the underlying implementation.
A nil Range.Start is treated as a key before all keys in the DB. And a nil Range.Limit is treated as a key after all keys in the DB. Therefore if both is nil then it will compact entire DB.
func (*DB) Delete ¶
func (db *DB) Delete(key []byte, wo *opt.WriteOptions) error
Delete deletes the value for the given key. It returns ErrNotFound if the DB does not contain the key.
It is safe to modify the contents of the arguments after Delete returns.
func (*DB) Get ¶
Get gets the value for the given key. It returns ErrNotFound if the DB does not contain the key.
The returned slice is its own copy, it is safe to modify the contents of the returned slice. It is safe to modify the contents of the argument after Get returns.
func (*DB) GetProperty ¶
GetProperty returns value of the given property name.
Property names:
leveldb.num-files-at-level{n} Returns the number of files at level 'n'. leveldb.stats Returns statistics of the underlying DB. leveldb.sstables Returns sstables list for each level. leveldb.blockpool Returns block pool stats. leveldb.cachedblock Returns size of cached block. leveldb.openedtables Returns number of opened tables. leveldb.alivesnaps Returns number of alive snapshots. leveldb.aliveiters Returns number of alive iterators.
func (*DB) GetSnapshot ¶
GetSnapshot returns a latest snapshot of the underlying DB. A snapshot is a frozen snapshot of a DB state at a particular point in time. The content of snapshot are guaranteed to be consistent.
The snapshot must be released after use, by calling Release method.
func (*DB) NewIterator ¶
NewIterator returns an iterator for the latest snapshot of the uderlying DB. The returned iterator is not goroutine-safe, but it is safe to use multiple iterators concurrently, with each in a dedicated goroutine. It is also safe to use an iterator concurrently with modifying its underlying DB. The resultant key/value pairs are guaranteed to be consistent.
Slice allows slicing the iterator to only contains keys in the given range. A nil Range.Start is treated as a key before all keys in the DB. And a nil Range.Limit is treated as a key after all keys in the DB.
The iterator must be released after use, by calling Release method.
Also read Iterator documentation of the leveldb/iterator package.
func (*DB) Put ¶
func (db *DB) Put(key, value []byte, wo *opt.WriteOptions) error
Put sets the value for the given key. It overwrites any previous value for that key; a DB is not a multi-map.
It is safe to modify the contents of the arguments after Put returns.
func (*DB) SizeOf ¶
SizeOf calculates approximate sizes of the given key ranges. The length of the returned sizes are equal with the length of the given ranges. The returned sizes measure storage space usage, so if the user data compresses by a factor of ten, the returned sizes will be one-tenth the size of the corresponding user data size. The results may not include the sizes of recently written data.
type ErrBatchCorrupted ¶ added in v0.10.6
type ErrBatchCorrupted struct {
Reason string
}
func (*ErrBatchCorrupted) Error ¶ added in v0.10.6
func (e *ErrBatchCorrupted) Error() string
type ErrIkeyCorrupted ¶ added in v0.10.6
func (*ErrIkeyCorrupted) Error ¶ added in v0.10.6
func (e *ErrIkeyCorrupted) Error() string
type ErrManifestCorrupted ¶ added in v0.10.6
func (*ErrManifestCorrupted) Error ¶ added in v0.10.6
func (e *ErrManifestCorrupted) Error() string
type Reader ¶
type Reader interface { Get(key []byte, ro *opt.ReadOptions) (value []byte, err error) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator }
Reader is the interface that wraps basic Get and NewIterator methods. This interface implemented by both DB and Snapshot.
type Snapshot ¶
type Snapshot struct {
// contains filtered or unexported fields
}
Snapshot is a DB snapshot.
func (*Snapshot) Get ¶
Get gets the value for the given key. It returns ErrNotFound if the DB does not contain the key.
The caller should not modify the contents of the returned slice, but it is safe to modify the contents of the argument after Get returns.
func (*Snapshot) NewIterator ¶
NewIterator returns an iterator for the snapshot of the uderlying DB. The returned iterator is not goroutine-safe, but it is safe to use multiple iterators concurrently, with each in a dedicated goroutine. It is also safe to use an iterator concurrently with modifying its underlying DB. The resultant key/value pairs are guaranteed to be consistent.
Slice allows slicing the iterator to only contains keys in the given range. A nil Range.Start is treated as a key before all keys in the DB. And a nil Range.Limit is treated as a key after all keys in the DB.
The iterator must be released after use, by calling Release method. Releasing the snapshot doesn't mean releasing the iterator too, the iterator would be still valid until released.
Also read Iterator documentation of the leveldb/iterator package.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package cache provides interface and implementation of a cache algorithms.
|
Package cache provides interface and implementation of a cache algorithms. |
Package comparer provides interface and implementation for ordering sets of data.
|
Package comparer provides interface and implementation for ordering sets of data. |
Package errors provides common error types used throughout leveldb.
|
Package errors provides common error types used throughout leveldb. |
Package filter provides interface and implementation of probabilistic data structure.
|
Package filter provides interface and implementation of probabilistic data structure. |
Package iterator provides interface and implementation to traverse over contents of a database.
|
Package iterator provides interface and implementation to traverse over contents of a database. |
Package journal reads and writes sequences of journals.
|
Package journal reads and writes sequences of journals. |
Package memdb provides in-memory key/value database implementation.
|
Package memdb provides in-memory key/value database implementation. |
Package opt provides sets of options used by LevelDB.
|
Package opt provides sets of options used by LevelDB. |
Package storage provides storage abstraction for LevelDB.
|
Package storage provides storage abstraction for LevelDB. |
Package table allows read and write sorted key/value.
|
Package table allows read and write sorted key/value. |
Package util provides utilities used throughout leveldb.
|
Package util provides utilities used throughout leveldb. |