2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2011-03-30 20:35:40 +02:00
|
|
|
#include "leveldb/cache.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
#include <vector>
|
2013-04-04 03:53:42 +02:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
// Conversions between numeric keys/values and the types expected by Cache.
|
|
|
|
static std::string EncodeKey(int k) {
|
|
|
|
std::string result;
|
|
|
|
PutFixed32(&result, k);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static int DecodeKey(const Slice& k) {
|
|
|
|
assert(k.size() == 4);
|
|
|
|
return DecodeFixed32(k.data());
|
|
|
|
}
|
|
|
|
static void* EncodeValue(uintptr_t v) { return reinterpret_cast<void*>(v); }
|
|
|
|
static int DecodeValue(void* v) { return reinterpret_cast<uintptr_t>(v); }
|
|
|
|
|
|
|
|
class CacheTest {
|
|
|
|
public:
|
|
|
|
static CacheTest* current_;
|
|
|
|
|
|
|
|
static void Deleter(const Slice& key, void* v) {
|
|
|
|
current_->deleted_keys_.push_back(DecodeKey(key));
|
|
|
|
current_->deleted_values_.push_back(DecodeValue(v));
|
|
|
|
}
|
|
|
|
|
2011-08-22 23:08:51 +02:00
|
|
|
static const int kCacheSize = 1000;
|
2011-03-18 23:37:00 +01:00
|
|
|
std::vector<int> deleted_keys_;
|
|
|
|
std::vector<int> deleted_values_;
|
2013-01-20 11:07:13 +01:00
|
|
|
shared_ptr<Cache> cache_;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
CacheTest() : cache_(NewLRUCache(kCacheSize)) {
|
|
|
|
current_ = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~CacheTest() {
|
|
|
|
}
|
|
|
|
|
|
|
|
int Lookup(int key) {
|
|
|
|
Cache::Handle* handle = cache_->Lookup(EncodeKey(key));
|
2013-03-01 03:04:58 +01:00
|
|
|
const int r = (handle == nullptr) ? -1 : DecodeValue(cache_->Value(handle));
|
|
|
|
if (handle != nullptr) {
|
2011-03-18 23:37:00 +01:00
|
|
|
cache_->Release(handle);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Insert(int key, int value, int charge = 1) {
|
|
|
|
cache_->Release(cache_->Insert(EncodeKey(key), EncodeValue(value), charge,
|
|
|
|
&CacheTest::Deleter));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Erase(int key) {
|
|
|
|
cache_->Erase(EncodeKey(key));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
CacheTest* CacheTest::current_;
|
|
|
|
|
|
|
|
TEST(CacheTest, HitAndMiss) {
|
|
|
|
ASSERT_EQ(-1, Lookup(100));
|
|
|
|
|
|
|
|
Insert(100, 101);
|
|
|
|
ASSERT_EQ(101, Lookup(100));
|
|
|
|
ASSERT_EQ(-1, Lookup(200));
|
|
|
|
ASSERT_EQ(-1, Lookup(300));
|
|
|
|
|
|
|
|
Insert(200, 201);
|
|
|
|
ASSERT_EQ(101, Lookup(100));
|
|
|
|
ASSERT_EQ(201, Lookup(200));
|
|
|
|
ASSERT_EQ(-1, Lookup(300));
|
|
|
|
|
|
|
|
Insert(100, 102);
|
|
|
|
ASSERT_EQ(102, Lookup(100));
|
|
|
|
ASSERT_EQ(201, Lookup(200));
|
|
|
|
ASSERT_EQ(-1, Lookup(300));
|
|
|
|
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(1U, deleted_keys_.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
ASSERT_EQ(100, deleted_keys_[0]);
|
|
|
|
ASSERT_EQ(101, deleted_values_[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CacheTest, Erase) {
|
|
|
|
Erase(200);
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(0U, deleted_keys_.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
Insert(100, 101);
|
|
|
|
Insert(200, 201);
|
|
|
|
Erase(100);
|
|
|
|
ASSERT_EQ(-1, Lookup(100));
|
|
|
|
ASSERT_EQ(201, Lookup(200));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(1U, deleted_keys_.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
ASSERT_EQ(100, deleted_keys_[0]);
|
|
|
|
ASSERT_EQ(101, deleted_values_[0]);
|
|
|
|
|
|
|
|
Erase(100);
|
|
|
|
ASSERT_EQ(-1, Lookup(100));
|
|
|
|
ASSERT_EQ(201, Lookup(200));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(1U, deleted_keys_.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CacheTest, EntriesArePinned) {
|
|
|
|
Insert(100, 101);
|
|
|
|
Cache::Handle* h1 = cache_->Lookup(EncodeKey(100));
|
|
|
|
ASSERT_EQ(101, DecodeValue(cache_->Value(h1)));
|
|
|
|
|
|
|
|
Insert(100, 102);
|
|
|
|
Cache::Handle* h2 = cache_->Lookup(EncodeKey(100));
|
|
|
|
ASSERT_EQ(102, DecodeValue(cache_->Value(h2)));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(0U, deleted_keys_.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
cache_->Release(h1);
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(1U, deleted_keys_.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
ASSERT_EQ(100, deleted_keys_[0]);
|
|
|
|
ASSERT_EQ(101, deleted_values_[0]);
|
|
|
|
|
|
|
|
Erase(100);
|
|
|
|
ASSERT_EQ(-1, Lookup(100));
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(1U, deleted_keys_.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
cache_->Release(h2);
|
2012-11-06 21:02:18 +01:00
|
|
|
ASSERT_EQ(2U, deleted_keys_.size());
|
2011-03-18 23:37:00 +01:00
|
|
|
ASSERT_EQ(100, deleted_keys_[1]);
|
|
|
|
ASSERT_EQ(102, deleted_values_[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CacheTest, EvictionPolicy) {
|
|
|
|
Insert(100, 101);
|
|
|
|
Insert(200, 201);
|
|
|
|
|
|
|
|
// Frequently used entry must be kept around
|
2011-08-22 23:08:51 +02:00
|
|
|
for (int i = 0; i < kCacheSize + 100; i++) {
|
2011-03-18 23:37:00 +01:00
|
|
|
Insert(1000+i, 2000+i);
|
|
|
|
ASSERT_EQ(2000+i, Lookup(1000+i));
|
|
|
|
ASSERT_EQ(101, Lookup(100));
|
|
|
|
}
|
|
|
|
ASSERT_EQ(101, Lookup(100));
|
2011-08-22 23:08:51 +02:00
|
|
|
ASSERT_EQ(-1, Lookup(200));
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2011-08-22 23:08:51 +02:00
|
|
|
TEST(CacheTest, HeavyEntries) {
|
|
|
|
// Add a bunch of light and heavy entries and then count the combined
|
|
|
|
// size of items still in the cache, which must be approximately the
|
|
|
|
// same as the total capacity.
|
|
|
|
const int kLight = 1;
|
|
|
|
const int kHeavy = 10;
|
|
|
|
int added = 0;
|
|
|
|
int index = 0;
|
|
|
|
while (added < 2*kCacheSize) {
|
|
|
|
const int weight = (index & 1) ? kLight : kHeavy;
|
|
|
|
Insert(index, 1000+index, weight);
|
|
|
|
added += weight;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cached_weight = 0;
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
const int weight = (i & 1 ? kLight : kHeavy);
|
|
|
|
int r = Lookup(i);
|
|
|
|
if (r >= 0) {
|
|
|
|
cached_weight += weight;
|
|
|
|
ASSERT_EQ(1000+i, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_LE(cached_weight, kCacheSize + kCacheSize/10);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CacheTest, NewId) {
|
|
|
|
uint64_t a = cache_->NewId();
|
|
|
|
uint64_t b = cache_->NewId();
|
|
|
|
ASSERT_NE(a, b);
|
|
|
|
}
|
|
|
|
|
2013-04-04 03:53:42 +02:00
|
|
|
|
|
|
|
class Value {
|
|
|
|
private:
|
|
|
|
int v_;
|
|
|
|
public:
|
|
|
|
Value(int v) : v_(v) { }
|
|
|
|
|
|
|
|
~Value() { std::cout << v_ << " is destructed\n"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
void deleter(const Slice& key, void* value) {
|
|
|
|
delete (Value *)value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CacheTest, BadEviction) {
|
|
|
|
int n = 10;
|
|
|
|
|
|
|
|
// a LRUCache with n entries and one shard only
|
|
|
|
std::shared_ptr<Cache> cache = NewLRUCache(n, 0);
|
|
|
|
|
|
|
|
std::vector<Cache::Handle*> handles(n+1);
|
|
|
|
|
|
|
|
// Insert n+1 entries, but not releasing.
|
|
|
|
for (int i = 0; i < n+1; i++) {
|
|
|
|
std::string key = std::to_string(i+1);
|
|
|
|
handles[i] = cache->Insert(key, new Value(i+1), 1, &deleter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Guess what's in the cache now?
|
|
|
|
for (int i = 0; i < n+1; i++) {
|
|
|
|
std::string key = std::to_string(i+1);
|
|
|
|
auto h = cache->Lookup(key);
|
|
|
|
std::cout << key << (h?" found\n":" not found\n");
|
|
|
|
// Only the first entry should be missing
|
|
|
|
ASSERT_TRUE(h || i == 0);
|
|
|
|
if (h) cache->Release(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < n+1; i++) {
|
|
|
|
cache->Release(handles[i]);
|
|
|
|
}
|
|
|
|
std::cout << "Poor entries\n";
|
|
|
|
}
|
|
|
|
|
2011-10-31 18:22:06 +01:00
|
|
|
} // namespace leveldb
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
return leveldb::test::RunAllTests();
|
|
|
|
}
|