managed_db.go 2.5 KB

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