1 /*
2  * Copyright (c) 2023 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 
16 #ifndef UDMF_RUNTIMESTORE_H
17 #define UDMF_RUNTIMESTORE_H
18 
19 #include "store.h"
20 #include "kv_store_delegate_manager.h"
21 #include "metadata/store_meta_data.h"
22 #include "visibility.h"
23 
24 namespace OHOS {
25 namespace UDMF {
26 class API_EXPORT RuntimeStore final : public Store {
27 public:
28     explicit RuntimeStore(const std::string &storeId);
29     ~RuntimeStore();
30     Status Put(const UnifiedData &unifiedData) override;
31     Status Get(const std::string &key, UnifiedData &unifiedData) override;
32     Status GetSummary(const std::string &key, Summary &summary) override;
33     Status Update(const UnifiedData &unifiedData) override;
34     Status Delete(const std::string &key) override;
35     Status DeleteBatch(const std::vector<std::string> &unifiedKeys) override;
36     Status Sync(const std::vector<std::string> &devices) override;
37     Status Clear() override;
38     Status GetBatchData(const std::string &dataPrefix, std::vector<UnifiedData> &unifiedDataSet) override;
39     Status PutLocal(const std::string &key, const std::string &value) override;
40     Status GetLocal(const std::string &key, std::string &value) override;
41     Status DeleteLocal(const std::string &key) override;
42     void Close() override;
43     bool Init() override;
44 
45 private:
46     static constexpr const char *DATA_PREFIX = "udmf://";
47     static constexpr std::int32_t SLASH_COUNT_IN_KEY = 4;
48     static constexpr std::int32_t MAX_BATCH_SIZE = 128;
49     std::shared_ptr<DistributedDB::KvStoreDelegateManager> delegateManager_ = nullptr;
50     std::shared_ptr<DistributedDB::KvStoreNbDelegate> kvStore_;
51     std::string storeId_;
52     void SetDelegateManager(const std::string &dataDir, const std::string &appId, const std::string &userId,
53         const std::string &subUser);
54     bool SaveMetaData();
55     Status GetEntries(const std::string &dataPrefix, std::vector<DistributedDB::Entry> &entries);
56     Status PutEntries(const std::vector<DistributedDB::Entry> &entries);
57     Status DeleteEntries(const std::vector<DistributedDB::Key> &keys);
58     Status UnmarshalEntries(
59         const std::string &key, std::vector<DistributedDB::Entry> &entries, UnifiedData &unifiedData);
60     bool GetDetailsFromUData(UnifiedData &data, UDDetails &details);
61     Status GetSummaryFromDetails(const UDDetails &details, Summary &summary);
62     bool BuildMetaDataParam(DistributedData::StoreMetaData &metaData);
63 };
64 } // namespace UDMF
65 } // namespace OHOS
66 #endif // UDMF_RUNTIMESTORE_H
67