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 // 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.
9 
10 #pragma once
11 #ifndef ROCKSDB_LITE
12 #include <string>
13 #include <memory>
14 #include <utility>
15 #include <vector>
16 
17 #include "db/dbformat.h"
18 #include "file/random_access_file_reader.h"
19 #include "options/cf_options.h"
20 #include "rocksdb/env.h"
21 #include "rocksdb/options.h"
22 #include "table/table_reader.h"
23 
24 namespace ROCKSDB_NAMESPACE {
25 
26 class Arena;
27 class TableReader;
28 
29 class CuckooTableReader: public TableReader {
30  public:
31   CuckooTableReader(const ImmutableCFOptions& ioptions,
32                     std::unique_ptr<RandomAccessFileReader>&& file,
33                     uint64_t file_size, const Comparator* user_comparator,
34                     uint64_t (*get_slice_hash)(const Slice&, uint32_t,
35                                                uint64_t));
~CuckooTableReader()36   ~CuckooTableReader() {}
37 
GetTableProperties()38   std::shared_ptr<const TableProperties> GetTableProperties() const override {
39     return table_props_;
40   }
41 
status()42   Status status() const { return status_; }
43 
44   Status Get(const ReadOptions& readOptions, const Slice& key,
45              GetContext* get_context, const SliceTransform* prefix_extractor,
46              bool skip_filters = false) override;
47 
48   // Returns a new iterator over table contents
49   // compaction_readahead_size: its value will only be used if for_compaction =
50   // true
51   InternalIterator* NewIterator(const ReadOptions&,
52                                 const SliceTransform* prefix_extractor,
53                                 Arena* arena, bool skip_filters,
54                                 TableReaderCaller caller,
55                                 size_t compaction_readahead_size = 0) override;
56   void Prepare(const Slice& target) override;
57 
58   // Report an approximation of how much memory has been used.
59   size_t ApproximateMemoryUsage() const override;
60 
61   // Following methods are not implemented for Cuckoo Table Reader
ApproximateOffsetOf(const Slice &,TableReaderCaller)62   uint64_t ApproximateOffsetOf(const Slice& /*key*/,
63                                TableReaderCaller /*caller*/) override {
64     return 0;
65   }
66 
ApproximateSize(const Slice &,const Slice &,TableReaderCaller)67   uint64_t ApproximateSize(const Slice& /*start*/, const Slice& /*end*/,
68                            TableReaderCaller /*caller*/) override {
69     return 0;
70   }
71 
SetupForCompaction()72   void SetupForCompaction() override {}
73   // End of methods not implemented.
74 
75  private:
76   friend class CuckooTableIterator;
77   void LoadAllKeys(std::vector<std::pair<Slice, uint32_t>>* key_to_bucket_id);
78   std::unique_ptr<RandomAccessFileReader> file_;
79   Slice file_data_;
80   bool is_last_level_;
81   bool identity_as_first_hash_;
82   bool use_module_hash_;
83   std::shared_ptr<const TableProperties> table_props_;
84   Status status_;
85   uint32_t num_hash_func_;
86   std::string unused_key_;
87   uint32_t key_length_;
88   uint32_t user_key_length_;
89   uint32_t value_length_;
90   uint32_t bucket_length_;
91   uint32_t cuckoo_block_size_;
92   uint32_t cuckoo_block_bytes_minus_one_;
93   uint64_t table_size_;
94   const Comparator* ucomp_;
95   uint64_t (*get_slice_hash_)(const Slice& s, uint32_t index,
96       uint64_t max_num_buckets);
97 };
98 
99 }  // namespace ROCKSDB_NAMESPACE
100 #endif  // ROCKSDB_LITE
101