• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "constant.h"
17 #include <dirent.h>
18 #include <unistd.h>
19 #include <cerrno>
20 
21 namespace OHOS {
22 namespace DistributedKv {
23 // the Key Prefix for Meta data of KvStore.
24 const std::string KvStoreMetaRow::KEY_PREFIX = "KvStoreMetaData";
25 
26 const std::string SecretMetaRow::KEY_PREFIX = "SecretKey";
27 
28 /* version for distributed kv data service. */
29 const std::string Constant::VERSION = "1";
30 
31 /* meta name for distributed kv data service. */
32 const std::string Constant::META_DIR_NAME = "Meta";
33 
34 /* name for distributed kv data service. */
35 const std::string Constant::SERVICE_NAME = "mdds";
36 
37 /* root path for distributed kv data service. */
38 const std::string Constant::ROOT_PATH = "/data/misc_de/0";
39 
40 /* root path for distributeddata service and system services. */
41 const std::string Constant::ROOT_PATH_DE = "/data/misc_de/0";
42 
43 /* root path for self-developed and non-self-developed app. */
44 const std::string Constant::ROOT_PATH_CE = "/data/misc_ce/0";
45 
46 // the max length for key is 1024.
47 const size_t Constant::MAX_KEY_LENGTH = 1024;
48 
49 // the max length for StoreId is 128.
50 const size_t Constant::MAX_STORE_ID_LENGTH = 128;
51 
52 // the max length for value is 4M.
53 const size_t Constant::MAX_VALUE_LENGTH = 4 * 1024 * 1024;
54 
55 // the max batch for putBatch is 128.
56 const size_t Constant::MAX_BATCH_SIZE = 128;
57 
58 // the max capacity for ipc is 800K.
59 const size_t Constant::MAX_IPC_CAPACITY = 800 * 1024;
60 
61 // the default mode is 0755, stands for r/w/x for user, r/x for group, r/x for others.
62 const mode_t Constant::DEFAULT_MODE = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
63 
64 // the mode for dir is 0755, r/w/x for user, r/-/x for group, r/-/x for others.
65 const mode_t Constant::DEFAULT_MODE_DIR = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
66 
67 // the mode for file is 0600, r/w/- for user, -/-/- for group, -/-/- for others.
68 const mode_t Constant::DEFAULT_MODE_FILE = S_IRUSR | S_IWUSR;
69 
70 // Size threshold of switching to large data is a little smaller than MAX_IPC_CAPACITY.
71 const int Constant::SWITCH_RAW_DATA_SIZE = 700 * 1024;
72 
73 const int Constant::MAX_OPEN_KVSTORES = 16;
74 
75 // default group id for synchronization.
76 const std::string Constant::DEFAULT_GROUP_ID = "default";
77 
78 // true indicates the ownership of distributed data is DEVICE, otherwise, ACCOUNT
79 const bool Constant::STOREID_ONLY_FLAG = true;
80 
81 // service meta db name.
82 const std::string Constant::SERVICE_META_DB_NAME = "service_meta";
83 
84 const std::string Constant::KEY_SEPARATOR = "###";
85 
86 const std::string Constant::ROOT_KEY_GENERATED = "RootKeyGenerated";
87 
GetKeyFor(const std::string & key)88 std::vector<uint8_t> KvStoreMetaRow::GetKeyFor(const std::string &key)
89 {
90     std::string str = Constant::Concatenate({KvStoreMetaRow::KEY_PREFIX, Constant::KEY_SEPARATOR, key });
91     return std::vector<uint8_t>(str.begin(), str.end());
92 }
93 
GetKeyFor(const std::string & key)94 std::vector<uint8_t> SecretMetaRow::GetKeyFor(const std::string &key)
95 {
96     std::string str = Constant::Concatenate({SecretMetaRow::KEY_PREFIX, Constant::KEY_SEPARATOR, key });
97     return std::vector<uint8_t>(str.begin(), str.end());
98 }
99 
Concatenate(std::initializer_list<std::string> stringList)100 std::string Constant::Concatenate(std::initializer_list<std::string> stringList)
101 {
102     std::string result;
103     size_t result_size = 0;
104     for (const std::string &str : stringList) {
105         result_size += str.size();
106     }
107     result.reserve(result_size);
108     for (const std::string &str : stringList) {
109         result.append(str.data(), str.size());
110     }
111     return result;
112 }
113 
GetDefaultDeviceAccountId()114 std::string Constant::GetDefaultDeviceAccountId()
115 {
116     return "0";
117 }
118 
GetDefaultHarmonyAccountName()119 std::string Constant::GetDefaultHarmonyAccountName()
120 {
121     return "default";
122 }
123 }  // namespace DistributedKv
124 }  // namespace OHOS
125