]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/table/cuckoo/cuckoo_table_factory.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / table / cuckoo / cuckoo_table_factory.h
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
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).
7c673cae
FG
5
6#pragma once
7#ifndef ROCKSDB_LITE
8
9#include <string>
10#include "rocksdb/table.h"
11#include "util/murmurhash.h"
12#include "rocksdb/options.h"
13
f67539c2 14namespace ROCKSDB_NAMESPACE {
7c673cae
FG
15
16const uint32_t kCuckooMurmurSeedMultiplier = 816922183;
17static inline uint64_t CuckooHash(
18 const Slice& user_key, uint32_t hash_cnt, bool use_module_hash,
19 uint64_t table_size_, bool identity_as_first_hash,
20 uint64_t (*get_slice_hash)(const Slice&, uint32_t, uint64_t)) {
21#if !defined NDEBUG || defined OS_WIN
22 // This part is used only in unit tests but we have to keep it for Windows
23 // build as we run test in both debug and release modes under Windows.
24 if (get_slice_hash != nullptr) {
25 return get_slice_hash(user_key, hash_cnt, table_size_);
26 }
11fdf7f2
TL
27#else
28 (void)get_slice_hash;
7c673cae
FG
29#endif
30
31 uint64_t value = 0;
32 if (hash_cnt == 0 && identity_as_first_hash) {
33 value = (*reinterpret_cast<const int64_t*>(user_key.data()));
34 } else {
35 value = MurmurHash(user_key.data(), static_cast<int>(user_key.size()),
36 kCuckooMurmurSeedMultiplier * hash_cnt);
37 }
38 if (use_module_hash) {
39 return value % table_size_;
40 } else {
41 return value & (table_size_ - 1);
42 }
43}
44
45// Cuckoo Table is designed for applications that require fast point lookups
46// but not fast range scans.
47//
48// Some assumptions:
49// - Key length and Value length are fixed.
50// - Does not support Snapshot.
51// - Does not support Merge operations.
11fdf7f2 52// - Does not support prefix bloom filters.
7c673cae
FG
53class CuckooTableFactory : public TableFactory {
54 public:
20effc67
TL
55 explicit CuckooTableFactory(
56 const CuckooTableOptions& table_option = CuckooTableOptions());
7c673cae
FG
57 ~CuckooTableFactory() {}
58
20effc67
TL
59 // Method to allow CheckedCast to work for this class
60 static const char* kClassName() { return kCuckooTableName(); }
61 const char* Name() const override { return kCuckooTableName(); }
7c673cae 62
20effc67 63 using TableFactory::NewTableReader;
7c673cae 64 Status NewTableReader(
20effc67 65 const ReadOptions& ro, const TableReaderOptions& table_reader_options,
494da23a
TL
66 std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
67 std::unique_ptr<TableReader>* table,
7c673cae
FG
68 bool prefetch_index_and_filter_in_cache = true) const override;
69
70 TableBuilder* NewTableBuilder(
71 const TableBuilderOptions& table_builder_options,
72 uint32_t column_family_id, WritableFileWriter* file) const override;
73
20effc67 74 std::string GetPrintableOptions() const override;
11fdf7f2 75
7c673cae
FG
76 private:
77 CuckooTableOptions table_options_;
78};
79
f67539c2 80} // namespace ROCKSDB_NAMESPACE
7c673cae 81#endif // ROCKSDB_LITE