diff --git a/demo_storage/src/topology/adjacentlist.cc b/demo_storage/src/topology/adjacentlist.cc new file mode 100644 index 0000000..6763097 --- /dev/null +++ b/demo_storage/src/topology/adjacentlist.cc @@ -0,0 +1,237 @@ +/** 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 "topology/adjacentlist.h" + +#if defined(GRIN_ENABLE_ADJACENT_LIST) && !defined(GRIN_WITH_EDGE_PROPERTY) +GRIN_ADJACENT_LIST grin_get_adjacent_list(GRIN_GRAPH, GRIN_DIRECTION, + GRIN_VERTEX); +#endif + +#ifdef GRIN_ENABLE_ADJACENT_LIST +void grin_destroy_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIST al) { + auto _al = static_cast(al); + delete _al; +} +#endif + +#ifdef GRIN_ENABLE_ADJACENT_LIST_ARRAY +size_t grin_get_adjacent_list_size(GRIN_GRAPH g, GRIN_ADJACENT_LIST al) { + auto _g = static_cast(g); + auto _al = static_cast(al); + auto vtype = _al->vtype_id; + auto vid = _al->vid; + auto etype = _al->etype_id; + auto dir = _al->dir; + auto partition_num = _g->GetPartitionNum(); + + if (_al->partition_type == ALL_PARTITION) { + auto num = 0; + for (auto i = 0; i < partition_num; i++) { + num += _g->GetAdjacentListSize(vtype, vid, etype, i, dir); + } + return num; + } + + auto partition_id = _al->partition_id; + if (_al->partition_type == ONE_PARTITION) { + return _g->GetAdjacentListSize(vtype, vid, etype, partition_id, dir); + } + + if (_al->partition_type == ALL_BUT_ONE_PARTITION) { + auto num = 0; + for (auto i = 0; i < partition_num; i++) { + if (i != partition_id) { + num += _g->GetAdjacentListSize(vtype, vid, etype, i, dir); + } + } + return num; + } + + return GRIN_NULL_SIZE; +} + +GRIN_VERTEX grin_get_neighbor_from_adjacent_list(GRIN_GRAPH g, + GRIN_ADJACENT_LIST al, + size_t idx) { + auto e = grin_get_edge_from_adjacent_list(g, al, idx); + if (e == GRIN_NULL_EDGE) { + return GRIN_NULL_VERTEX; + } + auto _g = static_cast(g); + auto _al = static_cast(al); + auto& edge = _g->GetEdge(e); + auto v = DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id( + _al->vtype_id, _al->vid); + if (edge.GetSource() == v) { + return edge.GetDest(); + } else if (edge.GetDest() == v) { + return edge.GetSource(); + } else { + return GRIN_NULL_VERTEX; + } +} + +GRIN_EDGE grin_get_edge_from_adjacent_list(GRIN_GRAPH g, GRIN_ADJACENT_LIST al, + size_t idx) { + auto _g = static_cast(g); + auto _al = static_cast(al); + auto vtype = _al->vtype_id; + auto vid = _al->vid; + auto etype = _al->etype_id; + auto dir = _al->dir; + auto partition_num = _g->GetPartitionNum(); + size_t prefix = 0; + for (auto i = 0; i < partition_num; i++) { + auto size = _g->GetAdjacentListSize(vtype, vid, etype, i, dir); + if (_al->partition_type == ONE_PARTITION && i != _al->partition_id) { + size = 0; + } + if (_al->partition_type == ALL_BUT_ONE_PARTITION && + i == _al->partition_id) { + size = 0; + } + if (idx >= prefix && idx < prefix + size) { + auto& edges = _g->GetAdjacentList(vtype, vid, etype, i, dir); + return edges[idx - prefix]; + } + prefix += size; + } + return GRIN_NULL_EDGE; +} +#endif + +#ifdef GRIN_ENABLE_ADJACENT_LIST_ITERATOR +GRIN_ADJACENT_LIST_ITERATOR grin_get_adjacent_list_begin( + GRIN_GRAPH g, GRIN_ADJACENT_LIST al) { + auto _g = static_cast(g); + auto _al = static_cast(al); + auto vtype = _al->vtype_id; + auto vid = _al->vid; + auto etype = _al->etype_id; + auto dir = _al->dir; + auto partition_id = _al->partition_id; + auto partition_num = _g->GetPartitionNum(); + + auto current_partition = 0; + while (current_partition < partition_num) { + auto size = + _g->GetAdjacentListSize(vtype, vid, etype, current_partition, dir); + if (_al->partition_type == ONE_PARTITION && + current_partition != partition_id) { + size = 0; + } + if (_al->partition_type == ALL_BUT_ONE_PARTITION && + current_partition == partition_id) { + size = 0; + } + if (size > 0) { + return new GRIN_ADJACENT_LIST_ITERATOR_T( + vtype, vid, dir, etype, _al->partition_type, partition_id, + current_partition, 0); + } + current_partition++; + } + + return new GRIN_ADJACENT_LIST_ITERATOR_T(vtype, vid, dir, etype, + _al->partition_type, partition_id, + current_partition, 0); +} + +void grin_destroy_adjacent_list_iter(GRIN_GRAPH g, + GRIN_ADJACENT_LIST_ITERATOR ali) { + auto _ali = static_cast(ali); + delete _ali; +} + +void grin_get_next_adjacent_list_iter(GRIN_GRAPH g, + GRIN_ADJACENT_LIST_ITERATOR ali) { + auto _g = static_cast(g); + auto _ali = static_cast(ali); + auto vtype = _ali->vtype_id; + auto vid = _ali->vid; + auto etype = _ali->etype_id; + auto dir = _ali->dir; + auto partition_num = _g->GetPartitionNum(); + auto partition_id = _ali->partition_id; + + _ali->current_offset++; + if (_ali->current_offset >= _g->GetAdjacentListSize(vtype, vid, etype, + _ali->current_partition, + dir)) { + _ali->current_offset = 0; + _ali->current_partition++; + while (_ali->current_partition < partition_num) { + auto size = _g->GetAdjacentListSize(vtype, vid, etype, + _ali->current_partition, dir); + if (_ali->partition_type == ONE_PARTITION && + _ali->current_partition != partition_id) { + size = 0; + } + if (_ali->partition_type == ALL_BUT_ONE_PARTITION && + _ali->current_partition == partition_id) { + size = 0; + } + if (size > 0) { + return; + } + _ali->current_partition++; + } + } +} + +bool grin_is_adjacent_list_end(GRIN_GRAPH g, GRIN_ADJACENT_LIST_ITERATOR ali) { + if (ali == GRIN_NULL_ADJACENT_LIST_ITERATOR) { + return true; + } + auto _g = static_cast(g); + auto _ali = static_cast(ali); + return _ali->current_partition >= _g->GetPartitionNum(); +} + +GRIN_VERTEX grin_get_neighbor_from_adjacent_list_iter( + GRIN_GRAPH g, GRIN_ADJACENT_LIST_ITERATOR ali) { + auto e = grin_get_edge_from_adjacent_list_iter(g, ali); + if (e == GRIN_NULL_EDGE) { + return GRIN_NULL_VERTEX; + } + auto _g = static_cast(g); + auto _ali = static_cast(ali); + auto& edge = _g->GetEdge(e); + auto v = DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id( + _ali->vtype_id, _ali->vid); + if (edge.GetSource() == v) { + return edge.GetDest(); + } else if (edge.GetDest() == v) { + return edge.GetSource(); + } else { + return GRIN_NULL_VERTEX; + } +} + +GRIN_EDGE grin_get_edge_from_adjacent_list_iter( + GRIN_GRAPH g, GRIN_ADJACENT_LIST_ITERATOR ali) { + auto _g = static_cast(g); + auto _ali = static_cast(ali); + auto& edges = _g->GetAdjacentList(_ali->vtype_id, _ali->vid, _ali->etype_id, + _ali->current_partition, _ali->dir); + if (_ali->current_offset >= edges.size()) { + return GRIN_NULL_EDGE; + } + return edges[_ali->current_offset]; +} +#endif diff --git a/demo_storage/src/topology/edgelist.cc b/demo_storage/src/topology/edgelist.cc new file mode 100644 index 0000000..5b3402c --- /dev/null +++ b/demo_storage/src/topology/edgelist.cc @@ -0,0 +1,185 @@ +/** 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 "topology/edgelist.h" + +#if defined(GRIN_ENABLE_EDGE_LIST) && !defined(GRIN_WITH_EDGE_PROPERTY) +GRIN_EDGE_LIST grin_get_edge_list(GRIN_GRAPH); +#endif + +#ifdef GRIN_ENABLE_EDGE_LIST +void grin_destroy_edge_list(GRIN_GRAPH g, GRIN_EDGE_LIST el) { + auto _el = static_cast(el); + delete _el; +} +#endif + +#ifdef GRIN_ENABLE_EDGE_LIST_ARRAY +size_t grin_get_edge_list_size(GRIN_GRAPH g, GRIN_EDGE_LIST el) { + auto _g = static_cast(g); + auto _el = static_cast(el); + auto etype = _el->type_id; + if (etype >= _g->GetEdgeTypeNum()) + return GRIN_NULL_SIZE; + + if (_el->partition_type == ALL_PARTITION) { + return _g->GetEdgeNum(etype); + } else if (_el->partition_type == ONE_PARTITION) { + return _g->GetPartitionedEdgeNum(etype, _el->partition_id); + } else if (_el->partition_type == ALL_BUT_ONE_PARTITION) { + return _g->GetEdgeNum(etype) - + _g->GetPartitionedEdgeNum(etype, _el->partition_id); + } + + return GRIN_NULL_SIZE; +} + +GRIN_EDGE grin_get_edge_from_list(GRIN_GRAPH g, GRIN_EDGE_LIST el, size_t idx) { + auto _g = static_cast(g); + auto _el = static_cast(el); + auto etype = _el->type_id; + auto num = _g->GetEdgeNum(etype); + + if (_el->partition_type == ALL_PARTITION) { + if (idx < num) { + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(etype, + idx); + } else { + return GRIN_NULL_EDGE; + } + } + + auto partition_id = _el->partition_id; + auto partition_num = _g->GetPartitionNum(); + auto partitioned_num = _g->GetPartitionedEdgeNum(etype, partition_id); + + if (_el->partition_type == ONE_PARTITION) { + if (idx < partitioned_num) { + auto _idx = idx * partition_num + partition_id; + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(etype, + _idx); + } else { + return GRIN_NULL_EDGE; + } + } + if (_el->partition_type == ALL_BUT_ONE_PARTITION) { + if (idx < num - partitioned_num) { + auto _idx = idx + idx / (partition_num - 1); + if (idx % (partition_num - 1) >= partition_id) + _idx++; + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(etype, + _idx); + } else { + return GRIN_NULL_EDGE; + } + } + return GRIN_NULL_EDGE; +} +#endif + +#ifdef GRIN_ENABLE_EDGE_LIST_ITERATOR +GRIN_EDGE_LIST_ITERATOR grin_get_edge_list_begin(GRIN_GRAPH g, + GRIN_EDGE_LIST el) { + auto _g = static_cast(g); + auto _el = static_cast(el); + auto etype = _el->type_id; + + if (_el->partition_type == ALL_PARTITION) { + return new GRIN_EDGE_LIST_ITERATOR_T(etype, ALL_PARTITION, 0, 0); + } + + auto num = _g->GetEdgeNum(etype); + auto partition_id = _el->partition_id; + auto partition_num = _g->GetPartitionNum(); + + if (_el->partition_type == ONE_PARTITION) { + auto _offset = partition_id; + if (_offset >= num) + _offset = -1; + return new GRIN_EDGE_LIST_ITERATOR_T(etype, ONE_PARTITION, partition_id, + _offset); + } + + if (_el->partition_type == ALL_BUT_ONE_PARTITION) { + auto _offset = 0; + if (_offset % partition_num == partition_id) + _offset++; + if (_offset >= num) + _offset = -1; + return new GRIN_EDGE_LIST_ITERATOR_T(etype, ALL_BUT_ONE_PARTITION, + partition_id, _offset); + } + + return GRIN_NULL_EDGE_LIST_ITERATOR; +} + +void grin_destroy_edge_list_iter(GRIN_GRAPH g, GRIN_EDGE_LIST_ITERATOR eli) { + auto _eli = static_cast(eli); + delete _eli; +} + +void grin_get_next_edge_list_iter(GRIN_GRAPH g, GRIN_EDGE_LIST_ITERATOR eli) { + auto _g = static_cast(g); + auto _eli = static_cast(eli); + auto etype = _eli->type_id; + auto num = _g->GetEdgeNum(etype); + + if (_eli->partition_type == ALL_PARTITION) { + _eli->current_offset++; + if (_eli->current_offset >= num) { + _eli->current_offset = -1; + } + return; + } + + auto partition_id = _eli->partition_id; + auto partition_num = _g->GetPartitionNum(); + + if (_eli->partition_type == ONE_PARTITION) { + _eli->current_offset += partition_num; + if (_eli->current_offset >= num) { + _eli->current_offset = -1; + } + return; + } + + if (_eli->partition_type == ALL_BUT_ONE_PARTITION) { + _eli->current_offset++; + if (_eli->current_offset % partition_num == partition_id) { + _eli->current_offset++; + } + if (_eli->current_offset >= num) { + _eli->current_offset = -1; + } + return; + } +} + +bool grin_is_edge_list_end(GRIN_GRAPH g, GRIN_EDGE_LIST_ITERATOR eli) { + if (eli == GRIN_NULL_EDGE_LIST_ITERATOR) { + return true; + } + auto _eli = static_cast(eli); + return _eli->current_offset == -1; +} + +GRIN_EDGE grin_get_edge_from_iter(GRIN_GRAPH g, GRIN_EDGE_LIST_ITERATOR eli) { + auto _eli = static_cast(eli); + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id( + _eli->type_id, _eli->current_offset); +} +#endif diff --git a/demo_storage/src/topology/vertexlist.cc b/demo_storage/src/topology/vertexlist.cc new file mode 100644 index 0000000..f6d35c8 --- /dev/null +++ b/demo_storage/src/topology/vertexlist.cc @@ -0,0 +1,190 @@ +/** 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 "topology/vertexlist.h" + +#if defined(GRIN_ENABLE_VERTEX_LIST) && !defined(GRIN_WITH_VERTEX_PROPERTY) +GRIN_VERTEX_LIST grin_get_vertex_list(GRIN_GRAPH); +#endif + +#ifdef GRIN_ENABLE_VERTEX_LIST +void grin_destroy_vertex_list(GRIN_GRAPH g, GRIN_VERTEX_LIST vl) { + auto _vl = static_cast(vl); + delete _vl; +} +#endif + +#ifdef GRIN_ENABLE_VERTEX_LIST_ARRAY +size_t grin_get_vertex_list_size(GRIN_GRAPH g, GRIN_VERTEX_LIST vl) { + auto _g = static_cast(g); + auto _vl = static_cast(vl); + auto vtype = _vl->type_id; + if (vtype >= _g->GetVertexTypeNum()) + GRIN_NULL_SIZE; + + if (_vl->partition_type == ALL_PARTITION) { + return _g->GetVertexNum(vtype); + } else if (_vl->partition_type == ONE_PARTITION) { + return _g->GetPartitionedVertexNum(vtype, _vl->partition_id); + } else if (_vl->partition_type == ALL_BUT_ONE_PARTITION) { + return _g->GetVertexNum(vtype) - + _g->GetPartitionedVertexNum(vtype, _vl->partition_id); + } + + return GRIN_NULL_SIZE; +} + +GRIN_VERTEX grin_get_vertex_from_list(GRIN_GRAPH g, GRIN_VERTEX_LIST vl, + size_t idx) { + auto _g = static_cast(g); + auto _vl = static_cast(vl); + auto vtype = _vl->type_id; + auto num = _g->GetVertexNum(vtype); + + if (_vl->partition_type == ALL_PARTITION) { + if (idx < num) { + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(vtype, + idx); + } else { + return GRIN_NULL_VERTEX; + } + } + + auto partition_id = _vl->partition_id; + auto partition_num = _g->GetPartitionNum(); + auto partitioned_num = _g->GetPartitionedVertexNum(vtype, partition_id); + + if (_vl->partition_type == ONE_PARTITION) { + if (idx < partitioned_num) { + auto _idx = idx * partition_num + partition_id; + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(vtype, + _idx); + } else { + return GRIN_NULL_VERTEX; + } + } + + if (_vl->partition_type == ALL_BUT_ONE_PARTITION) { + if (idx < num - partitioned_num) { + auto _idx = idx + idx / (partition_num - 1); + if (idx % (partition_num - 1) >= partition_id) + _idx++; + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id(vtype, + _idx); + } else { + return GRIN_NULL_VERTEX; + } + } + return GRIN_NULL_VERTEX; +} +#endif + +#ifdef GRIN_ENABLE_VERTEX_LIST_ITERATOR +GRIN_VERTEX_LIST_ITERATOR grin_get_vertex_list_begin(GRIN_GRAPH g, + GRIN_VERTEX_LIST vl) { + auto _g = static_cast(g); + auto _vl = static_cast(vl); + auto vtype = _vl->type_id; + + if (_vl->partition_type == ALL_PARTITION) { + return new GRIN_VERTEX_LIST_ITERATOR_T(vtype, ALL_PARTITION, 0, 0); + } + + auto num = _g->GetVertexNum(vtype); + auto partition_id = _vl->partition_id; + auto partition_num = _g->GetPartitionNum(); + + if (_vl->partition_type == ONE_PARTITION) { + auto _offset = partition_id; + if (_offset >= num) + _offset = -1; + return new GRIN_VERTEX_LIST_ITERATOR_T(vtype, ONE_PARTITION, partition_id, + _offset); + } + + if (_vl->partition_type == ALL_BUT_ONE_PARTITION) { + auto _offset = 0; + if (_offset % partition_num == partition_id) + _offset++; + if (_offset >= num) + _offset = -1; + return new GRIN_VERTEX_LIST_ITERATOR_T(vtype, ALL_BUT_ONE_PARTITION, + partition_id, _offset); + } + + return GRIN_NULL_VERTEX_LIST_ITERATOR; +} + +void grin_destroy_vertex_list_iter(GRIN_GRAPH g, + GRIN_VERTEX_LIST_ITERATOR vli) { + auto _vli = static_cast(vli); + delete _vli; +} + +void grin_get_next_vertex_list_iter(GRIN_GRAPH g, + GRIN_VERTEX_LIST_ITERATOR vli) { + auto _g = static_cast(g); + auto _vli = static_cast(vli); + auto vtype = _vli->type_id; + auto num = _g->GetVertexNum(vtype); + + if (_vli->partition_type == ALL_PARTITION) { + _vli->current_offset++; + if (_vli->current_offset >= num) { + _vli->current_offset = -1; + } + return; + } + + auto partition_id = _vli->partition_id; + auto partition_num = _g->GetPartitionNum(); + + if (_vli->partition_type == ONE_PARTITION) { + _vli->current_offset += partition_num; + if (_vli->current_offset >= num) { + _vli->current_offset = -1; + } + return; + } + + if (_vli->partition_type == ALL_BUT_ONE_PARTITION) { + _vli->current_offset++; + if (_vli->current_offset % partition_num == partition_id) { + _vli->current_offset++; + } + if (_vli->current_offset >= num) { + _vli->current_offset = -1; + } + return; + } +} + +bool grin_is_vertex_list_end(GRIN_GRAPH g, GRIN_VERTEX_LIST_ITERATOR vli) { + if (vli == GRIN_NULL_VERTEX_LIST_ITERATOR) { + return true; + } + auto _vli = static_cast(vli); + return _vli->current_offset == -1; +} + +GRIN_VERTEX grin_get_vertex_from_iter(GRIN_GRAPH g, + GRIN_VERTEX_LIST_ITERATOR vli) { + auto _vli = static_cast(vli); + return DEMO_STORAGE_NAMESPACE::generate_gid_from_type_id_and_id( + _vli->type_id, _vli->current_offset); +} +#endif