blob: 8f76d72c18a3d573d935cae67ef728e691ae33b8 [file] [log] [blame]
Jamie Gennis1a209932011-04-28 16:19:45 -07001/*
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
17#ifndef ANDROID_BLOB_CACHE_H
18#define ANDROID_BLOB_CACHE_H
19
20#include <stddef.h>
21
22#include <utils/RefBase.h>
23#include <utils/SortedVector.h>
24#include <utils/threads.h>
25
26namespace android {
27
28// A BlobCache is an in-memory cache for binary key/value pairs. All the public
29// methods are thread-safe.
30//
31// The cache contents can be serialized to a file and reloaded in a subsequent
32// execution of the program. This serialization is non-portable and should only
33// be loaded by the device that generated it.
34class BlobCache : public RefBase {
35public:
36
37 // Create an empty blob cache. The blob cache will cache key/value pairs
38 // with key and value sizes less than or equal to maxKeySize and
39 // maxValueSize, respectively. The total combined size of ALL cache entries
40 // (key sizes plus value sizes) will not exceed maxTotalSize.
41 BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize);
42
43 // set inserts a new binary value into the cache and associates it with the
44 // given binary key. If the key or value are too large for the cache then
45 // the cache remains unchanged. This includes the case where a different
46 // value was previously associated with the given key - the old value will
47 // remain in the cache. If the given key and value are small enough to be
48 // put in the cache (based on the maxKeySize, maxValueSize, and maxTotalSize
49 // values specified to the BlobCache constructor), then the key/value pair
50 // will be in the cache after set returns. Note, however, that a subsequent
51 // call to set may evict old key/value pairs from the cache.
52 //
53 // Preconditions:
54 // key != NULL
55 // 0 < keySize
56 // value != NULL
57 // 0 < valueSize
58 void set(const void* key, size_t keySize, const void* value,
59 size_t valueSize);
60
61 // The get function retrieves from the cache the binary value associated
62 // with a given binary key. If the key is present in the cache then the
63 // length of the binary value associated with that key is returned. If the
64 // value argument is non-NULL and the size of the cached value is less than
65 // valueSize bytes then the cached value is copied into the buffer pointed
66 // to by the value argument. If the key is not present in the cache then 0
67 // is returned and the buffer pointed to by the value argument is not
68 // modified.
69 //
70 // Note that when calling get multiple times with the same key, the later
71 // calls may fail, returning 0, even if earlier calls succeeded. The return
72 // value must be checked for each call.
73 //
74 // Preconditions:
75 // key != NULL
76 // 0 < keySize
77 // 0 <= valueSize
78 size_t get(const void* key, size_t keySize, void* value, size_t valueSize);
79
80private:
81 // Copying is disallowed.
82 BlobCache(const BlobCache&);
83 void operator=(const BlobCache&);
84
85 // clean evicts a randomly chosen set of entries from the cache such that
86 // the total size of all remaining entries is less than mMaxTotalSize/2.
87 void clean();
88
89 // isCleanable returns true if the cache is full enough for the clean method
90 // to have some effect, and false otherwise.
91 bool isCleanable() const;
92
93 // A Blob is an immutable sized unstructured data blob.
94 class Blob : public RefBase {
95 public:
96 Blob(const void* data, size_t size, bool copyData);
97 ~Blob();
98
99 bool operator<(const Blob& rhs) const;
100
101 const void* getData() const;
102 size_t getSize() const;
103
104 private:
105 // Copying is not allowed.
106 Blob(const Blob&);
107 void operator=(const Blob&);
108
109 // mData points to the buffer containing the blob data.
110 const void* mData;
111
112 // mSize is the size of the blob data in bytes.
113 size_t mSize;
114
115 // mOwnsData indicates whether or not this Blob object should free the
116 // memory pointed to by mData when the Blob gets destructed.
117 bool mOwnsData;
118 };
119
120 // A CacheEntry is a single key/value pair in the cache.
121 class CacheEntry {
122 public:
123 CacheEntry();
124 CacheEntry(const sp<Blob>& key, const sp<Blob>& value);
125 CacheEntry(const CacheEntry& ce);
126
127 bool operator<(const CacheEntry& rhs) const;
128 const CacheEntry& operator=(const CacheEntry&);
129
130 sp<Blob> getKey() const;
131 sp<Blob> getValue() const;
132
133 void setValue(const sp<Blob>& value);
134
135 private:
136
137 // mKey is the key that identifies the cache entry.
138 sp<Blob> mKey;
139
140 // mValue is the cached data associated with the key.
141 sp<Blob> mValue;
142 };
143
144 // mMaxKeySize is the maximum key size that will be cached. Calls to
145 // BlobCache::set with a keySize parameter larger than mMaxKeySize will
146 // simply not add the key/value pair to the cache.
147 const size_t mMaxKeySize;
148
149 // mMaxValueSize is the maximum value size that will be cached. Calls to
150 // BlobCache::set with a valueSize parameter larger than mMaxValueSize will
151 // simply not add the key/value pair to the cache.
152 const size_t mMaxValueSize;
153
154 // mMaxTotalSize is the maximum size that all cache entries can occupy. This
155 // includes space for both keys and values. When a call to BlobCache::set
156 // would otherwise cause this limit to be exceeded, either the key/value
157 // pair passed to BlobCache::set will not be cached or other cache entries
158 // will be evicted from the cache to make room for the new entry.
159 const size_t mMaxTotalSize;
160
161 // mTotalSize is the total combined size of all keys and values currently in
162 // the cache.
163 size_t mTotalSize;
164
165 // mRandState is the pseudo-random number generator state. It is passed to
166 // nrand48 to generate random numbers when needed. It must be protected by
167 // mMutex.
168 unsigned short mRandState[3];
169
170 // mCacheEntries stores all the cache entries that are resident in memory.
171 // Cache entries are added to it by the 'set' method.
172 SortedVector<CacheEntry> mCacheEntries;
173
174 // mMutex is used to synchronize access to all member variables. It must be
175 // locked any time the member variables are written or read.
176 Mutex mMutex;
177};
178
179}
180
181#endif // ANDROID_BLOB_CACHE_H