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 #ifndef OMIT_MULTI_VER
17 #include "kv_store_snapshot_delegate_impl.h"
18
19 #include "db_errno.h"
20 #include "kv_store_errno.h"
21 #include "log_print.h"
22
23 namespace DistributedDB {
KvStoreSnapshotDelegateImpl(IKvDBSnapshot * snapshot,KvStoreObserver * observer)24 KvStoreSnapshotDelegateImpl::KvStoreSnapshotDelegateImpl(IKvDBSnapshot *snapshot, KvStoreObserver *observer)
25 : snapShot_(snapshot),
26 observer_(observer)
27 {}
28
Get(const Key & key,const std::function<void (DBStatus,const Value &)> & callback) const29 void KvStoreSnapshotDelegateImpl::Get(
30 const Key &key, const std::function<void(DBStatus, const Value &)> &callback) const
31 {
32 if (!callback) {
33 LOGE("[KvStoreSnapshot] Invalid callback!");
34 return;
35 }
36
37 DBStatus status = DB_ERROR;
38 Value value;
39 if (snapShot_ != nullptr) {
40 int errCode = snapShot_->Get(key, value);
41 if (errCode == E_OK) {
42 status = OK;
43 } else {
44 if (errCode != -E_NOT_FOUND) {
45 LOGE("[KvStoreSnapshot] Get data failed:%d", errCode);
46 }
47 status = TransferDBErrno(errCode);
48 }
49 }
50
51 callback(status, value);
52 }
53
GetEntries(const Key & keyPrefix,const std::function<void (DBStatus,const std::vector<Entry> &)> & callback) const54 void KvStoreSnapshotDelegateImpl::GetEntries(
55 const Key &keyPrefix, const std::function<void(DBStatus, const std::vector<Entry> &)> &callback) const
56 {
57 if (!callback) {
58 LOGE("[KvStoreSnapshot] Invalid callback!");
59 return;
60 }
61
62 DBStatus status = DB_ERROR;
63 std::vector<Entry> entries;
64 if (snapShot_ != nullptr) {
65 int errCode = snapShot_->GetEntries(keyPrefix, entries);
66 if (errCode == E_OK) {
67 status = OK;
68 } else {
69 if (errCode != -E_NOT_FOUND) {
70 LOGE("[KvStoreSnapshot] Get entries failed:%d", errCode);
71 }
72 status = TransferDBErrno(errCode);
73 }
74 }
75
76 callback(status, entries);
77 }
78
GetSnapshot(IKvDBSnapshot * & snapshot) const79 void KvStoreSnapshotDelegateImpl::GetSnapshot(IKvDBSnapshot *&snapshot) const
80 {
81 snapshot = snapShot_;
82 }
83
GetObserver(KvStoreObserver * & observer) const84 void KvStoreSnapshotDelegateImpl::GetObserver(KvStoreObserver *&observer) const
85 {
86 observer = observer_;
87 }
88 } // namespace DistributedDB
89 #endif
90