• 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 "sqlite_storage_engine.h"
17 
18 #include "db_errno.h"
19 #include "log_print.h"
20 #include "sqlite_storage_executor.h"
21 #include "sqlite_utils.h"
22 #include "runtime_context.h"
23 
24 namespace DistributedDB {
SQLiteStorageEngine()25 SQLiteStorageEngine::SQLiteStorageEngine()
26 {}
27 
~SQLiteStorageEngine()28 SQLiteStorageEngine::~SQLiteStorageEngine()
29 {}
30 
InitSQLiteStorageEngine(const StorageEngineAttr & poolSize,const OpenDbProperties & option,const std::string & identifier)31 int SQLiteStorageEngine::InitSQLiteStorageEngine(const StorageEngineAttr &poolSize, const OpenDbProperties &option,
32     const std::string &identifier)
33 {
34     if (StorageEngine::CheckEngineAttr(poolSize)) {
35         LOGE("Invalid storage engine attributes!");
36         return -E_INVALID_ARGS;
37     }
38     engineAttr_ = poolSize;
39     option_ = option;
40     identifier_ = identifier;
41     if (GetEngineState() == EngineState::CACHEDB) {
42         LOGI("Is alive! not need to create executor, only fix option.");
43         return E_OK;
44     }
45     int errCode = Init();
46     if (errCode != E_OK) {
47         LOGI("Storage engine init fail! errCode = [%d]", errCode);
48     }
49     return errCode;
50 }
51 
NewSQLiteStorageExecutor(sqlite3 * dbHandle,bool isWrite,bool isMemDb)52 StorageExecutor *SQLiteStorageEngine::NewSQLiteStorageExecutor(sqlite3 *dbHandle, bool isWrite, bool isMemDb)
53 {
54     return new (std::nothrow) SQLiteStorageExecutor(dbHandle, isWrite, isMemDb);
55 }
56 
Upgrade(sqlite3 * db)57 int SQLiteStorageEngine::Upgrade(sqlite3 *db)
58 {
59     // SQLiteSingleVerStorageEngine override this function to do table structure and even content upgrade
60     // SQLiteLocalStorageEngine is used by SQLiteLocalKvDB, and SQLiteLocalKvDB is used as LocalStore, CommitStorage,
61     //      ValueSliceStorage, MetaDataStorage, all of them will not change the table structure,
62     //      so the version of these dbFile only represent for the content version.
63     // SQLiteLocalStorageEngine do not override this function so content upgrade can only be done by the Storage
64     //      who use the SQLiteLocalKvDB.
65     // MultiVerStorageEngine do not inherit SQLiteStorageEngine.
66     (void)db;
67     return E_OK;
68 }
69 
CreateNewExecutor(bool isWrite,StorageExecutor * & handle)70 int SQLiteStorageEngine::CreateNewExecutor(bool isWrite, StorageExecutor *&handle)
71 {
72     sqlite3 *dbHandle = nullptr;
73     int errCode = SQLiteUtils::OpenDatabase(option_, dbHandle);
74     if (errCode != E_OK) {
75         return errCode;
76     }
77     bool isMemDb = option_.isMemDb;
78     if (!isUpdated_) {
79         errCode = Upgrade(dbHandle);
80         if (errCode != E_OK) {
81             (void)sqlite3_close_v2(dbHandle);
82             dbHandle = nullptr;
83             return errCode;
84         }
85         SQLiteUtils::ExecuteCheckPoint(dbHandle);
86         isUpdated_ = true;
87     }
88 
89     handle = NewSQLiteStorageExecutor(dbHandle, isWrite, isMemDb);
90     if (handle == nullptr) {
91         LOGE("New SQLiteStorageExecutor[%d] for the pool failed.", isWrite);
92         (void)sqlite3_close_v2(dbHandle);
93         dbHandle = nullptr;
94         return -E_OUT_OF_MEMORY;
95     }
96     return E_OK;
97 }
98 
ReInit()99 int SQLiteStorageEngine::ReInit()
100 {
101     return E_OK;
102 }
103 
IsNeedTobeReleased() const104 bool SQLiteStorageEngine::IsNeedTobeReleased() const
105 {
106     EngineState engineState = GetEngineState();
107     return ((engineState == EngineState::MAINDB) || (engineState == EngineState::INVALID));
108 }
109 
GetIdentifier() const110 const std::string &SQLiteStorageEngine::GetIdentifier() const
111 {
112     return identifier_;
113 }
114 
GetEngineState() const115 EngineState SQLiteStorageEngine::GetEngineState() const
116 {
117     return engineState_;
118 }
119 
SetEngineState(EngineState state)120 void SQLiteStorageEngine::SetEngineState(EngineState state)
121 {
122     LOGD("[SQLiteStorageEngine::SetEngineState] Engine State : [%d]", state);
123     engineState_ = state; // Current usage logically can guarantee no concurrency
124 }
125 
ExecuteMigrate()126 int SQLiteStorageEngine::ExecuteMigrate()
127 {
128     return -E_NOT_SUPPORT;
129 }
130 
IncreaseCacheRecordVersion()131 void SQLiteStorageEngine::IncreaseCacheRecordVersion()
132 {
133     return;
134 }
135 
GetCacheRecordVersion() const136 uint64_t SQLiteStorageEngine::GetCacheRecordVersion() const
137 {
138     return 0;
139 }
140 
GetAndIncreaseCacheRecordVersion()141 uint64_t SQLiteStorageEngine::GetAndIncreaseCacheRecordVersion()
142 {
143     return 0;
144 }
145 
IsEngineCorrupted() const146 bool SQLiteStorageEngine::IsEngineCorrupted() const
147 {
148     return false;
149 }
150 
ClearEnginePasswd()151 void SQLiteStorageEngine::ClearEnginePasswd()
152 {
153     option_.passwd.Clear();
154 }
155 
CheckEngineOption(const KvDBProperties & kvDBProp) const156 int SQLiteStorageEngine::CheckEngineOption(const KvDBProperties &kvDBProp) const
157 {
158     SecurityOption securityOpt;
159     if (RuntimeContext::GetInstance()->IsProcessSystemApiAdapterValid()) {
160         securityOpt.securityLabel = kvDBProp.GetSecLabel();
161         securityOpt.securityFlag = kvDBProp.GetSecFlag();
162     }
163 
164     int conflictResolvePolicy = kvDBProp.GetIntProp(KvDBProperties::CONFLICT_RESOLVE_POLICY, DEFAULT_LAST_WIN);
165     bool createDirByStoreIdOnly = kvDBProp.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false);
166 
167     if (kvDBProp.GetSchemaConstRef().IsSchemaValid() == option_.schema.empty()) {
168         // If both true, newSchema not empty, oriEngineSchema empty, not same
169         // If both false, newSchema empty, oriEngineSchema not empty, not same
170         LOGE("Engine and kvdb schema only one not empty! kvdb schema is [%d]", option_.schema.empty());
171         return -E_SCHEMA_MISMATCH;
172     }
173     // Here both schema empty or not empty, be the same
174     if (kvDBProp.GetSchemaConstRef().IsSchemaValid()) {
175         int errCode = kvDBProp.GetSchemaConstRef().CompareAgainstSchemaString(option_.schema);
176         if (errCode != -E_SCHEMA_EQUAL_EXACTLY) {
177             LOGE("Engine and kvdb schema mismatch!");
178             return -E_SCHEMA_MISMATCH;
179         }
180     }
181 
182     bool isMemDb = kvDBProp.GetBoolProp(KvDBProperties::MEMORY_MODE, false);
183     // Here both schema empty or "not empty and equal exactly", this is ok as required
184     if (isMemDb == false &&
185         option_.createDirByStoreIdOnly == createDirByStoreIdOnly &&
186         option_.securityOpt == securityOpt &&
187         option_.conflictReslovePolicy == conflictResolvePolicy) {
188         return E_OK;
189     }
190     return -E_INVALID_ARGS;
191 }
192 }
193