2011-02-25 03:56:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ANDROID_UTILS_GENERATION_CACHE_H
|
|
|
|
#define ANDROID_UTILS_GENERATION_CACHE_H
|
|
|
|
|
|
|
|
#include <utils/KeyedVector.h>
|
|
|
|
#include <utils/RefBase.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GenerationCache callback used when an item is removed
|
|
|
|
*/
|
|
|
|
template<typename EntryKey, typename EntryValue>
|
|
|
|
class OnEntryRemoved {
|
|
|
|
public:
|
|
|
|
virtual ~OnEntryRemoved() { };
|
|
|
|
virtual void operator()(EntryKey& key, EntryValue& value) = 0;
|
|
|
|
}; // class OnEntryRemoved
|
|
|
|
|
|
|
|
template<typename EntryKey, typename EntryValue>
|
|
|
|
struct Entry: public LightRefBase<Entry<EntryKey, EntryValue> > {
|
2011-11-11 23:40:13 +00:00
|
|
|
Entry(const Entry<EntryKey, EntryValue>& e) :
|
|
|
|
key(e.key), value(e.value),
|
|
|
|
parent(e.parent), child(e.child) { }
|
|
|
|
Entry(const EntryKey& key, const EntryValue& value) :
|
|
|
|
key(key), value(value) { }
|
2011-02-25 03:56:18 +00:00
|
|
|
|
|
|
|
EntryKey key;
|
|
|
|
EntryValue value;
|
|
|
|
|
2011-11-11 23:40:13 +00:00
|
|
|
sp<Entry<EntryKey, EntryValue> > parent; // next older entry
|
|
|
|
sp<Entry<EntryKey, EntryValue> > child; // next younger entry
|
2011-02-25 03:56:18 +00:00
|
|
|
}; // struct Entry
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A LRU type cache
|
|
|
|
*/
|
|
|
|
template<typename K, typename V>
|
|
|
|
class GenerationCache {
|
|
|
|
public:
|
|
|
|
GenerationCache(uint32_t maxCapacity);
|
|
|
|
virtual ~GenerationCache();
|
|
|
|
|
|
|
|
enum Capacity {
|
|
|
|
kUnlimitedCapacity,
|
|
|
|
};
|
|
|
|
|
|
|
|
void setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener);
|
|
|
|
|
2011-11-11 23:40:13 +00:00
|
|
|
size_t size() const;
|
2011-02-25 03:56:18 +00:00
|
|
|
|
2011-11-11 23:40:13 +00:00
|
|
|
void clear();
|
2011-02-25 03:56:18 +00:00
|
|
|
|
2011-11-11 23:40:13 +00:00
|
|
|
bool contains(const K& key) const;
|
|
|
|
const K& getKeyAt(size_t index) const;
|
|
|
|
const V& getValueAt(size_t index) const;
|
2011-02-25 03:56:18 +00:00
|
|
|
|
2011-11-11 23:40:13 +00:00
|
|
|
const V& get(const K& key);
|
|
|
|
bool put(const K& key, const V& value);
|
2011-02-25 03:56:18 +00:00
|
|
|
|
2011-11-11 23:40:13 +00:00
|
|
|
void removeAt(ssize_t index);
|
|
|
|
bool remove(const K& key);
|
|
|
|
bool removeOldest();
|
2011-02-25 03:56:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
KeyedVector<K, sp<Entry<K, V> > > mCache;
|
|
|
|
uint32_t mMaxCapacity;
|
|
|
|
|
|
|
|
OnEntryRemoved<K, V>* mListener;
|
|
|
|
|
|
|
|
sp<Entry<K, V> > mOldest;
|
|
|
|
sp<Entry<K, V> > mYoungest;
|
2011-11-11 23:40:13 +00:00
|
|
|
|
|
|
|
void attachToCache(const sp<Entry<K, V> >& entry);
|
|
|
|
void detachFromCache(const sp<Entry<K, V> >& entry);
|
2011-12-09 02:19:39 +00:00
|
|
|
|
|
|
|
const V mNullValue;
|
2011-02-25 03:56:18 +00:00
|
|
|
}; // class GenerationCache
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
GenerationCache<K, V>::GenerationCache(uint32_t maxCapacity): mMaxCapacity(maxCapacity),
|
2011-12-09 02:19:39 +00:00
|
|
|
mListener(NULL), mNullValue(NULL) {
|
2011-02-25 03:56:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
GenerationCache<K, V>::~GenerationCache() {
|
|
|
|
clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
uint32_t GenerationCache<K, V>::size() const {
|
|
|
|
return mCache.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should be set by the user of the Cache so that the callback is called whenever an item is
|
|
|
|
* removed from the cache
|
|
|
|
*/
|
|
|
|
template<typename K, typename V>
|
|
|
|
void GenerationCache<K, V>::setOnEntryRemovedListener(OnEntryRemoved<K, V>* listener) {
|
|
|
|
mListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
void GenerationCache<K, V>::clear() {
|
|
|
|
if (mListener) {
|
|
|
|
for (uint32_t i = 0; i < mCache.size(); i++) {
|
|
|
|
sp<Entry<K, V> > entry = mCache.valueAt(i);
|
|
|
|
if (mListener) {
|
|
|
|
(*mListener)(entry->key, entry->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mCache.clear();
|
|
|
|
mYoungest.clear();
|
|
|
|
mOldest.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
bool GenerationCache<K, V>::contains(const K& key) const {
|
2011-02-25 03:56:18 +00:00
|
|
|
return mCache.indexOfKey(key) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
const K& GenerationCache<K, V>::getKeyAt(size_t index) const {
|
2011-02-25 03:56:18 +00:00
|
|
|
return mCache.keyAt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
const V& GenerationCache<K, V>::getValueAt(size_t index) const {
|
2011-02-25 03:56:18 +00:00
|
|
|
return mCache.valueAt(index)->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
const V& GenerationCache<K, V>::get(const K& key) {
|
2011-02-25 03:56:18 +00:00
|
|
|
ssize_t index = mCache.indexOfKey(key);
|
|
|
|
if (index >= 0) {
|
2011-11-11 23:40:13 +00:00
|
|
|
const sp<Entry<K, V> >& entry = mCache.valueAt(index);
|
|
|
|
detachFromCache(entry);
|
|
|
|
attachToCache(entry);
|
|
|
|
return entry->value;
|
2011-02-25 03:56:18 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 02:19:39 +00:00
|
|
|
return mNullValue;
|
2011-02-25 03:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
bool GenerationCache<K, V>::put(const K& key, const V& value) {
|
2011-02-25 03:56:18 +00:00
|
|
|
if (mMaxCapacity != kUnlimitedCapacity && mCache.size() >= mMaxCapacity) {
|
|
|
|
removeOldest();
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t index = mCache.indexOfKey(key);
|
|
|
|
if (index < 0) {
|
2011-11-11 23:40:13 +00:00
|
|
|
sp<Entry<K, V> > entry = new Entry<K, V>(key, value);
|
|
|
|
mCache.add(key, entry);
|
|
|
|
attachToCache(entry);
|
2011-02-25 03:56:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
bool GenerationCache<K, V>::remove(const K& key) {
|
2011-02-25 03:56:18 +00:00
|
|
|
ssize_t index = mCache.indexOfKey(key);
|
|
|
|
if (index >= 0) {
|
2011-11-11 23:40:13 +00:00
|
|
|
removeAt(index);
|
|
|
|
return true;
|
2011-02-25 03:56:18 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 23:40:13 +00:00
|
|
|
return false;
|
2011-02-25 03:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
void GenerationCache<K, V>::removeAt(ssize_t index) {
|
2011-02-25 03:56:18 +00:00
|
|
|
sp<Entry<K, V> > entry = mCache.valueAt(index);
|
|
|
|
if (mListener) {
|
|
|
|
(*mListener)(entry->key, entry->value);
|
|
|
|
}
|
|
|
|
mCache.removeItemsAt(index, 1);
|
|
|
|
detachFromCache(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
bool GenerationCache<K, V>::removeOldest() {
|
2011-02-25 03:56:18 +00:00
|
|
|
if (mOldest.get()) {
|
|
|
|
ssize_t index = mCache.indexOfKey(mOldest->key);
|
|
|
|
if (index >= 0) {
|
2011-11-11 23:40:13 +00:00
|
|
|
removeAt(index);
|
|
|
|
return true;
|
2011-02-25 03:56:18 +00:00
|
|
|
}
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("GenerationCache: removeOldest failed to find the item in the cache "
|
2011-11-11 23:40:13 +00:00
|
|
|
"with the given key, but we know it must be in there. "
|
|
|
|
"Is the key comparator kaput?");
|
2011-02-25 03:56:18 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 23:40:13 +00:00
|
|
|
return false;
|
2011-02-25 03:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
void GenerationCache<K, V>::attachToCache(const sp<Entry<K, V> >& entry) {
|
2011-02-25 03:56:18 +00:00
|
|
|
if (!mYoungest.get()) {
|
|
|
|
mYoungest = mOldest = entry;
|
|
|
|
} else {
|
|
|
|
entry->parent = mYoungest;
|
|
|
|
mYoungest->child = entry;
|
|
|
|
mYoungest = entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
2011-11-11 23:40:13 +00:00
|
|
|
void GenerationCache<K, V>::detachFromCache(const sp<Entry<K, V> >& entry) {
|
2011-02-25 03:56:18 +00:00
|
|
|
if (entry->parent.get()) {
|
|
|
|
entry->parent->child = entry->child;
|
2011-11-11 23:40:13 +00:00
|
|
|
} else {
|
|
|
|
mOldest = entry->child;
|
2011-02-25 03:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->child.get()) {
|
|
|
|
entry->child->parent = entry->parent;
|
2011-11-11 23:40:13 +00:00
|
|
|
} else {
|
2011-02-25 03:56:18 +00:00
|
|
|
mYoungest = entry->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->parent.clear();
|
|
|
|
entry->child.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
#endif // ANDROID_UTILS_GENERATION_CACHE_H
|