Moved AsyncFileEncrypted::RandomCache implementation to cpp file
This commit is contained in:
parent
bbf8230462
commit
4563eeba5b
|
@ -219,6 +219,38 @@ Future<Void> AsyncFileEncrypted::initializeKey(const Reference<IAsyncFile>& keyF
|
||||||
return AsyncFileEncryptedImpl::initializeKey(keyFile, offset);
|
return AsyncFileEncryptedImpl::initializeKey(keyFile, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t AsyncFileEncrypted::RandomCache::evict() {
|
||||||
|
ASSERT(vec.size() == maxSize);
|
||||||
|
auto index = deterministicRandom()->randomInt(0, maxSize);
|
||||||
|
hashMap.erase(vec[index]);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
AsyncFileEncrypted::RandomCache::RandomCache(size_t maxSize) : maxSize(maxSize) {
|
||||||
|
vec.reserve(maxSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AsyncFileEncrypted::RandomCache::insert(uint16_t block, const Standalone<StringRef>& value) {
|
||||||
|
auto [_, found] = hashMap.insert({ block, value });
|
||||||
|
if (found) {
|
||||||
|
return;
|
||||||
|
} else if (vec.size() < maxSize) {
|
||||||
|
vec.push_back(block);
|
||||||
|
} else {
|
||||||
|
auto index = evict();
|
||||||
|
vec[index] = block;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<Standalone<StringRef>> AsyncFileEncrypted::RandomCache::get(uint16_t block) const {
|
||||||
|
auto it = hashMap.find(block);
|
||||||
|
if (it == hashMap.end()) {
|
||||||
|
return {};
|
||||||
|
} else {
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("fdbrpc/AsyncFileEncrypted") {
|
TEST_CASE("fdbrpc/AsyncFileEncrypted") {
|
||||||
state const int bytes = FLOW_KNOBS->ENCRYPTION_BLOCK_SIZE * deterministicRandom()->randomInt(0, 1000);
|
state const int bytes = FLOW_KNOBS->ENCRYPTION_BLOCK_SIZE * deterministicRandom()->randomInt(0, 1000);
|
||||||
state std::vector<unsigned char> writeBuffer(bytes, 0);
|
state std::vector<unsigned char> writeBuffer(bytes, 0);
|
||||||
|
|
|
@ -42,46 +42,18 @@ class AsyncFileEncrypted : public IAsyncFile, public ReferenceCounted<AsyncFileE
|
||||||
static Optional<StreamCipher::Key> key;
|
static Optional<StreamCipher::Key> key;
|
||||||
static StreamCipher::Key getKey();
|
static StreamCipher::Key getKey();
|
||||||
|
|
||||||
template <class K, class V>
|
// Reading:
|
||||||
class RandomCache {
|
class RandomCache {
|
||||||
size_t maxSize;
|
size_t maxSize;
|
||||||
std::vector<K> vec;
|
std::vector<uint16_t> vec;
|
||||||
std::unordered_map<K, V> hashMap;
|
std::unordered_map<uint16_t, Standalone<StringRef>> hashMap;
|
||||||
|
size_t evict();
|
||||||
size_t evict() {
|
|
||||||
ASSERT(vec.size() == maxSize);
|
|
||||||
auto index = deterministicRandom()->randomInt(0, maxSize);
|
|
||||||
hashMap.erase(vec[index]);
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RandomCache(size_t maxSize) : maxSize(maxSize) { vec.reserve(maxSize); }
|
RandomCache(size_t maxSize);
|
||||||
|
void insert(uint16_t block, const Standalone<StringRef>& value);
|
||||||
void insert(const K& key, const V& value) {
|
Optional<Standalone<StringRef>> get(uint16_t block) const;
|
||||||
auto [it, found] = hashMap.insert({ key, value });
|
} readBuffers;
|
||||||
if (found) {
|
|
||||||
return;
|
|
||||||
} else if (vec.size() < maxSize) {
|
|
||||||
vec.push_back(key);
|
|
||||||
} else {
|
|
||||||
auto index = evict();
|
|
||||||
vec[index] = key;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Optional<V> get(const K& key) const {
|
|
||||||
auto it = hashMap.find(key);
|
|
||||||
if (it == hashMap.end()) {
|
|
||||||
return {};
|
|
||||||
} else {
|
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Reading:
|
|
||||||
RandomCache<uint16_t, Standalone<StringRef>> readBuffers;
|
|
||||||
|
|
||||||
// Writing (append only):
|
// Writing (append only):
|
||||||
std::unique_ptr<EncryptionStreamCipher> encryptor;
|
std::unique_ptr<EncryptionStreamCipher> encryptor;
|
||||||
|
|
Loading…
Reference in New Issue