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).
8 #include "rocksdb/sst_file_reader.h"
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"
19 namespace ROCKSDB_NAMESPACE
{
21 struct SstFileReader::Rep
{
24 ImmutableCFOptions ioptions
;
25 MutableCFOptions moptions
;
27 std::unique_ptr
<TableReader
> table_reader
;
29 Rep(const Options
& opts
)
33 moptions(ColumnFamilyOptions(options
)) {}
36 SstFileReader::SstFileReader(const Options
& options
) : rep_(new Rep(options
)) {}
38 SstFileReader::~SstFileReader() {}
40 Status
SstFileReader::Open(const std::string
& file_path
) {
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
);
48 s
= r
->options
.env
->NewRandomAccessFile(file_path
, &file
, r
->soptions
);
51 file_reader
.reset(new RandomAccessFileReader(
52 NewLegacyRandomAccessFileWrapper(file
), file_path
));
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
);
65 Iterator
* SstFileReader::NewIterator(const ReadOptions
& options
) {
67 auto sequence
= options
.snapshot
!= nullptr
68 ? options
.snapshot
->GetSequenceNumber()
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 */);
79 std::shared_ptr
<const TableProperties
> SstFileReader::GetTableProperties()
81 return rep_
->table_reader
->GetTableProperties();
84 Status
SstFileReader::VerifyChecksum(const ReadOptions
& read_options
) {
85 return rep_
->table_reader
->VerifyChecksum(read_options
,
86 TableReaderCaller::kSSTFileReader
);
89 } // namespace ROCKSDB_NAMESPACE
91 #endif // !ROCKSDB_LITE