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_connection.h"
17 
18 #include "db_errno.h"
19 #include "log_print.h"
20 #include "single_ver_natural_store.h"
21 
22 namespace DistributedDB {
SingleVerNaturalStoreConnection(SyncAbleKvDB * kvDB)23 SingleVerNaturalStoreConnection::SingleVerNaturalStoreConnection(SyncAbleKvDB *kvDB) : SyncAbleKvDBConnection(kvDB)
24 {
25 }
26 
~SingleVerNaturalStoreConnection()27 SingleVerNaturalStoreConnection::~SingleVerNaturalStoreConnection()
28 {
29 }
30 
Put(const IOption & option,const Key & key,const Value & value)31 int SingleVerNaturalStoreConnection::Put(const IOption &option, const Key &key, const Value &value)
32 {
33     std::vector<Entry> entries;
34     Entry entry{key, value};
35     entries.emplace_back(std::move(entry));
36 
37     return PutBatch(option, entries);
38 }
39 
PutBatch(const IOption & option,const std::vector<Entry> & entries)40 int SingleVerNaturalStoreConnection::PutBatch(const IOption &option, const std::vector<Entry> &entries)
41 {
42     LOGD("[PutBatch] entries size is : %zu, dataType : %d", entries.size(), option.dataType);
43     if (option.dataType != IOption::SYNC_DATA) {
44         return -E_NOT_SUPPORT;
45     }
46     int errCode = CheckSyncEntriesValid(entries);
47     if (errCode != E_OK) {
48         return errCode;
49     }
50     return PutBatchInner(option, entries);
51 }
52 
IsExtendedCacheDBMode() const53 bool SingleVerNaturalStoreConnection::IsExtendedCacheDBMode() const
54 {
55     return false;
56 }
57 
CheckAndGetEntryLen(const std::vector<Entry> & entries,uint32_t limit,uint32_t & entryLen) const58 bool SingleVerNaturalStoreConnection::CheckAndGetEntryLen(const std::vector<Entry> &entries, uint32_t limit,
59     uint32_t &entryLen) const
60 {
61     uint32_t len = 0;
62     for (const auto &entry : entries) {
63         len += (entry.key.size() + entry.value.size());
64         if (len > limit) {
65             return false; // stop calculate key len when it excced limit.
66         }
67     }
68     entryLen = len;
69     return true;
70 }
71 
CheckAndGetKeyLen(const std::vector<Key> & keys,const uint32_t limit,uint32_t & keyLen) const72 bool SingleVerNaturalStoreConnection::CheckAndGetKeyLen(const std::vector<Key> &keys, const uint32_t limit,
73     uint32_t &keyLen) const
74 {
75     uint32_t len = 0;
76     for (const auto &key : keys) {
77         len += key.size();
78         if (len > limit) {
79             return false; // stop calculate key len when it excced limit.
80         }
81     }
82     keyLen = len;
83     return true;
84 }
85 
CheckSyncEntriesValid(const std::vector<Entry> & entries) const86 int SingleVerNaturalStoreConnection::CheckSyncEntriesValid(const std::vector<Entry> &entries) const
87 {
88     return E_OK;
89 }
90 
CheckWritePermission() const91 int SingleVerNaturalStoreConnection::CheckWritePermission() const
92 {
93     return E_OK;
94 }
95 
Get(const IOption & option,const Key & key,Value & value) const96 int SingleVerNaturalStoreConnection::Get(const IOption &option, const Key &key, Value &value) const
97 {
98     return E_OK;
99 }
100 
Delete(const IOption & option,const Key & key)101 int SingleVerNaturalStoreConnection::Delete(const IOption &option, const Key &key)
102 {
103     std::vector<Key> keys;
104     keys.push_back(key);
105 
106     return DeleteBatch(option, keys);
107 }
108 
DeleteBatch(const IOption & option,const std::vector<Key> & keys)109 int SingleVerNaturalStoreConnection::DeleteBatch(const IOption &option, const std::vector<Key> &keys)
110 {
111     LOGD("[DeleteBatch] keys size is : %zu, dataType : %d", keys.size(), option.dataType);
112     if (option.dataType != IOption::SYNC_DATA) {
113         return -E_NOT_SUPPORT;
114     }
115     int errCode = CheckSyncKeysValid(keys);
116     if (errCode != E_OK) {
117         return errCode;
118     }
119     return DeleteBatchInner(option, keys);
120 }
121 } // namespace DistributedDB