Moved AsyncFileEncrypted::RandomCache implementation to cpp file

This commit is contained in:
sfc-gh-tclinkenbeard 2021-01-30 00:12:05 -08:00
parent bbf8230462
commit 4563eeba5b
2 changed files with 40 additions and 36 deletions

View File

@ -219,6 +219,38 @@ Future<Void> AsyncFileEncrypted::initializeKey(const Reference<IAsyncFile>& keyF
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") {
state const int bytes = FLOW_KNOBS->ENCRYPTION_BLOCK_SIZE * deterministicRandom()->randomInt(0, 1000);
state std::vector<unsigned char> writeBuffer(bytes, 0);

View File

@ -42,46 +42,18 @@ class AsyncFileEncrypted : public IAsyncFile, public ReferenceCounted<AsyncFileE
static Optional<StreamCipher::Key> key;
static StreamCipher::Key getKey();
template <class K, class V>
// Reading:
class RandomCache {
size_t maxSize;
std::vector<K> vec;
std::unordered_map<K, V> hashMap;
size_t evict() {
ASSERT(vec.size() == maxSize);
auto index = deterministicRandom()->randomInt(0, maxSize);
hashMap.erase(vec[index]);
return index;
}
std::vector<uint16_t> vec;
std::unordered_map<uint16_t, Standalone<StringRef>> hashMap;
size_t evict();
public:
RandomCache(size_t maxSize) : maxSize(maxSize) { vec.reserve(maxSize); }
void insert(const K& key, const V& value) {
auto [it, found] = hashMap.insert({ key, value });
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;
RandomCache(size_t maxSize);
void insert(uint16_t block, const Standalone<StringRef>& value);
Optional<Standalone<StringRef>> get(uint16_t block) const;
} readBuffers;
// Writing (append only):
std::unique_ptr<EncryptionStreamCipher> encryptor;