2017-10-23 14:42:24 +05:30
|
|
|
/*
|
2025-12-10 17:11:18 -05:00
|
|
|
* SPDX-FileCopyrightText: © 2017-2025 Istari Digital, Inc.
|
2025-02-05 17:00:32 -05:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-10-23 14:42:24 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package badger
|
|
|
|
|
|
2018-09-19 10:44:01 -07:00
|
|
|
// OpenManaged returns a new DB, which allows more control over setting
|
2018-12-31 13:45:06 -08:00
|
|
|
// transaction timestamps, aka managed mode.
|
2017-10-23 14:42:24 +05:30
|
|
|
//
|
|
|
|
|
// This is only useful for databases built on top of Badger (like Dgraph), and
|
|
|
|
|
// can be ignored by most users.
|
2018-09-19 10:44:01 -07:00
|
|
|
func OpenManaged(opts Options) (*DB, error) {
|
2018-10-02 15:05:04 -07:00
|
|
|
opts.managedTxns = true
|
2018-09-19 10:44:01 -07:00
|
|
|
return Open(opts)
|
2017-10-23 14:42:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
2018-09-19 10:44:01 -07:00
|
|
|
func (db *DB) NewTransactionAt(readTs uint64, update bool) *Txn {
|
2018-10-02 15:05:04 -07:00
|
|
|
if !db.opt.managedTxns {
|
|
|
|
|
panic("Cannot use NewTransactionAt with managedDB=false. Use NewTransaction instead.")
|
2018-09-19 10:44:01 -07:00
|
|
|
}
|
2018-10-08 18:05:00 -07:00
|
|
|
txn := db.newTransaction(update, true)
|
2017-10-23 14:42:24 +05:30
|
|
|
txn.readTs = readTs
|
|
|
|
|
return txn
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 12:17:34 +05:30
|
|
|
// NewWriteBatchAt is similar to NewWriteBatch but it allows user to set the commit timestamp.
|
|
|
|
|
// NewWriteBatchAt is supposed to be used only in the managed mode.
|
|
|
|
|
func (db *DB) NewWriteBatchAt(commitTs uint64) *WriteBatch {
|
|
|
|
|
if !db.opt.managedTxns {
|
|
|
|
|
panic("cannot use NewWriteBatchAt with managedDB=false. Use NewWriteBatch instead")
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 15:00:18 +02:00
|
|
|
wb := db.newWriteBatch(true)
|
2019-08-03 12:17:34 +05:30
|
|
|
wb.commitTs = commitTs
|
|
|
|
|
wb.txn.commitTs = commitTs
|
|
|
|
|
return wb
|
|
|
|
|
}
|
2020-04-21 11:54:41 +05:30
|
|
|
func (db *DB) NewManagedWriteBatch() *WriteBatch {
|
|
|
|
|
if !db.opt.managedTxns {
|
2020-04-30 15:41:40 +05:30
|
|
|
panic("cannot use NewManagedWriteBatch with managedDB=false. Use NewWriteBatch instead")
|
2020-04-21 11:54:41 +05:30
|
|
|
}
|
|
|
|
|
|
2020-05-18 15:00:18 +02:00
|
|
|
wb := db.newWriteBatch(true)
|
2020-04-21 11:54:41 +05:30
|
|
|
return wb
|
|
|
|
|
}
|
2019-08-03 12:17:34 +05:30
|
|
|
|
2017-10-23 14:42:24 +05:30
|
|
|
// CommitAt commits the transaction, following the same logic as Commit(), but
|
2018-09-19 10:44:01 -07:00
|
|
|
// at the given commit timestamp. This will panic if not used with managed transactions.
|
2017-10-23 14:42:24 +05:30
|
|
|
//
|
|
|
|
|
// This is only useful for databases built on top of Badger (like Dgraph), and
|
|
|
|
|
// can be ignored by most users.
|
|
|
|
|
func (txn *Txn) CommitAt(commitTs uint64, callback func(error)) error {
|
2018-10-02 15:05:04 -07:00
|
|
|
if !txn.db.opt.managedTxns {
|
|
|
|
|
panic("Cannot use CommitAt with managedDB=false. Use Commit instead.")
|
2017-10-23 14:42:24 +05:30
|
|
|
}
|
|
|
|
|
txn.commitTs = commitTs
|
2018-10-08 18:05:00 -07:00
|
|
|
if callback == nil {
|
|
|
|
|
return txn.Commit()
|
|
|
|
|
}
|
|
|
|
|
txn.CommitWith(callback)
|
|
|
|
|
return nil
|
2017-10-23 14:42:24 +05:30
|
|
|
}
|
2017-10-25 12:53:20 +11:00
|
|
|
|
2018-07-11 15:35:56 -07:00
|
|
|
// SetDiscardTs sets a timestamp at or below which, any invalid or deleted
|
|
|
|
|
// versions can be discarded from the LSM tree, and thence from the value log to
|
2018-09-19 10:44:01 -07:00
|
|
|
// reclaim disk space. Can only be used with managed transactions.
|
|
|
|
|
func (db *DB) SetDiscardTs(ts uint64) {
|
2018-10-02 15:05:04 -07:00
|
|
|
if !db.opt.managedTxns {
|
|
|
|
|
panic("Cannot use SetDiscardTs with managedDB=false.")
|
2018-09-19 10:44:01 -07:00
|
|
|
}
|
2018-07-11 15:35:56 -07:00
|
|
|
db.orc.setDiscardTs(ts)
|
|
|
|
|
}
|