fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
4.0 KiB
103 lines
4.0 KiB
8 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
|
// This source code is licensed under the BSD-style license found in the
|
||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||
|
//
|
||
|
// 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.
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
#include <atomic>
|
||
8 years ago
|
#include <string>
|
||
8 years ago
|
|
||
|
#include "port/port.h"
|
||
|
#include "rocksdb/cache.h"
|
||
|
#include "util/hash.h"
|
||
|
|
||
|
namespace rocksdb {
|
||
|
|
||
|
// Single cache shard interface.
|
||
|
class CacheShard {
|
||
|
public:
|
||
|
CacheShard() = default;
|
||
|
virtual ~CacheShard() = default;
|
||
|
|
||
|
virtual Status Insert(const Slice& key, uint32_t hash, void* value,
|
||
|
size_t charge,
|
||
|
void (*deleter)(const Slice& key, void* value),
|
||
8 years ago
|
Cache::Handle** handle, Cache::Priority priority) = 0;
|
||
8 years ago
|
virtual Cache::Handle* Lookup(const Slice& key, uint32_t hash) = 0;
|
||
8 years ago
|
virtual bool Ref(Cache::Handle* handle) = 0;
|
||
8 years ago
|
virtual void Release(Cache::Handle* handle) = 0;
|
||
|
virtual void Erase(const Slice& key, uint32_t hash) = 0;
|
||
|
virtual void SetCapacity(size_t capacity) = 0;
|
||
|
virtual void SetStrictCapacityLimit(bool strict_capacity_limit) = 0;
|
||
|
virtual size_t GetUsage() const = 0;
|
||
|
virtual size_t GetPinnedUsage() const = 0;
|
||
|
virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t),
|
||
|
bool thread_safe) = 0;
|
||
|
virtual void EraseUnRefEntries() = 0;
|
||
8 years ago
|
virtual std::string GetPrintableOptions() const { return ""; }
|
||
8 years ago
|
};
|
||
|
|
||
|
// Generic cache interface which shards cache by hash of keys. 2^num_shard_bits
|
||
|
// shards will be created, with capacity split evenly to each of the shards.
|
||
|
// Keys are sharded by the highest num_shard_bits bits of hash value.
|
||
|
class ShardedCache : public Cache {
|
||
|
public:
|
||
|
ShardedCache(size_t capacity, int num_shard_bits, bool strict_capacity_limit);
|
||
|
virtual ~ShardedCache() = default;
|
||
8 years ago
|
virtual const char* Name() const override = 0;
|
||
8 years ago
|
virtual CacheShard* GetShard(int shard) = 0;
|
||
|
virtual const CacheShard* GetShard(int shard) const = 0;
|
||
|
virtual void* Value(Handle* handle) override = 0;
|
||
|
virtual size_t GetCharge(Handle* handle) const = 0;
|
||
|
virtual uint32_t GetHash(Handle* handle) const = 0;
|
||
|
virtual void DisownData() override = 0;
|
||
|
|
||
|
virtual void SetCapacity(size_t capacity) override;
|
||
|
virtual void SetStrictCapacityLimit(bool strict_capacity_limit) override;
|
||
|
|
||
|
virtual Status Insert(const Slice& key, void* value, size_t charge,
|
||
|
void (*deleter)(const Slice& key, void* value),
|
||
8 years ago
|
Handle** handle, Priority priority) override;
|
||
8 years ago
|
virtual Handle* Lookup(const Slice& key, Statistics* stats) override;
|
||
8 years ago
|
virtual bool Ref(Handle* handle) override;
|
||
8 years ago
|
virtual void Release(Handle* handle) override;
|
||
|
virtual void Erase(const Slice& key) override;
|
||
|
virtual uint64_t NewId() override;
|
||
|
virtual size_t GetCapacity() const override;
|
||
|
virtual bool HasStrictCapacityLimit() const override;
|
||
|
virtual size_t GetUsage() const override;
|
||
|
virtual size_t GetUsage(Handle* handle) const override;
|
||
|
virtual size_t GetPinnedUsage() const override;
|
||
|
virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t),
|
||
|
bool thread_safe) override;
|
||
|
virtual void EraseUnRefEntries() override;
|
||
8 years ago
|
virtual std::string GetPrintableOptions() const override;
|
||
8 years ago
|
|
||
8 years ago
|
int GetNumShardBits() const { return num_shard_bits_; }
|
||
|
|
||
8 years ago
|
private:
|
||
|
static inline uint32_t HashSlice(const Slice& s) {
|
||
|
return Hash(s.data(), s.size(), 0);
|
||
|
}
|
||
|
|
||
|
uint32_t Shard(uint32_t hash) {
|
||
|
// Note, hash >> 32 yields hash in gcc, not the zero we expect!
|
||
|
return (num_shard_bits_ > 0) ? (hash >> (32 - num_shard_bits_)) : 0;
|
||
|
}
|
||
|
|
||
|
int num_shard_bits_;
|
||
|
mutable port::Mutex capacity_mutex_;
|
||
|
size_t capacity_;
|
||
|
bool strict_capacity_limit_;
|
||
|
std::atomic<uint64_t> last_id_;
|
||
|
};
|
||
|
|
||
8 years ago
|
extern int GetDefaultCacheShardBits(size_t capacity);
|
||
|
|
||
8 years ago
|
} // namespace rocksdb
|