managed_db.go raw
1 /*
2 * SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 package badger
7
8 // OpenManaged returns a new DB, which allows more control over setting
9 // transaction timestamps, aka managed mode.
10 //
11 // This is only useful for databases built on top of Badger (like Dgraph), and
12 // can be ignored by most users.
13 func OpenManaged(opts Options) (*DB, error) {
14 opts.managedTxns = true
15 return Open(opts)
16 }
17
18 // NewTransactionAt follows the same logic as DB.NewTransaction(), but uses the
19 // provided read timestamp.
20 //
21 // This is only useful for databases built on top of Badger (like Dgraph), and
22 // can be ignored by most users.
23 func (db *DB) NewTransactionAt(readTs uint64, update bool) *Txn {
24 if !db.opt.managedTxns {
25 panic("Cannot use NewTransactionAt with managedDB=false. Use NewTransaction instead.")
26 }
27 txn := db.newTransaction(update, true)
28 txn.readTs = readTs
29 return txn
30 }
31
32 // NewWriteBatchAt is similar to NewWriteBatch but it allows user to set the commit timestamp.
33 // NewWriteBatchAt is supposed to be used only in the managed mode.
34 func (db *DB) NewWriteBatchAt(commitTs uint64) *WriteBatch {
35 if !db.opt.managedTxns {
36 panic("cannot use NewWriteBatchAt with managedDB=false. Use NewWriteBatch instead")
37 }
38
39 wb := db.newWriteBatch(true)
40 wb.commitTs = commitTs
41 wb.txn.commitTs = commitTs
42 return wb
43 }
44 func (db *DB) NewManagedWriteBatch() *WriteBatch {
45 if !db.opt.managedTxns {
46 panic("cannot use NewManagedWriteBatch with managedDB=false. Use NewWriteBatch instead")
47 }
48
49 wb := db.newWriteBatch(true)
50 return wb
51 }
52
53 // CommitAt commits the transaction, following the same logic as Commit(), but
54 // at the given commit timestamp. This will panic if not used with managed transactions.
55 //
56 // This is only useful for databases built on top of Badger (like Dgraph), and
57 // can be ignored by most users.
58 func (txn *Txn) CommitAt(commitTs uint64, callback func(error)) error {
59 if !txn.db.opt.managedTxns {
60 panic("Cannot use CommitAt with managedDB=false. Use Commit instead.")
61 }
62 txn.commitTs = commitTs
63 if callback == nil {
64 return txn.Commit()
65 }
66 txn.CommitWith(callback)
67 return nil
68 }
69
70 // SetDiscardTs sets a timestamp at or below which, any invalid or deleted
71 // versions can be discarded from the LSM tree, and thence from the value log to
72 // reclaim disk space. Can only be used with managed transactions.
73 func (db *DB) SetDiscardTs(ts uint64) {
74 if !db.opt.managedTxns {
75 panic("Cannot use SetDiscardTs with managedDB=false.")
76 }
77 db.orc.setDiscardTs(ts)
78 }
79