managed_db.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2017 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package badger
  17. // OpenManaged returns a new DB, which allows more control over setting
  18. // transaction timestamps, aka managed mode.
  19. //
  20. // This is only useful for databases built on top of Badger (like Dgraph), and
  21. // can be ignored by most users.
  22. func OpenManaged(opts Options) (*DB, error) {
  23. opts.managedTxns = true
  24. return Open(opts)
  25. }
  26. // NewTransactionAt follows the same logic as DB.NewTransaction(), but uses the
  27. // provided read timestamp.
  28. //
  29. // This is only useful for databases built on top of Badger (like Dgraph), and
  30. // can be ignored by most users.
  31. func (db *DB) NewTransactionAt(readTs uint64, update bool) *Txn {
  32. if !db.opt.managedTxns {
  33. panic("Cannot use NewTransactionAt with managedDB=false. Use NewTransaction instead.")
  34. }
  35. txn := db.newTransaction(update, true)
  36. txn.readTs = readTs
  37. return txn
  38. }
  39. // NewWriteBatchAt is similar to NewWriteBatch but it allows user to set the commit timestamp.
  40. // NewWriteBatchAt is supposed to be used only in the managed mode.
  41. func (db *DB) NewWriteBatchAt(commitTs uint64) *WriteBatch {
  42. if !db.opt.managedTxns {
  43. panic("cannot use NewWriteBatchAt with managedDB=false. Use NewWriteBatch instead")
  44. }
  45. wb := db.newWriteBatch(true)
  46. wb.commitTs = commitTs
  47. wb.txn.commitTs = commitTs
  48. return wb
  49. }
  50. func (db *DB) NewManagedWriteBatch() *WriteBatch {
  51. if !db.opt.managedTxns {
  52. panic("cannot use NewManagedWriteBatch with managedDB=false. Use NewWriteBatch instead")
  53. }
  54. wb := db.newWriteBatch(true)
  55. return wb
  56. }
  57. // CommitAt commits the transaction, following the same logic as Commit(), but
  58. // at the given commit timestamp. This will panic if not used with managed transactions.
  59. //
  60. // This is only useful for databases built on top of Badger (like Dgraph), and
  61. // can be ignored by most users.
  62. func (txn *Txn) CommitAt(commitTs uint64, callback func(error)) error {
  63. if !txn.db.opt.managedTxns {
  64. panic("Cannot use CommitAt with managedDB=false. Use Commit instead.")
  65. }
  66. txn.commitTs = commitTs
  67. if callback == nil {
  68. return txn.Commit()
  69. }
  70. txn.CommitWith(callback)
  71. return nil
  72. }
  73. // SetDiscardTs sets a timestamp at or below which, any invalid or deleted
  74. // versions can be discarded from the LSM tree, and thence from the value log to
  75. // reclaim disk space. Can only be used with managed transactions.
  76. func (db *DB) SetDiscardTs(ts uint64) {
  77. if !db.opt.managedTxns {
  78. panic("Cannot use SetDiscardTs with managedDB=false.")
  79. }
  80. db.orc.setDiscardTs(ts)
  81. }