Skip to content

Commit

Permalink
fix: from phmap to std::unordered_map
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Nov 7, 2024
1 parent 7f21324 commit de2f63f
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 35 deletions.
4 changes: 0 additions & 4 deletions src/kv/kv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

int64_t KV::lastTimestamp_ = 0;
uint64_t KV::counter_ = 0;
std::mutex KV::mutex_ = {};

KVStore &KVStore::getInstance() {
return inject<KVStore>();
Expand All @@ -31,7 +30,6 @@ void KVStore::set(const std::string &key, const std::initializer_list<std::pair<
}

void KVStore::set(const std::string &key, const ValueWrapper &value) {
std::scoped_lock lock(mutex_);
return setLocked(key, value);
}

Expand All @@ -58,7 +56,6 @@ void KVStore::setLocked(const std::string &key, const ValueWrapper &value) {

std::optional<ValueWrapper> KVStore::get(const std::string &key, bool forceLoad /*= false */) {
logger.trace("KVStore::get({})", key);
std::scoped_lock lock(mutex_);
if (forceLoad || !store_.contains(key)) {
auto value = load(key);
if (value) {
Expand All @@ -77,7 +74,6 @@ std::optional<ValueWrapper> KVStore::get(const std::string &key, bool forceLoad
}

std::unordered_set<std::string> KVStore::keys(const std::string &prefix /*= ""*/) {
std::scoped_lock lock(mutex_);
std::unordered_set<std::string> keys;
for (const auto &[key, value] : store_) {
if (key.find(prefix) == 0) {
Expand Down
18 changes: 3 additions & 15 deletions src/kv/kv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

#ifndef USE_PRECOMPILED_HEADERS
#include <string>
#include <mutex>
#include <initializer_list>
#include <parallel_hashmap/phmap.h>
#include <optional>
#include <unordered_set>
#include <iomanip>
Expand Down Expand Up @@ -47,8 +45,6 @@ class KV : public std::enable_shared_from_this<KV> {
}

static std::string generateUUID() {
std::lock_guard<std::mutex> lock(mutex_);

const auto now = std::chrono::system_clock::now().time_since_epoch();
const auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();

Expand All @@ -69,7 +65,6 @@ class KV : public std::enable_shared_from_this<KV> {
private:
static int64_t lastTimestamp_;
static uint64_t counter_;
static std::mutex mutex_;
};

class KVStore : public KV {
Expand All @@ -87,7 +82,6 @@ class KVStore : public KV {
std::optional<ValueWrapper> get(const std::string &key, bool forceLoad = false) override;

void flush() override {
std::scoped_lock lock(mutex_);
KV::flush();
store_.clear();
}
Expand All @@ -96,13 +90,8 @@ class KVStore : public KV {
std::unordered_set<std::string> keys(const std::string &prefix = "") override;

protected:
phmap::parallel_flat_hash_map<std::string, std::pair<ValueWrapper, std::list<std::string>::iterator>> getStore() {
std::scoped_lock lock(mutex_);
phmap::parallel_flat_hash_map<std::string, std::pair<ValueWrapper, std::list<std::string>::iterator>> copy;
for (const auto &[key, value] : store_) {
copy.try_emplace(key, value);
}
return copy;
const std::unordered_map<std::string, std::pair<ValueWrapper, std::list<std::string>::iterator>> &getStore() {
return store_;
}

protected:
Expand All @@ -115,9 +104,8 @@ class KVStore : public KV {
private:
void setLocked(const std::string &key, const ValueWrapper &value);

phmap::parallel_flat_hash_map<std::string, std::pair<ValueWrapper, std::list<std::string>::iterator>> store_;
std::unordered_map<std::string, std::pair<ValueWrapper, std::list<std::string>::iterator>> store_;
std::list<std::string> lruQueue_;
std::mutex mutex_;
};

class ScopedKV final : public KV {
Expand Down
3 changes: 1 addition & 2 deletions src/kv/kv_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ class ValueWrapper;
#include <vector>
#include <memory>
#include <variant>
#include <parallel_hashmap/phmap.h>
#endif

using StringType = std::string;
using BooleanType = bool;
using IntType = int;
using DoubleType = double;
using ArrayType = std::vector<ValueWrapper>;
using MapType = phmap::flat_hash_map<std::string, std::shared_ptr<ValueWrapper>>;
using MapType = std::unordered_map<std::string, std::shared_ptr<ValueWrapper>>;

using ValueVariant = std::variant<StringType, BooleanType, IntType, DoubleType, ArrayType, MapType>;
2 changes: 1 addition & 1 deletion src/kv/value_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ValueWrapper::ValueWrapper(int value, uint64_t timestamp) :
ValueWrapper::ValueWrapper(double value, uint64_t timestamp) :
data_(value), timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }

ValueWrapper::ValueWrapper(const phmap::flat_hash_map<std::string, ValueWrapper> &value, uint64_t timestamp) :
ValueWrapper::ValueWrapper(const std::unordered_map<std::string, ValueWrapper> &value, uint64_t timestamp) :
data_(createMapFromRange(value.begin(), value.end(), timestamp)),
timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }

Expand Down
2 changes: 1 addition & 1 deletion src/kv/value_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ValueWrapper {
explicit(false) ValueWrapper(bool value, uint64_t timestamp = 0);
explicit(false) ValueWrapper(int value, uint64_t timestamp = 0);
explicit(false) ValueWrapper(double value, uint64_t timestamp = 0);
explicit(false) ValueWrapper(const phmap::flat_hash_map<std::string, ValueWrapper> &value, uint64_t timestamp = 0);
explicit(false) ValueWrapper(const std::unordered_map<std::string, ValueWrapper> &value, uint64_t timestamp = 0);
explicit(false) ValueWrapper(const std::initializer_list<std::pair<const std::string, ValueWrapper>> &init_list, uint64_t timestamp = 0);

static ValueWrapper deleted() {
Expand Down
9 changes: 1 addition & 8 deletions src/kv/value_wrapper_proto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@

#pragma once

class ValueWrapper;

using StringType = std::string;
using BooleanType = bool;
using IntType = int;
using DoubleType = double;
using ArrayType = std::vector<ValueWrapper>;
using MapType = phmap::flat_hash_map<std::string, std::shared_ptr<ValueWrapper>>;
#include "kv/kv_definitions.hpp"

using ValueVariant = std::variant<StringType, BooleanType, IntType, DoubleType, ArrayType, MapType>;

Expand Down
5 changes: 1 addition & 4 deletions src/lua/functions/core/libs/kv_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#pragma once

#include "lua/scripts/luascript.hpp"

class ValueWrapper;
#include "kv/kv_definitions.hpp"

#ifndef USE_PRECOMPILED_HEADERS
#include <string>
Expand All @@ -21,8 +20,6 @@ class ValueWrapper;
#include <parallel_hashmap/phmap.h>
#endif

using MapType = phmap::flat_hash_map<std::string, std::shared_ptr<ValueWrapper>>;

struct lua_State;

class KVFunctions final : LuaScriptInterface {
Expand Down

0 comments on commit de2f63f

Please sign in to comment.