/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/write_batch_with_index/write_batch_with_index_internal.h
Line | Count | Source |
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 | | #pragma once |
21 | | |
22 | | #ifndef ROCKSDB_LITE |
23 | | |
24 | | #include <limits> |
25 | | #include <string> |
26 | | #include <unordered_map> |
27 | | |
28 | | #include "yb/rocksdb/comparator.h" |
29 | | #include "yb/rocksdb/iterator.h" |
30 | | #include "yb/util/slice.h" |
31 | | #include "yb/rocksdb/status.h" |
32 | | #include "yb/rocksdb/utilities/write_batch_with_index.h" |
33 | | #include "yb/rocksdb/port/port.h" |
34 | | |
35 | | namespace rocksdb { |
36 | | |
37 | | class MergeContext; |
38 | | struct Options; |
39 | | |
40 | | // Key used by skip list, as the binary searchable index of WriteBatchWithIndex. |
41 | | struct WriteBatchIndexEntry { |
42 | | WriteBatchIndexEntry(size_t o, uint32_t c) |
43 | 7.28M | : offset(o), column_family(c), search_key(nullptr) {} |
44 | | WriteBatchIndexEntry(const Slice* sk, uint32_t c) |
45 | 753k | : offset(0), column_family(c), search_key(sk) {} |
46 | | |
47 | | // If this flag appears in the offset, it indicates a key that is smaller |
48 | | // than any other entry for the same column family |
49 | | static const size_t kFlagMin = port::kMaxSizet; |
50 | | |
51 | | size_t offset; // offset of an entry in write batch's string buffer. |
52 | | uint32_t column_family; // column family of the entry |
53 | | const Slice* search_key; // if not null, instead of reading keys from |
54 | | // write batch, use it to compare. This is used |
55 | | // for lookup key. |
56 | | }; |
57 | | |
58 | | class ReadableWriteBatch : public WriteBatch { |
59 | | public: |
60 | | explicit ReadableWriteBatch(size_t reserved_bytes = 0) |
61 | 732k | : WriteBatch(reserved_bytes) {} |
62 | | // Retrieve some information from a write entry in the write batch, given |
63 | | // the start offset of the write entry. |
64 | | Status GetEntryFromDataOffset(size_t data_offset, WriteType* type, Slice* Key, |
65 | | Slice* value, Slice* blob) const; |
66 | | }; |
67 | | |
68 | | class WriteBatchEntryComparator { |
69 | | public: |
70 | | WriteBatchEntryComparator(const Comparator* _default_comparator, |
71 | | const ReadableWriteBatch* write_batch) |
72 | 733k | : default_comparator_(_default_comparator), write_batch_(write_batch) {} |
73 | | // Compare a and b. Return a negative value if a is less than b, 0 if they |
74 | | // are equal, and a positive value if a is greater than b |
75 | | int operator()(const WriteBatchIndexEntry* entry1, |
76 | | const WriteBatchIndexEntry* entry2) const; |
77 | | |
78 | | int CompareKey(uint32_t column_family, const Slice& key1, |
79 | | const Slice& key2) const; |
80 | | |
81 | | void SetComparatorForCF(uint32_t column_family_id, |
82 | 7.27M | const Comparator* comparator) { |
83 | 7.27M | cf_comparator_map_[column_family_id] = comparator; |
84 | 7.27M | } |
85 | | |
86 | 9 | const Comparator* default_comparator() { return default_comparator_; } |
87 | | |
88 | | private: |
89 | | const Comparator* default_comparator_; |
90 | | std::unordered_map<uint32_t, const Comparator*> cf_comparator_map_; |
91 | | const ReadableWriteBatch* write_batch_; |
92 | | }; |
93 | | |
94 | | class WriteBatchWithIndexInternal { |
95 | | public: |
96 | | enum Result { kFound, kDeleted, kNotFound, kMergeInProgress, kError }; |
97 | | |
98 | | // If batch contains a value for key, store it in *value and return kFound. |
99 | | // If batch contains a deletion for key, return Deleted. |
100 | | // If batch contains Merge operations as the most recent entry for a key, |
101 | | // and the merge process does not stop (not reaching a value or delete), |
102 | | // prepend the current merge operands to *operands, |
103 | | // and return kMergeInProgress |
104 | | // If batch does not contain this key, return kNotFound |
105 | | // Else, return kError on error with error Status stored in *s. |
106 | | static WriteBatchWithIndexInternal::Result GetFromBatch( |
107 | | const DBOptions& options, WriteBatchWithIndex* batch, |
108 | | ColumnFamilyHandle* column_family, const Slice& key, |
109 | | MergeContext* merge_context, WriteBatchEntryComparator* cmp, |
110 | | std::string* value, bool overwrite_key, Status* s); |
111 | | }; |
112 | | |
113 | | } // namespace rocksdb |
114 | | #endif // !ROCKSDB_LITE |