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/arena_wrapped_db_iter.h"
11 #include "db/db_iter.h"
12 #include "db/dbformat.h"
13 #include "file/random_access_file_reader.h"
14 #include "options/cf_options.h"
15 #include "rocksdb/env.h"
16 #include "rocksdb/file_system.h"
17 #include "table/get_context.h"
18 #include "table/table_builder.h"
19 #include "table/table_reader.h"
21 namespace ROCKSDB_NAMESPACE
{
23 struct SstFileReader::Rep
{
26 ImmutableOptions ioptions
;
27 MutableCFOptions moptions
;
29 std::unique_ptr
<TableReader
> table_reader
;
31 Rep(const Options
& opts
)
35 moptions(ColumnFamilyOptions(options
)) {}
38 SstFileReader::SstFileReader(const Options
& options
) : rep_(new Rep(options
)) {}
40 SstFileReader::~SstFileReader() {}
42 Status
SstFileReader::Open(const std::string
& file_path
) {
45 uint64_t file_size
= 0;
46 std::unique_ptr
<FSRandomAccessFile
> file
;
47 std::unique_ptr
<RandomAccessFileReader
> file_reader
;
48 FileOptions
fopts(r
->soptions
);
49 const auto& fs
= r
->options
.env
->GetFileSystem();
51 s
= fs
->GetFileSize(file_path
, fopts
.io_options
, &file_size
, nullptr);
53 s
= fs
->NewRandomAccessFile(file_path
, fopts
, &file
, nullptr);
56 file_reader
.reset(new RandomAccessFileReader(std::move(file
), file_path
));
59 TableReaderOptions
t_opt(r
->ioptions
, r
->moptions
.prefix_extractor
,
60 r
->soptions
, r
->ioptions
.internal_comparator
);
61 // Allow open file with global sequence number for backward compatibility.
62 t_opt
.largest_seqno
= kMaxSequenceNumber
;
63 s
= r
->options
.table_factory
->NewTableReader(t_opt
, std::move(file_reader
),
64 file_size
, &r
->table_reader
);
69 Iterator
* SstFileReader::NewIterator(const ReadOptions
& roptions
) {
71 auto sequence
= roptions
.snapshot
!= nullptr
72 ? roptions
.snapshot
->GetSequenceNumber()
74 ArenaWrappedDBIter
* res
= new ArenaWrappedDBIter();
75 res
->Init(r
->options
.env
, roptions
, r
->ioptions
, r
->moptions
,
76 nullptr /* version */, sequence
,
77 r
->moptions
.max_sequential_skip_in_iterations
,
78 0 /* version_number */, nullptr /* read_callback */,
79 nullptr /* db_impl */, nullptr /* cfd */,
80 true /* expose_blob_index */, false /* allow_refresh */);
81 auto internal_iter
= r
->table_reader
->NewIterator(
82 res
->GetReadOptions(), r
->moptions
.prefix_extractor
.get(),
83 res
->GetArena(), false /* skip_filters */,
84 TableReaderCaller::kSSTFileReader
);
85 res
->SetIterUnderDBIter(internal_iter
);
89 std::shared_ptr
<const TableProperties
> SstFileReader::GetTableProperties()
91 return rep_
->table_reader
->GetTableProperties();
94 Status
SstFileReader::VerifyChecksum(const ReadOptions
& read_options
) {
95 return rep_
->table_reader
->VerifyChecksum(read_options
,
96 TableReaderCaller::kSSTFileReader
);
99 } // namespace ROCKSDB_NAMESPACE
101 #endif // !ROCKSDB_LITE