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).
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
11 #include "table/block.h"
12 #include "table/format.h"
13 #include "util/memory_allocator.h"
18 // Read the block identified by "handle" from "file".
19 // The only relevant option is options.verify_checksums for now.
20 // On failure return non-OK.
21 // On success fill *result and return OK - caller owns *result
22 // @param uncompression_dict Data for presetting the compression library's
24 BlockFetcher(RandomAccessFileReader
* file
,
25 FilePrefetchBuffer
* prefetch_buffer
, const Footer
& footer
,
26 const ReadOptions
& read_options
, const BlockHandle
& handle
,
27 BlockContents
* contents
, const ImmutableCFOptions
& ioptions
,
28 bool do_uncompress
, bool maybe_compressed
,
29 const UncompressionDict
& uncompression_dict
,
30 const PersistentCacheOptions
& cache_options
,
31 MemoryAllocator
* memory_allocator
= nullptr,
32 MemoryAllocator
* memory_allocator_compressed
= nullptr)
34 prefetch_buffer_(prefetch_buffer
),
36 read_options_(read_options
),
40 do_uncompress_(do_uncompress
),
41 maybe_compressed_(maybe_compressed
),
42 uncompression_dict_(uncompression_dict
),
43 cache_options_(cache_options
),
44 memory_allocator_(memory_allocator
),
45 memory_allocator_compressed_(memory_allocator_compressed
) {}
46 Status
ReadBlockContents();
47 CompressionType
get_compression_type() const { return compression_type_
; }
50 static const uint32_t kDefaultStackBufferSize
= 5000;
52 RandomAccessFileReader
* file_
;
53 FilePrefetchBuffer
* prefetch_buffer_
;
54 const Footer
& footer_
;
55 const ReadOptions read_options_
;
56 const BlockHandle
& handle_
;
57 BlockContents
* contents_
;
58 const ImmutableCFOptions
& ioptions_
;
60 bool maybe_compressed_
;
61 const UncompressionDict
& uncompression_dict_
;
62 const PersistentCacheOptions
& cache_options_
;
63 MemoryAllocator
* memory_allocator_
;
64 MemoryAllocator
* memory_allocator_compressed_
;
67 char* used_buf_
= nullptr;
69 CacheAllocationPtr heap_buf_
;
70 CacheAllocationPtr compressed_buf_
;
71 char stack_buf_
[kDefaultStackBufferSize
];
72 bool got_from_prefetch_buffer_
= false;
73 rocksdb::CompressionType compression_type_
;
75 // return true if found
76 bool TryGetUncompressBlockFromPersistentCache();
77 // return true if found
78 bool TryGetFromPrefetchBuffer();
79 bool TryGetCompressedBlockFromPersistentCache();
80 void PrepareBufferForBlockFromFile();
81 // Copy content from used_buf_ to new heap buffer.
82 void CopyBufferToHeap();
83 void GetBlockContents();
84 void InsertCompressedBlockToPersistentCacheIfNeeded();
85 void InsertUncompressedBlockToPersistentCacheIfNeeded();
86 void CheckBlockChecksum();
88 } // namespace rocksdb