github.com/pingcap/badger@v1.5.1-0.20230103063557-828f39b09b6d/managed_db.go (about)

     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  
    17  package badger
    18  
    19  // ManagedDB allows end users to manage the transactions themselves. Transaction
    20  // start and commit timestamps are set by end-user.
    21  //
    22  // This is only useful for databases built on Top of Badger (like Dgraph), and
    23  // can be ignored by most users.
    24  //
    25  // WARNING: This is an experimental feature and may be changed significantly in
    26  // a future release. So please proceed with caution.
    27  type ManagedDB struct {
    28  	*DB
    29  }
    30  
    31  // OpenManaged returns a new ManagedDB, which allows more control over setting
    32  // transaction timestamps.
    33  //
    34  // This is only useful for databases built on Top of Badger (like Dgraph), and
    35  // can be ignored by most users.
    36  func OpenManaged(opts Options) (*ManagedDB, error) {
    37  	opts.ManagedTxns = true
    38  	db, err := Open(opts)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	return &ManagedDB{db}, nil
    43  }
    44  
    45  // NewTransaction overrides DB.NewTransaction() and panics when invoked. Use
    46  // NewTransactionAt() instead.
    47  func (db *ManagedDB) NewTransaction(update bool) {
    48  	panic("Cannot use NewTransaction() for ManagedDB. Use NewTransactionAt() instead.")
    49  }
    50  
    51  // NewTransactionAt follows the same logic as DB.NewTransaction(), but uses the
    52  // provided read timestamp.
    53  //
    54  // This is only useful for databases built on Top of Badger (like Dgraph), and
    55  // can be ignored by most users.
    56  func (db *ManagedDB) NewTransactionAt(readTs uint64, update bool) *Txn {
    57  	txn := db.DB.NewTransaction(update)
    58  	txn.readTs = readTs
    59  	return txn
    60  }
    61  
    62  // CommitAt commits the transaction, following the same logic as Commit(), but
    63  // at the given commit timestamp. This will panic if not used with ManagedDB.
    64  //
    65  // This is only useful for databases built on Top of Badger (like Dgraph), and
    66  // can be ignored by most users.
    67  func (txn *Txn) CommitAt(commitTs uint64) error {
    68  	if !txn.db.IsManaged() {
    69  		return ErrManagedTxn
    70  	}
    71  	txn.commitTs = commitTs
    72  	return txn.Commit()
    73  }