Skip to content

Commit

Permalink
add cache for vertex properties
Browse files Browse the repository at this point in the history
  • Loading branch information
liulx20 committed Jul 19, 2023
1 parent 02d8a5a commit b2dd5ea
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 84 deletions.
2 changes: 1 addition & 1 deletion flex/engines/graph_db/grin/src/index/internal_id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ GRIN_VERTEX grin_get_vertex_by_internal_id_by_type(GRIN_GRAPH g,
long long int grin_get_vertex_internal_id_upper_bound_by_type(
GRIN_GRAPH g, GRIN_VERTEX_TYPE vt) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
return _g->vertex_num(vt);
return _g->g.vertex_num(vt);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion flex/engines/graph_db/grin/src/index/pk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ GRIN_VERTEX grin_get_vertex_by_primary_keys_row(GRIN_GRAPH g,
auto oid = *static_cast<const gs::oid_t*>((*_r)[0]);
uint32_t vid;

if (!_g->get_lid(label, oid, vid)) {
if (!_g->g.get_lid(label, oid, vid)) {
return GRIN_NULL_VERTEX;
}
uint64_t v = ((label * 1ull) << 32) + vid;
Expand Down
36 changes: 36 additions & 0 deletions flex/engines/graph_db/grin/src/predefine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,40 @@ GRIN_DATATYPE _get_data_type(const gs::PropertyType& type) {
} else {
return GRIN_DATATYPE::Undefined;
}
}

void init_cache(GRIN_GRAPH_T* g) {
auto v_label_num = g->g.vertex_label_num_;
for (size_t i = 0; i < v_label_num; ++i) {
std::vector<const void*> tmp;
const auto& vec = g->g.schema().get_vertex_properties(i);
const auto& table = g->g.get_vertex_table(i);
for (size_t idx = 0; idx < vec.size(); ++idx) {
const auto& type = vec[idx];
if (type == gs::PropertyType::kInt32) {
tmp.emplace_back(std::dynamic_pointer_cast<gs::IntColumn>(
table.get_column_by_id(idx))
.get());
} else if (type == gs::PropertyType::kInt64) {
tmp.emplace_back(std::dynamic_pointer_cast<gs::LongColumn>(
table.get_column_by_id(idx))
.get());
} else if (type == gs::PropertyType::kString) {
tmp.emplace_back(std::dynamic_pointer_cast<gs::StringColumn>(
table.get_column_by_id(idx))
.get());
} else if (type == gs::PropertyType::kDate) {
tmp.emplace_back(std::dynamic_pointer_cast<gs::DateColumn>(
table.get_column_by_id(idx))
.get());
} else if (type == gs::PropertyType::kDouble) {
tmp.emplace_back(std::dynamic_pointer_cast<gs::DoubleColumn>(
table.get_column_by_id(idx))
.get());
} else {
tmp.emplace_back((const void*) NULL);
}
}
g->vproperties.emplace_back(tmp);
}
}
12 changes: 9 additions & 3 deletions flex/engines/graph_db/grin/src/predefine.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include "grin/predefine.h"
#include <vector>

#include "grin/predefine.h"
#include "storages/rt_mutable_graph/mutable_property_fragment.h"

typedef gs::oid_t GRIN_OID_T;
typedef gs::vid_t GRIN_VID_T;

typedef gs::MutablePropertyFragment GRIN_GRAPH_T;
typedef struct GRIN_GRAPH_T {
gs::MutablePropertyFragment g;
std::vector<std::vector<const void*>> vproperties;
// std::vector<std::vector<const void*>> eproperties;
} GRIN_GRAPH_T;

typedef struct GRIN_EDGE_T {
GRIN_VERTEX dst;
Expand All @@ -29,4 +34,5 @@ typedef std::vector<unsigned> GRIN_EDGE_TYPE_LIST_T;
typedef std::vector<unsigned> GRIN_EDGE_PROPERTY_LIST_T;
#endif

GRIN_DATATYPE _get_data_type(const gs::PropertyType& type);
GRIN_DATATYPE _get_data_type(const gs::PropertyType& type);
void init_cache(GRIN_GRAPH_T* g);
4 changes: 2 additions & 2 deletions flex/engines/graph_db/grin/src/property/primarykey.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
GRIN_VERTEX_TYPE_LIST grin_get_vertex_types_with_primary_keys(GRIN_GRAPH g) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
GRIN_VERTEX_TYPE_LIST_T* vtl = new GRIN_VERTEX_TYPE_LIST_T();
for (size_t idx = 0; idx < _g->vertex_label_num_; ++idx) {
for (size_t idx = 0; idx < _g->g.vertex_label_num_; ++idx) {
vtl->push_back(idx);
}
return vtl;
Expand Down Expand Up @@ -49,7 +49,7 @@ GRIN_ROW grin_get_vertex_primary_keys_row(GRIN_GRAPH g, GRIN_VERTEX v) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
auto vid = v & (0xffffffff);
auto label = v >> 32;
auto oid = _g->get_oid(label, vid);
auto oid = _g->g.get_oid(label, vid);
auto p = new gs::oid_t(oid);
row->emplace_back(p);
return row;
Expand Down
76 changes: 38 additions & 38 deletions flex/engines/graph_db/grin/src/property/property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void grin_destroy_string_value(GRIN_GRAPH g, const char* value) {
const char* grin_get_vertex_property_name(GRIN_GRAPH g, GRIN_VERTEX_TYPE vt,
GRIN_VERTEX_PROPERTY vp) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
auto& table = _g->get_vertex_table(vt);
auto& table = _g->g.get_vertex_table(vt);

const auto& name = table.column_name(vp & (0xff));
auto len = name.length() + 1;
Expand All @@ -36,7 +36,7 @@ GRIN_VERTEX_PROPERTY grin_get_vertex_property_by_name(GRIN_GRAPH g,
GRIN_VERTEX_TYPE vt,
const char* name) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
auto& table = _g->get_vertex_table(vt);
auto& table = _g->g.get_vertex_table(vt);
auto col = table.get_column(name);
if (col == nullptr) {
return GRIN_NULL_VERTEX_PROPERTY;
Expand All @@ -54,8 +54,8 @@ GRIN_VERTEX_PROPERTY_LIST grin_get_vertex_properties_by_name(GRIN_GRAPH g,
std::string prop_name(name);
auto vps = new GRIN_VERTEX_PROPERTY_LIST_T();
std::string _name = std::string(name);
for (auto idx = 0; idx < _g->schema().vertex_label_num(); idx++) {
auto& table = _g->get_vertex_table(static_cast<GRIN_VERTEX_TYPE>(idx));
for (auto idx = 0; idx < _g->g.vertex_label_num_; idx++) {
auto& table = _g->g.get_vertex_table(static_cast<GRIN_VERTEX_TYPE>(idx));

auto col = table.get_column(name);

Expand Down Expand Up @@ -103,13 +103,12 @@ int grin_get_vertex_property_value_of_int32(GRIN_GRAPH g, GRIN_VERTEX v,
grin_error_code = INVALID_VALUE;
return 0;
}
auto& table = _g->get_vertex_table(plabel);
auto col =
std::dynamic_pointer_cast<gs::IntColumn>(table.get_column_by_id(pid));
if (col == nullptr) {
auto pcol = _g->vproperties[label][pid];
if (pcol == NULL) {
grin_error_code = INVALID_VALUE;
return 0;
}
auto col = static_cast<const gs::IntColumn*>(pcol);
return col->get_view(vid);
}

Expand All @@ -134,13 +133,12 @@ long long int grin_get_vertex_property_value_of_int64(GRIN_GRAPH g,
grin_error_code = INVALID_VALUE;
return 0;
}
auto& table = _g->get_vertex_table(plabel);
auto col =
std::dynamic_pointer_cast<gs::LongColumn>(table.get_column_by_id(pid));
if (col == nullptr) {
auto pcol = _g->vproperties[label][pid];
if (pcol == NULL) {
grin_error_code = INVALID_VALUE;
return 0;
}
auto col = static_cast<const gs::LongColumn*>(pcol);
return col->get_view(vid);
}

Expand Down Expand Up @@ -169,14 +167,12 @@ double grin_get_vertex_property_value_of_double(GRIN_GRAPH g, GRIN_VERTEX v,
grin_error_code = INVALID_VALUE;
return 0.0;
}
auto& table = _g->get_vertex_table(plabel);

auto col =
std::dynamic_pointer_cast<gs::DoubleColumn>(table.get_column_by_id(pid));
if (col == nullptr) {
auto pcol = _g->vproperties[label][pid];
if (pcol == NULL) {
grin_error_code = INVALID_VALUE;
return 0.0;
return 0;
}
auto col = static_cast<const gs::DoubleColumn*>(pcol);
return col->get_view(vid);
}

Expand All @@ -194,13 +190,13 @@ const char* grin_get_vertex_property_value_of_string(GRIN_GRAPH g,
grin_error_code = INVALID_VALUE;
return NULL;
}
auto& table = _g->get_vertex_table(plabel);
auto col =
std::dynamic_pointer_cast<gs::StringColumn>(table.get_column_by_id(pid));
if (col == nullptr) {
auto pcol = _g->vproperties[label][pid];
if (pcol == NULL) {
grin_error_code = INVALID_VALUE;
return NULL;
return 0;
}
auto col = static_cast<const gs::StringColumn*>(pcol);

auto s = col->get_view(vid);
auto len = s.size() + 1;
char* out = new char[len];
Expand Down Expand Up @@ -234,13 +230,12 @@ long long int grin_get_vertex_property_value_of_timestamp64(
return 0;
}

auto& table = _g->get_vertex_table(plabel);
auto col =
std::dynamic_pointer_cast<gs::DateColumn>(table.get_column_by_id(pid));
if (col == nullptr) {
auto pcol = _g->vproperties[label][pid];
if (pcol == NULL) {
grin_error_code = INVALID_VALUE;
return 0;
}
auto col = static_cast<const gs::DateColumn*>(pcol);
return col->get_view(vid).milli_second;
}

Expand All @@ -258,29 +253,34 @@ const void* grin_get_vertex_property_value(GRIN_GRAPH g, GRIN_VERTEX v,
auto pid = vp & (0xff);

auto _g = static_cast<GRIN_GRAPH_T*>(g);
auto& table = _g->get_vertex_table(plabel);
const auto& col = table.get_column_by_id(pid);

auto col = _g->vproperties[plabel][pid];
if (col == NULL) {
grin_error_code = UNKNOWN_DATATYPE;
return 0;
}

auto vid = v & (0xffffffff);

switch (type) {
case GRIN_DATATYPE::Int32: {
auto _col = std::dynamic_pointer_cast<gs::IntColumn>(col);
auto _col = static_cast<const gs::IntColumn*>(col);
return _col->buffer().data() + vid;
}
case GRIN_DATATYPE::Int64: {
auto _col = std::dynamic_pointer_cast<gs::LongColumn>(col);
auto _col = static_cast<const gs::LongColumn*>(col);
return _col->buffer().data() + vid;
}
case GRIN_DATATYPE::String: {
auto _col = std::dynamic_pointer_cast<gs::StringColumn>(col);
auto _col = static_cast<const gs::StringColumn*>(col);
return _col->buffer()[vid].data();
}
case GRIN_DATATYPE::Timestamp64: {
auto _col = std::dynamic_pointer_cast<gs::DateColumn>(col);
auto _col = static_cast<const gs::DateColumn*>(col);
return _col->buffer().data() + vid;
}
case GRIN_DATATYPE::Double: {
auto _col = std::dynamic_pointer_cast<gs::DoubleColumn>(col);
auto _col = static_cast<const gs::DoubleColumn*>(col);
return _col->buffer().data() + vid;
}
default:
Expand All @@ -302,13 +302,13 @@ GRIN_DATATYPE grin_get_edge_property_datatype(GRIN_GRAPH g,
GRIN_EDGE_PROPERTY ep) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
auto src_label_i = (ep >> 16) & 0xff;
const auto& src_label = _g->schema().get_vertex_label_name(src_label_i);
const auto& src_label = _g->g.schema().get_vertex_label_name(src_label_i);
auto dst_label_i = (ep >> 8) & 0xff;
const auto& dst_label = _g->schema().get_vertex_label_name(dst_label_i);
const auto& dst_label = _g->g.schema().get_vertex_label_name(dst_label_i);
auto edge_label_i = ep & 0xff;
const auto& edge_label = _g->schema().get_edge_label_name(edge_label_i);
const auto& edge_label = _g->g.schema().get_edge_label_name(edge_label_i);
const auto& type =
_g->schema().get_edge_properties(src_label, dst_label, edge_label);
_g->g.schema().get_edge_properties(src_label, dst_label, edge_label);
auto idx = ep >> 24;
return _get_data_type(type[idx]);
}
Expand Down
15 changes: 8 additions & 7 deletions flex/engines/graph_db/grin/src/property/propertylist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
GRIN_VERTEX_PROPERTY_LIST grin_get_vertex_property_list_by_type(
GRIN_GRAPH g, GRIN_VERTEX_TYPE vt) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
auto& table = _g->get_vertex_table(vt);
auto& table = _g->g.get_vertex_table(vt);

auto vertex_prop_num = table.col_num();
GRIN_VERTEX_PROPERTY_LIST_T* vpl = new GRIN_VERTEX_PROPERTY_LIST_T();
Expand Down Expand Up @@ -69,7 +69,7 @@ GRIN_VERTEX_PROPERTY grin_get_vertex_property_by_id(
GRIN_GRAPH g, GRIN_VERTEX_TYPE vt, GRIN_VERTEX_PROPERTY_ID pid) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);

auto& table = _g->get_vertex_table(vt);
auto& table = _g->g.get_vertex_table(vt);
auto vertex_prop_num = table.col_num();

if (pid >= vertex_prop_num) {
Expand Down Expand Up @@ -97,13 +97,14 @@ GRIN_EDGE_PROPERTY_LIST grin_get_edge_property_list_by_type(GRIN_GRAPH g,

auto _g = static_cast<GRIN_GRAPH_T*>(g);
auto src_label_i = et >> 16;
auto src_label = _g->schema().get_vertex_label_name(src_label_i);
auto src_label = _g->g.schema().get_vertex_label_name(src_label_i);
auto dst_label_i = (et >> 8) & (0xff);
auto dst_label = _g->schema().get_vertex_label_name(dst_label_i);
auto dst_label = _g->g.schema().get_vertex_label_name(dst_label_i);
auto edge_label_i = et & 0xff;
auto edge_label = _g->schema().get_edge_label_name(edge_label_i);
auto sz =
_g->schema().get_edge_properties(src_label, dst_label, edge_label).size();
auto edge_label = _g->g.schema().get_edge_label_name(edge_label_i);
auto sz = _g->g.schema()
.get_edge_properties(src_label, dst_label, edge_label)
.size();
for (size_t i = 0; i < sz; ++i) {
p->emplace_back(et + (i << 24));
}
Expand Down
15 changes: 7 additions & 8 deletions flex/engines/graph_db/grin/src/property/row.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,41 +180,40 @@ GRIN_ROW grin_get_vertex_row(GRIN_GRAPH g, GRIN_VERTEX v) {
auto _g = static_cast<GRIN_GRAPH_T*>(g);
auto vid = v & (0xffffffff);
auto label = v >> 32;
auto& table = _g->get_vertex_table(label);
auto& table = _g->g.get_vertex_table(label);
auto prop_size = table.col_num();
const auto& types = table.column_types();
auto r = new GRIN_ROW_T();
for (size_t prop_id = 0; prop_id < prop_size; prop_id++) {
auto col = table.get_column_by_id(prop_id);
auto col = _g->vproperties[label][prop_id];
auto type = _get_data_type(types[prop_id]);
switch (type) {
case GRIN_DATATYPE::Int32: {
auto _col = std::dynamic_pointer_cast<gs::IntColumn>(col);
auto _col = static_cast<const gs::IntColumn*>(col);
r->emplace_back(_col->buffer().data() + vid);
break;
}
case GRIN_DATATYPE::Int64: {
auto _col = std::dynamic_pointer_cast<gs::LongColumn>(col);
auto _col = static_cast<const gs::LongColumn*>(col);
r->emplace_back(_col->buffer().data() + vid);
break;
}
case GRIN_DATATYPE::String: {
auto _col = std::dynamic_pointer_cast<gs::StringColumn>(col);
auto _col = static_cast<const gs::StringColumn*>(col);
auto s = _col->get_view(vid);
auto len = s.size() + 1;
char* out = new char[len];
snprintf(out, len, "%s", s.data());

r->emplace_back(out);
break;
}
case GRIN_DATATYPE::Timestamp64: {
auto _col = std::dynamic_pointer_cast<gs::DateColumn>(col);
auto _col = static_cast<const gs::DateColumn*>(col);
r->emplace_back(_col->buffer().data() + vid);
break;
}
case GRIN_DATATYPE::Double: {
auto _col = std::dynamic_pointer_cast<gs::DoubleColumn>(col);
auto _col = static_cast<const gs::DoubleColumn*>(col);
r->emplace_back(_col->buffer().data() + vid);
break;
}
Expand Down
Loading

0 comments on commit b2dd5ea

Please sign in to comment.