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 "multi_ver_natural_store_snapshot.h"
18 
19 #include "db_constant.h"
20 #include "db_errno.h"
21 #include "log_print.h"
22 #include "multi_ver_storage_executor.h"
23 
24 namespace DistributedDB {
MultiVerNaturalStoreSnapshot(StorageExecutor * handle)25 MultiVerNaturalStoreSnapshot::MultiVerNaturalStoreSnapshot(StorageExecutor *handle)
26     : databaseHandle_(handle)
27 {}
28 
~MultiVerNaturalStoreSnapshot()29 MultiVerNaturalStoreSnapshot::~MultiVerNaturalStoreSnapshot()
30 {
31     databaseHandle_ = nullptr;
32 }
33 
Get(const Key & key,Value & value) const34 int MultiVerNaturalStoreSnapshot::Get(const Key &key, Value &value) const
35 {
36     if (databaseHandle_ == nullptr) {
37         return -E_INVALID_DB;
38     }
39     if (key.empty() || key.size() > DBConstant::MAX_KEY_SIZE) {
40         LOGE("[MultiSnapshot] Invalid key[%zu]", key.size());
41         return -E_INVALID_ARGS;
42     }
43     return static_cast<MultiVerStorageExecutor *>(databaseHandle_)->Get(key, value);
44 }
45 
GetEntries(const Key & keyPrefix,std::vector<Entry> & entries) const46 int MultiVerNaturalStoreSnapshot::GetEntries(const Key &keyPrefix, std::vector<Entry> &entries) const
47 {
48     if (databaseHandle_ == nullptr) {
49         return -E_INVALID_DB;
50     }
51     if (keyPrefix.size() > DBConstant::MAX_KEY_SIZE) {
52         LOGE("[MultiSnapshot] Invalid prefix[%zu]", keyPrefix.size());
53         return -E_INVALID_ARGS;
54     }
55     return static_cast<MultiVerStorageExecutor *>(databaseHandle_)->GetEntries(keyPrefix, entries);
56 }
57 
Close()58 void MultiVerNaturalStoreSnapshot::Close()
59 {
60     if (databaseHandle_ != nullptr) {
61         static_cast<MultiVerStorageExecutor *>(databaseHandle_)->Close();
62         databaseHandle_ = nullptr;
63     }
64 }
65 } // namespace DistributedDB
66 #endif
67