1 /*
2 * Copyright (c) 2025 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 "virtual_sqlite_storage_engine.h"
17
18 namespace DistributedDB {
ForkNewExecutorMethod(const NewExecutorMethod & method)19 void VirtualSingleVerStorageEngine::ForkNewExecutorMethod(const NewExecutorMethod &method)
20 {
21 std::lock_guard<std::mutex> autoLock(functionMutex_);
22 forkNewFunc_ = method;
23 }
24
ForkOpenMainDatabaseMethod(const OpenMainDatabaseMethod & method)25 void VirtualSingleVerStorageEngine::ForkOpenMainDatabaseMethod(const OpenMainDatabaseMethod &method)
26 {
27 std::lock_guard<std::mutex> autoLock(functionMutex_);
28 forkOpenMainFunc_ = method;
29 }
30
SetMaxNum(int maxRead,int maxWrite)31 void VirtualSingleVerStorageEngine::SetMaxNum(int maxRead, int maxWrite)
32 {
33 engineAttr_.maxReadNum = maxRead;
34 engineAttr_.maxWriteNum = maxWrite;
35 }
36
SetEnhance(bool isEnhance)37 void VirtualSingleVerStorageEngine::SetEnhance(bool isEnhance)
38 {
39 isEnhance_ = isEnhance;
40 }
41
CallSetSQL(const std::vector<std::string> & sql)42 void VirtualSingleVerStorageEngine::CallSetSQL(const std::vector<std::string> &sql)
43 {
44 SetSQL(sql);
45 }
46
GetCacheHandle(OpenDbProperties & option)47 std::pair<int, sqlite3 *> VirtualSingleVerStorageEngine::GetCacheHandle(OpenDbProperties &option)
48 {
49 std::pair<int, sqlite3 *> res;
50 auto &[errCode, db] = res;
51 errCode = SQLiteSingleVerStorageEngine::GetCacheDbHandle(db, option);
52 return res;
53 }
54
CreateNewExecutor(bool isWrite,StorageExecutor * & handle)55 int VirtualSingleVerStorageEngine::CreateNewExecutor(bool isWrite, StorageExecutor *&handle)
56 {
57 {
58 std::lock_guard<std::mutex> autoLock(functionMutex_);
59 if (forkNewFunc_ != nullptr) {
60 return forkNewFunc_(isWrite, handle);
61 }
62 }
63 return SQLiteSingleVerStorageEngine::CreateNewExecutor(isWrite, handle);
64 }
65
TryToOpenMainDatabase(bool isWrite,sqlite3 * & db,OpenDbProperties & option)66 int VirtualSingleVerStorageEngine::TryToOpenMainDatabase(bool isWrite, sqlite3 *&db, OpenDbProperties &option)
67 {
68 {
69 std::lock_guard<std::mutex> autoLock(functionMutex_);
70 if (forkOpenMainFunc_ != nullptr) {
71 return forkOpenMainFunc_(isWrite, db, option);
72 }
73 }
74 return SQLiteSingleVerStorageEngine::TryToOpenMainDatabase(isWrite, db, option);
75 }
76 }