1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef VIRTUAL_MULTI_VER_SYNC_INTERFACE_H
16 #define VIRTUAL_MULTI_VER_SYNC_INTERFACE_H
17 
18 #ifndef OMIT_MULTI_VER
19 #include "distributeddb_tools_unit_test.h"
20 #include "multi_ver_natural_store.h"
21 #include "multi_ver_natural_store_connection.h"
22 
23 namespace DistributedDB {
24 class VirtualMultiVerSyncDBInterface final : public MultiVerKvDBSyncInterface {
25 public:
26     VirtualMultiVerSyncDBInterface();
27     ~VirtualMultiVerSyncDBInterface() override;
28 
29     int GetInterfaceType() const override;
30 
31     void IncRefCount() override;
32 
33     void DecRefCount() override;
34 
35     std::vector<uint8_t> GetIdentifier() const override;
36 
37     void GetMaxTimestamp(Timestamp &stamp) const override;
38 
39     int GetMetaData(const Key &key, Value &value) const override;
40 
41     int PutMetaData(const Key &key, const Value &value, bool isInTransaction) override;
42 
43     // Delete multiple meta data records in a transaction.
44     int DeleteMetaData(const std::vector<Key> &keys) override;
45 
46     // Delete multiple meta data records with key prefix in a transaction.
47     int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const override;
48 
49     int GetAllMetaKeys(std::vector<Key> &keys) const override;
50 
51     bool IsCommitExisted(const MultiVerCommitNode &) const override;
52 
53     int GetDeviceLatestCommit(std::map<std::string, MultiVerCommitNode> &) const override;
54 
55     int GetCommitTree(const std::map<std::string, MultiVerCommitNode> &,
56         std::vector<MultiVerCommitNode> &) const override;
57 
58     int GetCommitData(const MultiVerCommitNode &commit, std::vector<MultiVerKvEntry *> &entries) const override;
59 
60     MultiVerKvEntry *CreateKvEntry(const std::vector<uint8_t> &) override;
61 
62     void ReleaseKvEntry(const MultiVerKvEntry *entry) override;
63 
64     bool IsValueSliceExisted(const ValueSliceHash &value) const override;
65 
66     int GetValueSlice(const ValueSliceHash &hashValue, ValueSlice &sliceValue) const override;
67 
68     int PutValueSlice(const ValueSliceHash &hashValue, const ValueSlice &sliceValue) const override;
69 
70     int PutCommitData(const MultiVerCommitNode &commit, const std::vector<MultiVerKvEntry *> &entries,
71         const std::string &deviceName) override;
72 
73     int MergeSyncCommit(const MultiVerCommitNode &commit, const std::vector<MultiVerCommitNode> &commits) override;
74 
NotifyStartSyncOperation()75     void NotifyStartSyncOperation() override {};
76 
NotifyFinishSyncOperation()77     void NotifyFinishSyncOperation() override {};
78 
79     int TransferSyncCommitDevInfo(MultiVerCommitNode &commit, const std::string &devId, bool isSyncedIn) const override;
80 
81     int Initialize(const std::string &deviceId);
82 
83     int GetData(const Key &key, Key &value);
84 
85     int PutData(const Key &key, const Key &value);
86 
87     int DeleteData(const Key &key);
88 
89     int StartTransaction();
90 
91     int Commit();
92 
93     int DeleteDatabase();
94 
95     const KvDBProperties &GetDbProperties() const override;
96 
97 private:
98     DistributedDBUnitTest::DistributedDBToolsUnitTest testTool_;
99     MultiVerNaturalStore *kvStore_;
100     MultiVerNaturalStoreConnection *connection_;
101     KvDBProperties properties_;
102 };
103 }  // namespace DistributedDB
104 #endif // OMIT_MULTI_VER
105 #endif // VIRTUAL_MULTI_VER_SYNC_INTERFACE
106