• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "store_manager.h"
17 
18 #include <cstdio>
19 #include <memory>
20 #include <string>
21 
22 #include "doc_db.h"
23 #include "doc_store.h"
24 #include "ejdb2.h"
25 #include "hilog/log.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002D10, "HiView-DOCDB"};
GetEjdbFlag(const Option & option)30 static iwkv_openflags GetEjdbFlag(const Option& option)
31 {
32     switch (option.flag) {
33         case Option::RDONLY:
34             return IWKV_RDONLY;
35         case Option::TRUNC:
36             return IWKV_TRUNC;
37         default:
38             return IWKV_NO_TRIM_ON_CLOSE;
39     }
40 }
41 
GetDocStore(const Option & option)42 std::shared_ptr<DocStore> StoreManager::GetDocStore(const Option& option)
43 {
44     std::lock_guard<std::mutex> lock(mutex_);
45     auto it = stores_.find(option.db);
46     if (it != stores_.end()) {
47         return it->second;
48     }
49 
50     std::shared_ptr<DocStore> docStore = std::make_shared<DocStore>();
51     docStore->dbPtr = std::make_shared<DocDB>();
52     int retCode = docStore->dbPtr->OpenDB(option.db, GetEjdbFlag(option));
53     if (retCode != 0) {
54         HiLog::Error(LABEL, "can not open doc store");
55     } else {
56         HiLog::Info(LABEL, "open doc store");
57     }
58     stores_[option.db] = docStore;
59     return docStore;
60 }
61 
OnlineBackupDocStore(const Option & option,const std::string & bakFile)62 int StoreManager::OnlineBackupDocStore(const Option& option, const std::string &bakFile)
63 {
64     std::lock_guard<std::mutex> lock(mutex_);
65     auto it = stores_.find(option.db);
66     if (it == stores_.end()) {
67         HiLog::Error(LABEL, "can not find db to backup");
68         return -1;
69     }
70 
71     std::shared_ptr<DocStore> docStore = it->second;
72     if (docStore->dbPtr == nullptr) {
73         HiLog::Error(LABEL, "do not init db ptr");
74         return -1;
75     }
76 
77     int retCode = docStore->dbPtr->OnlineBackupDB(bakFile);
78     if (retCode != 0) {
79         HiLog::Error(LABEL, "can not backup doc store");
80         return retCode;
81     }
82     return 0;
83 }
84 
InnerCloseDocStore(const Option & option)85 int StoreManager::InnerCloseDocStore(const Option& option)
86 {
87     auto it = stores_.find(option.db);
88     if (it == stores_.end()) {
89         HiLog::Error(LABEL, "can not find db to close");
90         return -1;
91     }
92 
93     std::shared_ptr<DocStore> docStore = it->second;
94     if (docStore->dbPtr == nullptr) {
95         stores_.erase(it);
96         return -1;
97     }
98     int retCode = docStore->dbPtr->CloseDB();
99     if (retCode != 0) {
100         HiLog::Error(LABEL, "can not close doc store");
101         return retCode;
102     }
103     stores_.erase(it);
104     return 0;
105 }
106 
CloseDocStore(const Option & option)107 int StoreManager::CloseDocStore(const Option& option)
108 {
109     std::lock_guard<std::mutex> lock(mutex_);
110     return InnerCloseDocStore(option);
111 }
112 
DeleteDocStore(const Option & option)113 int StoreManager::DeleteDocStore(const Option& option)
114 {
115     std::lock_guard<std::mutex> lock(mutex_);
116     int retCode = InnerCloseDocStore(option);
117     if (retCode != 0) {
118         if (retCode > 0) {
119             return retCode;
120         }
121         HiLog::Error(LABEL, "close doc store fail to delete");
122         return retCode;
123     }
124 
125     HiLog::Debug(LABEL, "delete ejdb file %{public}s", option.db.c_str());
126     retCode = std::remove(option.db.c_str());
127     if (retCode != 0) {
128         HiLog::Error(LABEL, "remove doc store fail");
129         return -1;
130     }
131 
132     const std::string walDbFile = option.db + "-wal";
133     HiLog::Debug(LABEL, "delete ejdb wal file %{public}s", walDbFile.c_str());
134     retCode = std::remove(walDbFile.c_str());
135     if (retCode != 0) {
136         HiLog::Error(LABEL, "remove wal doc store fail");
137         return -1;
138     }
139     HiLog::Info(LABEL, "remove doc store success");
140     return 0;
141 }
142 } // HiviewDFX
143 } // OHOS