• 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 guarante no concurrency
124 }
125 
GetOpenOption() const126 const OpenDbProperties &SQLiteStorageEngine::GetOpenOption() const
127 {
128     return option_;
129 }
130 
IsNeedMigrate() const131 bool SQLiteStorageEngine::IsNeedMigrate() const
132 {
133     return false;
134 }
135 
ExecuteMigrate()136 int SQLiteStorageEngine::ExecuteMigrate()
137 {
138     return -E_NOT_SUPPORT;
139 }
140 
IncreaseCacheRecordVersion()141 void SQLiteStorageEngine::IncreaseCacheRecordVersion()
142 {
143     return;
144 }
145 
GetCacheRecordVersion() const146 uint64_t SQLiteStorageEngine::GetCacheRecordVersion() const
147 {
148     return 0;
149 }
150 
GetAndIncreaseCacheRecordVersion()151 uint64_t SQLiteStorageEngine::GetAndIncreaseCacheRecordVersion()
152 {
153     return 0;
154 }
155 
IsEngineCorrupted() const156 bool SQLiteStorageEngine::IsEngineCorrupted() const
157 {
158     return false;
159 }
160 
ClearEnginePasswd()161 void SQLiteStorageEngine::ClearEnginePasswd()
162 {
163     option_.passwd.Clear();
164 }
165 
CheckEngineOption(const KvDBProperties & kvDBProp) const166 int SQLiteStorageEngine::CheckEngineOption(const KvDBProperties &kvDBProp) const
167 {
168     SecurityOption securityOpt;
169     if (RuntimeContext::GetInstance()->IsProcessSystemApiAdapterValid()) {
170         securityOpt.securityLabel = kvDBProp.GetSecLabel();
171         securityOpt.securityFlag = kvDBProp.GetSecFlag();
172     }
173 
174     int conflictReslovePolicy = kvDBProp.GetIntProp(KvDBProperties::CONFLICT_RESOLVE_POLICY, DEFAULT_LAST_WIN);
175     bool createDirByStoreIdOnly = kvDBProp.GetBoolProp(KvDBProperties::CREATE_DIR_BY_STORE_ID_ONLY, false);
176 
177     if (kvDBProp.GetSchemaConstRef().IsSchemaValid() == option_.schema.empty()) {
178         // If both true, newSchema not empty, oriEngineSchema empty, not same
179         // If both false, newSchema empty, oriEngineSchema not empty, not same
180         LOGE("Engine and kvdb schema only one not empty! kvdb schema is [%d]", option_.schema.empty());
181         return -E_SCHEMA_MISMATCH;
182     }
183     // Here both schema empty or not empty, be the same
184     if (kvDBProp.GetSchemaConstRef().IsSchemaValid()) {
185         int errCode = kvDBProp.GetSchemaConstRef().CompareAgainstSchemaString(option_.schema);
186         if (errCode != -E_SCHEMA_EQUAL_EXACTLY) {
187             LOGE("Engine and kvdb schema mismatch!");
188             return -E_SCHEMA_MISMATCH;
189         }
190     }
191 
192     bool isMemDb = kvDBProp.GetBoolProp(KvDBProperties::MEMORY_MODE, false);
193     // Here both schema empty or "not empty and equal exactly", this is ok as required
194     if (isMemDb == false &&
195         option_.createDirByStoreIdOnly == createDirByStoreIdOnly &&
196         option_.securityOpt == securityOpt &&
197         option_.conflictReslovePolicy == conflictReslovePolicy) {
198         return E_OK;
199     }
200     return -E_INVALID_ARGS;
201 }
202 }
203