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 "single_ver_natural_store.h"
17 
18 #include "db_common.h"
19 #include "db_errno.h"
20 #include "kvdb_utils.h"
21 #include "log_print.h"
22 #include "sqlite_single_ver_storage_engine.h"
23 #include "storage_engine_manager.h"
24 
25 namespace DistributedDB {
SingleVerNaturalStore()26 SingleVerNaturalStore::SingleVerNaturalStore()
27 {
28 }
29 
~SingleVerNaturalStore()30 SingleVerNaturalStore::~SingleVerNaturalStore()
31 {
32 }
33 
EnableHandle()34 void SingleVerNaturalStore::EnableHandle()
35 {
36     return;
37 }
38 
AbortHandle()39 void SingleVerNaturalStore::AbortHandle()
40 {
41     return;
42 }
43 
RemoveKvDB(const KvDBProperties & properties)44 int SingleVerNaturalStore::RemoveKvDB(const KvDBProperties &properties)
45 {
46     // To avoid leakage, the engine resources are forced to be released
47     const std::string identifier = properties.GetStringProp(KvDBProperties::IDENTIFIER_DATA, "");
48     (void)StorageEngineManager::ForceReleaseStorageEngine(identifier);
49 
50     // Only care the data directory and the db name.
51     std::string storeOnlyDir;
52     std::string storeDir;
53     GenericKvDB::GetStoreDirectory(properties, KvDBProperties::SINGLE_VER_TYPE_SQLITE, storeDir, storeOnlyDir);
54 
55     const std::vector<std::pair<const std::string &, const std::string &>> dbDir {
56         { DBConstant::MAINDB_DIR, DBConstant::SINGLE_VER_DATA_STORE },
57         { DBConstant::METADB_DIR, DBConstant::SINGLE_VER_META_STORE },
58         { DBConstant::CACHEDB_DIR, DBConstant::SINGLE_VER_CACHE_STORE }
59     };
60 
61     bool isAllNotFound = true;
62     for (const auto &item : dbDir) {
63         std::string currentDir = storeDir + item.first + "/";
64         std::string currentOnlyDir = storeOnlyDir + item.first + "/";
65         int errCode = KvDBUtils::RemoveKvDB(currentDir, currentOnlyDir, item.second);
66         if (errCode != -E_NOT_FOUND) {
67             if (errCode != E_OK) {
68                 return errCode;
69             }
70             isAllNotFound = false;
71         }
72     };
73     if (isAllNotFound) {
74         return -E_NOT_FOUND;
75     }
76 
77     int errCode = DBCommon::RemoveAllFilesOfDirectory(storeDir, true);
78     if (errCode != E_OK) {
79         return errCode;
80     }
81     return DBCommon::RemoveAllFilesOfDirectory(storeOnlyDir, true);
82 }
83 
GetFuncType(int index,const TransPair * transMap,int32_t len)84 RegisterFuncType SingleVerNaturalStore::GetFuncType(int index, const TransPair *transMap, int32_t len)
85 {
86     int32_t head = 0;
87     int32_t end = len - 1;
88     while (head <= end) {
89         int32_t mid = head + (end - head) / 2;
90         if (transMap[mid].index < index) {
91             head = mid + 1;
92             continue;
93         }
94         if (transMap[mid].index > index) {
95             end = mid - 1;
96             continue;
97         }
98         return transMap[mid].funcType;
99     }
100     return RegisterFuncType::REGISTER_FUNC_TYPE_MAX;
101 }
102 
CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData * & committedData,bool isNeedCommit,int eventType)103 void SingleVerNaturalStore::CommitAndReleaseNotifyData(SingleVerNaturalStoreCommitNotifyData *&committedData,
104     bool isNeedCommit, int eventType)
105 {
106     if (isNeedCommit) {
107         if (committedData != nullptr) {
108             if (!committedData->IsChangedDataEmpty()) {
109                 CommitNotify(eventType, committedData);
110             }
111             if (!committedData->IsConflictedDataEmpty()) {
112                 CommitNotify(static_cast<int>(SQLiteGeneralNSNotificationEventType::SQLITE_GENERAL_CONFLICT_EVENT),
113                     committedData);
114             }
115         }
116     }
117 
118     if (committedData != nullptr) {
119         committedData->DecObjRef(committedData);
120         committedData = nullptr;
121     }
122 }
123 
124 } // namespace DistributedDB