15f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian/*
25f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** Copyright 2011, The Android Open Source Project
35f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian **
45f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** Licensed under the Apache License, Version 2.0 (the "License");
55f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** you may not use this file except in compliance with the License.
65f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** You may obtain a copy of the License at
75f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian **
85f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian **     http://www.apache.org/licenses/LICENSE-2.0
95f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian **
105f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** Unless required by applicable law or agreed to in writing, software
115f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** distributed under the License is distributed on an "AS IS" BASIS,
125f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** See the License for the specific language governing permissions and
145f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian ** limitations under the License.
155f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian */
165f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
175f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian#ifndef ANDROID_BLOB_CACHE_H
185f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian#define ANDROID_BLOB_CACHE_H
195f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
205f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian#include <stddef.h>
215f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
22b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian#include <memory>
23b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian#include <vector>
245f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
255f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopiannamespace android {
265f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
275f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian// A BlobCache is an in-memory cache for binary key/value pairs.  A BlobCache
285f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian// does NOT provide any thread-safety guarantees.
295f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian//
305f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian// The cache contents can be serialized to an in-memory buffer or mmap'd file
315f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian// and then reloaded in a subsequent execution of the program.  This
325f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian// serialization is non-portable and the data should only be used by the device
335f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian// that generated it.
34b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopianclass BlobCache {
355f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopianpublic:
365f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // Create an empty blob cache. The blob cache will cache key/value pairs
375f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // with key and value sizes less than or equal to maxKeySize and
385f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // maxValueSize, respectively. The total combined size of ALL cache entries
395f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // (key sizes plus value sizes) will not exceed maxTotalSize.
405f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize);
415f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
425f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // set inserts a new binary value into the cache and associates it with the
435f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // given binary key.  If the key or value are too large for the cache then
445f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // the cache remains unchanged.  This includes the case where a different
455f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // value was previously associated with the given key - the old value will
465f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // remain in the cache.  If the given key and value are small enough to be
475f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // put in the cache (based on the maxKeySize, maxValueSize, and maxTotalSize
485f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // values specified to the BlobCache constructor), then the key/value pair
495f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // will be in the cache after set returns.  Note, however, that a subsequent
505f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // call to set may evict old key/value pairs from the cache.
515f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //
525f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // Preconditions:
535f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   key != NULL
545f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   0 < keySize
555f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   value != NULL
565f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   0 < valueSize
575f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    void set(const void* key, size_t keySize, const void* value,
585f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian            size_t valueSize);
595f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
605f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // get retrieves from the cache the binary value associated with a given
615f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // binary key.  If the key is present in the cache then the length of the
625f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // binary value associated with that key is returned.  If the value argument
635f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // is non-NULL and the size of the cached value is less than valueSize bytes
645f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // then the cached value is copied into the buffer pointed to by the value
655f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // argument.  If the key is not present in the cache then 0 is returned and
665f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // the buffer pointed to by the value argument is not modified.
675f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //
685f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // Note that when calling get multiple times with the same key, the later
695f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // calls may fail, returning 0, even if earlier calls succeeded.  The return
705f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // value must be checked for each call.
715f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //
725f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // Preconditions:
735f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   key != NULL
745f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   0 < keySize
755f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   0 <= valueSize
765f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    size_t get(const void* key, size_t keySize, void* value, size_t valueSize);
775f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
785f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
795f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // getFlattenedSize returns the number of bytes needed to store the entire
805f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // serialized cache.
815f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    size_t getFlattenedSize() const;
825f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
835f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // flatten serializes the current contents of the cache into the memory
845f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // pointed to by 'buffer'.  The serialized cache contents can later be
855f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // loaded into a BlobCache object using the unflatten method.  The contents
865f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // of the BlobCache object will not be modified.
875f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //
885f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // Preconditions:
895f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   size >= this.getFlattenedSize()
90b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian    int flatten(void* buffer, size_t size) const;
915f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
925f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // unflatten replaces the contents of the cache with the serialized cache
935f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // contents in the memory pointed to by 'buffer'.  The previous contents of
945f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // the BlobCache will be evicted from the cache.  If an error occurs while
955f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // unflattening the serialized cache contents then the BlobCache will be
965f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // left in an empty state.
975f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //
98b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian    int unflatten(void const* buffer, size_t size);
995f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1009e7cd07a30a0ed27852ad04d2997f00387b55dcfStan Ilievprotected:
1019e7cd07a30a0ed27852ad04d2997f00387b55dcfStan Iliev    // mMaxTotalSize is the maximum size that all cache entries can occupy. This
1029e7cd07a30a0ed27852ad04d2997f00387b55dcfStan Iliev    // includes space for both keys and values. When a call to BlobCache::set
1039e7cd07a30a0ed27852ad04d2997f00387b55dcfStan Iliev    // would otherwise cause this limit to be exceeded, either the key/value
1049e7cd07a30a0ed27852ad04d2997f00387b55dcfStan Iliev    // pair passed to BlobCache::set will not be cached or other cache entries
1059e7cd07a30a0ed27852ad04d2997f00387b55dcfStan Iliev    // will be evicted from the cache to make room for the new entry.
1069e7cd07a30a0ed27852ad04d2997f00387b55dcfStan Iliev    const size_t mMaxTotalSize;
1079e7cd07a30a0ed27852ad04d2997f00387b55dcfStan Iliev
1085f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopianprivate:
1095f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // Copying is disallowed.
1105f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    BlobCache(const BlobCache&);
1115f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    void operator=(const BlobCache&);
1125f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1135f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // A random function helper to get around MinGW not having nrand48()
1145f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    long int blob_random();
1155f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1165f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // clean evicts a randomly chosen set of entries from the cache such that
1175f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // the total size of all remaining entries is less than mMaxTotalSize/2.
1185f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    void clean();
1195f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1205f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // isCleanable returns true if the cache is full enough for the clean method
1215f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // to have some effect, and false otherwise.
1225f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    bool isCleanable() const;
1235f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1245f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // A Blob is an immutable sized unstructured data blob.
125b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian    class Blob {
1265f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    public:
1275f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        Blob(const void* data, size_t size, bool copyData);
1285f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        ~Blob();
1295f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1305f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        bool operator<(const Blob& rhs) const;
1315f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1325f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        const void* getData() const;
1335f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        size_t getSize() const;
1345f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1355f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    private:
1365f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // Copying is not allowed.
1375f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        Blob(const Blob&);
1385f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        void operator=(const Blob&);
1395f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1405f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mData points to the buffer containing the blob data.
1415f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        const void* mData;
1425f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1435f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mSize is the size of the blob data in bytes.
1445f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        size_t mSize;
1455f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1465f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mOwnsData indicates whether or not this Blob object should free the
1475f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // memory pointed to by mData when the Blob gets destructed.
1485f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        bool mOwnsData;
1495f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    };
1505f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1515f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // A CacheEntry is a single key/value pair in the cache.
1525f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    class CacheEntry {
1535f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    public:
1545f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        CacheEntry();
155b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian        CacheEntry(const std::shared_ptr<Blob>& key, const std::shared_ptr<Blob>& value);
1565f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        CacheEntry(const CacheEntry& ce);
1575f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1585f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        bool operator<(const CacheEntry& rhs) const;
1595f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        const CacheEntry& operator=(const CacheEntry&);
1605f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
161b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian        std::shared_ptr<Blob> getKey() const;
162b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian        std::shared_ptr<Blob> getValue() const;
1635f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
164b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian        void setValue(const std::shared_ptr<Blob>& value);
1655f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1665f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    private:
1675f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1685f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mKey is the key that identifies the cache entry.
169b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian        std::shared_ptr<Blob> mKey;
1705f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1715f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mValue is the cached data associated with the key.
172b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian        std::shared_ptr<Blob> mValue;
1735f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    };
1745f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1755f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // A Header is the header for the entire BlobCache serialization format. No
1765f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // need to make this portable, so we simply write the struct out.
1775f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    struct Header {
1785f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mMagicNumber is the magic number that identifies the data as
1795f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // serialized BlobCache contents.  It must always contain 'Blb$'.
1805f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        uint32_t mMagicNumber;
1815f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1825f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mBlobCacheVersion is the serialization format version.
1835f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        uint32_t mBlobCacheVersion;
1845f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1855f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mDeviceVersion is the device-specific version of the cache.  This can
1865f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // be used to invalidate the cache.
1875f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        uint32_t mDeviceVersion;
1885f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1895f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mNumEntries is number of cache entries following the header in the
1905f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // data.
1915f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        size_t mNumEntries;
1925f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
1935f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mBuildId is the build id of the device when the cache was created.
1945f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // When an update to the build happens (via an OTA or other update) this
1955f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // is used to invalidate the cache.
1965f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        int mBuildIdLength;
1975f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        char mBuildId[];
1985f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    };
1995f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2005f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // An EntryHeader is the header for a serialized cache entry.  No need to
2015f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // make this portable, so we simply write the struct out.  Each EntryHeader
2025f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // is followed imediately by the key data and then the value data.
2035f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //
2045f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // The beginning of each serialized EntryHeader is 4-byte aligned, so the
2055f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // number of bytes that a serialized cache entry will occupy is:
2065f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //
2075f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //   ((sizeof(EntryHeader) + keySize + valueSize) + 3) & ~3
2085f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    //
2095f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    struct EntryHeader {
2105f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mKeySize is the size of the entry key in bytes.
2115f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        size_t mKeySize;
2125f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2135f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mValueSize is the size of the entry value in bytes.
2145f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        size_t mValueSize;
2155f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2165f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // mData contains both the key and value data for the cache entry.  The
2175f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        // key comes first followed immediately by the value.
2185f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian        uint8_t mData[];
2195f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    };
2205f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2215f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // mMaxKeySize is the maximum key size that will be cached. Calls to
2225f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // BlobCache::set with a keySize parameter larger than mMaxKeySize will
2235f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // simply not add the key/value pair to the cache.
2245f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    const size_t mMaxKeySize;
2255f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2265f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // mMaxValueSize is the maximum value size that will be cached. Calls to
2275f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // BlobCache::set with a valueSize parameter larger than mMaxValueSize will
2285f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // simply not add the key/value pair to the cache.
2295f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    const size_t mMaxValueSize;
2305f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2315f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // mTotalSize is the total combined size of all keys and values currently in
2325f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // the cache.
2335f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    size_t mTotalSize;
2345f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2355f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // mRandState is the pseudo-random number generator state. It is passed to
2365f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // nrand48 to generate random numbers when needed.
2375f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    unsigned short mRandState[3];
2385f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2395f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // mCacheEntries stores all the cache entries that are resident in memory.
2405f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian    // Cache entries are added to it by the 'set' method.
241b7f9a2400aaa2e0d29ffefd91576e90036d4cf83Mathias Agopian    std::vector<CacheEntry> mCacheEntries;
2425f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian};
2435f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2445f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian}
2455f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian
2465f549b2089442cb80e8d7f4bd00ac69560375b2cMathias Agopian#endif // ANDROID_BLOB_CACHE_H
247