BlobCache.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. ** Copyright 2011, The Android Open Source Project
  3. **
  4. ** Licensed under the Apache License, Version 2.0 (the "License");
  5. ** you may not use this file except in compliance with the License.
  6. ** You may obtain a copy of the License at
  7. **
  8. ** http://www.apache.org/licenses/LICENSE-2.0
  9. **
  10. ** Unless required by applicable law or agreed to in writing, software
  11. ** distributed under the License is distributed on an "AS IS" BASIS,
  12. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ** See the License for the specific language governing permissions and
  14. ** limitations under the License.
  15. */
  16. #ifndef ANDROID_BLOB_CACHE_H
  17. #define ANDROID_BLOB_CACHE_H
  18. #include <stddef.h>
  19. #include <memory>
  20. #include <vector>
  21. namespace android {
  22. // A BlobCache is an in-memory cache for binary key/value pairs. A BlobCache
  23. // does NOT provide any thread-safety guarantees.
  24. //
  25. // The cache contents can be serialized to an in-memory buffer or mmap'd file
  26. // and then reloaded in a subsequent execution of the program. This
  27. // serialization is non-portable and the data should only be used by the device
  28. // that generated it.
  29. class BlobCache {
  30. public:
  31. // Create an empty blob cache. The blob cache will cache key/value pairs
  32. // with key and value sizes less than or equal to maxKeySize and
  33. // maxValueSize, respectively. The total combined size of ALL cache entries
  34. // (key sizes plus value sizes) will not exceed maxTotalSize.
  35. BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize);
  36. // set inserts a new binary value into the cache and associates it with the
  37. // given binary key. If the key or value are too large for the cache then
  38. // the cache remains unchanged. This includes the case where a different
  39. // value was previously associated with the given key - the old value will
  40. // remain in the cache. If the given key and value are small enough to be
  41. // put in the cache (based on the maxKeySize, maxValueSize, and maxTotalSize
  42. // values specified to the BlobCache constructor), then the key/value pair
  43. // will be in the cache after set returns. Note, however, that a subsequent
  44. // call to set may evict old key/value pairs from the cache.
  45. //
  46. // Preconditions:
  47. // key != NULL
  48. // 0 < keySize
  49. // value != NULL
  50. // 0 < valueSize
  51. void set(const void* key, size_t keySize, const void* value,
  52. size_t valueSize);
  53. // get retrieves from the cache the binary value associated with a given
  54. // binary key. If the key is present in the cache then the length of the
  55. // binary value associated with that key is returned. If the value argument
  56. // is non-NULL and the size of the cached value is less than valueSize bytes
  57. // then the cached value is copied into the buffer pointed to by the value
  58. // argument. If the key is not present in the cache then 0 is returned and
  59. // the buffer pointed to by the value argument is not modified.
  60. //
  61. // Note that when calling get multiple times with the same key, the later
  62. // calls may fail, returning 0, even if earlier calls succeeded. The return
  63. // value must be checked for each call.
  64. //
  65. // Preconditions:
  66. // key != NULL
  67. // 0 < keySize
  68. // 0 <= valueSize
  69. size_t get(const void* key, size_t keySize, void* value, size_t valueSize);
  70. // getFlattenedSize returns the number of bytes needed to store the entire
  71. // serialized cache.
  72. size_t getFlattenedSize() const;
  73. // flatten serializes the current contents of the cache into the memory
  74. // pointed to by 'buffer'. The serialized cache contents can later be
  75. // loaded into a BlobCache object using the unflatten method. The contents
  76. // of the BlobCache object will not be modified.
  77. //
  78. // Preconditions:
  79. // size >= this.getFlattenedSize()
  80. int flatten(void* buffer, size_t size) const;
  81. // unflatten replaces the contents of the cache with the serialized cache
  82. // contents in the memory pointed to by 'buffer'. The previous contents of
  83. // the BlobCache will be evicted from the cache. If an error occurs while
  84. // unflattening the serialized cache contents then the BlobCache will be
  85. // left in an empty state.
  86. //
  87. int unflatten(void const* buffer, size_t size);
  88. // clear flushes out all contents of the cache then the BlobCache, leaving
  89. // it in an empty state.
  90. void clear() { mCacheEntries.clear(); }
  91. protected:
  92. // mMaxTotalSize is the maximum size that all cache entries can occupy. This
  93. // includes space for both keys and values. When a call to BlobCache::set
  94. // would otherwise cause this limit to be exceeded, either the key/value
  95. // pair passed to BlobCache::set will not be cached or other cache entries
  96. // will be evicted from the cache to make room for the new entry.
  97. const size_t mMaxTotalSize;
  98. private:
  99. // Copying is disallowed.
  100. BlobCache(const BlobCache&);
  101. void operator=(const BlobCache&);
  102. // A random function helper to get around MinGW not having nrand48()
  103. long int blob_random();
  104. // clean evicts a randomly chosen set of entries from the cache such that
  105. // the total size of all remaining entries is less than mMaxTotalSize/2.
  106. void clean();
  107. // isCleanable returns true if the cache is full enough for the clean method
  108. // to have some effect, and false otherwise.
  109. bool isCleanable() const;
  110. // A Blob is an immutable sized unstructured data blob.
  111. class Blob {
  112. public:
  113. Blob(const void* data, size_t size, bool copyData);
  114. ~Blob();
  115. bool operator<(const Blob& rhs) const;
  116. const void* getData() const;
  117. size_t getSize() const;
  118. private:
  119. // Copying is not allowed.
  120. Blob(const Blob&);
  121. void operator=(const Blob&);
  122. // mData points to the buffer containing the blob data.
  123. const void* mData;
  124. // mSize is the size of the blob data in bytes.
  125. size_t mSize;
  126. // mOwnsData indicates whether or not this Blob object should free the
  127. // memory pointed to by mData when the Blob gets destructed.
  128. bool mOwnsData;
  129. };
  130. // A CacheEntry is a single key/value pair in the cache.
  131. class CacheEntry {
  132. public:
  133. CacheEntry();
  134. CacheEntry(const std::shared_ptr<Blob>& key, const std::shared_ptr<Blob>& value);
  135. CacheEntry(const CacheEntry& ce);
  136. bool operator<(const CacheEntry& rhs) const;
  137. const CacheEntry& operator=(const CacheEntry&);
  138. std::shared_ptr<Blob> getKey() const;
  139. std::shared_ptr<Blob> getValue() const;
  140. void setValue(const std::shared_ptr<Blob>& value);
  141. private:
  142. // mKey is the key that identifies the cache entry.
  143. std::shared_ptr<Blob> mKey;
  144. // mValue is the cached data associated with the key.
  145. std::shared_ptr<Blob> mValue;
  146. };
  147. // A Header is the header for the entire BlobCache serialization format. No
  148. // need to make this portable, so we simply write the struct out.
  149. struct Header {
  150. // mMagicNumber is the magic number that identifies the data as
  151. // serialized BlobCache contents. It must always contain 'Blb$'.
  152. uint32_t mMagicNumber;
  153. // mBlobCacheVersion is the serialization format version.
  154. uint32_t mBlobCacheVersion;
  155. // mDeviceVersion is the device-specific version of the cache. This can
  156. // be used to invalidate the cache.
  157. uint32_t mDeviceVersion;
  158. // mNumEntries is number of cache entries following the header in the
  159. // data.
  160. size_t mNumEntries;
  161. // mBuildId is the build id of the device when the cache was created.
  162. // When an update to the build happens (via an OTA or other update) this
  163. // is used to invalidate the cache.
  164. int mBuildIdLength;
  165. char mBuildId[];
  166. };
  167. // An EntryHeader is the header for a serialized cache entry. No need to
  168. // make this portable, so we simply write the struct out. Each EntryHeader
  169. // is followed imediately by the key data and then the value data.
  170. //
  171. // The beginning of each serialized EntryHeader is 4-byte aligned, so the
  172. // number of bytes that a serialized cache entry will occupy is:
  173. //
  174. // ((sizeof(EntryHeader) + keySize + valueSize) + 3) & ~3
  175. //
  176. struct EntryHeader {
  177. // mKeySize is the size of the entry key in bytes.
  178. size_t mKeySize;
  179. // mValueSize is the size of the entry value in bytes.
  180. size_t mValueSize;
  181. // mData contains both the key and value data for the cache entry. The
  182. // key comes first followed immediately by the value.
  183. uint8_t mData[];
  184. };
  185. // mMaxKeySize is the maximum key size that will be cached. Calls to
  186. // BlobCache::set with a keySize parameter larger than mMaxKeySize will
  187. // simply not add the key/value pair to the cache.
  188. const size_t mMaxKeySize;
  189. // mMaxValueSize is the maximum value size that will be cached. Calls to
  190. // BlobCache::set with a valueSize parameter larger than mMaxValueSize will
  191. // simply not add the key/value pair to the cache.
  192. const size_t mMaxValueSize;
  193. // mTotalSize is the total combined size of all keys and values currently in
  194. // the cache.
  195. size_t mTotalSize;
  196. // mRandState is the pseudo-random number generator state. It is passed to
  197. // nrand48 to generate random numbers when needed.
  198. unsigned short mRandState[3];
  199. // mCacheEntries stores all the cache entries that are resident in memory.
  200. // Cache entries are added to it by the 'set' method.
  201. std::vector<CacheEntry> mCacheEntries;
  202. };
  203. }
  204. #endif // ANDROID_BLOB_CACHE_H