sst_file_reader.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef ROCKSDB_LITE
  6. #include "rocksdb/sst_file_reader.h"
  7. #include "db/db_iter.h"
  8. #include "db/dbformat.h"
  9. #include "env/composite_env_wrapper.h"
  10. #include "file/random_access_file_reader.h"
  11. #include "options/cf_options.h"
  12. #include "table/get_context.h"
  13. #include "table/table_builder.h"
  14. #include "table/table_reader.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. struct SstFileReader::Rep {
  17. Options options;
  18. EnvOptions soptions;
  19. ImmutableCFOptions ioptions;
  20. MutableCFOptions moptions;
  21. std::unique_ptr<TableReader> table_reader;
  22. Rep(const Options& opts)
  23. : options(opts),
  24. soptions(options),
  25. ioptions(options),
  26. moptions(ColumnFamilyOptions(options)) {}
  27. };
  28. SstFileReader::SstFileReader(const Options& options) : rep_(new Rep(options)) {}
  29. SstFileReader::~SstFileReader() {}
  30. Status SstFileReader::Open(const std::string& file_path) {
  31. auto r = rep_.get();
  32. Status s;
  33. uint64_t file_size = 0;
  34. std::unique_ptr<RandomAccessFile> file;
  35. std::unique_ptr<RandomAccessFileReader> file_reader;
  36. s = r->options.env->GetFileSize(file_path, &file_size);
  37. if (s.ok()) {
  38. s = r->options.env->NewRandomAccessFile(file_path, &file, r->soptions);
  39. }
  40. if (s.ok()) {
  41. file_reader.reset(new RandomAccessFileReader(
  42. NewLegacyRandomAccessFileWrapper(file), file_path));
  43. }
  44. if (s.ok()) {
  45. TableReaderOptions t_opt(r->ioptions, r->moptions.prefix_extractor.get(),
  46. r->soptions, r->ioptions.internal_comparator);
  47. // Allow open file with global sequence number for backward compatibility.
  48. t_opt.largest_seqno = kMaxSequenceNumber;
  49. s = r->options.table_factory->NewTableReader(t_opt, std::move(file_reader),
  50. file_size, &r->table_reader);
  51. }
  52. return s;
  53. }
  54. Iterator* SstFileReader::NewIterator(const ReadOptions& options) {
  55. auto r = rep_.get();
  56. auto sequence = options.snapshot != nullptr
  57. ? options.snapshot->GetSequenceNumber()
  58. : kMaxSequenceNumber;
  59. auto internal_iter = r->table_reader->NewIterator(
  60. options, r->moptions.prefix_extractor.get(), /*arena=*/nullptr,
  61. /*skip_filters=*/false, TableReaderCaller::kSSTFileReader);
  62. return NewDBIterator(r->options.env, options, r->ioptions, r->moptions,
  63. r->ioptions.user_comparator, internal_iter, sequence,
  64. r->moptions.max_sequential_skip_in_iterations,
  65. nullptr /* read_callback */);
  66. }
  67. std::shared_ptr<const TableProperties> SstFileReader::GetTableProperties()
  68. const {
  69. return rep_->table_reader->GetTableProperties();
  70. }
  71. Status SstFileReader::VerifyChecksum(const ReadOptions& read_options) {
  72. return rep_->table_reader->VerifyChecksum(read_options,
  73. TableReaderCaller::kSSTFileReader);
  74. }
  75. } // namespace ROCKSDB_NAMESPACE
  76. #endif // !ROCKSDB_LITE