• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RELATIONAL_SYNC_ABLE_STORAGE_H
16 #define RELATIONAL_SYNC_ABLE_STORAGE_H
17 #ifdef RELATIONAL_STORE
18 
19 #include "relational_db_sync_interface.h"
20 #include "relationaldb_properties.h"
21 #include "runtime_context.h"
22 #include "sqlite_single_relational_storage_engine.h"
23 
24 #include "sqlite_single_ver_relational_continue_token.h"
25 
26 namespace DistributedDB {
27 using RelationalObserverAction = std::function<void(const std::string &device)>;
28 class RelationalSyncAbleStorage : public RelationalDBSyncInterface, public virtual RefObject {
29 public:
30     explicit RelationalSyncAbleStorage(StorageEngine *engine);
31     ~RelationalSyncAbleStorage() override;
32 
33     // Get interface type of this kvdb.
34     int GetInterfaceType() const override;
35 
36     // Get the interface ref-count, in order to access asynchronously.
37     void IncRefCount() override;
38 
39     // Drop the interface ref-count.
40     void DecRefCount() override;
41 
42     // Get the identifier of this kvdb.
43     std::vector<uint8_t> GetIdentifier() const override;
44 
45     // Get the max timestamp of all entries in database.
46     void GetMaxTimeStamp(TimeStamp &stamp) const override;
47 
48     // Get meta data associated with the given key.
49     int GetMetaData(const Key &key, Value &value) const override;
50 
51     // Put meta data as a key-value entry.
52     int PutMetaData(const Key &key, const Value &value) override;
53 
54     // Delete multiple meta data records in a transaction.
55     int DeleteMetaData(const std::vector<Key> &keys) override;
56 
57     // Delete multiple meta data records with key prefix in a transaction.
58     int DeleteMetaDataByPrefixKey(const Key &keyPrefix) const override;
59 
60     // Get all meta data keys.
61     int GetAllMetaKeys(std::vector<Key> &keys) const override;
62 
63     const KvDBProperties &GetDbProperties() const override;
64 
65     // Get the data which would be synced with query condition
66     int GetSyncData(QueryObject &query, const SyncTimeRange &timeRange,
67         const DataSizeSpecInfo &dataSizeInfo, ContinueToken &continueStmtToken,
68         std::vector<SingleVerKvEntry *> &entries) const override;
69 
70     int GetSyncDataNext(std::vector<SingleVerKvEntry *> &entries, ContinueToken &continueStmtToken,
71         const DataSizeSpecInfo &dataSizeInfo) const override;
72 
73     int PutSyncDataWithQuery(const QueryObject &object, const std::vector<SingleVerKvEntry *> &entries,
74         const DeviceID &deviceName) override;
75 
76     int RemoveDeviceData(const std::string &deviceName, bool isNeedNotify) override;
77 
78     RelationalSchemaObject GetSchemaInfo() const override;
79 
80     int GetSecurityOption(SecurityOption &option) const override;
81 
82     void NotifyRemotePushFinished(const std::string &deviceId) const override;
83 
84     // Get the timestamp when database created or imported
85     int GetDatabaseCreateTimeStamp(TimeStamp &outTime) const override;
86 
87     // Get batch meta data associated with the given key.
88     int GetBatchMetaData(const std::vector<Key> &keys, std::vector<Entry> &entries) const override;
89     // Put batch meta data as a key-value entry vector
90     int PutBatchMetaData(std::vector<Entry> &entries) override;
91 
92     std::vector<QuerySyncObject> GetTablesQuery() override;
93 
94     int LocalDataChanged(int notifyEvent, std::vector<QuerySyncObject> &queryObj) override;
95 
InterceptData(std::vector<SingleVerKvEntry * > & entries,const std::string & sourceID,const std::string & targetID)96     int InterceptData(std::vector<SingleVerKvEntry *> &entries, const std::string &sourceID,
97         const std::string &targetID) const override
98     {
99         return E_OK;
100     }
101 
102     int CheckAndInitQueryCondition(QueryObject &query) const override;
103     void RegisterObserverAction(const RelationalObserverAction &action);
104     void TriggerObserverAction(const std::string &deviceName);
105 
106     int CreateDistributedDeviceTable(const std::string &device, const RelationalSyncStrategy &syncStrategy) override;
107 
108     int RegisterSchemaChangedCallback(const std::function<void()> &callback) override;
109 
110     void NotifySchemaChanged();
111 
112     void RegisterHeartBeatListener(const std::function<void()> &listener);
113 
114     int GetCompressionAlgo(std::set<CompressAlgorithm> &algorithmSet) const override;
115 
116 private:
117     SQLiteSingleVerRelationalStorageExecutor *GetHandle(bool isWrite, int &errCode,
118         OperatePerm perm = OperatePerm::NORMAL_PERM) const;
119     void ReleaseHandle(SQLiteSingleVerRelationalStorageExecutor *&handle) const;
120     int SetMaxTimeStamp(TimeStamp timestamp);
121 
122     // get
123     int GetSyncDataForQuerySync(std::vector<DataItem> &dataItems, SQLiteSingleVerRelationalContinueToken *&token,
124         const DataSizeSpecInfo &dataSizeInfo) const;
125 
126     // put
127     int PutSyncData(const QueryObject &object, std::vector<DataItem> &dataItems, const std::string &deviceName);
128     int SaveSyncDataItems(const QueryObject &object, std::vector<DataItem> &dataItems, const std::string &deviceName);
129 
130     // data
131     SQLiteSingleRelationalStorageEngine *storageEngine_ = nullptr;
132     TimeStamp currentMaxTimeStamp_ = 0;
133     KvDBProperties properties_;
134     mutable std::mutex maxTimeStampMutex_;
135 
136     std::function<void()> onSchemaChanged_;
137     mutable std::mutex onSchemaChangedMutex_;
138     std::mutex dataChangeDeviceMutex_;
139     RelationalObserverAction dataChangeDeviceCallback_;
140     std::function<void()> heartBeatListener_;
141     mutable std::mutex heartBeatMutex_;
142 };
143 }  // namespace DistributedDB
144 #endif
145 #endif // RELATIONAL_SYNC_ABLE_STORAGE_H