diff --git a/modules/graph/grin/src/index/order.cc b/modules/graph/grin/src/index/order.cc index bc87bbfa..ea8a9bc5 100644 --- a/modules/graph/grin/src/index/order.cc +++ b/modules/graph/grin/src/index/order.cc @@ -26,7 +26,8 @@ bool grin_smaller_vertex(GRIN_GRAPH g, GRIN_VERTEX v1, GRIN_VERTEX v2) { size_t grin_get_position_of_vertex_from_sorted_list(GRIN_GRAPH g, GRIN_VERTEX_LIST vl, GRIN_VERTEX v) { auto _g = static_cast(g)->g; auto _vl = static_cast(vl); - auto vtype = (unsigned)_g->vertex_label(_GRIN_VERTEX_T(v)); // TODO: optimize after rebase + auto bg = static_cast(g); + auto vtype = bg->cache->id_parser.GetLabelId(v); if (vtype < _vl->type_begin || vtype >= _vl->type_end) return GRIN_NULL_SIZE; auto offset = v - _vl->vrs[vtype - _vl->type_begin].begin_value(); if (offset < _vl->vrs[vtype - _vl->type_begin].size()) { diff --git a/modules/graph/grin/src/partition/partition.cc b/modules/graph/grin/src/partition/partition.cc index 9cca7633..d291f05d 100644 --- a/modules/graph/grin/src/partition/partition.cc +++ b/modules/graph/grin/src/partition/partition.cc @@ -105,6 +105,7 @@ GRIN_GRAPH grin_get_local_graph_by_partition(GRIN_PARTITIONED_GRAPH pg, GRIN_PAR g->client.Connect(_pg->socket); g->_g = std::dynamic_pointer_cast<_GRIN_GRAPH_T>(g->client.GetObject(_pg->lgs[p])); g->g = g->_g.get(); + _prepare_cache(g); return g; } #endif diff --git a/modules/graph/grin/src/partition/reference.cc b/modules/graph/grin/src/partition/reference.cc index dcf30791..97d2f7d9 100644 --- a/modules/graph/grin/src/partition/reference.cc +++ b/modules/graph/grin/src/partition/reference.cc @@ -37,9 +37,8 @@ GRIN_VERTEX grin_get_vertex_from_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) { GRIN_PARTITION grin_get_master_partition_from_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) { auto _g = static_cast(g)->g; - auto id_parser = vineyard::IdParser(); //TODO optimize after rebase - id_parser.Init(_g->fnum(), _g->vertex_label_num()); - return id_parser.GetFid(vr); + auto bg = static_cast(g); + return bg->cache->id_parser.GetFid(vr); } const char* grin_serialize_vertex_ref(GRIN_GRAPH g, GRIN_VERTEX_REF vr) { @@ -74,12 +73,12 @@ GRIN_VERTEX_REF grin_deserialize_to_vertex_ref(GRIN_GRAPH g, const char* msg) { bool grin_is_master_vertex(GRIN_GRAPH g, GRIN_VERTEX v) { auto _g = static_cast(g)->g; - return _g->IsInnerVertex(_GRIN_VERTEX_T(v)); // TODO + return _g->IsInnerVertex(_GRIN_VERTEX_T(v)); } bool grin_is_mirror_vertex(GRIN_GRAPH g, GRIN_VERTEX v) { auto _g = static_cast(g)->g; - return _g->IsOuterVertex(_GRIN_VERTEX_T(v)); // TODO + return _g->IsOuterVertex(_GRIN_VERTEX_T(v)); } #endif diff --git a/modules/graph/grin/src/predefine.cc b/modules/graph/grin/src/predefine.cc index bb60a706..d1fc2fce 100644 --- a/modules/graph/grin/src/predefine.cc +++ b/modules/graph/grin/src/predefine.cc @@ -72,11 +72,11 @@ const void* _get_value_from_vertex_property_table(GRIN_GRAPH g, GRIN_VERTEX_PROP grin_error_code = GRIN_ERROR_CODE::NO_ERROR; auto _g = static_cast(g)->g; auto _vpt = static_cast(vpt); - if (v < _vpt->vbegin || v >= _vpt->vend) { + unsigned vtype = _grin_get_type_from_property(vp); + if (v < _vpt->vbegin || v >= _vpt->vend || vtype != _vpt->vtype) { grin_error_code = GRIN_ERROR_CODE::INVALID_VALUE; return NULL; - } - unsigned vtype = _grin_get_type_from_property(vp); + } unsigned vprop = _grin_get_prop_from_property(vp); auto offset = v - _vpt->vbegin; auto array = _g->vertex_data_table(vtype)->column(vprop)->chunk(0); @@ -153,3 +153,25 @@ unsigned _grin_get_type_from_property(unsigned long long int prop) { unsigned _grin_get_prop_from_property(unsigned long long int prop) { return (unsigned)(prop & 0xffffffff); } + +void _prepare_cache(GRIN_GRAPH_T* g) { + g->cache = new _GRAPH_CACHE(); + g->cache->id_parser = vineyard::IdParser<_GRIN_GRAPH_T::vid_t>(); + g->cache->id_parser.Init(g->g->fnum(), g->g->vertex_label_num()); + + for (int i = 0; i < g->g->vertex_label_num(); ++i) { + g->cache->vtype_names.push_back(g->g->schema().GetVertexLabelName(i)); + g->cache->vprop_names.push_back(std::vector()); + for (int j = 0; j < g->g->vertex_property_num(i); ++j) { + g->cache->vprop_names[i].push_back(g->g->schema().GetVertexPropertyName(i, j)); + } + } + + for (int i = 0; i < g->g->edge_label_num(); ++i) { + g->cache->etype_names.push_back(g->g->schema().GetEdgeLabelName(i)); + g->cache->eprop_names.push_back(std::vector()); + for (int j = 0; j < g->g->edge_property_num(i); ++j) { + g->cache->eprop_names[i].push_back(g->g->schema().GetEdgePropertyName(i, j)); + } + } +} diff --git a/modules/graph/grin/src/predefine.h b/modules/graph/grin/src/predefine.h index 01fc6671..d446c3d1 100644 --- a/modules/graph/grin/src/predefine.h +++ b/modules/graph/grin/src/predefine.h @@ -96,13 +96,26 @@ unsigned _grin_get_prop_from_property(unsigned long long int); #define GRIN_VID_T uint64_t /* The following data types shall be defined through typedef. */ -typedef vineyard::ArrowFragment _GRIN_GRAPH_T; +typedef vineyard::ArrowFragment _GRIN_GRAPH_T; +struct _GRAPH_CACHE { + vineyard::IdParser<_GRIN_GRAPH_T::vid_t> id_parser; + std::vector vtype_names; + std::vector etype_names; + std::vector> vprop_names; + std::vector> eprop_names; +}; + struct GRIN_GRAPH_T { vineyard::Client client; std::shared_ptr<_GRIN_GRAPH_T> _g; _GRIN_GRAPH_T* g; + _GRAPH_CACHE* cache; }; -typedef _GRIN_GRAPH_T::vertex_t _GRIN_VERTEX_T; + +void _prepare_cache(GRIN_GRAPH_T* g); + +typedef _GRIN_GRAPH_T::vertex_t _GRIN_VERTEX_T; + struct GRIN_EDGE_T { _GRIN_GRAPH_T::vid_t src; _GRIN_GRAPH_T::vid_t dst; diff --git a/modules/graph/grin/src/property/property.cc b/modules/graph/grin/src/property/property.cc index da060a55..896245c0 100644 --- a/modules/graph/grin/src/property/property.cc +++ b/modules/graph/grin/src/property/property.cc @@ -17,12 +17,8 @@ extern "C" { #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)->g; - auto s = _g->schema().GetVertexPropertyName(_grin_get_type_from_property(vp), _grin_get_prop_from_property(vp)); - int len = s.length() + 1; - char* out = new char[len]; - snprintf(out, len, "%s", s.c_str()); - return out; // TODO: optimize after rebase, put strings in g. + auto bg = static_cast(g); + return bg->cache->vprop_names[_grin_get_type_from_property(vp)][_grin_get_prop_from_property(vp)].c_str(); } GRIN_VERTEX_PROPERTY grin_get_vertex_property_by_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vtype, @@ -54,12 +50,8 @@ GRIN_VERTEX_PROPERTY_LIST grin_get_vertex_properties_by_name(GRIN_GRAPH g, const #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)->g; - auto s = _g->schema().GetEdgePropertyName(_grin_get_type_from_property(ep), _grin_get_prop_from_property(ep)); - int len = s.length() + 1; - char* out = new char[len]; - snprintf(out, len, "%s", s.c_str()); - return out; // TODO + auto bg = static_cast(g); + return bg->cache->eprop_names[_grin_get_type_from_property(ep)][_grin_get_prop_from_property(ep)].c_str(); } GRIN_EDGE_PROPERTY grin_get_edge_property_by_name(GRIN_GRAPH g, GRIN_EDGE_TYPE etype, diff --git a/modules/graph/grin/src/property/type.cc b/modules/graph/grin/src/property/type.cc index bb0de2c9..1a678cce 100644 --- a/modules/graph/grin/src/property/type.cc +++ b/modules/graph/grin/src/property/type.cc @@ -22,7 +22,8 @@ bool grin_equal_vertex_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vt1, GRIN_VERTEX_TYPE GRIN_VERTEX_TYPE grin_get_vertex_type(GRIN_GRAPH g, GRIN_VERTEX v) { auto _g = static_cast(g)->g; - return _g->vertex_label(_GRIN_VERTEX_T(v)); // TODO + auto bg = static_cast(g); + return bg->cache->id_parser.GetLabelId(v); } void grin_destroy_vertex_type(GRIN_GRAPH g, GRIN_VERTEX_TYPE vt) {} @@ -66,11 +67,8 @@ GRIN_VERTEX_TYPE grin_get_vertex_type_from_list(GRIN_GRAPH g, GRIN_VERTEX_TYPE_L #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)->g; - auto s = _g->schema().GetVertexLabelName(vtype); - int len = s.length() + 1; - char* out = new char[len]; - snprintf(out, len, "%s", s.c_str()); - return out; // TODO + auto bg = static_cast(g); + return bg->cache->vtype_names[vtype].c_str(); } GRIN_VERTEX_TYPE grin_get_vertex_type_by_name(GRIN_GRAPH g, const char* name) { @@ -144,11 +142,8 @@ GRIN_EDGE_TYPE grin_get_edge_type_from_list(GRIN_GRAPH g, GRIN_EDGE_TYPE_LIST et #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)->g; - auto s = _g->schema().GetEdgeLabelName(etype); - int len = s.length() + 1; - char* out = new char[len]; - snprintf(out, len, "%s", s.c_str()); - return out; // TODO + auto bg = static_cast(g); + return bg->cache->etype_names[etype].c_str(); } GRIN_EDGE_TYPE grin_get_edge_type_by_name(GRIN_GRAPH g, const char* name) { diff --git a/modules/graph/grin/src/topology/structure.cc b/modules/graph/grin/src/topology/structure.cc index 8f8503bb..c29e433a 100644 --- a/modules/graph/grin/src/topology/structure.cc +++ b/modules/graph/grin/src/topology/structure.cc @@ -33,11 +33,13 @@ GRIN_GRAPH grin_get_graph_from_storage(int argc, char** argv) { g->_g = std::dynamic_pointer_cast<_GRIN_GRAPH_T>(g->client.GetObject(obj_id)); g->g = g->_g.get(); + _prepare_cache(g); return g; } void grin_destroy_graph(GRIN_GRAPH g) { auto _g = static_cast(g); + delete _g->cache; delete _g; }