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 23 namespace OHOS { 24 namespace UDMF { 25 using DevNameMap = std::map<std::string, std::string>; 26 using DevSyncProcessMap = std::map<std::string, DistributedDB::DeviceSyncProcess>; 27 class RuntimeStore final : public Store { 28 public: 29 explicit RuntimeStore(const std::string &storeId); 30 ~RuntimeStore(); 31 Status Put(const UnifiedData &unifiedData) override; 32 Status Get(const std::string &key, UnifiedData &unifiedData) override; 33 Status GetSummary(UnifiedKey &key, Summary &summary) override; 34 Status Update(const UnifiedData &unifiedData) override; 35 Status Delete(const std::string &key) override; 36 Status DeleteBatch(const std::vector<std::string> &unifiedKeys) override; 37 Status Sync(const std::vector<std::string> &devices) override; 38 Status Sync(const std::vector<std::string> &devices, ProcessCallback callback) override; 39 Status Clear() override; 40 Status GetBatchData(const std::string &dataPrefix, std::vector<UnifiedData> &unifiedDataSet) override; 41 Status PutLocal(const std::string &key, const std::string &value) override; 42 Status GetLocal(const std::string &key, std::string &value) override; 43 Status DeleteLocal(const std::string &key) override; 44 Status PutRuntime(const std::string &key, const Runtime &runtime) override; 45 Status GetRuntime(const std::string &key, Runtime &runtime) override; 46 Status PutSummary(UnifiedKey &key, const Summary &summary) override; 47 bool Init() override; 48 49 private: 50 static constexpr const char *DATA_PREFIX = "udmf://"; 51 static constexpr std::int32_t SLASH_COUNT_IN_KEY = 4; 52 static constexpr std::int32_t MAX_BATCH_SIZE = 128; 53 std::shared_ptr<DistributedDB::KvStoreDelegateManager> delegateManager_ = nullptr; 54 std::shared_ptr<DistributedDB::KvStoreNbDelegate> kvStore_; 55 std::string storeId_; 56 void SetDelegateManager(const std::string &dataDir, const std::string &appId, const std::string &userId); 57 bool SaveMetaData(); 58 Status GetEntries(const std::string &dataPrefix, std::vector<DistributedDB::Entry> &entries); 59 Status PutEntries(const std::vector<DistributedDB::Entry> &entries); 60 Status DeleteEntries(const std::vector<DistributedDB::Key> &keys); 61 Status UnmarshalEntries( 62 const std::string &key, std::vector<DistributedDB::Entry> &entries, UnifiedData &unifiedData); 63 bool BuildMetaDataParam(DistributedData::StoreMetaData &metaData); 64 void NotifySyncProcss(const DevSyncProcessMap &processMap, ProcessCallback callback, 65 const DevNameMap &deviceNameMap); 66 Status PutSummary(const UnifiedData &data, std::vector<DistributedDB::Entry> &entries); 67 Status MarkWhenCorrupted(DistributedDB::DBStatus status); 68 void ReleaseStore(DistributedDB::KvStoreNbDelegate *delegate); 69 70 bool isCorrupted_ = false; 71 }; 72 } // namespace UDMF 73 } // namespace OHOS 74 #endif // UDMF_RUNTIMESTORE_H 75