]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/utilities/transactions/optimistic_transaction_db_impl.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / utilities / transactions / optimistic_transaction_db_impl.h
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
2// This source code is licensed under both the GPLv2 (found in the
3// COPYING file in the root directory) and Apache 2.0 License
4// (found in the LICENSE.Apache file in the root directory).
7c673cae
FG
5
6#pragma once
7#ifndef ROCKSDB_LITE
8
f67539c2
TL
9#include <mutex>
10#include <vector>
11#include <algorithm>
12
7c673cae
FG
13#include "rocksdb/db.h"
14#include "rocksdb/options.h"
15#include "rocksdb/utilities/optimistic_transaction_db.h"
16
f67539c2 17namespace ROCKSDB_NAMESPACE {
7c673cae
FG
18
19class OptimisticTransactionDBImpl : public OptimisticTransactionDB {
20 public:
f67539c2
TL
21 explicit OptimisticTransactionDBImpl(
22 DB* db, const OptimisticTransactionDBOptions& occ_options,
23 bool take_ownership = true)
24 : OptimisticTransactionDB(db),
25 db_owner_(take_ownership),
26 validate_policy_(occ_options.validate_policy) {
27 if (validate_policy_ == OccValidationPolicy::kValidateParallel) {
28 uint32_t bucket_size = std::max(16u, occ_options.occ_lock_buckets);
29 bucketed_locks_.reserve(bucket_size);
30 for (size_t i = 0; i < bucket_size; ++i) {
31 bucketed_locks_.emplace_back(
32 std::unique_ptr<std::mutex>(new std::mutex));
33 }
34 }
35 }
7c673cae 36
11fdf7f2
TL
37 ~OptimisticTransactionDBImpl() {
38 // Prevent this stackable from destroying
39 // base db
40 if (!db_owner_) {
41 db_ = nullptr;
42 }
43 }
7c673cae
FG
44
45 Transaction* BeginTransaction(const WriteOptions& write_options,
46 const OptimisticTransactionOptions& txn_options,
47 Transaction* old_txn) override;
48
20effc67
TL
49 // Transactional `DeleteRange()` is not yet supported.
50 virtual Status DeleteRange(const WriteOptions&, ColumnFamilyHandle*,
51 const Slice&, const Slice&) override {
52 return Status::NotSupported();
53 }
54
55 // Range deletions also must not be snuck into `WriteBatch`es as they are
56 // incompatible with `OptimisticTransactionDB`.
57 virtual Status Write(const WriteOptions& write_opts,
58 WriteBatch* batch) override {
59 if (batch->HasDeleteRange()) {
60 return Status::NotSupported();
61 }
62 return OptimisticTransactionDB::Write(write_opts, batch);
63 }
64
f67539c2
TL
65 size_t GetLockBucketsSize() const { return bucketed_locks_.size(); }
66
67 OccValidationPolicy GetValidatePolicy() const { return validate_policy_; }
68
69 std::unique_lock<std::mutex> LockBucket(size_t idx);
70
7c673cae 71 private:
f67539c2
TL
72 // NOTE: used in validation phase. Each key is hashed into some
73 // bucket. We then take the lock in the hash value order to avoid deadlock.
74 std::vector<std::unique_ptr<std::mutex>> bucketed_locks_;
75
76 bool db_owner_;
11fdf7f2 77
f67539c2 78 const OccValidationPolicy validate_policy_;
7c673cae
FG
79
80 void ReinitializeTransaction(Transaction* txn,
81 const WriteOptions& write_options,
82 const OptimisticTransactionOptions& txn_options =
83 OptimisticTransactionOptions());
84};
85
f67539c2 86} // namespace ROCKSDB_NAMESPACE
7c673cae 87#endif // ROCKSDB_LITE