/Users/deen/code/yugabyte-db/src/yb/docdb/in_mem_docdb.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_DOCDB_IN_MEM_DOCDB_H_ |
15 | | #define YB_DOCDB_IN_MEM_DOCDB_H_ |
16 | | |
17 | | #include <map> |
18 | | |
19 | | #include "yb/rocksdb/db.h" |
20 | | |
21 | | #include "yb/docdb/subdocument.h" |
22 | | #include "yb/docdb/doc_path.h" |
23 | | #include "yb/docdb/value.h" |
24 | | #include "yb/util/status_fwd.h" |
25 | | |
26 | | namespace yb { |
27 | | namespace docdb { |
28 | | |
29 | | // An in-memory single-versioned single-threaded DocDB representation for testing. |
30 | | class InMemDocDbState { |
31 | | public: |
32 | | CHECKED_STATUS SetPrimitive(const DocPath& doc_path, const PrimitiveValue& value); |
33 | | CHECKED_STATUS DeleteSubDoc(const DocPath& doc_path); |
34 | | |
35 | | void SetDocument(const KeyBytes& encoded_doc_key, SubDocument&& doc); |
36 | | const SubDocument* GetSubDocument(const SubDocKey &subdoc_key) const; |
37 | | const SubDocument* GetDocument(const DocKey& doc_key) const; |
38 | | |
39 | | // Capture all documents present in the given RocksDB database at the given hybrid_time and save |
40 | | // them into this in-memory DocDB. All current contents of this object are overwritten. |
41 | | void CaptureAt(const DocDB& doc_db, HybridTime hybrid_time, |
42 | | rocksdb::QueryId = rocksdb::kDefaultQueryId); |
43 | | |
44 | | void SetCaptureHybridTime(HybridTime hybrid_time); |
45 | | |
46 | 0 | int num_docs() const { return root_.object_num_keys(); } |
47 | | |
48 | | // Compares this state of DocDB to the other one (which it is expected to match), and reports |
49 | | // any differences to the log. The reporting of differences to the log can be turned off, |
50 | | // but is turned on by default. |
51 | | // |
52 | | // @param log_diff Whether to log the differences between the two DocDB states. |
53 | | // @return true if the two databases have the same state. |
54 | | bool EqualsAndLogDiff(const InMemDocDbState &expected, bool log_diff = true); |
55 | | |
56 | | std::string ToDebugString() const; |
57 | | |
58 | | HybridTime captured_at() const; |
59 | | |
60 | | // Check for internal corruption. Used to catch bugs in tests. |
61 | | void SanityCheck() const; |
62 | | |
63 | | private: |
64 | | // We reuse SubDocument for the top-level map, as this is only being used in testing. We wrap |
65 | | // encoded DocKeys into PrimitiveValues of type string. |
66 | | SubDocument root_; |
67 | | |
68 | | // We use this field when capturing a DocDB state into an InMemDocDbState. This is the latest |
69 | | // write hybrid_time at which the capture happened. |
70 | | HybridTime captured_at_; |
71 | | }; |
72 | | |
73 | | } // namespace docdb |
74 | | } // namespace yb |
75 | | |
76 | | #endif // YB_DOCDB_IN_MEM_DOCDB_H_ |