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 "sqlite_storage_engine.h"
17
18 #include "db_common.h"
19 #include "db_errno.h"
20 #include "log_print.h"
21 #include "sqlite_storage_executor.h"
22 #include "sqlite_utils.h"
23 #include "runtime_context.h"
24
25 namespace DistributedDB {
SQLiteStorageEngine()26 SQLiteStorageEngine::SQLiteStorageEngine()
27 {}
28
~SQLiteStorageEngine()29 SQLiteStorageEngine::~SQLiteStorageEngine()
30 {}
31
InitSQLiteStorageEngine(const StorageEngineAttr & poolSize,const OpenDbProperties & option,const std::string & identifier)32 int SQLiteStorageEngine::InitSQLiteStorageEngine(const StorageEngineAttr &poolSize, const OpenDbProperties &option,
33 const std::string &identifier)
34 {
35 if (StorageEngine::CheckEngineAttr(poolSize)) {
36 LOGE("Invalid storage engine attributes!");
37 return -E_INVALID_ARGS;
38 }
39 engineAttr_ = poolSize;
40 option_ = option;
41 identifier_ = identifier;
42 hashIdentifier_ = DBCommon::TransferStringToHex(identifier_);
43 if (GetEngineState() == EngineState::CACHEDB) {
44 LOGI("Is alive! not need to create executor, only fix option.");
45 return E_OK;
46 }
47 int errCode = Init();
48 if (errCode != E_OK) {
49 LOGI("Storage engine init fail! errCode = [%d]", errCode);
50 }
51 return errCode;
52 }
53
NewSQLiteStorageExecutor(sqlite3 * dbHandle,bool isWrite,bool isMemDb)54 StorageExecutor *SQLiteStorageEngine::NewSQLiteStorageExecutor(sqlite3 *dbHandle, bool isWrite, bool isMemDb)
55 {
56 return new (std::nothrow) SQLiteStorageExecutor(dbHandle, isWrite, isMemDb);
57 }
58
Upgrade(sqlite3 * db)59 int SQLiteStorageEngine::Upgrade(sqlite3 *db)
60 {
61 // SQLiteSingleVerStorageEngine override this function to do table structure and even content upgrade
62 // SQLiteLocalStorageEngine is used by SQLiteLocalKvDB, and SQLiteLocalKvDB is used as LocalStore, CommitStorage,
63 // ValueSliceStorage, MetaDataStorage, all of them will not change the table structure,
64 // so the version of these dbFile only represent for the content version.
65 // SQLiteLocalStorageEngine do not override this function so content upgrade can only be done by the Storage
66 // who use the SQLiteLocalKvDB.
67 // MultiVerStorageEngine do not inherit SQLiteStorageEngine.
68 (void)db;
69 return E_OK;
70 }
71
CreateNewExecutor(bool isWrite,StorageExecutor * & handle)72 int SQLiteStorageEngine::CreateNewExecutor(bool isWrite, StorageExecutor *&handle)
73 {
74 sqlite3 *dbHandle = nullptr;
75 int errCode = SQLiteUtils::OpenDatabase(option_, dbHandle);
76 if (errCode != E_OK) { // LCOV_EXCL_BR_LINE
77 return errCode;
78 }
79 bool isMemDb = option_.isMemDb;
80 if (!isUpdated_) { // LCOV_EXCL_BR_LINE
81 errCode = Upgrade(dbHandle);
82 if (errCode != E_OK) { // LCOV_EXCL_BR_LINE
83 (void)sqlite3_close_v2(dbHandle);
84 dbHandle = nullptr;
85 return errCode;
86 }
87 SQLiteUtils::ExecuteCheckPoint(dbHandle);
88 isUpdated_ = true;
89 }
90
91 handle = NewSQLiteStorageExecutor(dbHandle, isWrite, isMemDb);
92 if (handle == nullptr) { // LCOV_EXCL_BR_LINE
93 LOGE("New SQLiteStorageExecutor[%d] for the pool failed.", isWrite);
94 (void)sqlite3_close_v2(dbHandle);
95 dbHandle = nullptr;
96 return -E_OUT_OF_MEMORY;
97 }
98 return E_OK;
99 }
100
ClearEnginePasswd()101 void SQLiteStorageEngine::ClearEnginePasswd()
102 {
103 option_.passwd.Clear();
104 }
105
CheckEngineOption(const KvDBProperties & kvDBProp) const106 int SQLiteStorageEngine::CheckEngineOption(const KvDBProperties &kvDBProp) const
107 {
108 SecurityOption securityOpt;
109 if (RuntimeContext::GetInstance()->IsProcessSystemApiAdapterValid()) {
110 securityOpt.securityLabel = kvDBProp.GetSecLabel();
111 securityOpt.securityFlag = kvDBProp.GetSecFlag();
112 }
113
114 int conflictResolvePolicy = kvDBProp.GetIntProp(KvDBProperties::CONFLICT_RESOLVE_POLICY, DEFAULT_LAST_WIN);
115 bool createDirByStoreIdOnly = kvDBProp.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false);
116
117 if (kvDBProp.GetSchemaConstRef().IsSchemaValid() == option_.schema.empty()) {
118 // If both true, newSchema not empty, oriEngineSchema empty, not same
119 // If both false, newSchema empty, oriEngineSchema not empty, not same
120 LOGE("Engine and kvdb schema only one not empty! kvdb schema is [%d]", option_.schema.empty());
121 return -E_SCHEMA_MISMATCH;
122 }
123 // Here both schema empty or not empty, be the same
124 if (kvDBProp.GetSchemaConstRef().IsSchemaValid()) {
125 int errCode = kvDBProp.GetSchemaConstRef().CompareAgainstSchemaString(option_.schema);
126 if (errCode != -E_SCHEMA_EQUAL_EXACTLY) {
127 LOGE("Engine and kvdb schema mismatch!");
128 return -E_SCHEMA_MISMATCH;
129 }
130 }
131
132 bool isMemDb = kvDBProp.GetBoolProp(KvDBProperties::MEMORY_MODE, false);
133 // Here both schema empty or "not empty and equal exactly", this is ok as required
134 if (isMemDb == false &&
135 option_.createDirByStoreIdOnly == createDirByStoreIdOnly &&
136 option_.securityOpt == securityOpt &&
137 option_.conflictReslovePolicy == conflictResolvePolicy) {
138 return E_OK;
139 }
140 return -E_INVALID_ARGS;
141 }
142 }
143