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 OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H
17 #define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H
18 #include <functional>
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <variant>
23 #include <vector>
24
25 #include "error/general_error.h"
26 #include "traits.h"
27 namespace OHOS::DistributedData {
28 enum GenProgress {
29 SYNC_BEGIN,
30 SYNC_IN_PROGRESS,
31 SYNC_FINISH,
32 };
33
34 struct GenStatistic {
35 uint32_t total;
36 uint32_t success;
37 uint32_t failed;
38 uint32_t untreated;
39 };
40
41 struct GenTableDetail {
42 GenStatistic upload;
43 GenStatistic download;
44 };
45
46 struct GenProgressDetail {
47 int32_t progress;
48 int32_t code;
49 std::map<std::string, GenTableDetail> details;
50 };
51
52 struct Asset {
53 enum Status : int32_t {
54 STATUS_UNKNOWN,
55 STATUS_NORMAL,
56 STATUS_INSERT,
57 STATUS_UPDATE,
58 STATUS_DELETE,
59 STATUS_ABNORMAL,
60 STATUS_DOWNLOADING,
61 STATUS_BUTT
62 };
63
64 static constexpr uint64_t NO_EXPIRES_TIME = 0;
65 uint32_t version = 0;
66 uint32_t status = STATUS_UNKNOWN;
67 uint64_t expiresTime = NO_EXPIRES_TIME;
68 std::string id;
69 std::string name;
70 std::string uri;
71 std::string createTime;
72 std::string modifyTime;
73 std::string size;
74 std::string hash;
75 std::string path;
76 };
77
78 struct GenQuery {
79 virtual ~GenQuery() = default;
80 virtual bool IsEqual(uint64_t tid) = 0;
81 virtual std::vector<std::string> GetTables() = 0;
82
83 template<typename T>
QueryInterfaceGenQuery84 int32_t QueryInterface(T *&query)
85 {
86 if (!IsEqual(T::TYPE_ID)) {
87 return E_INVALID_ARGS;
88 }
89 query = static_cast<T *>(this);
90 return E_OK;
91 };
92 };
93
94 using Assets = std::vector<Asset>;
95 using Bytes = std::vector<uint8_t>;
96 using Value = std::variant<std::monostate, int64_t, double, std::string, bool, Bytes, Asset, Assets>;
97 using Values = std::vector<Value>;
98 using VBucket = std::map<std::string, Value>;
99 using VBuckets = std::vector<VBucket>;
100 using GenDetails = std::map<std::string, GenProgressDetail>;
101 using GenAsync = std::function<void(const GenDetails &details)>;
102 template<typename T>
103 inline constexpr size_t TYPE_INDEX = Traits::variant_index_of_v<T, Value>;
104
105 inline constexpr size_t TYPE_MAX = Traits::variant_size_of_v<Value>;
106
107 template<typename T, typename O>
GetItem(T && input,O & output)108 bool GetItem(T &&input, O &output)
109 {
110 return false;
111 }
112
113 template<typename T, typename O, typename First, typename... Rest>
GetItem(T && input,O & output)114 bool GetItem(T &&input, O &output)
115 {
116 auto val = Traits::get_if<First>(&input);
117 if (val != nullptr) {
118 output = std::move(*val);
119 return true;
120 }
121 return GetItem<T, O, Rest...>(std::move(input), output);
122 }
123
124 template<typename T, typename... Types>
Convert(T && input,std::variant<Types...> & output)125 bool Convert(T &&input, std::variant<Types...> &output)
126 {
127 return GetItem<T, decltype(output), Types...>(std::move(input), output);
128 }
129 } // namespace OHOS::DistributedData
130 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_STORE_GENERAL_VALUE_H
131