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_transfer_data.h"
18
19 #include "db_constant.h"
20 #include "log_print.h"
21 #include "db_errno.h"
22
23 namespace DistributedDB {
SegmentAndTransferValueToHash(const Value & oriValue,std::vector<Value> & partValues) const24 int MultiVerNaturalStoreTransferData::SegmentAndTransferValueToHash(const Value &oriValue,
25 std::vector<Value> &partValues) const
26 {
27 if (oriValue.size() <= sliceLengthThreshold_) {
28 return -E_UNEXPECTED_DATA;
29 }
30
31 const uint32_t sizeByte = blockSizeByte_;
32 if (sizeByte == 0) {
33 return -E_UNEXPECTED_DATA;
34 }
35
36 const size_t partNum = oriValue.size() / sizeByte;
37
38 for (size_t i = 0; i < partNum; i++) {
39 Value tempValue(sizeByte);
40 // When the hash value is combined, the overlapped part is removed. So not need -1 at tail
41 std::copy(oriValue.begin() + i * sizeByte, oriValue.begin() + sizeByte * (i + 1), tempValue.begin());
42 partValues.push_back(std::move(tempValue));
43 }
44 Value tailValue(oriValue.size() - partNum * sizeByte);
45 std::copy(oriValue.begin() + partNum * sizeByte, oriValue.end(), tailValue.begin());
46 if (!tailValue.empty()) {
47 partValues.push_back(tailValue);
48 }
49
50 return E_OK;
51 }
52 } // namespace DistributedDB
53 #endif
54