|
20 | 20 | #include <unordered_set> |
21 | 21 | #include <utility> |
22 | 22 |
|
| 23 | +#include "graphar/status.h" |
23 | 24 | #include "mini-yaml/yaml/Yaml.hpp" |
24 | 25 |
|
25 | 26 | #include "graphar/filesystem.h" |
@@ -86,7 +87,8 @@ std::string BuildPath(const std::vector<std::string>& paths) { |
86 | 87 | bool operator==(const Property& lhs, const Property& rhs) { |
87 | 88 | return (lhs.name == rhs.name) && (lhs.type == rhs.type) && |
88 | 89 | (lhs.is_primary == rhs.is_primary) && |
89 | | - (lhs.is_nullable == rhs.is_nullable); |
| 90 | + (lhs.is_nullable == rhs.is_nullable) && |
| 91 | + (lhs.cardinality == rhs.cardinality); |
90 | 92 | } |
91 | 93 |
|
92 | 94 | PropertyGroup::PropertyGroup(const std::vector<Property>& properties, |
@@ -138,6 +140,11 @@ bool PropertyGroup::IsValidated() const { |
138 | 140 | // list type is not supported in csv file |
139 | 141 | return false; |
140 | 142 | } |
| 143 | + // TODO(@yangxk): support cardinality in csv file |
| 144 | + if (p.cardinality != Cardinality::SINGLE && file_type_ == FileType::CSV) { |
| 145 | + // list cardinality is not supported in csv file |
| 146 | + return false; |
| 147 | + } |
141 | 148 | } |
142 | 149 | return true; |
143 | 150 | } |
@@ -212,6 +219,7 @@ class VertexInfo::Impl { |
212 | 219 | property_name_to_primary_.emplace(p.name, p.is_primary); |
213 | 220 | property_name_to_nullable_.emplace(p.name, p.is_nullable); |
214 | 221 | property_name_to_type_.emplace(p.name, p.type); |
| 222 | + property_name_to_cardinality_.emplace(p.name, p.cardinality); |
215 | 223 | } |
216 | 224 | } |
217 | 225 | } |
@@ -251,6 +259,7 @@ class VertexInfo::Impl { |
251 | 259 | std::unordered_map<std::string, bool> property_name_to_nullable_; |
252 | 260 | std::unordered_map<std::string, std::shared_ptr<DataType>> |
253 | 261 | property_name_to_type_; |
| 262 | + std::unordered_map<std::string, Cardinality> property_name_to_cardinality_; |
254 | 263 | }; |
255 | 264 |
|
256 | 265 | VertexInfo::VertexInfo(const std::string& type, IdType chunk_size, |
@@ -363,6 +372,15 @@ Result<std::shared_ptr<DataType>> VertexInfo::GetPropertyType( |
363 | 372 | return it->second; |
364 | 373 | } |
365 | 374 |
|
| 375 | +Result<Cardinality> VertexInfo::GetPropertyCardinality( |
| 376 | + const std::string& property_name) const { |
| 377 | + auto it = impl_->property_name_to_cardinality_.find(property_name); |
| 378 | + if (it == impl_->property_name_to_cardinality_.end()) { |
| 379 | + return Status::Invalid("property name not found: ", property_name); |
| 380 | + } |
| 381 | + return it->second; |
| 382 | +} |
| 383 | + |
366 | 384 | Result<std::shared_ptr<VertexInfo>> VertexInfo::AddPropertyGroup( |
367 | 385 | std::shared_ptr<PropertyGroup> property_group) const { |
368 | 386 | if (property_group == nullptr) { |
@@ -440,8 +458,13 @@ Result<std::shared_ptr<VertexInfo>> VertexInfo::Load( |
440 | 458 | bool is_primary = p_node["is_primary"].As<bool>(); |
441 | 459 | bool is_nullable = |
442 | 460 | p_node["is_nullable"].IsNone() || p_node["is_nullable"].As<bool>(); |
| 461 | + Cardinality cardinality = Cardinality::SINGLE; |
| 462 | + if (!p_node["cardinality"].IsNone()) { |
| 463 | + cardinality = |
| 464 | + StringToCardinality(p_node["cardinality"].As<std::string>()); |
| 465 | + } |
443 | 466 | property_vec.emplace_back(property_name, property_type, is_primary, |
444 | | - is_nullable); |
| 467 | + is_nullable, cardinality); |
445 | 468 | } |
446 | 469 | property_groups.push_back( |
447 | 470 | std::make_shared<PropertyGroup>(property_vec, file_type, pg_prefix)); |
@@ -485,6 +508,9 @@ Result<std::string> VertexInfo::Dump() const noexcept { |
485 | 508 | p_node["data_type"] = p.type->ToTypeName(); |
486 | 509 | p_node["is_primary"] = p.is_primary ? "true" : "false"; |
487 | 510 | p_node["is_nullable"] = p.is_nullable ? "true" : "false"; |
| 511 | + if (p.cardinality != Cardinality::SINGLE) { |
| 512 | + p_node["cardinality"] = CardinalityToString(p.cardinality); |
| 513 | + } |
488 | 514 | pg_node["properties"].PushBack(); |
489 | 515 | pg_node["properties"][pg_node["properties"].Size() - 1] = p_node; |
490 | 516 | } |
@@ -574,6 +600,13 @@ class EdgeInfo::Impl { |
574 | 600 | } |
575 | 601 | // check if property name is unique in all property groups |
576 | 602 | for (const auto& p : pg->GetProperties()) { |
| 603 | + if (p.cardinality != Cardinality::SINGLE) { |
| 604 | + // edge property only supports single cardinality |
| 605 | + std::cout |
| 606 | + << "Edge property only supports single cardinality, but got: " |
| 607 | + << CardinalityToString(p.cardinality) << std::endl; |
| 608 | + return false; |
| 609 | + } |
577 | 610 | if (check_property_unique_set.find(p.name) != |
578 | 611 | check_property_unique_set.end()) { |
579 | 612 | return false; |
@@ -910,6 +943,12 @@ Result<std::shared_ptr<EdgeInfo>> EdgeInfo::Load(std::shared_ptr<Yaml> yaml) { |
910 | 943 | auto property_name = p_node["name"].As<std::string>(); |
911 | 944 | auto property_type = |
912 | 945 | DataType::TypeNameToDataType(p_node["data_type"].As<std::string>()); |
| 946 | + if (!p_node["cardinality"].IsNone() && |
| 947 | + StringToCardinality(p_node["cardinality"].As<std::string>()) != |
| 948 | + Cardinality::SINGLE) { |
| 949 | + return Status::YamlError( |
| 950 | + "Unsupported set cardinality for edge property."); |
| 951 | + } |
913 | 952 | bool is_primary = p_node["is_primary"].As<bool>(); |
914 | 953 | bool is_nullable = |
915 | 954 | p_node["is_nullable"].IsNone() || p_node["is_nullable"].As<bool>(); |
|
0 commit comments