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 GENERIC_SINGLE_VER_KV_ENTRY_H 17 #define GENERIC_SINGLE_VER_KV_ENTRY_H 18 19 #include <cstdint> 20 #include <vector> 21 #include <string> 22 23 #include "db_types.h" 24 #include "single_ver_kv_entry.h" 25 26 namespace DistributedDB { 27 struct CompressInfo { 28 CompressAlgorithm compressAlgo = CompressAlgorithm::ZLIB; 29 uint32_t targetVersion = 0; 30 }; 31 32 class GenericSingleVerKvEntry : public SingleVerKvEntry { 33 public: 34 GenericSingleVerKvEntry(); 35 ~GenericSingleVerKvEntry() override; 36 DISABLE_COPY_ASSIGN_MOVE(GenericSingleVerKvEntry); 37 38 std::string GetOrigDevice() const override; 39 40 void SetOrigDevice(const std::string &dev) override; 41 42 Timestamp GetTimestamp() const override; 43 44 void SetTimestamp(Timestamp time) override; 45 46 Timestamp GetWriteTimestamp() const override; 47 48 void SetWriteTimestamp(Timestamp time) override; 49 50 void GetKey(Key &key) const; 51 const Key &GetKey() const override; 52 53 void GetHashKey(Key &key) const; 54 55 void GetValue(Value &value) const; 56 const Value &GetValue() const override; 57 58 uint64_t GetFlag() const override; 59 60 void SetEntryData(DataItem &&dataItem); 61 62 int SerializeData(Parcel &parcel, uint32_t targetVersion) override; 63 64 int DeSerializeData(Parcel &parcel) override; 65 66 uint32_t CalculateLen(uint32_t targetVersion) override; 67 68 static int SerializeDatas(const std::vector<SingleVerKvEntry *> &kvEntries, Parcel &parcel, uint32_t targetVersion); 69 70 static int DeSerializeDatas(std::vector<SingleVerKvEntry *> &kvEntries, Parcel &parcel); 71 72 void SetKey(const Key &key) override; 73 void SetValue(const Value &value) override; 74 void SetHashKey(const Key &hashKey) override; 75 76 static uint32_t CalculateLens(const std::vector<SingleVerKvEntry *> &kvEntries, uint32_t targetVersion); 77 static uint32_t CalculateCompressedLens(const std::vector<uint8_t> &compressedData); 78 static int Compress(const std::vector<SingleVerKvEntry *> &kvEntries, std::vector<uint8_t> &destData, 79 const CompressInfo &compressInfo); 80 static int Uncompress(const std::vector<uint8_t> &srcData, std::vector<SingleVerKvEntry *> &kvEntries, 81 uint32_t destLen, CompressAlgorithm algo); 82 static int SerializeCompressedDatas(const std::vector<SingleVerKvEntry *> &kvEntries, 83 const std::vector<uint8_t> &compressedEntries, Parcel &parcel, uint32_t targetVersion, CompressAlgorithm algo); 84 static int DeSerializeCompressedDatas(std::vector<SingleVerKvEntry *> &kvEntries, Parcel &parcel); 85 86 private: 87 enum class OperType { 88 SERIALIZE, 89 DESERIALIZE, 90 CAL_LEN, 91 }; 92 int AdaptToVersion(OperType operType, uint32_t targetVersion, Parcel &parcel, uint64_t &dataLen); 93 int AdaptToVersion(OperType operType, uint32_t targetVersion, uint64_t &dataLen); 94 95 int SerializeDataByVersion(uint32_t targetVersion, Parcel &parcel) const; 96 int SerializeDataByFirstVersion(Parcel &parcel) const; 97 int SerializeDataByLaterVersion(Parcel &parcel, uint32_t targetVersion) const; 98 99 int CalLenByVersion(uint32_t targetVersion, uint64_t &len) const; 100 void CalLenByFirstVersion(uint64_t &len) const; 101 void CalLenByLaterVersion(uint64_t &len, uint32_t targetVersion) const; 102 103 int DeSerializeByVersion(uint32_t targetVersion, Parcel &parcel, uint64_t &len); 104 void DeSerializeByFirstVersion(uint64_t &len, Parcel &parcel); 105 void DeSerializeByLaterVersion(uint64_t &len, Parcel &parcel, uint32_t targetVersion); 106 107 DataItem dataItem_; 108 }; 109 } // namespace DistributedDB 110 111 #endif // GENERIC_SINGLE_VER_KV_ENTRY_H 112