1 //  Copyright (c) 2017-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 #ifndef ROCKSDB_LITE
8 
9 #include "rocksdb/utilities/checkpoint.h"
10 
11 #include <string>
12 #include "file/filename.h"
13 #include "rocksdb/db.h"
14 
15 namespace ROCKSDB_NAMESPACE {
16 
17 class CheckpointImpl : public Checkpoint {
18  public:
19   // Creates a Checkpoint object to be used for creating openable snapshots
CheckpointImpl(DB * db)20   explicit CheckpointImpl(DB* db) : db_(db) {}
21 
22   // Builds an openable snapshot of RocksDB on the same disk, which
23   // accepts an output directory on the same disk, and under the directory
24   // (1) hard-linked SST files pointing to existing live SST files
25   // SST files will be copied if output directory is on a different filesystem
26   // (2) a copied manifest files and other files
27   // The directory should not already exist and will be created by this API.
28   // The directory will be an absolute path
29   using Checkpoint::CreateCheckpoint;
30   virtual Status CreateCheckpoint(const std::string& checkpoint_dir,
31                                   uint64_t log_size_for_flush,
32                                   uint64_t* sequence_number_ptr) override;
33 
34   // Exports all live SST files of a specified Column Family onto export_dir
35   // and returning SST files information in metadata.
36   //  - SST files will be created as hard links when the directory specified
37   //    is in the same partition as the db directory, copied otherwise.
38   //  - export_dir should not already exist and will be created by this API.
39   //  - Always triggers a flush.
40   using Checkpoint::ExportColumnFamily;
41   virtual Status ExportColumnFamily(
42       ColumnFamilyHandle* handle, const std::string& export_dir,
43       ExportImportFilesMetaData** metadata) override;
44 
45   // Checkpoint logic can be customized by providing callbacks for link, copy,
46   // or create.
47   Status CreateCustomCheckpoint(
48       const DBOptions& db_options,
49       std::function<Status(const std::string& src_dirname,
50                            const std::string& fname, FileType type)>
51           link_file_cb,
52       std::function<Status(const std::string& src_dirname,
53                            const std::string& fname, uint64_t size_limit_bytes,
54                            FileType type)>
55           copy_file_cb,
56       std::function<Status(const std::string& fname,
57                            const std::string& contents, FileType type)>
58           create_file_cb,
59       uint64_t* sequence_number, uint64_t log_size_for_flush);
60 
61  private:
62   void CleanStagingDirectory(const std::string& path, Logger* info_log);
63 
64   // Export logic customization by providing callbacks for link or copy.
65   Status ExportFilesInMetaData(
66       const DBOptions& db_options, const ColumnFamilyMetaData& metadata,
67       std::function<Status(const std::string& src_dirname,
68                            const std::string& fname)>
69           link_file_cb,
70       std::function<Status(const std::string& src_dirname,
71                            const std::string& fname)>
72           copy_file_cb);
73 
74  private:
75   DB* db_;
76 };
77 
78 }  // namespace ROCKSDB_NAMESPACE
79 
80 #endif  // ROCKSDB_LITE
81