diff --git a/demo_storage/src/property/primarykey.cc b/demo_storage/src/property/primarykey.cc new file mode 100644 index 0000000..7ffe608 --- /dev/null +++ b/demo_storage/src/property/primarykey.cc @@ -0,0 +1,111 @@ +/** Copyright 2020 Alibaba Group Holding Limited. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "src/predefine.h" +// GRIN headers +#include "property/primarykey.h" + +#ifdef GRIN_ENABLE_VERTEX_PRIMARY_KEYS +GRIN_VERTEX_TYPE_LIST grin_get_vertex_types_with_primary_keys(GRIN_GRAPH g) { + auto _g = static_cast(g); + auto vtl = new GRIN_VERTEX_TYPE_LIST_T(); + auto vtype_num = _g->GetVertexTypeNum(); + for (auto i = 0; i < vtype_num; i++) { + auto& properties = _g->GetVertexProperties(i); + for (auto& property : properties) { + if (property.is_primary_) { + vtl->push_back(i); + break; + } + } + } + return vtl; +} + +GRIN_VERTEX_PROPERTY_LIST grin_get_primary_keys_by_vertex_type( + GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype) { + auto _g = static_cast(g); + auto vpl = new GRIN_VERTEX_PROPERTY_LIST_T(); + auto& properties = _g->GetVertexProperties(vtype); + for (auto i = 0; i < properties.size(); i++) { + auto& property = properties[i]; + if (property.is_primary_) { + vpl->push_back( + DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(vtype, i)); + } + } + return vpl; +} + +GRIN_ROW grin_get_vertex_primary_keys_row(GRIN_GRAPH g, GRIN_VERTEX v) { + auto _g = static_cast(g); + auto& _v = _g->GetVertex(v); + auto vtype = DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(v); + auto& properties = _g->GetVertexProperties(vtype); + auto row = new GRIN_ROW_T(); + for (auto i = 0; i < properties.size(); i++) { + auto& property = properties[i]; + if (property.is_primary_) { + row->push_back(_v.GetPropertyAny(property.name_)); + } + } + return row; +} +#endif + +#ifdef GRIN_ENABLE_EDGE_PRIMARY_KEYS +GRIN_EDGE_TYPE_LIST grin_get_edge_types_with_primary_keys(GRIN_GRAPH g) { + auto _g = static_cast(g); + auto etl = new GRIN_EDGE_TYPE_LIST_T(); + auto etype_num = _g->GetEdgeTypeNum(); + for (auto i = 0; i < etype_num; i++) { + auto& properties = _g->GetEdgeProperties(i); + for (auto& property : properties) { + if (property.is_primary_) { + etl->push_back(i); + break; + } + } + } + return etl; +} + +GRIN_EDGE_PROPERTY_LIST grin_get_primary_keys_by_edge_type( + GRIN_GRAPH g, GRIN_EDGE_TYPE etype) { + auto _g = static_cast(g); + auto epl = new GRIN_EDGE_PROPERTY_LIST_T(); + auto& properties = _g->GetEdgeProperties(etype); + for (auto i = 0; i < properties.size(); i++) { + auto& property = properties[i]; + if (property.is_primary_) { + epl->push_back( + DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(etype, i)); + } + } + return epl; +} + +GRIN_ROW grin_get_edge_primary_keys_row(GRIN_GRAPH g, GRIN_EDGE e) { + auto _g = static_cast(g); + auto& _e = _g->GetEdge(e); + auto etype = DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(e); + auto& properties = _g->GetEdgeProperties(etype); + auto row = new GRIN_ROW_T(); + for (auto i = 0; i < properties.size(); i++) { + auto& property = properties[i]; + if (property.is_primary_) { + row->push_back(_e.GetPropertyAny(property.name_)); + } + } + return row; +} +#endif diff --git a/demo_storage/src/property/property.cc b/demo_storage/src/property/property.cc new file mode 100644 index 0000000..22d9e75 --- /dev/null +++ b/demo_storage/src/property/property.cc @@ -0,0 +1,419 @@ +/** Copyright 2020 Alibaba Group Holding Limited. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "src/predefine.h" +// GRIN headers +#include "common/error.h" +#include "property/property.h" + +#define __grin_check_vertex_property(v, vp, x) \ + grin_error_code = NO_ERROR; \ + auto vtype0 = DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(v); \ + auto vtype1 = DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(vp); \ + if (vtype0 != vtype1) { \ + grin_error_code = INVALID_VALUE; \ + return x; \ + } + +#define __grin_check_edge_property(e, ep, x) \ + grin_error_code = NO_ERROR; \ + auto etype0 = DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(e); \ + auto etype1 = DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(ep); \ + if (etype0 != etype1) { \ + grin_error_code = INVALID_VALUE; \ + return x; \ + } + +void grin_destroy_string_value(GRIN_GRAPH g, const char* value) { return; } + +#ifdef GRIN_WITH_VERTEX_PROPERTY_NAME +const char* grin_get_vertex_property_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype, + GRIN_VERTEX_PROPERTY vp) { + auto _g = static_cast(g); + if (vtype >= _g->GetVertexTypeNum()) + return nullptr; + auto vp_id = DEMO_STORAGE_NAMESPACE::get_id_from_gid(vp); + return _g->GetVertexPropertyName(vtype, vp_id).c_str(); +} + +GRIN_VERTEX_PROPERTY grin_get_vertex_property_by_name(GRIN_GRAPH g, + GRIN_VERTEX_TYPE vtype, + const char* name) { + auto _g = static_cast(g); + if (vtype >= _g->GetVertexTypeNum()) + return GRIN_NULL_VERTEX_PROPERTY; + auto vp_id = _g->GetVertexPropertyId(vtype, name); + if (vp_id == -1) + return GRIN_NULL_VERTEX_PROPERTY; + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(vtype, vp_id); +} + +GRIN_VERTEX_PROPERTY_LIST grin_get_vertex_properties_by_name(GRIN_GRAPH g, + const char* name) { + auto vpl = new GRIN_VERTEX_PROPERTY_LIST_T(); + auto _g = static_cast(g); + for (unsigned i = 0; i < _g->GetVertexTypeNum(); ++i) { + auto vp_id = _g->GetVertexPropertyId(i, name); + if (vp_id != -1) + vpl->push_back( + DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(i, vp_id)); + } + if (vpl->size() == 0) { + delete vpl; + return GRIN_NULL_VERTEX_PROPERTY_LIST; + } + return vpl; +} +#endif + +#ifdef GRIN_WITH_EDGE_PROPERTY_NAME +const char* grin_get_edge_property_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype, + GRIN_EDGE_PROPERTY ep) { + auto _g = static_cast(g); + if (etype >= _g->GetEdgeTypeNum()) + return nullptr; + auto ep_id = DEMO_STORAGE_NAMESPACE::get_id_from_gid(ep); + return _g->GetEdgePropertyName(etype, ep_id).c_str(); +} + +GRIN_EDGE_PROPERTY grin_get_edge_property_by_name(GRIN_GRAPH g, + GRIN_EDGE_TYPE etype, + const char* name) { + auto _g = static_cast(g); + if (etype >= _g->GetEdgeTypeNum()) + return GRIN_NULL_EDGE_PROPERTY; + auto ep_id = _g->GetEdgePropertyId(etype, name); + if (ep_id == -1) + return GRIN_NULL_EDGE_PROPERTY; + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(etype, ep_id); +} + +GRIN_EDGE_PROPERTY_LIST grin_get_edge_properties_by_name(GRIN_GRAPH g, + const char* name) { + auto epl = new GRIN_EDGE_PROPERTY_LIST_T(); + auto _g = static_cast(g); + for (unsigned i = 0; i < _g->GetEdgeTypeNum(); ++i) { + auto ep_id = _g->GetEdgePropertyId(i, name); + if (ep_id != -1) + epl->push_back( + DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(i, ep_id)); + } + if (epl->size() == 0) { + delete epl; + return GRIN_NULL_EDGE_PROPERTY_LIST; + } + return epl; +} +#endif + +#ifdef GRIN_WITH_VERTEX_PROPERTY +bool grin_equal_vertex_property(GRIN_GRAPH g, GRIN_VERTEX_PROPERTY vp1, + GRIN_VERTEX_PROPERTY vp2) { + return vp1 == vp2; +} + +void grin_destroy_vertex_property(GRIN_GRAPH g, GRIN_VERTEX_PROPERTY vp) { + return; +} + +GRIN_DATATYPE grin_get_vertex_property_datatype(GRIN_GRAPH g, + GRIN_VERTEX_PROPERTY vp) { + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + return property.datatype_; +} + +int grin_get_vertex_property_value_of_int32(GRIN_GRAPH g, GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +unsigned int grin_get_vertex_property_value_of_uint32(GRIN_GRAPH g, // NOLINT + GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +long long int grin_get_vertex_property_value_of_int64(GRIN_GRAPH g, // NOLINT + GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +unsigned long long int grin_get_vertex_property_value_of_uint64( // NOLINT + GRIN_GRAPH g, GRIN_VERTEX v, GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +float grin_get_vertex_property_value_of_float(GRIN_GRAPH g, GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +double grin_get_vertex_property_value_of_double(GRIN_GRAPH g, GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +const char* grin_get_vertex_property_value_of_string(GRIN_GRAPH g, + GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, NULL); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_).c_str(); +} + +int grin_get_vertex_property_value_of_date32(GRIN_GRAPH g, GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +int grin_get_vertex_property_value_of_time32(GRIN_GRAPH g, GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +long long int grin_get_vertex_property_value_of_timestamp64( // NOLINT + GRIN_GRAPH g, GRIN_VERTEX v, GRIN_VERTEX_PROPERTY vp) { + __grin_check_vertex_property(v, vp, 0); + auto _g = static_cast(g); + auto& property = _g->GetVertexProperty(vp); + auto& _v = _g->GetVertex(v); + return _v.GetProperty(property.name_); +} + +GRIN_VERTEX_TYPE grin_get_vertex_type_from_property(GRIN_GRAPH g, + GRIN_VERTEX_PROPERTY vp) { + return DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(vp); +} +#endif + +#if defined(GRIN_WITH_VERTEX_PROPERTY) && defined(GRIN_TRAIT_CONST_VALUE_PTR) +const void* grin_get_vertex_property_value(GRIN_GRAPH g, GRIN_VERTEX v, + GRIN_VERTEX_PROPERTY vp) { + auto _g = static_cast(g); + __grin_check_vertex_property(v, vp, NULL); + auto& _v = _g->GetVertex(v); + auto& property = _g->GetVertexProperty(vp); + auto& name = property.name_; + auto& type = property.datatype_; + switch (type) { + case GRIN_DATATYPE::Int32: + return &_v.GetProperty(name); + case GRIN_DATATYPE::UInt32: + return &_v.GetProperty(name); + case GRIN_DATATYPE::Int64: + return &_v.GetProperty(name); + case GRIN_DATATYPE::UInt64: + return &_v.GetProperty(name); + case GRIN_DATATYPE::Float: + return &_v.GetProperty(name); + case GRIN_DATATYPE::Double: + return &_v.GetProperty(name); + case GRIN_DATATYPE::String: + return _v.GetProperty(name).c_str(); + case GRIN_DATATYPE::Date32: + return &_v.GetProperty(name); + case GRIN_DATATYPE::Time32: + return &_v.GetProperty(name); + case GRIN_DATATYPE::Timestamp64: + return &_v.GetProperty(name); + default: + return NULL; + } + return NULL; +} +#endif + +#ifdef GRIN_WITH_EDGE_PROPERTY +bool grin_equal_edge_property(GRIN_GRAPH g, GRIN_EDGE_PROPERTY ep1, + GRIN_EDGE_PROPERTY ep2) { + return ep1 == ep2; +} + +void grin_destroy_edge_property(GRIN_GRAPH g, GRIN_EDGE_PROPERTY ep) { return; } + +GRIN_DATATYPE grin_get_edge_property_datatype(GRIN_GRAPH g, + GRIN_EDGE_PROPERTY ep) { + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + return property.datatype_; +} + +int grin_get_edge_property_value_of_int32(GRIN_GRAPH g, GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +unsigned int grin_get_edge_property_value_of_uint32(GRIN_GRAPH g, // NOLINT + GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +long long int grin_get_edge_property_value_of_int64(GRIN_GRAPH g, // NOLINT + GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +unsigned long long int grin_get_edge_property_value_of_uint64( // NOLINT + GRIN_GRAPH g, GRIN_EDGE e, GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +float grin_get_edge_property_value_of_float(GRIN_GRAPH g, GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +double grin_get_edge_property_value_of_double(GRIN_GRAPH g, GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +const char* grin_get_edge_property_value_of_string(GRIN_GRAPH g, GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_).c_str(); +} + +int grin_get_edge_property_value_of_date32(GRIN_GRAPH g, GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +int grin_get_edge_property_value_of_time32(GRIN_GRAPH g, GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +long long int grin_get_edge_property_value_of_timestamp64( // NOLINT + GRIN_GRAPH g, GRIN_EDGE e, GRIN_EDGE_PROPERTY ep) { + __grin_check_edge_property(e, ep, 0); + auto _g = static_cast(g); + auto& property = _g->GetEdgeProperty(ep); + auto& _e = _g->GetEdge(e); + return _e.GetProperty(property.name_); +} + +GRIN_EDGE_TYPE grin_get_edge_type_from_property(GRIN_GRAPH g, + GRIN_EDGE_PROPERTY ep) { + return DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(ep); +} +#endif + +#if defined(GRIN_WITH_EDGE_PROPERTY) && defined(GRIN_TRAIT_CONST_VALUE_PTR) +const void* grin_get_edge_property_value(GRIN_GRAPH g, GRIN_EDGE e, + GRIN_EDGE_PROPERTY ep) { + auto _g = static_cast(g); + __grin_check_edge_property(e, ep, NULL); + auto& _e = _g->GetEdge(e); + auto& property = _g->GetEdgeProperty(ep); + auto& name = property.name_; + auto& type = property.datatype_; + switch (type) { + case GRIN_DATATYPE::Int32: + return &_e.GetProperty(name); + case GRIN_DATATYPE::UInt32: + return &_e.GetProperty(name); + case GRIN_DATATYPE::Int64: + return &_e.GetProperty(name); + case GRIN_DATATYPE::UInt64: + return &_e.GetProperty(name); + case GRIN_DATATYPE::Float: + return &_e.GetProperty(name); + case GRIN_DATATYPE::Double: + return &_e.GetProperty(name); + case GRIN_DATATYPE::String: + return _e.GetProperty(name).c_str(); + case GRIN_DATATYPE::Date32: + return &_e.GetProperty(name); + case GRIN_DATATYPE::Time32: + return &_e.GetProperty(name); + case GRIN_DATATYPE::Timestamp64: + return &_e.GetProperty(name); + default: + return NULL; + } + return NULL; +} +#endif diff --git a/demo_storage/src/property/propertylist.cc b/demo_storage/src/property/propertylist.cc new file mode 100644 index 0000000..bc76e4a --- /dev/null +++ b/demo_storage/src/property/propertylist.cc @@ -0,0 +1,153 @@ +/** Copyright 2020 Alibaba Group Holding Limited. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "src/predefine.h" +// GRIN headers +#include "property/propertylist.h" + +#ifdef GRIN_WITH_VERTEX_PROPERTY +GRIN_VERTEX_PROPERTY_LIST grin_get_vertex_property_list_by_type( + GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype) { + auto _g = static_cast(g); + if (vtype >= _g->GetVertexTypeNum()) + return GRIN_NULL_VERTEX_PROPERTY_LIST; + auto vpl = new GRIN_VERTEX_PROPERTY_LIST_T(); + auto n = _g->GetVertexPropertyNum(vtype); + for (auto i = 0; i < n; ++i) { + auto vp = + DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(vtype, i); + vpl->push_back(vp); + } + return vpl; +} + +size_t grin_get_vertex_property_list_size(GRIN_GRAPH g, + GRIN_VERTEX_PROPERTY_LIST vpl) { + auto _vpl = static_cast(vpl); + return _vpl->size(); +} + +GRIN_VERTEX_PROPERTY grin_get_vertex_property_from_list( + GRIN_GRAPH g, GRIN_VERTEX_PROPERTY_LIST vpl, size_t idx) { + auto _vpl = static_cast(vpl); + if (idx >= _vpl->size()) + return GRIN_NULL_VERTEX_PROPERTY; + return (*_vpl)[idx]; +} + +GRIN_VERTEX_PROPERTY_LIST grin_create_vertex_property_list(GRIN_GRAPH g) { + return new GRIN_VERTEX_PROPERTY_LIST_T(); +} + +void grin_destroy_vertex_property_list(GRIN_GRAPH g, + GRIN_VERTEX_PROPERTY_LIST vpl) { + auto _vpl = static_cast(vpl); + delete _vpl; +} + +bool grin_insert_vertex_property_to_list(GRIN_GRAPH g, + GRIN_VERTEX_PROPERTY_LIST vpl, + GRIN_VERTEX_PROPERTY vp) { + auto _vpl = static_cast(vpl); + _vpl->push_back(vp); + return true; +} +#endif + +#ifdef GRIN_TRAIT_NATURAL_ID_FOR_VERTEX_PROPERTY +GRIN_VERTEX_PROPERTY grin_get_vertex_property_by_id( + GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype, GRIN_VERTEX_PROPERTY_ID id) { + auto _g = static_cast(g); + if (vtype >= 0 & vtype < _g->GetVertexTypeNum() && id >= 0 && + id < _g->GetVertexPropertyNum(vtype)) { + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(vtype, id); + } else { + return GRIN_NULL_VERTEX_PROPERTY; + } +} + +GRIN_VERTEX_PROPERTY_ID grin_get_vertex_property_id(GRIN_GRAPH g, + GRIN_VERTEX_TYPE vtype, + GRIN_VERTEX_PROPERTY vp) { + return DEMO_STORAGE_NAMESPACE::get_id_from_gid(vp); +} +#endif + +#ifdef GRIN_WITH_EDGE_PROPERTY +GRIN_EDGE_PROPERTY_LIST grin_get_edge_property_list_by_type( + GRIN_GRAPH g, GRIN_EDGE_TYPE etype) { + auto _g = static_cast(g); + if (etype >= _g->GetEdgeTypeNum()) + return GRIN_NULL_VERTEX_PROPERTY_LIST; + auto epl = new GRIN_EDGE_PROPERTY_LIST_T(); + auto n = _g->GetEdgePropertyNum(etype); + for (auto i = 0; i < n; ++i) { + auto ep = + DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(etype, i); + epl->push_back(ep); + } + return epl; +} + +size_t grin_get_edge_property_list_size(GRIN_GRAPH g, + GRIN_EDGE_PROPERTY_LIST epl) { + auto _epl = static_cast(epl); + return _epl->size(); +} + +GRIN_EDGE_PROPERTY grin_get_edge_property_from_list(GRIN_GRAPH g, + GRIN_EDGE_PROPERTY_LIST epl, + size_t idx) { + auto _epl = static_cast(epl); + if (idx >= _epl->size()) + return GRIN_NULL_EDGE_PROPERTY; + return (*_epl)[idx]; +} + +GRIN_EDGE_PROPERTY_LIST grin_create_edge_property_list(GRIN_GRAPH g) { + return new GRIN_EDGE_PROPERTY_LIST_T(); +} + +void grin_destroy_edge_property_list(GRIN_GRAPH g, + GRIN_EDGE_PROPERTY_LIST epl) { + auto _epl = static_cast(epl); + delete _epl; +} + +bool grin_insert_edge_property_to_list(GRIN_GRAPH g, + GRIN_EDGE_PROPERTY_LIST epl, + GRIN_EDGE_PROPERTY ep) { + auto _epl = static_cast(epl); + _epl->push_back(ep); + return true; +} +#endif + +#ifdef GRIN_TRAIT_NATURAL_ID_FOR_EDGE_PROPERTY +GRIN_EDGE_PROPERTY grin_get_edge_property_by_id(GRIN_GRAPH g, + GRIN_EDGE_TYPE etype, + GRIN_EDGE_PROPERTY_ID id) { + auto _g = static_cast(g); + if (etype >= 0 && etype < _g->GetEdgeTypeNum() && id >= 0 && + id < _g->GetEdgePropertyNum(etype)) { + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(etype, id); + } else { + return GRIN_NULL_EDGE_PROPERTY; + } +} + +GRIN_EDGE_PROPERTY_ID grin_get_edge_property_id(GRIN_GRAPH g, + GRIN_EDGE_TYPE etype, + GRIN_EDGE_PROPERTY ep) { + return DEMO_STORAGE_NAMESPACE::get_id_from_gid(ep); +} +#endif diff --git a/demo_storage/src/property/row.cc b/demo_storage/src/property/row.cc new file mode 100644 index 0000000..0ba60fd --- /dev/null +++ b/demo_storage/src/property/row.cc @@ -0,0 +1,222 @@ +/** Copyright 2020 Alibaba Group Holding Limited. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "src/predefine.h" +// GRIN headers +#include "common/error.h" +#include "property/row.h" + +#define __grin_check_row(_r, x) \ + grin_error_code = NO_ERROR; \ + if (idx >= _r->size()) { \ + grin_error_code = INVALID_VALUE; \ + return x; \ + } + +#ifdef GRIN_ENABLE_ROW +void grin_destroy_row(GRIN_GRAPH g, GRIN_ROW r) { + auto _r = static_cast(r); + delete _r; +} + +int grin_get_int32_from_row(GRIN_GRAPH g, GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +unsigned int grin_get_uint32_from_row(GRIN_GRAPH g, GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +long long int grin_get_int64_from_row(GRIN_GRAPH g, // NOLINT + GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +unsigned long long int grin_get_uint64_from_row(GRIN_GRAPH g, // NOLINT + GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +float grin_get_float_from_row(GRIN_GRAPH g, GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +double grin_get_double_from_row(GRIN_GRAPH g, GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +const char* grin_get_string_from_row(GRIN_GRAPH g, GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, NULL); + return std::any_cast((*_r)[idx]).c_str(); +} + +int grin_get_date32_from_row(GRIN_GRAPH g, GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +int grin_get_time32_from_row(GRIN_GRAPH g, GRIN_ROW r, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +long long int grin_get_timestamp64_from_row(GRIN_GRAPH g, GRIN_ROW r, // NOLINT + size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, 0); + return std::any_cast((*_r)[idx]); +} + +GRIN_ROW grin_create_row(GRIN_GRAPH g) { + auto r = new GRIN_ROW_T(); + return r; +} + +bool grin_insert_int32_to_row(GRIN_GRAPH g, GRIN_ROW r, int value) { + auto _r = static_cast(r); + _r->push_back(value); + return true; +} + +bool grin_insert_uint32_to_row(GRIN_GRAPH g, GRIN_ROW r, unsigned int value) { + auto _r = static_cast(r); + _r->push_back(value); + return true; +} + +bool grin_insert_int64_to_row(GRIN_GRAPH g, GRIN_ROW r, + long long int value) { // NOLINT + auto _r = static_cast(r); + _r->push_back(value); + return true; +} + +bool grin_insert_uint64_to_row(GRIN_GRAPH g, GRIN_ROW r, + unsigned long long int value) { // NOLINT + auto _r = static_cast(r); + _r->push_back(value); + return true; +} + +bool grin_insert_float_to_row(GRIN_GRAPH g, GRIN_ROW r, float value) { + auto _r = static_cast(r); + _r->push_back(value); + return true; +} + +bool grin_insert_double_to_row(GRIN_GRAPH g, GRIN_ROW r, double value) { + auto _r = static_cast(r); + _r->push_back(value); + return true; +} + +bool grin_insert_string_to_row(GRIN_GRAPH g, GRIN_ROW r, const char* value) { + auto _r = static_cast(r); + _r->push_back(std::string(value)); + return true; +} + +bool grin_insert_date32_to_row(GRIN_GRAPH g, GRIN_ROW r, int value) { + auto _r = static_cast(r); + _r->push_back(value); + return true; +} + +bool grin_insert_time32_to_row(GRIN_GRAPH g, GRIN_ROW r, int value) { + auto _r = static_cast(r); + _r->push_back(value); + return true; +} + +bool grin_insert_timestamp64_to_row(GRIN_GRAPH g, GRIN_ROW r, + long long int value) { // NOLINT + auto _r = static_cast(r); + _r->push_back(value); + return true; +} +#endif + +#if defined(GRIN_ENABLE_ROW) && defined(GRIN_TRAIT_CONST_VALUE_PTR) +const void* grin_get_value_from_row(GRIN_GRAPH g, GRIN_ROW r, + GRIN_DATATYPE type, size_t idx) { + auto _r = static_cast(r); + __grin_check_row(_r, NULL); + switch (type) { + case GRIN_DATATYPE::Int32: + return &std::any_cast((*_r)[idx]); + case GRIN_DATATYPE::UInt32: + return &std::any_cast((*_r)[idx]); + case GRIN_DATATYPE::Int64: + return &std::any_cast((*_r)[idx]); + case GRIN_DATATYPE::UInt64: + return &std::any_cast((*_r)[idx]); + case GRIN_DATATYPE::Float: + return &std::any_cast((*_r)[idx]); + case GRIN_DATATYPE::Double: + return &std::any_cast((*_r)[idx]); + case GRIN_DATATYPE::String: + return std::any_cast((*_r)[idx]).c_str(); + case GRIN_DATATYPE::Date32: + return &std::any_cast((*_r)[idx]); + case GRIN_DATATYPE::Time32: + return &std::any_cast((*_r)[idx]); + case GRIN_DATATYPE::Timestamp64: + return &std::any_cast((*_r)[idx]); + default: + return NULL; + } + return NULL; +} +#endif + +#if defined(GRIN_WITH_VERTEX_PROPERTY) && defined(GRIN_ENABLE_ROW) +GRIN_ROW grin_get_vertex_row(GRIN_GRAPH g, GRIN_VERTEX v) { + auto _g = static_cast(g); + auto r = new GRIN_ROW_T(); + auto vtype = DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(v); + auto _v = _g->GetVertex(v); + auto& properties = _g->GetVertexProperties(vtype); + for (auto& property : properties) { + r->push_back(_v.GetPropertyAny(property.name_)); + } + return r; +} +#endif + +#if defined(GRIN_WITH_EDGE_PROPERTY) && defined(GRIN_ENABLE_ROW) +GRIN_ROW grin_get_edge_row(GRIN_GRAPH g, GRIN_EDGE e) { + auto _g = static_cast(g); + auto r = new GRIN_ROW_T(); + auto etype = DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(e); + auto _e = _g->GetEdge(e); + auto& properties = _g->GetEdgeProperties(etype); + for (auto& property : properties) { + r->push_back(_e.GetPropertyAny(property.name_)); + } + return r; +} +#endif diff --git a/demo_storage/src/property/topology.cc b/demo_storage/src/property/topology.cc new file mode 100644 index 0000000..ffff9ac --- /dev/null +++ b/demo_storage/src/property/topology.cc @@ -0,0 +1,65 @@ +/** Copyright 2020 Alibaba Group Holding Limited. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "src/predefine.h" +// GRIN headers +#include "property/topology.h" + +#ifdef GRIN_WITH_VERTEX_PROPERTY +size_t grin_get_vertex_num_by_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype) { + auto _g = static_cast(g); + return _g->GetVertexNum(vtype); +} +#endif + +#ifdef GRIN_WITH_EDGE_PROPERTY +size_t grin_get_edge_num_by_type(GRIN_GRAPH g, GRIN_EDGE_TYPE etype) { + auto _g = static_cast(g); + return _g->GetEdgeNum(etype); +} +#endif + +#if defined(GRIN_ENABLE_VERTEX_LIST) && defined(GRIN_WITH_VERTEX_PROPERTY) +GRIN_VERTEX_LIST grin_get_vertex_list_by_type(GRIN_GRAPH g, + GRIN_VERTEX_TYPE vtype) { + auto _g = static_cast(g); + if (vtype >= _g->GetVertexTypeNum()) + return GRIN_NULL_VERTEX_LIST; + auto vl = new GRIN_VERTEX_LIST_T(vtype); + return vl; +} +#endif + +#if defined(GRIN_ENABLE_EDGE_LIST) && defined(GRIN_WITH_EDGE_PROPERTY) +GRIN_EDGE_LIST grin_get_edge_list_by_type(GRIN_GRAPH g, GRIN_EDGE_TYPE etype) { + auto _g = static_cast(g); + if (etype >= _g->GetEdgeTypeNum()) + return GRIN_NULL_EDGE_LIST; + auto el = new GRIN_EDGE_LIST_T(etype); + return el; +} +#endif + +#if defined(GRIN_ENABLE_ADJACENT_LIST) && defined(GRIN_WITH_EDGE_PROPERTY) +GRIN_ADJACENT_LIST grin_get_adjacent_list_by_edge_type(GRIN_GRAPH g, + GRIN_DIRECTION d, + GRIN_VERTEX v, + GRIN_EDGE_TYPE etype) { + auto _g = static_cast(g); + if (etype >= _g->GetEdgeTypeNum()) + return GRIN_NULL_ADJACENT_LIST; + auto al = new GRIN_ADJACENT_LIST_T( + DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(v), + DEMO_STORAGE_NAMESPACE::get_id_from_gid(v), d, etype); + return al; +} +#endif diff --git a/demo_storage/src/property/type.cc b/demo_storage/src/property/type.cc new file mode 100644 index 0000000..234a86c --- /dev/null +++ b/demo_storage/src/property/type.cc @@ -0,0 +1,223 @@ +/** Copyright 2020 Alibaba Group Holding Limited. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "src/predefine.h" +// GRIN headers +#include "property/type.h" + +#ifdef GRIN_WITH_VERTEX_PROPERTY +// Vertex type +bool grin_equal_vertex_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype1, + GRIN_VERTEX_TYPE vtype2) { + return vtype1 == vtype2; +} + +GRIN_VERTEX_TYPE grin_get_vertex_type(GRIN_GRAPH g, GRIN_VERTEX v) { + return DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(v); +} + +void grin_destroy_vertex_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype) { return; } + +// Vertex type list +GRIN_VERTEX_TYPE_LIST grin_get_vertex_type_list(GRIN_GRAPH g) { + auto _g = static_cast(g); + auto vtl = new GRIN_VERTEX_TYPE_LIST_T(); + auto num = _g->GetVertexTypeNum(); + for (auto i = 0; i < num; i++) + vtl->push_back(i); + return vtl; +} + +void grin_destroy_vertex_type_list(GRIN_GRAPH g, GRIN_VERTEX_TYPE_LIST vtl) { + auto _vtl = static_cast(vtl); + delete _vtl; +} + +GRIN_VERTEX_TYPE_LIST grin_create_vertex_type_list(GRIN_GRAPH g) { + auto vtl = new GRIN_VERTEX_TYPE_LIST_T(); + return vtl; +} + +bool grin_insert_vertex_type_to_list(GRIN_GRAPH g, GRIN_VERTEX_TYPE_LIST vtl, + GRIN_VERTEX_TYPE vtype) { + auto _vtl = static_cast(vtl); + _vtl->push_back(vtype); + return true; +} + +size_t grin_get_vertex_type_list_size(GRIN_GRAPH g, GRIN_VERTEX_TYPE_LIST vtl) { + auto _vtl = static_cast(vtl); + return _vtl->size(); +} + +GRIN_VERTEX_TYPE grin_get_vertex_type_from_list(GRIN_GRAPH g, + GRIN_VERTEX_TYPE_LIST vtl, + size_t idx) { + auto _vtl = static_cast(vtl); + if (idx >= _vtl->size()) + return GRIN_NULL_VERTEX_TYPE; + return (*_vtl)[idx]; +} +#endif + +#ifdef GRIN_WITH_VERTEX_TYPE_NAME +const char* grin_get_vertex_type_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype) { + auto _g = static_cast(g); + if (vtype >= _g->GetVertexTypeNum()) + return NULL; + return _g->GetVertexTypeName(vtype).c_str(); +} + +GRIN_VERTEX_TYPE grin_get_vertex_type_by_name(GRIN_GRAPH g, const char* name) { + auto _g = static_cast(g); + auto type_id = _g->GetVertexTypeId(std::string(name)); + if (type_id == -1) + return GRIN_NULL_VERTEX_TYPE; + return type_id; +} +#endif + +#ifdef GRIN_TRAIT_NATURAL_ID_FOR_VERTEX_TYPE +GRIN_VERTEX_TYPE_ID grin_get_vertex_type_id(GRIN_GRAPH g, + GRIN_VERTEX_TYPE vtype) { + return vtype; +} + +GRIN_VERTEX_TYPE grin_get_vertex_type_by_id(GRIN_GRAPH g, + GRIN_VERTEX_TYPE_ID vti) { + return vti; +} +#endif + +#ifdef GRIN_WITH_EDGE_PROPERTY +// Edge type +bool grin_equal_edge_type(GRIN_GRAPH g, GRIN_EDGE_TYPE etype1, + GRIN_EDGE_TYPE etype2) { + return etype1 == etype2; +} + +GRIN_EDGE_TYPE grin_get_edge_type(GRIN_GRAPH g, GRIN_EDGE e) { + return DEMO_STORAGE_NAMESPACE::get_type_id_from_gid(e); +} + +void grin_destroy_edge_type(GRIN_GRAPH g, GRIN_EDGE_TYPE etype) { return; } + +// Edge type list +GRIN_EDGE_TYPE_LIST grin_get_edge_type_list(GRIN_GRAPH g) { + auto _g = static_cast(g); + auto etl = new GRIN_EDGE_TYPE_LIST_T(); + auto num = _g->GetEdgeTypeNum(); + for (auto i = 0; i < num; i++) + etl->push_back(i); + return etl; +} + +void grin_destroy_edge_type_list(GRIN_GRAPH g, GRIN_EDGE_TYPE_LIST etl) { + auto _etl = static_cast(etl); + delete _etl; +} + +GRIN_EDGE_TYPE_LIST grin_create_edge_type_list(GRIN_GRAPH g) { + auto etl = new GRIN_EDGE_TYPE_LIST_T(); + return etl; +} + +bool grin_insert_edge_type_to_list(GRIN_GRAPH g, GRIN_EDGE_TYPE_LIST etl, + GRIN_EDGE_TYPE etype) { + auto _etl = static_cast(etl); + _etl->push_back(etype); + return true; +} + +size_t grin_get_edge_type_list_size(GRIN_GRAPH g, GRIN_EDGE_TYPE_LIST etl) { + auto _etl = static_cast(etl); + return _etl->size(); +} + +GRIN_EDGE_TYPE grin_get_edge_type_from_list(GRIN_GRAPH g, + GRIN_EDGE_TYPE_LIST etl, + size_t idx) { + auto _etl = static_cast(etl); + if (idx >= _etl->size()) + return GRIN_NULL_EDGE_TYPE; + return (*_etl)[idx]; +} +#endif + +#ifdef GRIN_WITH_EDGE_TYPE_NAME +const char* grin_get_edge_type_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype) { + auto _g = static_cast(g); + if (etype >= _g->GetEdgeTypeNum()) + return NULL; + return _g->GetEdgeTypeName(etype).c_str(); +} + +GRIN_EDGE_TYPE grin_get_edge_type_by_name(GRIN_GRAPH g, const char* name) { + auto _g = static_cast(g); + auto type_id = _g->GetEdgeTypeId(std::string(name)); + if (type_id == -1) + return GRIN_NULL_EDGE_TYPE; + return type_id; +} +#endif + +#ifdef GRIN_TRAIT_NATURAL_ID_FOR_EDGE_TYPE +GRIN_EDGE_TYPE_ID grin_get_edge_type_id(GRIN_GRAPH g, GRIN_EDGE_TYPE etype) { + return etype; +} + +GRIN_EDGE_TYPE grin_get_edge_type_by_id(GRIN_GRAPH g, GRIN_EDGE_TYPE_ID eti) { + return eti; +} +#endif + +#if defined(GRIN_WITH_VERTEX_PROPERTY) && defined(GRIN_WITH_EDGE_PROPERTY) +GRIN_VERTEX_TYPE_LIST grin_get_src_types_by_edge_type(GRIN_GRAPH g, + GRIN_EDGE_TYPE etype) { + auto _g = static_cast(g); + auto vtl = new GRIN_VERTEX_TYPE_LIST_T(); + auto& vev_types = _g->GetVertexEdgeVertexTypes(); + for (auto& [vev_type, relation_type] : vev_types) { + if (std::get<1>(vev_type) == etype) { + vtl->push_back(std::get<0>(vev_type)); + } + } + return vtl; +} + +GRIN_VERTEX_TYPE_LIST grin_get_dst_types_by_edge_type(GRIN_GRAPH g, + GRIN_EDGE_TYPE etype) { + auto _g = static_cast(g); + auto vtl = new GRIN_VERTEX_TYPE_LIST_T(); + auto& vev_types = _g->GetVertexEdgeVertexTypes(); + for (auto& [vev_type, relation_type] : vev_types) { + if (std::get<1>(vev_type) == etype) { + vtl->push_back(std::get<2>(vev_type)); + } + } + return vtl; +} + +GRIN_EDGE_TYPE_LIST +grin_get_edge_types_by_vertex_type_pair(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype1, + GRIN_VERTEX_TYPE vtype2) { + auto _g = static_cast(g); + auto etl = new GRIN_EDGE_TYPE_LIST_T(); + auto& vev_types = _g->GetVertexEdgeVertexTypes(); + for (auto& [vev_type, relation_type] : vev_types) { + if (std::get<0>(vev_type) == vtype1 && std::get<2>(vev_type) == vtype2) { + etl->push_back(std::get<1>(vev_type)); + } + } + return etl; +} +#endif