filter_policy.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. //
  5. // A database can be configured with a custom FilterPolicy object.
  6. // This object is responsible for creating a small filter from a set
  7. // of keys. These filters are stored in leveldb and are consulted
  8. // automatically by leveldb to decide whether or not to read some
  9. // information from disk. In many cases, a filter can cut down the
  10. // number of disk seeks form a handful to a single disk seek per
  11. // DB::Get() call.
  12. //
  13. // Most people will want to use the builtin bloom filter support (see
  14. // NewBloomFilterPolicy() below).
  15. #ifndef STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
  16. #define STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_
  17. #include <string>
  18. #include "leveldb/export.h"
  19. namespace leveldb {
  20. class Slice;
  21. class LEVELDB_EXPORT FilterPolicy {
  22. public:
  23. virtual ~FilterPolicy();
  24. // Return the name of this policy. Note that if the filter encoding
  25. // changes in an incompatible way, the name returned by this method
  26. // must be changed. Otherwise, old incompatible filters may be
  27. // passed to methods of this type.
  28. virtual const char* Name() const = 0;
  29. // keys[0,n-1] contains a list of keys (potentially with duplicates)
  30. // that are ordered according to the user supplied comparator.
  31. // Append a filter that summarizes keys[0,n-1] to *dst.
  32. //
  33. // Warning: do not change the initial contents of *dst. Instead,
  34. // append the newly constructed filter to *dst.
  35. virtual void CreateFilter(const Slice* keys, int n,
  36. std::string* dst) const = 0;
  37. // "filter" contains the data appended by a preceding call to
  38. // CreateFilter() on this class. This method must return true if
  39. // the key was in the list of keys passed to CreateFilter().
  40. // This method may return true or false if the key was not on the
  41. // list, but it should aim to return false with a high probability.
  42. virtual bool KeyMayMatch(const Slice& key, const Slice& filter) const = 0;
  43. };
  44. // Return a new filter policy that uses a bloom filter with approximately
  45. // the specified number of bits per key. A good value for bits_per_key
  46. // is 10, which yields a filter with ~ 1% false positive rate.
  47. //
  48. // Callers must delete the result after any database that is using the
  49. // result has been closed.
  50. //
  51. // Note: if you are using a custom comparator that ignores some parts
  52. // of the keys being compared, you must not use NewBloomFilterPolicy()
  53. // and must provide your own FilterPolicy that also ignores the
  54. // corresponding parts of the keys. For example, if the comparator
  55. // ignores trailing spaces, it would be incorrect to use a
  56. // FilterPolicy (like NewBloomFilterPolicy) that does not ignore
  57. // trailing spaces in keys.
  58. LEVELDB_EXPORT const FilterPolicy* NewBloomFilterPolicy(int bits_per_key);
  59. } // namespace leveldb
  60. #endif // STORAGE_LEVELDB_INCLUDE_FILTER_POLICY_H_