作者: 谢瑞阳 10225101483 徐翔宇 10225101535

99 lines
3.4 KiB

  1. // Copyright (c) 2011 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 Cache is an interface that maps keys to values. It has internal
  6. // synchronization and may be safely accessed concurrently from
  7. // multiple threads. It may automatically evict entries to make room
  8. // for new entries. Values have a specified charge against the cache
  9. // capacity. For example, a cache where the values are variable
  10. // length strings, may use the length of the string as the charge for
  11. // the string.
  12. //
  13. // A builtin cache implementation with a least-recently-used eviction
  14. // policy is provided. Clients may use their own implementations if
  15. // they want something more sophisticated (like scan-resistance, a
  16. // custom eviction policy, variable cache sizing, etc.)
  17. #ifndef STORAGE_LEVELDB_INCLUDE_CACHE_H_
  18. #define STORAGE_LEVELDB_INCLUDE_CACHE_H_
  19. #include <stdint.h>
  20. #include "leveldb/slice.h"
  21. namespace leveldb {
  22. class Cache;
  23. // Create a new cache with a fixed size capacity. This implementation
  24. // of Cache uses a least-recently-used eviction policy.
  25. extern Cache* NewLRUCache(size_t capacity);
  26. class Cache {
  27. public:
  28. Cache() { }
  29. // Destroys all existing entries by calling the "deleter"
  30. // function that was passed to the constructor.
  31. virtual ~Cache();
  32. // Opaque handle to an entry stored in the cache.
  33. struct Handle { };
  34. // Insert a mapping from key->value into the cache and assign it
  35. // the specified charge against the total cache capacity.
  36. //
  37. // Returns a handle that corresponds to the mapping. The caller
  38. // must call this->Release(handle) when the returned mapping is no
  39. // longer needed.
  40. //
  41. // When the inserted entry is no longer needed, the key and
  42. // value will be passed to "deleter".
  43. virtual Handle* Insert(const Slice& key, void* value, size_t charge,
  44. void (*deleter)(const Slice& key, void* value)) = 0;
  45. // If the cache has no mapping for "key", returns NULL.
  46. //
  47. // Else return a handle that corresponds to the mapping. The caller
  48. // must call this->Release(handle) when the returned mapping is no
  49. // longer needed.
  50. virtual Handle* Lookup(const Slice& key) = 0;
  51. // Release a mapping returned by a previous Lookup().
  52. // REQUIRES: handle must not have been released yet.
  53. // REQUIRES: handle must have been returned by a method on *this.
  54. virtual void Release(Handle* handle) = 0;
  55. // Return the value encapsulated in a handle returned by a
  56. // successful Lookup().
  57. // REQUIRES: handle must not have been released yet.
  58. // REQUIRES: handle must have been returned by a method on *this.
  59. virtual void* Value(Handle* handle) = 0;
  60. // If the cache contains entry for key, erase it. Note that the
  61. // underlying entry will be kept around until all existing handles
  62. // to it have been released.
  63. virtual void Erase(const Slice& key) = 0;
  64. // Return a new numeric id. May be used by multiple clients who are
  65. // sharing the same cache to partition the key space. Typically the
  66. // client will allocate a new id at startup and prepend the id to
  67. // its cache keys.
  68. virtual uint64_t NewId() = 0;
  69. private:
  70. void LRU_Remove(Handle* e);
  71. void LRU_Append(Handle* e);
  72. void Unref(Handle* e);
  73. struct Rep;
  74. Rep* rep_;
  75. // No copying allowed
  76. Cache(const Cache&);
  77. void operator=(const Cache&);
  78. };
  79. }
  80. #endif // STORAGE_LEVELDB_UTIL_CACHE_H_