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_STORE_H 17 #define UDMF_STORE_H 18 19 #include <string> 20 #include <shared_mutex> 21 #include <mutex> 22 23 #include "error_code.h" 24 #include "unified_data.h" 25 #include "unified_key.h" 26 #include "unified_types.h" 27 28 namespace OHOS { 29 namespace UDMF { 30 class Store { 31 public: 32 using Time = std::chrono::steady_clock::time_point; 33 virtual Status Put(const UnifiedData &unifiedData) = 0; 34 virtual Status Get(const std::string &key, UnifiedData &unifiedData) = 0; 35 virtual Status GetSummary(const std::string &key, Summary &summary) = 0; 36 virtual Status Update(const UnifiedData &unifiedData) = 0; 37 virtual Status Delete(const std::string &key) = 0; 38 virtual Status DeleteBatch(const std::vector<std::string> &unifiedKeys) = 0; 39 virtual Status Sync(const std::vector<std::string> &devices) = 0; 40 virtual Status Clear() = 0; 41 virtual bool Init() = 0; 42 virtual void Close() = 0; 43 virtual Status GetBatchData(const std::string &dataPrefix, std::vector<UnifiedData> &unifiedDataSet) = 0; 44 45 bool operator<(const Time &time) const 46 { 47 std::shared_lock<decltype(timeMutex_)> lock(timeMutex_); 48 return time_ < time; 49 } 50 UpdateTime()51 void UpdateTime() 52 { 53 std::unique_lock<std::shared_mutex> lock(timeMutex_); 54 time_ = std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL); 55 } 56 private: 57 static constexpr int64_t INTERVAL = 1; // 1 min 58 mutable Time time_; 59 mutable std::shared_mutex timeMutex_; 60 }; 61 } // namespace UDMF 62 } // namespace OHOS 63 #endif // UDMF_STORE_H 64