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 using ProcessCallback = std::function<void(AsyncProcessInfo &syncInfo)>;
31 class Store {
32 public:
33     using Time = std::chrono::steady_clock::time_point;
34     virtual Status Put(const UnifiedData &unifiedData) = 0;
35     virtual Status Get(const std::string &key, UnifiedData &unifiedData) = 0;
36     virtual Status GetSummary(const std::string &key, Summary &summary) = 0;
37     virtual Status Update(const UnifiedData &unifiedData) = 0;
38     virtual Status Delete(const std::string &key) = 0;
39     virtual Status DeleteBatch(const std::vector<std::string> &unifiedKeys) = 0;
40     virtual Status Sync(const std::vector<std::string> &devices) = 0;
41     virtual Status Sync(const std::vector<std::string> &devices, ProcessCallback callback) = 0;
42     virtual Status Clear() = 0;
43     virtual Status GetBatchData(const std::string &dataPrefix, std::vector<UnifiedData> &unifiedDataSet) = 0;
44     virtual Status PutLocal(const std::string &key, const std::string &value) = 0;
45     virtual Status GetLocal(const std::string &key, std::string &value) = 0;
46     virtual Status DeleteLocal(const std::string &key) = 0;
47     virtual bool Init() = 0;
48     virtual void Close() = 0;
49 
operator <(const Time &time) const50     bool operator<(const Time &time) const
51     {
52         std::shared_lock<decltype(timeMutex_)> lock(timeMutex_);
53         return time_ < time;
54     }
55 
UpdateTime()56     void UpdateTime()
57     {
58         std::unique_lock<std::shared_mutex> lock(timeMutex_);
59         time_ = std::chrono::steady_clock::now() + std::chrono::minutes(INTERVAL);
60     }
61 private:
62     static constexpr int64_t INTERVAL = 1; // 1 min
63     mutable Time time_;
64     mutable std::shared_mutex timeMutex_;
65 };
66 } // namespace UDMF
67 } // namespace OHOS
68 #endif // UDMF_STORE_H
69