• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sqlite_meta_executor.h"
17 
18 #include "db_common.h"
19 #include "db_constant.h"
20 namespace DistributedDB {
GetMetaKeysByKeyPrefix(const std::string & keyPre,sqlite3 * dbHandle,MetaMode metaMode,bool isMemDb,std::set<std::string> & outKeys)21 int SqliteMetaExecutor::GetMetaKeysByKeyPrefix(const std::string &keyPre, sqlite3 *dbHandle, MetaMode metaMode,
22     bool isMemDb, std::set<std::string> &outKeys)
23 {
24     sqlite3_stmt *statement = nullptr;
25     std::string sqlStr;
26     switch (metaMode) {
27         case MetaMode::KV:
28             sqlStr = SELECT_META_KEYS_BY_PREFIX;
29             break;
30         case MetaMode::KV_ATTACH:
31             sqlStr = SELECT_ATTACH_META_KEYS_BY_PREFIX;
32             break;
33         case MetaMode::RDB:
34             sqlStr = SELECT_RDB_META_KEYS_BY_PREFIX;
35             break;
36         default:
37             return -E_INVALID_ARGS;
38     }
39     int errCode = SQLiteUtils::GetStatement(dbHandle, sqlStr, statement);
40     if (errCode != E_OK) {
41         LOGE("[SqliteMetaExecutor] Get statement failed:%d", errCode);
42         return errCode;
43     }
44 
45     Key keyPrefix;
46     DBCommon::StringToVector(keyPre + '%', keyPrefix);
47     int ret = E_OK;
48     errCode = SQLiteUtils::BindBlobToStatement(statement, 1, keyPrefix); // 1: bind index for prefix key
49     if (errCode != E_OK) {
50         LOGE("[SqliteMetaExecutor] Bind statement failed:%d", errCode);
51         SQLiteUtils::ResetStatement(statement, true, ret);
52         return errCode;
53     }
54 
55     std::vector<Key> keys;
56     errCode = GetAllKeys(statement, isMemDb, keys);
57     SQLiteUtils::ResetStatement(statement, true, ret);
58     for (const auto &it : keys) {
59         if (it.size() >= keyPre.size() + DBConstant::HASH_KEY_SIZE) {
60             outKeys.insert({it.begin() + keyPre.size(), it.begin() + keyPre.size() + DBConstant::HASH_KEY_SIZE});
61         } else {
62             LOGW("[SqliteMetaExecutor] Get invalid key, size=%zu", it.size());
63         }
64     }
65     return errCode != E_OK ? errCode : ret;
66 }
67 
GetAllKeys(sqlite3_stmt * statement,bool isMemDb,std::vector<Key> & keys)68 int SqliteMetaExecutor::GetAllKeys(sqlite3_stmt *statement, bool isMemDb, std::vector<Key> &keys)
69 {
70     if (statement == nullptr) {
71         return -E_INVALID_DB;
72     }
73     int errCode;
74     do {
75         errCode = SQLiteUtils::StepWithRetry(statement, isMemDb);
76         if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) {
77             Key key;
78             errCode = SQLiteUtils::GetColumnBlobValue(statement, 0, key);
79             if (errCode != E_OK) {
80                 break;
81             }
82 
83             keys.push_back(std::move(key));
84         } else if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) {
85             errCode = E_OK;
86             break;
87         } else {
88             LOGE("SQLite step for getting all keys failed:%d", errCode);
89             break;
90         }
91     } while (true);
92     return errCode;
93 }
94 
GetExistsDevicesFromMeta(sqlite3 * dbHandle,MetaMode metaMode,bool isMemDb,std::set<std::string> & devices)95 int SqliteMetaExecutor::GetExistsDevicesFromMeta(sqlite3 *dbHandle, MetaMode metaMode,
96     bool isMemDb, std::set<std::string> &devices)
97 {
98     int errCode = GetMetaKeysByKeyPrefix(DBConstant::DEVICEID_PREFIX_KEY, dbHandle, metaMode, isMemDb, devices);
99     if (errCode != E_OK) {
100         LOGE("Get meta data key failed. err=%d", errCode);
101         return errCode;
102     }
103     errCode = GetMetaKeysByKeyPrefix(DBConstant::QUERY_SYNC_PREFIX_KEY, dbHandle, metaMode, isMemDb, devices);
104     if (errCode != E_OK) {
105         LOGE("Get meta data key failed. err=%d", errCode);
106         return errCode;
107     }
108     errCode = GetMetaKeysByKeyPrefix(DBConstant::DELETE_SYNC_PREFIX_KEY, dbHandle, metaMode, isMemDb, devices);
109     if (errCode != E_OK) {
110         LOGE("Get meta data key failed. err=%d", errCode);
111     }
112     return errCode;
113 }
114 
GetMetaDataByPrefixKey(sqlite3 * dbHandle,bool isMemDb,const std::string & metaTableName,const Key & keyPrefix,std::map<Key,Value> & data)115 int SqliteMetaExecutor::GetMetaDataByPrefixKey(sqlite3 *dbHandle, bool isMemDb, const std::string &metaTableName,
116     const Key &keyPrefix, std::map<Key, Value> &data)
117 {
118     std::string sql = "SELECT key,value FROM " + metaTableName + " WHERE key >= ? AND key <= ?;";
119     sqlite3_stmt *statement = nullptr;
120     int errCode = SQLiteUtils::GetStatement(dbHandle, sql, statement);
121     if (errCode != E_OK) {
122         LOGE("[SqliteMetaExecutor][GetMetaDataByPrefixKey] Get statement failed:%d", errCode);
123         return errCode;
124     }
125 
126     errCode = SQLiteUtils::BindPrefixKey(statement, 1, keyPrefix); // 1 is first arg.
127     if (errCode != E_OK) {
128         LOGE("[SqliteMetaExecutor][GetMetaDataByPrefixKey] Bind prefix key failed:%d", errCode);
129         return SQLiteUtils::ProcessStatementErrCode(statement, true, errCode);
130     }
131     do {
132         errCode = SQLiteUtils::StepWithRetry(statement, isMemDb);
133         if (errCode != SQLiteUtils::MapSQLiteErrno(SQLITE_ROW)) {
134             break;
135         }
136         Key key;
137         errCode = SQLiteUtils::GetColumnBlobValue(statement, 0, key); // 0 is key.
138         if (errCode != E_OK) {
139             LOGE("[SqliteMetaExecutor][GetMetaDataByPrefixKey] Get key failed:%d", errCode);
140             return SQLiteUtils::ProcessStatementErrCode(statement, true, errCode);
141         }
142         Value value;
143         errCode = SQLiteUtils::GetColumnBlobValue(statement, 1, value); // 1 is value.
144         if (errCode != E_OK) {
145             LOGE("[SqliteMetaExecutor][GetMetaDataByPrefixKey] Get value failed:%d", errCode);
146             return SQLiteUtils::ProcessStatementErrCode(statement, true, errCode);
147         }
148         data[key] = value;
149     } while (errCode == E_OK);
150     if (errCode == SQLiteUtils::MapSQLiteErrno(SQLITE_DONE)) {
151         errCode = E_OK;
152     }
153     return SQLiteUtils::ProcessStatementErrCode(statement, true, errCode);
154 }
155 }