diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index 0b88fe5dd..53da04a70 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -275,17 +275,15 @@ class DbAccessor final { } storage::PropertyId NameToProperty(const std::string_view &name) { - // TODO: New storage should work with string_view to avoid needless - // allocation. - return accessor_->NameToProperty(std::string(name)); + return accessor_->NameToProperty(name); } storage::LabelId NameToLabel(const std::string_view &name) { - return accessor_->NameToLabel(std::string(name)); + return accessor_->NameToLabel(name); } storage::EdgeTypeId NameToEdgeType(const std::string_view &name) { - return accessor_->NameToEdgeType(std::string(name)); + return accessor_->NameToEdgeType(name); } const std::string &PropertyToName(storage::PropertyId prop) const { diff --git a/src/storage/v2/name_id_mapper.hpp b/src/storage/v2/name_id_mapper.hpp index c668305b1..dc631dfb2 100644 --- a/src/storage/v2/name_id_mapper.hpp +++ b/src/storage/v2/name_id_mapper.hpp @@ -2,6 +2,7 @@ #include <atomic> #include <string> +#include <string_view> #include "utils/skip_list.hpp" @@ -16,8 +17,8 @@ class NameIdMapper final { bool operator<(const MapNameToId &other) { return name < other.name; } bool operator==(const MapNameToId &other) { return name == other.name; } - bool operator<(const std::string &other) { return name < other; } - bool operator==(const std::string &other) { return name == other; } + bool operator<(const std::string_view &other) { return name < other; } + bool operator==(const std::string_view &other) { return name == other; } }; struct MapIdToName { @@ -33,7 +34,7 @@ class NameIdMapper final { public: /// @throw std::bad_alloc if unable to insert a new mapping - uint64_t NameToId(const std::string &name) { + uint64_t NameToId(const std::string_view &name) { auto name_to_id_acc = name_to_id_.access(); auto found = name_to_id_acc.find(name); uint64_t id; @@ -45,7 +46,7 @@ class NameIdMapper final { // return an iterator to the existing item. This prevents assignment of // two IDs to the same name when the mapping is being inserted // concurrently from two threads. One ID is wasted in that case, though. - id = name_to_id_acc.insert({name, new_id}).first->id; + id = name_to_id_acc.insert({std::string(name), new_id}).first->id; } else { id = found->id; } @@ -56,7 +57,7 @@ class NameIdMapper final { if (id_to_name_acc.find(id) == id_to_name_acc.end()) { // We first try to find the `id` in the map to avoid making an unnecessary // temporary memory allocation when the object already exists. - id_to_name_acc.insert({id, name}); + id_to_name_acc.insert({id, std::string(name)}); } return id; } diff --git a/src/storage/v2/storage.cpp b/src/storage/v2/storage.cpp index 244edd4c7..e6ec3ac20 100644 --- a/src/storage/v2/storage.cpp +++ b/src/storage/v2/storage.cpp @@ -625,15 +625,15 @@ const std::string &Storage::Accessor::EdgeTypeToName( return storage_->EdgeTypeToName(edge_type); } -LabelId Storage::Accessor::NameToLabel(const std::string &name) { +LabelId Storage::Accessor::NameToLabel(const std::string_view &name) { return storage_->NameToLabel(name); } -PropertyId Storage::Accessor::NameToProperty(const std::string &name) { +PropertyId Storage::Accessor::NameToProperty(const std::string_view &name) { return storage_->NameToProperty(name); } -EdgeTypeId Storage::Accessor::NameToEdgeType(const std::string &name) { +EdgeTypeId Storage::Accessor::NameToEdgeType(const std::string_view &name) { return storage_->NameToEdgeType(name); } @@ -966,15 +966,15 @@ const std::string &Storage::EdgeTypeToName(EdgeTypeId edge_type) const { return name_id_mapper_.IdToName(edge_type.AsUint()); } -LabelId Storage::NameToLabel(const std::string &name) { +LabelId Storage::NameToLabel(const std::string_view &name) { return LabelId::FromUint(name_id_mapper_.NameToId(name)); } -PropertyId Storage::NameToProperty(const std::string &name) { +PropertyId Storage::NameToProperty(const std::string_view &name) { return PropertyId::FromUint(name_id_mapper_.NameToId(name)); } -EdgeTypeId Storage::NameToEdgeType(const std::string &name) { +EdgeTypeId Storage::NameToEdgeType(const std::string_view &name) { return EdgeTypeId::FromUint(name_id_mapper_.NameToId(name)); } diff --git a/src/storage/v2/storage.hpp b/src/storage/v2/storage.hpp index 8115be5a6..4e82da7a7 100644 --- a/src/storage/v2/storage.hpp +++ b/src/storage/v2/storage.hpp @@ -265,13 +265,13 @@ class Storage final { const std::string &EdgeTypeToName(EdgeTypeId edge_type) const; /// @throw std::bad_alloc if unable to insert a new mapping - LabelId NameToLabel(const std::string &name); + LabelId NameToLabel(const std::string_view &name); /// @throw std::bad_alloc if unable to insert a new mapping - PropertyId NameToProperty(const std::string &name); + PropertyId NameToProperty(const std::string_view &name); /// @throw std::bad_alloc if unable to insert a new mapping - EdgeTypeId NameToEdgeType(const std::string &name); + EdgeTypeId NameToEdgeType(const std::string_view &name); bool LabelIndexExists(LabelId label) const { return storage_->indices_.label_index.IndexExists(label); @@ -318,13 +318,13 @@ class Storage final { const std::string &EdgeTypeToName(EdgeTypeId edge_type) const; /// @throw std::bad_alloc if unable to insert a new mapping - LabelId NameToLabel(const std::string &name); + LabelId NameToLabel(const std::string_view &name); /// @throw std::bad_alloc if unable to insert a new mapping - PropertyId NameToProperty(const std::string &name); + PropertyId NameToProperty(const std::string_view &name); /// @throw std::bad_alloc if unable to insert a new mapping - EdgeTypeId NameToEdgeType(const std::string &name); + EdgeTypeId NameToEdgeType(const std::string_view &name); /// @throw std::bad_alloc bool CreateIndex(LabelId label);