YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/transactions/optimistic_transaction_impl.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
// This source code is licensed under the BSD-style license found in the
3
// LICENSE file in the root directory of this source tree. An additional grant
4
// of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#pragma once
22
23
#ifndef ROCKSDB_LITE
24
25
#include <stack>
26
#include <string>
27
#include <unordered_map>
28
#include <vector>
29
30
#include "yb/rocksdb/db/write_callback.h"
31
#include "yb/rocksdb/db.h"
32
#include "yb/util/slice.h"
33
#include "yb/rocksdb/snapshot.h"
34
#include "yb/rocksdb/status.h"
35
#include "yb/rocksdb/types.h"
36
#include "yb/rocksdb/utilities/transaction.h"
37
#include "yb/rocksdb/utilities/optimistic_transaction_db.h"
38
#include "yb/rocksdb/utilities/write_batch_with_index.h"
39
#include "yb/rocksdb/utilities/transactions/transaction_base.h"
40
#include "yb/rocksdb/utilities/transactions/transaction_util.h"
41
42
namespace rocksdb {
43
44
class OptimisticTransactionImpl : public TransactionBaseImpl {
45
 public:
46
  OptimisticTransactionImpl(OptimisticTransactionDB* db,
47
                            const WriteOptions& write_options,
48
                            const OptimisticTransactionOptions& txn_options);
49
50
  virtual ~OptimisticTransactionImpl();
51
52
  void Reinitialize(OptimisticTransactionDB* txn_db,
53
                    const WriteOptions& write_options,
54
                    const OptimisticTransactionOptions& txn_options);
55
56
  Status Commit() override;
57
58
  void Rollback() override;
59
60
 protected:
61
  Status TryLock(ColumnFamilyHandle* column_family, const Slice& key,
62
                 bool read_only, bool untracked = false) override;
63
64
 private:
65
  OptimisticTransactionDB* const txn_db_;
66
67
  friend class OptimisticTransactionCallback;
68
69
  void Initialize(const OptimisticTransactionOptions& txn_options);
70
71
  // Returns OK if it is safe to commit this transaction.  Returns Status::Busy
72
  // if there are read or write conflicts that would prevent us from committing
73
  // OR if we can not determine whether there would be any such conflicts.
74
  //
75
  // Should only be called on writer thread.
76
  Status CheckTransactionForConflicts(DB* db);
77
78
  void Clear() override;
79
80
  void UnlockGetForUpdate(ColumnFamilyHandle* column_family,
81
3
                          const Slice& key) override {
82
    // Nothing to unlock.
83
3
  }
84
85
  // No copying allowed
86
  OptimisticTransactionImpl(const OptimisticTransactionImpl&);
87
  void operator=(const OptimisticTransactionImpl&);
88
};
89
90
// Used at commit time to trigger transaction validation
91
class OptimisticTransactionCallback : public WriteCallback {
92
 public:
93
  explicit OptimisticTransactionCallback(OptimisticTransactionImpl* txn)
94
55
      : txn_(txn) {}
95
96
55
  Status Callback(DB* db) override {
97
55
    return txn_->CheckTransactionForConflicts(db);
98
55
  }
99
100
0
  bool AllowWriteBatching() override { return false; }
101
102
 private:
103
  OptimisticTransactionImpl* txn_;
104
};
105
106
}  // namespace rocksdb
107
108
#endif  // ROCKSDB_LITE