/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 | | // This is the class to generate the file checksum based on Crc32. It |
18 | | // will be used as the default checksum method for SST file checksum |
19 | | class FileChecksumGenCrc32c : public FileChecksumGenerator { |
20 | | public: |
21 | 0 | FileChecksumGenCrc32c(const FileChecksumGenContext& /*context*/) { |
22 | 0 | checksum_ = 0; |
23 | 0 | } |
24 | | |
25 | 0 | void Update(const char* data, size_t n) override { |
26 | 0 | checksum_ = crc32c::Extend(checksum_, data, n); |
27 | 0 | } |
28 | | |
29 | 0 | void Finalize() override { |
30 | 0 | assert(checksum_str_.empty()); |
31 | | // Store as big endian raw bytes |
32 | 0 | PutFixed32(&checksum_str_, EndianSwapValue(checksum_)); |
33 | 0 | } |
34 | | |
35 | 0 | std::string GetChecksum() const override { |
36 | 0 | assert(!checksum_str_.empty()); |
37 | 0 | return checksum_str_; |
38 | 0 | } |
39 | | |
40 | 0 | const char* Name() const override { return "FileChecksumCrc32c"; } |
41 | | |
42 | | private: |
43 | | uint32_t checksum_; |
44 | | std::string checksum_str_; |
45 | | }; |
46 | | |
47 | | class FileChecksumGenCrc32cFactory : public FileChecksumGenFactory { |
48 | | public: |
49 | | std::unique_ptr<FileChecksumGenerator> CreateFileChecksumGenerator( |
50 | 0 | const FileChecksumGenContext& context) override { |
51 | 0 | if (context.requested_checksum_func_name.empty() || |
52 | 0 | context.requested_checksum_func_name == "FileChecksumCrc32c") { |
53 | 0 | return std::unique_ptr<FileChecksumGenerator>( |
54 | 0 | new FileChecksumGenCrc32c(context)); |
55 | 0 | } else { |
56 | 0 | return nullptr; |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | 86.7k | static const char* kClassName() { return "FileChecksumGenCrc32cFactory"; } |
61 | 0 | const char* Name() const override { return kClassName(); } |
62 | | }; |
63 | | |
64 | | // The default implementaion of FileChecksumList |
65 | | class FileChecksumListImpl : public FileChecksumList { |
66 | | public: |
67 | 0 | FileChecksumListImpl() {} |
68 | | void reset() override; |
69 | | |
70 | | size_t size() const override; |
71 | | |
72 | | Status GetAllFileChecksums( |
73 | | std::vector<uint64_t>* file_numbers, std::vector<std::string>* checksums, |
74 | | std::vector<std::string>* checksum_func_names) override; |
75 | | |
76 | | Status SearchOneFileChecksum(uint64_t file_number, std::string* checksum, |
77 | | std::string* checksum_func_name) override; |
78 | | |
79 | | Status InsertOneFileChecksum(uint64_t file_number, |
80 | | const std::string& checksum, |
81 | | const std::string& checksum_func_name) override; |
82 | | |
83 | | Status RemoveOneFileChecksum(uint64_t file_number) override; |
84 | | |
85 | | private: |
86 | | // Key is the file number, the first portion of the value is checksum, the |
87 | | // second portion of the value is checksum function name. |
88 | | std::unordered_map<uint64_t, std::pair<std::string, std::string>> |
89 | | checksum_map_; |
90 | | }; |
91 | | |
92 | | } // namespace ROCKSDB_NAMESPACE |