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 "doc_db.h"
17
18 #include "doc_util.h"
19 #include "ejdb2.h"
20 #include "hilog/log.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002D10, "HiView-DOCDB"};
OpenDB(const std::string & dbFile,iwkv_openflags flag)25 int DocDB::OpenDB(const std::string &dbFile, iwkv_openflags flag)
26 {
27 EJDB_OPTS opts;
28 opts.kv.path = dbFile.c_str();
29 opts.kv.oflags = flag;
30 opts.kv.random_seed = 0;
31 opts.kv.fmt_version = 0;
32 opts.kv.file_lock_fail_fast = 0;
33
34 opts.kv.wal.enabled = 0;
35 opts.kv.wal.check_crc_on_checkpoint = true;
36 opts.kv.wal.savepoint_timeout_sec = 0;
37 opts.kv.wal.checkpoint_timeout_sec = 0;
38 opts.kv.wal.wal_buffer_sz = 1024 * 1024; // 1M
39 opts.kv.wal.checkpoint_buffer_sz = 10 * 1024 * 1024; // 10M
40 opts.kv.wal.wal_lock_interceptor = nullptr;
41 opts.kv.wal.wal_lock_interceptor_opaque = nullptr;
42
43 opts.http.enabled = 0;
44 opts.http.port = 0;
45 opts.http.bind = nullptr;
46 opts.http.access_token = nullptr;
47 opts.http.access_token_len = 0;
48 opts.http.blocking = 0;
49 opts.http.read_anon = 0;
50 opts.http.max_body_size = 0;
51
52 opts.no_wal = 0;
53 opts.sort_buffer_sz = 0;
54 opts.document_buffer_sz = 0;
55
56 iwrc rc = ejdb_init();
57 if (rc) {
58 iwlog_ecode_error3(rc);
59 HiLog::Error(LABEL, "ejdb init failed, reason:%{public}s", iwlog_ecode_explained(rc));
60 return MapErrorCode(rc);
61 }
62
63 rc = ejdb_open(&opts, &db_);
64 if (rc) {
65 iwlog_ecode_error3(rc);
66 HiLog::Error(LABEL, "open ejdb failed, reason:%{public}s", iwlog_ecode_explained(rc));
67 return MapErrorCode(rc);
68 } else {
69 HiLog::Info(LABEL, "open ejdb success");
70 ejdb_ensure_index(db_, "collection", "/time_", EJDB_IDX_I64);
71 ejdb_ensure_index(db_, "collection", "/seq_", EJDB_IDX_I64);
72 HiLog::Debug(LABEL, "Indexes for time_ and seq_ columns are created successfully");
73 }
74 return 0;
75 }
76
OnlineBackupDB(const std::string & bakFile)77 int DocDB::OnlineBackupDB(const std::string &bakFile)
78 {
79 uint64_t ts = 0;
80 iwrc rc = ejdb_online_backup(db_, &ts, bakFile.c_str());
81 if (rc) {
82 iwlog_ecode_error3(rc);
83 HiLog::Error(LABEL, "ejdb online backup failed, reason:%{public}s", iwlog_ecode_explained(rc));
84 return MapErrorCode(rc);
85 } else {
86 HiLog::Info(LABEL, "ejdb online backup success");
87 }
88 return 0;
89 }
90
CloseDB()91 int DocDB::CloseDB()
92 {
93 iwrc rc = ejdb_close(&db_);
94 if (rc) {
95 iwlog_ecode_error3(rc);
96 HiLog::Error(LABEL, "close ejdb failed, reason:%{public}s", iwlog_ecode_explained(rc));
97 return MapErrorCode(rc);
98 } else {
99 HiLog::Info(LABEL, "close ejdb success");
100 }
101 return 0;
102 }
103 } // HiviewDFX
104 } // OHOS