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 // Copyright (c) 2013 The LevelDB Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style license that can be
7 // found in the LICENSE file. See the AUTHORS file for names of contributors.
8 
9 #pragma once
10 
11 #include <cassert>
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 #include "rocksdb/status.h"
18 
19 namespace ROCKSDB_NAMESPACE {
20 
21 struct FileChecksumGenContext {
22   std::string file_name;
23 };
24 
25 // FileChecksumGenerator is the class to generates the checksum value
26 // for each file when the file is written to the file system.
27 class FileChecksumGenerator {
28  public:
~FileChecksumGenerator()29   virtual ~FileChecksumGenerator() {}
30 
31   // Update the current result after process the data. For different checksum
32   // functions, the temporal results may be stored and used in Update to
33   // include the new data.
34   virtual void Update(const char* data, size_t n) = 0;
35 
36   // Generate the final results if no further new data will be updated.
37   virtual void Finalize() = 0;
38 
39   // Get the checksum
40   virtual std::string GetChecksum() const = 0;
41 
42   // Returns a name that identifies the current file checksum function.
43   virtual const char* Name() const = 0;
44 };
45 
46 // Create the FileChecksumGenerator object for each SST file.
47 class FileChecksumGenFactory {
48  public:
~FileChecksumGenFactory()49   virtual ~FileChecksumGenFactory() {}
50 
51   // Create a new FileChecksumGenerator.
52   virtual std::unique_ptr<FileChecksumGenerator> CreateFileChecksumGenerator(
53       const FileChecksumGenContext& context) = 0;
54 
55   // Return the name of this FileChecksumGenFactory.
56   virtual const char* Name() const = 0;
57 };
58 
59 // FileChecksumList stores the checksum information of a list of files (e.g.,
60 // SST files). The FileChecksumLIst can be used to store the checksum
61 // information of all SST file getting  from the MANIFEST, which are
62 // the checksum information of all valid SST file of a DB instance. It can
63 // also be used to store the checksum information of a list of SST files to
64 // be ingested.
65 class FileChecksumList {
66  public:
~FileChecksumList()67   virtual ~FileChecksumList() {}
68 
69   // Clean the previously stored file checksum information.
70   virtual void reset() = 0;
71 
72   // Get the number of checksums in the checksum list
73   virtual size_t size() const = 0;
74 
75   // Return all the file checksum information being stored in a unordered_map.
76   // File_number is the key, the first part of the value is checksum value,
77   // and the second part of the value is checksum function name.
78   virtual Status GetAllFileChecksums(
79       std::vector<uint64_t>* file_numbers, std::vector<std::string>* checksums,
80       std::vector<std::string>* checksum_func_names) = 0;
81 
82   // Given the file_number, it searches if the file checksum information is
83   // stored.
84   virtual Status SearchOneFileChecksum(uint64_t file_number,
85                                        std::string* checksum,
86                                        std::string* checksum_func_name) = 0;
87 
88   // Insert the checksum information of one file to the FileChecksumList.
89   virtual Status InsertOneFileChecksum(
90       uint64_t file_number, const std::string& checksum,
91       const std::string& checksum_func_name) = 0;
92 
93   // Remove the checksum information of one SST file.
94   virtual Status RemoveOneFileChecksum(uint64_t file_number) = 0;
95 };
96 
97 // Create a new file checksum list.
98 extern FileChecksumList* NewFileChecksumList();
99 
100 }  // namespace ROCKSDB_NAMESPACE
101