sapling/eden/fs/store/MemoryLocalStore.cpp
Xiao Shi b8542c7a74 use F14NodeMap with heterogeneous lookup / mutation as StringKeyedUnorderedMap|Set
Summary:
`F14NodeMap` has proven to be a safe and more performant drop-in replacement
for `std::unordered_map`. With the ability to do heterogeneous lookup and
mutation, we no longer need `StringKeyedUnorderedMap`, whose main purpose was
to avoid unnecessary string copies during lookups and inserts.

Reviewed By: nbronson

Differential Revision: D9124737

fbshipit-source-id: 81d5be1df9096ac258a3dc1523a6a0f5d6b9e862
2018-08-23 12:07:15 -07:00

105 lines
2.8 KiB
C++

/*
* Copyright (c) 2018-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.
*
*/
#include "eden/fs/store/MemoryLocalStore.h"
#include <folly/String.h>
#include "eden/fs/store/StoreResult.h"
namespace facebook {
namespace eden {
using folly::StringPiece;
namespace {
class MemoryWriteBatch : public LocalStore::WriteBatch {
public:
explicit MemoryWriteBatch(MemoryLocalStore* store) : store_(store) {
storage_.resize(LocalStore::KeySpace::End);
}
void put(
LocalStore::KeySpace keySpace,
folly::ByteRange key,
folly::ByteRange value) override {
storage_[keySpace][StringPiece(key)] = StringPiece(value).str();
}
void put(
LocalStore::KeySpace keySpace,
folly::ByteRange key,
std::vector<folly::ByteRange> valueSlices) override {
std::string value;
for (const auto& slice : valueSlices) {
value.append(reinterpret_cast<const char*>(slice.data()), slice.size());
}
put(keySpace, key, StringPiece(value));
}
void flush() override {
for (size_t keySpace = 0; keySpace < storage_.size(); ++keySpace) {
for (const auto& it : storage_[keySpace]) {
store_->put(
static_cast<LocalStore::KeySpace>(keySpace),
folly::StringPiece(it.first),
StringPiece(it.second));
}
storage_[keySpace].clear();
}
}
private:
MemoryLocalStore* store_;
std::vector<folly::StringKeyedUnorderedMap<std::string>> storage_;
};
} // namespace
MemoryLocalStore::MemoryLocalStore() {
storage_->resize(KeySpace::End);
}
void MemoryLocalStore::close() {}
void MemoryLocalStore::clearKeySpace(KeySpace keySpace) {
(*storage_.wlock())[keySpace].clear();
}
void MemoryLocalStore::compactKeySpace(KeySpace) {}
StoreResult MemoryLocalStore::get(
LocalStore::KeySpace keySpace,
folly::ByteRange key) const {
auto store = storage_.rlock();
auto it = (*store)[keySpace].find(StringPiece(key));
if (it == (*store)[keySpace].end()) {
return StoreResult();
}
return StoreResult(std::string(it->second));
}
bool MemoryLocalStore::hasKey(
LocalStore::KeySpace keySpace,
folly::ByteRange key) const {
auto store = storage_.rlock();
auto it = (*store)[keySpace].find(StringPiece(key));
return it != (*store)[keySpace].end();
}
void MemoryLocalStore::put(
LocalStore::KeySpace keySpace,
folly::ByteRange key,
folly::ByteRange value) {
(*storage_.wlock())[keySpace][StringPiece(key)] = StringPiece(value).str();
}
std::unique_ptr<LocalStore::WriteBatch> MemoryLocalStore::beginWrite(size_t) {
return std::make_unique<MemoryWriteBatch>(this);
}
} // namespace eden
} // namespace facebook