Set properties C API extension (#1131)

Add SetProperties into the C++ query module API
This commit is contained in:
Josipmrden
2023-09-04 16:17:43 +02:00
committed by GitHub
parent 9661c52179
commit 02eab6ab9c
15 changed files with 505 additions and 14 deletions

View File

@@ -401,6 +401,10 @@ inline void vertex_set_property(mgp_vertex *v, const char *property_name, mgp_va
MgInvokeVoid(mgp_vertex_set_property, v, property_name, property_value);
}
inline void vertex_set_properties(mgp_vertex *v, struct mgp_map *properties) {
MgInvokeVoid(mgp_vertex_set_properties, v, properties);
}
inline mgp_properties_iterator *vertex_iter_properties(mgp_vertex *v, mgp_memory *memory) {
return MgInvoke<mgp_properties_iterator *>(mgp_vertex_iter_properties, v, memory);
}
@@ -437,6 +441,10 @@ inline void edge_set_property(mgp_edge *e, const char *property_name, mgp_value
MgInvokeVoid(mgp_edge_set_property, e, property_name, property_value);
}
inline void edge_set_properties(mgp_edge *e, struct mgp_map *properties) {
MgInvokeVoid(mgp_edge_set_properties, e, properties);
}
inline mgp_properties_iterator *edge_iter_properties(mgp_edge *e, mgp_memory *memory) {
return MgInvoke<mgp_properties_iterator *>(mgp_edge_iter_properties, e, memory);
}

View File

@@ -664,6 +664,15 @@ enum mgp_error mgp_vertex_underlying_graph_is_mutable(struct mgp_vertex *v, int
enum mgp_error mgp_vertex_set_property(struct mgp_vertex *v, const char *property_name,
struct mgp_value *property_value);
/// Set the value of properties on a vertex.
/// When the value is `null`, then the property is removed from the vertex.
/// Return mgp_error::MGP_ERROR_UNABLE_TO_ALLOCATE if unable to allocate memory for storing the property.
/// Return mgp_error::MGP_ERROR_IMMUTABLE_OBJECT if `v` is immutable.
/// Return mgp_error::MGP_ERROR_DELETED_OBJECT if `v` has been deleted.
/// Return mgp_error::MGP_ERROR_SERIALIZATION_ERROR if `v` has been modified by another transaction.
/// Return mgp_error::MGP_ERROR_VALUE_CONVERSION if `property_value` is vertex, edge or path.
enum mgp_error mgp_vertex_set_properties(struct mgp_vertex *v, struct mgp_map *properties);
/// Add the label to the vertex.
/// If the vertex already has the label, this function does nothing.
/// Return mgp_error::MGP_ERROR_UNABLE_TO_ALLOCATE if unable to allocate memory for storing the label.
@@ -814,6 +823,15 @@ enum mgp_error mgp_edge_get_property(struct mgp_edge *e, const char *property_na
/// Return mgp_error::MGP_ERROR_VALUE_CONVERSION if `property_value` is vertex, edge or path.
enum mgp_error mgp_edge_set_property(struct mgp_edge *e, const char *property_name, struct mgp_value *property_value);
/// Set the value of properties on a vertex.
/// When the value is `null`, then the property is removed from the vertex.
/// Return mgp_error::MGP_ERROR_UNABLE_TO_ALLOCATE if unable to allocate memory for storing the property.
/// Return mgp_error::MGP_ERROR_IMMUTABLE_OBJECT if `v` is immutable.
/// Return mgp_error::MGP_ERROR_DELETED_OBJECT if `v` has been deleted.
/// Return mgp_error::MGP_ERROR_SERIALIZATION_ERROR if `v` has been modified by another transaction.
/// Return mgp_error::MGP_ERROR_VALUE_CONVERSION if `property_value` is vertex, edge or path.
enum mgp_error mgp_edge_set_properties(struct mgp_edge *e, struct mgp_map *properties);
/// Start iterating over properties stored in the given edge.
/// The properties of the edge are copied when the iterator is created, therefore later changes won't affect them.
/// Resulting mgp_properties_iterator needs to be deallocated with

View File

@@ -715,11 +715,14 @@ class Node {
bool HasLabel(std::string_view label) const;
/// @brief Returns an std::map of the nodes properties.
std::map<std::string, Value> Properties() const;
std::unordered_map<std::string, Value> Properties() const;
/// @brief Sets the chosen property to the given value.
void SetProperty(std::string property, Value value);
/// @brief Sets the chosen properties to the given values.
void SetProperties(std::unordered_map<std::string_view, Value> properties);
/// @brief Removes the chosen property.
void RemoveProperty(std::string property);
@@ -784,11 +787,14 @@ class Relationship {
std::string_view Type() const;
/// @brief Returns an std::map of the relationships properties.
std::map<std::string, Value> Properties() const;
std::unordered_map<std::string, Value> Properties() const;
/// @brief Sets the chosen property to the given value.
void SetProperty(std::string property, Value value);
/// @brief Sets the chosen properties to the given values.
void SetProperties(std::unordered_map<std::string_view, Value> properties);
/// @brief Removes the chosen property.
void RemoveProperty(std::string property);
@@ -2710,9 +2716,9 @@ inline void Node::RemoveLabel(const std::string_view label) {
mgp::vertex_remove_label(this->ptr_, mgp_label{.name = label.data()});
}
inline std::map<std::string, Value> Node::Properties() const {
inline std::unordered_map<std::string, Value> Node::Properties() const {
mgp_properties_iterator *properties_iterator = mgp::MemHandlerCallback(vertex_iter_properties, ptr_);
std::map<std::string, Value> property_map;
std::unordered_map<std::string, Value> property_map;
for (auto *property = mgp::properties_iterator_get(properties_iterator); property;
property = mgp::properties_iterator_next(properties_iterator)) {
property_map.emplace(std::string(property->name), Value(property->value));
@@ -2725,6 +2731,17 @@ inline void Node::SetProperty(std::string property, Value value) {
mgp::vertex_set_property(ptr_, property.data(), value.ptr());
}
inline void Node::SetProperties(std::unordered_map<std::string_view, Value> properties) {
mgp_map *map = mgp::MemHandlerCallback(map_make_empty);
for (auto const &[k, v] : properties) {
mgp::map_insert(map, k.data(), v.ptr());
}
mgp::vertex_set_properties(ptr_, map);
mgp::map_destroy(map);
}
inline void Node::RemoveProperty(std::string property) { SetProperty(property, Value()); }
inline Value Node::GetProperty(const std::string &property) const {
@@ -2740,7 +2757,7 @@ inline bool Node::operator!=(const Node &other) const { return !(*this == other)
// this functions is used both in relationship and node ToString
inline std::string PropertiesToString(const std::map<std::string, Value> &property_map) {
std::string properties{""};
std::string properties;
const auto map_size = property_map.size();
size_t i = 0;
for (const auto &[key, value] : property_map) {
@@ -2762,8 +2779,14 @@ inline const std::string Node::ToString() const {
if (labels == ", ") {
labels = ""; // dont use labels if they dont exist
}
std::map<std::string, Value> properties_map{Properties()};
std::string properties{PropertiesToString(properties_map)};
std::unordered_map<std::string, Value> properties_map{Properties()};
std::map<std::string, Value> properties_map_sorted{};
for (const auto &[k, v] : properties_map) {
properties_map_sorted.emplace(k, v);
}
std::string properties{PropertiesToString(properties_map_sorted)};
return "(id: " + std::to_string(Id().AsInt()) + labels + ", properties: {" + properties + "})";
}
@@ -2807,9 +2830,9 @@ inline mgp::Id Relationship::Id() const { return Id::FromInt(mgp::edge_get_id(pt
inline std::string_view Relationship::Type() const { return mgp::edge_get_type(ptr_).name; }
inline std::map<std::string, Value> Relationship::Properties() const {
inline std::unordered_map<std::string, Value> Relationship::Properties() const {
mgp_properties_iterator *properties_iterator = mgp::MemHandlerCallback(edge_iter_properties, ptr_);
std::map<std::string, Value> property_map;
std::unordered_map<std::string, Value> property_map;
for (mgp_property *property = mgp::properties_iterator_get(properties_iterator); property;
property = mgp::properties_iterator_next(properties_iterator)) {
property_map.emplace(property->name, Value(property->value));
@@ -2822,6 +2845,17 @@ inline void Relationship::SetProperty(std::string property, Value value) {
mgp::edge_set_property(ptr_, property.data(), value.ptr());
}
inline void Relationship::SetProperties(std::unordered_map<std::string_view, Value> properties) {
mgp_map *map = mgp::MemHandlerCallback(map_make_empty);
for (auto const &[k, v] : properties) {
mgp::map_insert(map, k.data(), v.ptr());
}
mgp::edge_set_properties(ptr_, map);
mgp::map_destroy(map);
}
inline void Relationship::RemoveProperty(std::string property) { SetProperty(property, Value()); }
inline Value Relationship::GetProperty(const std::string &property) const {
@@ -2846,8 +2880,14 @@ inline const std::string Relationship::ToString() const {
const auto to = To();
const std::string type{Type()};
std::map<std::string, Value> properties_map{Properties()};
std::string properties{PropertiesToString(properties_map)};
std::unordered_map<std::string, Value> properties_map{Properties()};
std::map<std::string, Value> properties_map_sorted{};
for (const auto &[k, v] : properties_map) {
properties_map_sorted.emplace(k, v);
}
std::string properties{PropertiesToString(properties_map_sorted)};
const std::string relationship{"[type: " + type + ", id: " + std::to_string(Id().AsInt()) + ", properties: {" +
properties + "}]"};
@@ -2924,8 +2964,14 @@ inline const std::string Path::ToString() const {
return_string.append(node.ToString() + "-");
const Relationship rel = GetRelationshipAt(i);
std::map<std::string, Value> properties_map{rel.Properties()};
std::string properties = PropertiesToString(properties_map);
std::unordered_map<std::string, Value> properties_map{rel.Properties()};
std::map<std::string, Value> properties_map_sorted{};
for (const auto &[k, v] : properties_map) {
properties_map_sorted.emplace(k, v);
}
std::string properties{PropertiesToString(properties_map_sorted)};
return_string.append("[type: " + std::string(rel.Type()) + ", id: " + std::to_string(rel.Id().AsInt()) +
", properties: {" + properties + "}]->");
}

View File

@@ -479,6 +479,12 @@ class Properties:
except KeyError:
return False
def set_properties(self, properties: dict) -> None:
if not self._vertex_or_edge.is_valid():
raise InvalidContextError()
self._vertex_or_edge.set_properties(properties)
class EdgeType:
"""Type of an Edge."""