Documentation
¶
Overview ¶
Package badger implements an embeddable, simple and fast key-value database, written in pure Go. It is designed to be highly performant for both reads and writes simultaneously. Badger uses Multi-Version Concurrency Control (MVCC), and supports transactions. It runs transactions concurrently, with serializable snapshot isolation guarantees.
Badger uses an LSM tree along with a value log to separate keys from values, hence reducing both write amplification and the size of the LSM tree. This allows LSM tree to be served entirely from RAM, while the values are served from SSD.
Usage ¶
Badger has the following main types: DB, Txn, Item and Iterator. DB contains keys that are associated with values. It must be opened with the appropriate options before it can be accessed.
All operations happen inside a Txn. Txn represents a transaction, which can be read-only or read-write. Read-only transactions can read values for a given key (which are returned inside an Item), or iterate over a set of key-value pairs using an Iterator (which are returned as Item type values as well). Read-write transactions can also update and delete keys from the DB.
See the examples for more usage details.
Index ¶
- Constants
- Variables
- func CompactTables(cd *CompactDef, stats *y.CompactionStats, discardStats *DiscardStats) ([]*sstable.BuildResult, error)
- type CompactDef
- type CompactionFilter
- type CompactionReq
- type CompactionResp
- type CompactionServer
- type DB
- func (db *DB) Backup(w io.Writer, since uint64) (uint64, error)
- func (db *DB) CacheMetrics() *cache.Metrics
- func (db *DB) Close() (err error)
- func (db *DB) DeleteFilesInRange(start, end []byte)
- func (db *DB) GetVLogOffset() uint64
- func (db *DB) IngestExternalFiles(files []ExternalTableSpec) (int, error)
- func (db *DB) IsManaged() bool
- func (db *DB) IterateVLog(offset uint64, fn func(e Entry)) error
- func (db *DB) Load(r io.Reader) error
- func (db *DB) NewExternalTableBuilder(f *os.File, compression options.CompressionType, limiter *rate.Limiter) *sstable.Builder
- func (db *DB) NewTransaction(update bool) *Txn
- func (db *DB) Size() (lsm int64, vlog int64)
- func (db *DB) Tables() []TableInfo
- func (db *DB) Update(fn func(txn *Txn) error) error
- func (db *DB) UpdateSafeTs(ts uint64)
- func (db *DB) View(fn func(txn *Txn) error) error
- type Decision
- type DiscardStats
- type Entry
- type ExternalTableSpec
- type Guard
- type Item
- func (item *Item) EstimatedSize() int64
- func (item *Item) IsDeleted() bool
- func (item *Item) IsEmpty() bool
- func (item *Item) Key() []byte
- func (item *Item) KeyCopy(dst []byte) []byte
- func (item *Item) String() string
- func (item *Item) UserMeta() []byte
- func (item *Item) Value() ([]byte, error)
- func (item *Item) ValueCopy(dst []byte) ([]byte, error)
- func (item *Item) ValueSize() int
- func (item *Item) Version() uint64
- type Iterator
- type IteratorOptions
- type ManagedDB
- type Manifest
- type Options
- type TableInfo
- type Txn
- func (txn *Txn) Commit() error
- func (txn *Txn) CommitAt(commitTs uint64) error
- func (txn *Txn) Delete(key []byte) error
- func (txn *Txn) Discard()
- func (txn *Txn) Get(key []byte) (item *Item, rerr error)
- func (txn *Txn) MultiGet(keys [][]byte) (items []*Item, err error)
- func (txn *Txn) NewIterator(opt IteratorOptions) *Iterator
- func (txn *Txn) Set(key, val []byte) error
- func (txn *Txn) SetEntry(e *Entry) error
- func (txn *Txn) SetReadTS(readTS uint64)
- func (txn *Txn) SetWithMeta(key, val []byte, meta byte) error
- func (txn *Txn) SetWithMetaSlice(key, val, meta []byte) error
Examples ¶
Constants ¶
const (
// ManifestFilename is the filename for the manifest file.
ManifestFilename = "MANIFEST"
)
Variables ¶
var ( // ErrValueLogSize is returned when opt.ValueLogFileSize option is not within the valid // range. ErrValueLogSize = errors.New("Invalid ValueLogFileSize, must be between 1MB and 2GB") // ErrValueThreshold is returned when ValueThreshold is set to a value close to or greater than // uint16. ErrValueThreshold = errors.New("Invalid ValueThreshold, must be lower than uint16.") // ErrKeyNotFound is returned when key isn't found on a txn.Get. ErrKeyNotFound = errors.New("Key not found") // ErrTxnTooBig is returned if too many writes are fit into a single transaction. ErrTxnTooBig = errors.New("Txn is too big to fit into one request") // ErrConflict is returned when a transaction conflicts with another transaction. This can happen if // the read rows had been updated concurrently by another transaction. ErrConflict = errors.New("Transaction Conflict. Please retry") // ErrReadOnlyTxn is returned if an update function is called on a read-only transaction. ErrReadOnlyTxn = errors.New("No sets or deletes are allowed in a read-only transaction") // ErrDiscardedTxn is returned if a previously discarded transaction is re-used. ErrDiscardedTxn = errors.New("This transaction has been discarded. Create a new one") // ErrEmptyKey is returned if an empty key is passed on an update function. ErrEmptyKey = errors.New("Key cannot be empty") // ErrRetry is returned when a log file containing the value is not found. // This usually indicates that it may have been garbage collected, and the // operation needs to be retried. ErrRetry = errors.New("Unable to find log file. Please retry") // ErrThresholdZero is returned if threshold is set to zero, and value log GC is called. // In such a case, GC can't be run. ErrThresholdZero = errors.New( "Value log GC can't run because threshold is set to zero") // ErrNoRewrite is returned if a call for value log GC doesn't result in a log file rewrite. ErrNoRewrite = errors.New( "Value log GC attempt didn't result in any cleanup") // ErrRejected is returned if a value log GC is called either while another GC is running, or // after DB::Close has been called. ErrRejected = errors.New("Value log GC request rejected") // ErrInvalidRequest is returned if the user request is invalid. ErrInvalidRequest = errors.New("Invalid request") // ErrManagedTxn is returned if the user tries to use an API which isn't // allowed due to external management of transactions, when using ManagedDB. ErrManagedTxn = errors.New( "Invalid API request. Not allowed to perform this action using ManagedDB") // ErrInvalidDump if a data dump made previously cannot be loaded into the database. ErrInvalidDump = errors.New("Data dump cannot be read") // ErrZeroBandwidth is returned if the user passes in zero bandwidth for sequence. ErrZeroBandwidth = errors.New("Bandwidth must be greater than zero") // ErrReplayNeeded is returned when opt.ReadOnly is set but the // database requires a value log replay. ErrReplayNeeded = errors.New("Database was not properly closed, cannot open read-only") // ErrWindowsNotSupported is returned when opt.ReadOnly is used on Windows ErrWindowsNotSupported = errors.New("Read-only mode is not supported on Windows") // ErrTruncateNeeded is returned when the value log gets corrupt, and requires truncation of // corrupt data to allow Badger to run properly. ErrTruncateNeeded = errors.New("Value log truncate required to run DB. This might result in data loss.") // ErrTruncateNeeded is returned when UserMate size exceed 255. ErrUserMetaTooLarge = errors.New("UserMate size exceed 255.") )
var DefaultIteratorOptions = IteratorOptions{ Reverse: false, AllVersions: false, }
DefaultIteratorOptions contains default options when iterating over Badger key-value stores.
var DefaultOptions = Options{ DoNotCompact: false, LevelOneSize: 256 << 20, MaxMemTableSize: 64 << 20, NumCompactors: 3, NumLevelZeroTables: 5, NumLevelZeroTablesStall: 10, NumMemtables: 5, SyncWrites: true, ValueLogFileSize: 256 << 20, ValueLogMaxEntries: 1000000, ValueLogMaxNumFiles: 1, ValueThreshold: 32, Truncate: false, MaxBlockCacheSize: 1 << 30, MaxIndexCacheSize: 1 << 30, TableBuilderOptions: options.TableBuilderOptions{ MaxTableSize: 8 << 20, SuRFStartLevel: 8, HashUtilRatio: 0.75, WriteBufferSize: 2 * 1024 * 1024, BytesPerSecond: -1, MaxLevels: 7, LevelSizeMultiplier: 10, BlockSize: 64 * 1024, CompressionPerLevel: []options.CompressionType{options.None, options.None, options.Snappy, options.Snappy, options.Snappy, options.ZSTD, options.ZSTD}, LogicalBloomFPR: 0.01, SuRFOptions: options.SuRFOptions{ HashSuffixLen: 8, RealSuffixLen: 8, BitsPerKeyHint: 40, }, }, ValueLogWriteOptions: options.ValueLogWriterOptions{ WriteBufferSize: 2 * 1024 * 1024, }, CompactL0WhenClose: true, }
DefaultOptions sets a list of recommended options for good performance. Feel free to modify these to suit your needs.
var ErrExternalTableOverlap = errors.New("keys of external tables has overlap")
ErrExternalTableOverlap returned by IngestExternalFiles when files overlaps.
var LSMOnlyOptions = Options{}
LSMOnlyOptions follows from DefaultOptions, but sets a higher ValueThreshold so values would be colocated with the LSM tree, with value log largely acting as a write-ahead log only. These options would reduce the disk usage of value log, and make Badger act like a typical LSM tree.
Functions ¶
func CompactTables ¶
func CompactTables(cd *CompactDef, stats *y.CompactionStats, discardStats *DiscardStats) ([]*sstable.BuildResult, error)
CompactTables compacts tables in CompactDef and returns the file names.
Types ¶
type CompactDef ¶
type CompactDef struct { Level int Top []table.Table Bot []table.Table SkippedTbls []table.Table SafeTS uint64 Guards []Guard Filter CompactionFilter HasOverlap bool Opt options.TableBuilderOptions Dir string AllocIDFunc func() uint64 Limiter *rate.Limiter InMemory bool // contains filtered or unexported fields }
func (*CompactDef) String ¶
func (cd *CompactDef) String() string
type CompactionFilter ¶
type CompactionFilter interface { // Filter is the method the compaction process invokes for kv that is being compacted. The returned decision // indicates that the kv should be preserved, deleted or dropped in the output of this compaction run. Filter(key, val, userMeta []byte) Decision // Guards returns specifications that may splits the SST files // A key is associated to a guard that has the longest matched Prefix. Guards() []Guard }
CompactionFilter is an interface that user can implement to remove certain keys.
type CompactionReq ¶
type CompactionResp ¶
type CompactionServer ¶
type CompactionServer struct {
// contains filtered or unexported fields
}
func NewCompactionServer ¶
func NewCompactionServer(addr string) (*CompactionServer, error)
func (*CompactionServer) Close ¶
func (s *CompactionServer) Close()
func (*CompactionServer) Run ¶
func (s *CompactionServer) Run()
type DB ¶ added in v0.9.0
type DB struct {
// contains filtered or unexported fields
}
DB provides the various functions required to interact with Badger. DB is thread-safe.
func Open ¶ added in v0.9.0
Open returns a new DB object.
Example ¶
dir, err := ioutil.TempDir("", "badger") if err != nil { log.Fatal(err) } defer os.RemoveAll(dir) opts := DefaultOptions opts.Dir = dir opts.ValueDir = dir db, err := Open(opts) if err != nil { log.Fatal(err) } defer db.Close() err = db.View(func(txn *Txn) error { _, err := txn.Get([]byte("key")) // We expect ErrKeyNotFound fmt.Println(err) return nil }) if err != nil { log.Fatal(err) } txn := db.NewTransaction(true) // Read-write txn err = txn.Set([]byte("key"), []byte("value")) if err != nil { log.Fatal(err) } err = txn.Commit() if err != nil { log.Fatal(err) } err = db.View(func(txn *Txn) error { item, err := txn.Get([]byte("key")) if err != nil { return err } val, err := item.Value() if err != nil { return err } fmt.Printf("%s\n", string(val)) return nil }) if err != nil { log.Fatal(err) }
Output: Key not found value
func (*DB) Backup ¶ added in v0.9.0
Backup dumps a protobuf-encoded list of all entries in the database into the given writer, that are newer than the specified version. It returns a timestamp indicating when the entries were dumped which can be passed into a later invocation to generate an incremental dump, of entries that have been added/modified since the last invocation of DB.Backup()
This can be used to backup the data in a database at a given point in time.
func (*DB) CacheMetrics ¶
CacheMetrics returns the metrics for the underlying cache.
func (*DB) Close ¶ added in v0.9.0
Close closes a DB. It's crucial to call it to ensure all the pending updates make their way to disk. Calling DB.Close() multiple times is not safe and would cause panic.
func (*DB) DeleteFilesInRange ¶
DeleteFilesInRange delete files in [start, end). If some file contains keys outside the range, they will not be deleted. This function is designed to reclaim space quickly. If you want to ensure no future transaction can read keys in range, considering iterate and delete the remained keys, or using compaction filter to cleanup them asynchronously.
func (*DB) GetVLogOffset ¶
func (*DB) IngestExternalFiles ¶
func (db *DB) IngestExternalFiles(files []ExternalTableSpec) (int, error)
IngestExternalFiles ingest external constructed tables into DB. Note: insure there is no concurrent write overlap with tables to be ingested.
func (*DB) IterateVLog ¶
IterateVLog iterates VLog for external replay, this function should be called only when there is no concurrent write operation on the DB.
func (*DB) Load ¶ added in v0.9.0
Load reads a protobuf-encoded list of all entries from a reader and writes them to the database. This can be used to restore the database from a backup made by calling DB.Backup().
DB.Load() should be called on a database that is not running any other concurrent transactions while it is running.
func (*DB) NewExternalTableBuilder ¶
func (db *DB) NewExternalTableBuilder(f *os.File, compression options.CompressionType, limiter *rate.Limiter) *sstable.Builder
NewExternalTableBuilder returns a new sst builder.
func (*DB) NewTransaction ¶ added in v0.9.0
NewTransaction creates a new transaction. Badger supports concurrent execution of transactions, providing serializable snapshot isolation, avoiding write skews. Badger achieves this by tracking the keys read and at Commit time, ensuring that these read keys weren't concurrently modified by another transaction.
For read-only transactions, set update to false. In this mode, we don't track the rows read for any changes. Thus, any long running iterations done in this mode wouldn't pay this overhead.
Running transactions concurrently is OK. However, a transaction itself isn't thread safe, and should only be run serially. It doesn't matter if a transaction is created by one goroutine and passed down to other, as long as the Txn APIs are called serially.
When you create a new transaction, it is absolutely essential to call Discard(). This should be done irrespective of what the update param is set to. Commit API internally runs Discard, but running it twice wouldn't cause any issues.
txn := db.NewTransaction(false) defer txn.Discard() // Call various APIs.
func (*DB) Size ¶ added in v1.3.0
Size returns the size of lsm and value log files in bytes. It can be used to decide how often to call RunValueLogGC.
func (*DB) Update ¶ added in v0.9.0
Update executes a function, creating and managing a read-write transaction for the user. Error returned by the function is relayed by the Update method.
func (*DB) UpdateSafeTs ¶
UpdateSafeTs is used for Managed DB, during compaction old version smaller than the safe ts will be discarded. If this is not called, all old versions are kept.
type DiscardStats ¶
type DiscardStats struct {
// contains filtered or unexported fields
}
func (*DiscardStats) String ¶
func (ds *DiscardStats) String() string
type Entry ¶
type Entry struct { Key y.Key Value []byte UserMeta []byte // contains filtered or unexported fields }
Entry provides Key, Value, UserMeta. This struct can be used by the user to set data.
type ExternalTableSpec ¶
type ExternalTableSpec struct {
Filename string
}
type Guard ¶
Guard specifies when to finish a SST file during compaction. The rule is the following: 1. The key must match the Prefix of the Guard, otherwise the SST should finish. 2. If the key up to MatchLen is the different than the previous key and MinSize is reached, the SST should finish.
type Item ¶ added in v0.9.0
type Item struct {
// contains filtered or unexported fields
}
Item is returned during iteration. Both the Key() and Value() output is only valid until iterator.Next() is called.
func (*Item) EstimatedSize ¶ added in v0.9.0
EstimatedSize returns approximate size of the key-value pair.
This can be called while iterating through a store to quickly estimate the size of a range of key-value pairs (without fetching the corresponding values).
func (*Item) Key ¶ added in v0.9.0
Key returns the key.
Key is only valid as long as item is valid, or transaction is valid. If you need to use it outside its validity, please use KeyCopy
func (*Item) KeyCopy ¶ added in v1.4.0
KeyCopy returns a copy of the key of the item, writing it to dst slice. If nil is passed, or capacity of dst isn't sufficient, a new slice would be allocated and returned.
func (*Item) UserMeta ¶ added in v0.9.0
UserMeta returns the userMeta set by the user. Typically, this byte, optionally set by the user is used to interpret the value.
func (*Item) Value ¶ added in v0.9.0
Value retrieves the value of the item from the value log.
This method must be called within a transaction. Calling it outside a transaction is considered undefined behavior. If an iterator is being used, then Item.Value() is defined in the current iteration only, because items are reused.
If you need to use a value outside a transaction, please use Item.ValueCopy instead, or copy it yourself. Value might change once discard or commit is called. Use ValueCopy if you want to do a Set after Get.
func (*Item) ValueCopy ¶ added in v1.1.0
ValueCopy returns a copy of the value of the item from the value log, writing it to dst slice. If nil is passed, or capacity of dst isn't sufficient, a new slice would be allocated and returned. Tip: It might make sense to reuse the returned slice as dst argument for the next call.
This function is useful in long running iterate/update transactions to avoid a write deadlock. See Github issue: https://github.com/pingcap/badger/issues/315
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator helps iterating over the KV pairs in a lexicographically sorted order.
func (*Iterator) Close ¶
func (it *Iterator) Close()
Close would close the iterator. It is important to call this when you're done with iteration.
func (*Iterator) Item ¶
Item returns pointer to the current key-value pair. This item is only valid until it.Next() gets called.
func (*Iterator) Next ¶
func (it *Iterator) Next()
Next would advance the iterator by one. Always check it.Valid() after a Next() to ensure you have access to a valid it.Item().
func (*Iterator) Rewind ¶
func (it *Iterator) Rewind()
Rewind would rewind the iterator cursor all the way to zero-th position, which would be the smallest key if iterating forward, and largest if iterating backward. It does not keep track of whether the cursor started with a Seek().
func (*Iterator) Seek ¶
Seek would seek to the provided key if present. If absent, it would seek to the next smallest key greater than provided if iterating in the forward direction. Behavior would be reversed is iterating backwards.
func (*Iterator) SetAllVersions ¶
func (*Iterator) ValidForPrefix ¶
ValidForPrefix returns false when iteration is done or when the current key is not prefixed by the specified prefix.
type IteratorOptions ¶
type IteratorOptions struct { Reverse bool // Direction of iteration. False is forward, true is backward. AllVersions bool // Fetch all valid versions of the same key. // StartKey and EndKey are used to prune non-overlapping table iterators. // They are not boundary limits, the EndKey is exclusive. StartKey y.Key EndKey y.Key // contains filtered or unexported fields }
IteratorOptions is used to set options when iterating over Badger key-value stores.
This package provides DefaultIteratorOptions which contains options that should work for most applications. Consider using that as a starting point before customizing it for your own needs.
func (*IteratorOptions) OverlapMemTable ¶
func (opts *IteratorOptions) OverlapMemTable(t *memtable.Table) bool
func (*IteratorOptions) OverlapPending ¶
func (opts *IteratorOptions) OverlapPending(it *pendingWritesIterator) bool
func (*IteratorOptions) OverlapTable ¶
func (opts *IteratorOptions) OverlapTable(t table.Table) bool
func (*IteratorOptions) OverlapTables ¶
func (opts *IteratorOptions) OverlapTables(tables []table.Table) []table.Table
type ManagedDB ¶ added in v0.9.0
type ManagedDB struct {
*DB
}
ManagedDB allows end users to manage the transactions themselves. Transaction start and commit timestamps are set by end-user.
This is only useful for databases built on Top of Badger (like Dgraph), and can be ignored by most users.
WARNING: This is an experimental feature and may be changed significantly in a future release. So please proceed with caution.
func OpenManaged ¶ added in v0.9.0
OpenManaged returns a new ManagedDB, which allows more control over setting transaction timestamps.
This is only useful for databases built on Top of Badger (like Dgraph), and can be ignored by most users.
func (*ManagedDB) NewTransaction ¶ added in v0.9.0
NewTransaction overrides DB.NewTransaction() and panics when invoked. Use NewTransactionAt() instead.
func (*ManagedDB) NewTransactionAt ¶ added in v0.9.0
NewTransactionAt follows the same logic as DB.NewTransaction(), but uses the provided read timestamp.
This is only useful for databases built on Top of Badger (like Dgraph), and can be ignored by most users.
type Manifest ¶
type Manifest struct { Levels []levelManifest Tables map[uint64]tableManifest // Contains total number of creation and deletion changes in the manifest -- used to compute // whether it'd be useful to rewrite the manifest. Creations int Deletions int Head *protos.HeadInfo }
Manifest represents the contents of the MANIFEST file in a Badger store.
The MANIFEST file describes the startup state of the db -- all LSM files and what level they're at.
It consists of a sequence of ManifestChangeSet objects. Each of these is treated atomically, and contains a sequence of ManifestChange's (file creations/deletions) which we use to reconstruct the manifest at startup.
func ReplayManifestFile ¶
ReplayManifestFile reads the manifest file and constructs two manifest objects. (We need one immutable copy and one mutable copy of the manifest. Easiest way is to construct two of them.) Also, returns the last offset after a completely read manifest entry -- the file must be truncated at that point before further appends are made (if there is a partial entry after that). In normal conditions, truncOffset is the file size.
type Options ¶
type Options struct { // 1. Mandatory flags // ------------------- // Directory to store the data in. Should exist and be writable. Dir string // Directory to store the value log in. Can be the same as Dir. Should // exist and be writable. ValueDir string // 2. Frequently modified flags // ----------------------------- // Sync all writes to disk. Setting this to true would slow down data // loading significantly. SyncWrites bool // 3. Flags that user might want to review // ---------------------------------------- // The following affect all levels of LSM tree. MaxMemTableSize int64 // Each mem table is at most this size. // If value size >= this threshold, only store value offsets in tree. // If set to 0, all values are stored in SST. ValueThreshold int // Maximum number of tables to keep in memory, before stalling. NumMemtables int // The following affect how we handle LSM tree L0. // Maximum number of Level 0 tables before we start compacting. NumLevelZeroTables int // If we hit this number of Level 0 tables, we will stall until L0 is // compacted away. NumLevelZeroTablesStall int MaxBlockCacheSize int64 MaxIndexCacheSize int64 // Maximum total size for L1. LevelOneSize int64 // Size of single value log file. ValueLogFileSize int64 // Max number of entries a value log file can hold (approximately). A value log file would be // determined by the smaller of its file size and max entries. ValueLogMaxEntries uint32 // Max number of value log files to keep before safely remove. ValueLogMaxNumFiles int // Number of compaction workers to run concurrently. NumCompactors int // Transaction start and commit timestamps are managed by end-user. // A managed transaction can only set values by SetEntry with a non-zero version key. ManagedTxns bool // 4. Flags for testing purposes // ------------------------------ VolatileMode bool DoNotCompact bool // Stops LSM tree from compactions. // Open the DB as read-only. With this set, multiple processes can // open the same Badger DB. Note: if the DB being opened had crashed // before and has vlog data to be replayed, ReadOnly will cause Open // to fail with an appropriate message. ReadOnly bool // Truncate value log to delete corrupt data, if any. Would not truncate if ReadOnly is set. Truncate bool TableBuilderOptions options.TableBuilderOptions ValueLogWriteOptions options.ValueLogWriterOptions CompactionFilterFactory func(targetLevel int, smallest, biggest []byte) CompactionFilter CompactL0WhenClose bool RemoteCompactionAddr string // contains filtered or unexported fields }
Options are params for creating DB object.
This package provides DefaultOptions which contains options that should work for most applications. Consider using that as a starting point before customizing it for your own needs.
type Txn ¶ added in v0.9.0
type Txn struct {
// contains filtered or unexported fields
}
Txn represents a Badger transaction.
func (*Txn) Commit ¶ added in v0.9.0
Commit commits the transaction, following these steps:
1. If there are no writes, return immediately.
2. Check if read rows were updated since txn started. If so, return ErrConflict.
3. If no conflict, generate a commit timestamp and update written rows' commit ts.
4. Batch up all writes, write them to value log and LSM tree.
If error is nil, the transaction is successfully committed. In case of a non-nil error, the LSM tree won't be updated, so there's no need for any rollback.
func (*Txn) CommitAt ¶ added in v0.9.0
CommitAt commits the transaction, following the same logic as Commit(), but at the given commit timestamp. This will panic if not used with ManagedDB.
This is only useful for databases built on Top of Badger (like Dgraph), and can be ignored by most users.
func (*Txn) Delete ¶ added in v0.9.0
Delete deletes a key. This is done by adding a delete marker for the key at commit timestamp. Any reads happening before this timestamp would be unaffected. Any reads after this commit would see the deletion.
func (*Txn) Discard ¶ added in v0.9.0
func (txn *Txn) Discard()
Discard discards a created transaction. This method is very important and must be called. Commit method calls this internally, however, calling this multiple times doesn't cause any issues. So, this can safely be called via a defer right when transaction is created.
NOTE: If any operations are run on a discarded transaction, ErrDiscardedTxn is returned.
func (*Txn) Get ¶ added in v0.9.0
Get looks for key and returns corresponding Item. If key is not found, ErrKeyNotFound is returned.
func (*Txn) MultiGet ¶
MultiGet gets items for keys, if not found, the corresponding item will be nil. It only supports read-only transaction for simplicity.
func (*Txn) NewIterator ¶ added in v0.9.0
func (txn *Txn) NewIterator(opt IteratorOptions) *Iterator
NewIterator returns a new iterator. Depending upon the options, either only keys, or both key-value pairs would be fetched. The keys are returned in lexicographically sorted order. Avoid long running iterations in update transactions.
Example ¶
dir, err := ioutil.TempDir("", "badger") if err != nil { log.Fatal(err) } defer os.RemoveAll(dir) opts := DefaultOptions opts.Dir = dir opts.ValueDir = dir db, err := Open(opts) if err != nil { log.Fatal(err) } defer db.Close() bkey := func(i int) []byte { return []byte(fmt.Sprintf("%09d", i)) } bval := func(i int) []byte { return []byte(fmt.Sprintf("%025d", i)) } txn := db.NewTransaction(true) // Fill in 1000 items n := 1000 for i := 0; i < n; i++ { err := txn.Set(bkey(i), bval(i)) if err != nil { log.Fatal(err) } } err = txn.Commit() if err != nil { log.Fatal(err) } // Iterate over 1000 items var count int err = db.View(func(txn *Txn) error { it := txn.NewIterator(DefaultIteratorOptions) defer it.Close() for it.Rewind(); it.Valid(); it.Next() { count++ } return nil }) if err != nil { log.Fatal(err) } fmt.Printf("Counted %d elements", count)
Output: Counted 1000 elements
func (*Txn) Set ¶ added in v0.9.0
Set adds a key-value pair to the database.
It will return ErrReadOnlyTxn if update flag was set to false when creating the transaction.
func (*Txn) SetEntry ¶ added in v1.2.0
SetEntry takes an Entry struct and adds the key-value pair in the struct, along with other metadata to the database.
func (*Txn) SetReadTS ¶
SetReadTS reads the DB with a given TS, it can only be used in a managed DB.
func (*Txn) SetWithMeta ¶ added in v1.0.0
SetWithMeta adds a key-value pair to the database, along with a metadata byte. This byte is stored alongside the key, and can be used as an aid to interpret the value or store other contextual bits corresponding to the key-value pair.
func (*Txn) SetWithMetaSlice ¶
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package buffer implements a variable-sized bytes pool.
|
Package buffer implements a variable-sized bytes pool. |
Ristretto is a fast, fixed size, in-memory cache with a dual focus on throughput and hit ratio performance.
|
Ristretto is a fast, fixed size, in-memory cache with a dual focus on throughput and hit ratio performance. |
cmd
|
|
directio is a proxy package for github.com/pingcap/badger/directio
|
directio is a proxy package for github.com/pingcap/badger/directio |