/src/rocksdb/util/file_checksum_helper.h
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under both the GPLv2 (found in the |
3 | | // COPYING file in the root directory) and Apache 2.0 License |
4 | | // (found in the LICENSE.Apache file in the root directory). |
5 | | |
6 | | #pragma once |
7 | | |
8 | | #include <unordered_map> |
9 | | |
10 | | #include "rocksdb/file_checksum.h" |
11 | | #include "util/coding.h" |
12 | | #include "util/crc32c.h" |
13 | | #include "util/math.h" |
14 | | |
15 | | namespace ROCKSDB_NAMESPACE { |
16 | | |
17 | | // Converts a 32-bit checksum value (e.g. a CRC32C) to its hex string |
18 | | // representation, matching the on-disk/backup-metadata encoding. Shared by the |
19 | | // backup and copy engines. |
20 | 0 | inline std::string ChecksumInt32ToHex(uint32_t checksum_value) { |
21 | 0 | std::string checksum_str; |
22 | 0 | PutFixed32(&checksum_str, EndianSwapValue(checksum_value)); |
23 | 0 | return Slice(checksum_str).ToString(/*hex=*/true); |
24 | 0 | } |
25 | | |
26 | | // This is the class to generate the file checksum based on Crc32. It |
27 | | // will be used as the default checksum method for SST file checksum |
28 | | class FileChecksumGenCrc32c : public FileChecksumGenerator { |
29 | | public: |
30 | 0 | FileChecksumGenCrc32c(const FileChecksumGenContext& /*context*/) { |
31 | 0 | checksum_ = 0; |
32 | 0 | } |
33 | | |
34 | 0 | void Update(const char* data, size_t n) override { |
35 | 0 | checksum_ = crc32c::Extend(checksum_, data, n); |
36 | 0 | } |
37 | | |
38 | 0 | void Finalize() override { |
39 | 0 | assert(checksum_str_.empty()); |
40 | | // Store as big endian raw bytes |
41 | 0 | PutFixed32(&checksum_str_, EndianSwapValue(checksum_)); |
42 | 0 | } |
43 | | |
44 | 0 | std::string GetChecksum() const override { |
45 | 0 | assert(!checksum_str_.empty()); |
46 | 0 | return checksum_str_; |
47 | 0 | } |
48 | | |
49 | 0 | const char* Name() const override { return "FileChecksumCrc32c"; } |
50 | | |
51 | | private: |
52 | | uint32_t checksum_; |
53 | | std::string checksum_str_; |
54 | | }; |
55 | | |
56 | | class FileChecksumGenCrc32cFactory : public FileChecksumGenFactory { |
57 | | public: |
58 | | std::unique_ptr<FileChecksumGenerator> CreateFileChecksumGenerator( |
59 | 0 | const FileChecksumGenContext& context) override { |
60 | 0 | if (context.requested_checksum_func_name.empty() || |
61 | 0 | context.requested_checksum_func_name == "FileChecksumCrc32c") { |
62 | 0 | return std::unique_ptr<FileChecksumGenerator>( |
63 | 0 | new FileChecksumGenCrc32c(context)); |
64 | 0 | } else { |
65 | 0 | return nullptr; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | 92.3k | static const char* kClassName() { return "FileChecksumGenCrc32cFactory"; } |
70 | 0 | const char* Name() const override { return kClassName(); } |
71 | | }; |
72 | | |
73 | | // The default implementaion of FileChecksumList |
74 | | class FileChecksumListImpl : public FileChecksumList { |
75 | | public: |
76 | 0 | FileChecksumListImpl() {} |
77 | | void reset() override; |
78 | | |
79 | | size_t size() const override; |
80 | | |
81 | | Status GetAllFileChecksums( |
82 | | std::vector<uint64_t>* file_numbers, std::vector<std::string>* checksums, |
83 | | std::vector<std::string>* checksum_func_names) override; |
84 | | |
85 | | Status SearchOneFileChecksum(uint64_t file_number, std::string* checksum, |
86 | | std::string* checksum_func_name) override; |
87 | | |
88 | | Status InsertOneFileChecksum(uint64_t file_number, |
89 | | const std::string& checksum, |
90 | | const std::string& checksum_func_name) override; |
91 | | |
92 | | Status RemoveOneFileChecksum(uint64_t file_number) override; |
93 | | |
94 | | private: |
95 | | // Key is the file number, the first portion of the value is checksum, the |
96 | | // second portion of the value is checksum function name. |
97 | | std::unordered_map<uint64_t, std::pair<std::string, std::string>> |
98 | | checksum_map_; |
99 | | }; |
100 | | |
101 | | } // namespace ROCKSDB_NAMESPACE |