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