Navigation Menu

Skip to content

Commit

Permalink
Gnx: add gnx_insert_rows() and gnx_set_values().
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Apr 8, 2015
1 parent dd1c2bd commit 74d342a
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
200 changes: 200 additions & 0 deletions go2/gnx/gnx.cpp
Expand Up @@ -119,6 +119,206 @@ gnx_bool gnx_set_value2(grn_ctx *ctx, grn_obj *column, gnx_int row_id,
return GNX_TRUE;
}


gnx_int gnx_insert_rows(grn_ctx *ctx, const char *table_name,
gnx_int num_keys, gnx_data_type key_type,
const void *keys, gnx_int *row_ids,
gnx_bool *inserted) {
grn_obj *table = grn_ctx_get(ctx, table_name, strlen(table_name));
if (!table) {
return GNX_NA_BOOL;
}
return gnx_insert_rows2(ctx, table, num_keys, key_type, keys, row_ids,
inserted);
}

gnx_int gnx_insert_rows2(grn_ctx *ctx, grn_obj *table,
gnx_int num_keys, gnx_data_type key_type,
const void *keys, gnx_int *row_ids,
gnx_bool *inserted) {
// TODO: type check.
gnx_int count = 0;
switch (key_type) {
case GNX_NA: {
for (gnx_int i = 0; i < num_keys; ++i) {
int added;
grn_id id = grn_table_add(ctx, table, nullptr, 0, &added);
if (id == GRN_ID_NIL) {
row_ids[i] = GNX_NA_INT;
} else {
row_ids[i] = id;
++count;
}
inserted[i] = added ? GNX_TRUE : GNX_FALSE;
}
break;
}
case GNX_INT: {
const gnx_int *int_keys = static_cast<const gnx_int *>(keys);
for (gnx_int i = 0; i < num_keys; ++i) {
int added;
grn_id id = grn_table_add(ctx, table, &int_keys[i],
sizeof(gnx_int), &added);
if (id == GRN_ID_NIL) {
row_ids[i] = GNX_NA_INT;
} else {
row_ids[i] = id;
++count;
}
inserted[i] = added ? GNX_TRUE : GNX_FALSE;
}
break;
}
case GNX_FLOAT: {
const gnx_float *float_keys = static_cast<const gnx_float *>(keys);
for (gnx_int i = 0; i < num_keys; ++i) {
int added;
grn_id id = grn_table_add(ctx, table, &float_keys[i],
sizeof(gnx_float), &added);
if (id == GRN_ID_NIL) {
row_ids[i] = GNX_NA_INT;
} else {
row_ids[i] = id;
++count;
}
inserted[i] = added ? GNX_TRUE : GNX_FALSE;
}
break;
}
// case GNX_GEO_POINT: {
// key_size = sizeof(gnx_geo_point);
// break;
// }
case GNX_TEXT: {
const gnx_text *text_keys = static_cast<const gnx_text *>(keys);
for (gnx_int i = 0; i < num_keys; ++i) {
const void *key = text_keys[i].data;
unsigned int key_size = text_keys[i].size;
int added;
grn_id id = grn_table_add(ctx, table, key, key_size, &added);
if (id == GRN_ID_NIL) {
row_ids[i] = GNX_NA_INT;
} else {
row_ids[i] = id;
++count;
}
inserted[i] = added ? GNX_TRUE : GNX_FALSE;
}
break;
}
default: {
return GNX_NA_INT;
}
}
return count;
}

gnx_int gnx_set_values(grn_ctx *ctx, const char *table_name,
const char *column_name, gnx_int num_values,
const gnx_int *row_ids, gnx_data_type value_type,
const void *values, gnx_bool *updated) {
grn_obj *table = grn_ctx_get(ctx, table_name, strlen(table_name));
if (!table) {
return GNX_NA_BOOL;
}
grn_obj *column = grn_obj_column(
ctx, table, column_name, strlen(column_name));
if (!column) {
return GNX_NA_BOOL;
}
return gnx_set_values2(ctx, table, column, num_values, row_ids,
value_type, values, updated);
}

gnx_int gnx_set_values2(grn_ctx *ctx, grn_obj *table, grn_obj *column,
gnx_int num_values, const gnx_int *row_ids,
gnx_data_type value_type, const void *values,
gnx_bool *updated) {
gnx_int count = 0;
switch (value_type) {
// case GNX_NA: {
// break;
// }
case GNX_BOOL: {
grn_obj obj;
GRN_BOOL_INIT(&obj, 0);
for (gnx_int i = 0; i < num_values; ++i) {
GRN_BOOL_SET(ctx, &obj,
static_cast<const gnx_bool *>(values)[i] == GNX_TRUE);
grn_rc rc = grn_obj_set_value(ctx, column, row_ids[i], &obj,
GRN_OBJ_SET);
if (rc == GRN_SUCCESS) {
updated[i] = GNX_TRUE;
++count;
} else {
updated[i] = GNX_FALSE;
}
}
GRN_OBJ_FIN(ctx, &obj);
break;
}
case GNX_INT: {
grn_obj obj;
GRN_INT64_INIT(&obj, 0);
for (gnx_int i = 0; i < num_values; ++i) {
GRN_INT64_SET(ctx, &obj, static_cast<const gnx_int *>(values)[i]);
grn_rc rc = grn_obj_set_value(ctx, column, row_ids[i], &obj,
GRN_OBJ_SET);
if (rc == GRN_SUCCESS) {
updated[i] = GNX_TRUE;
++count;
} else {
updated[i] = GNX_FALSE;
}
}
GRN_OBJ_FIN(ctx, &obj);
break;
}
case GNX_FLOAT: {
grn_obj obj;
GRN_FLOAT_INIT(&obj, 0);
for (gnx_int i = 0; i < num_values; ++i) {
GRN_FLOAT_SET(ctx, &obj, static_cast<const gnx_float *>(values)[i]);
grn_rc rc = grn_obj_set_value(ctx, column, row_ids[i], &obj,
GRN_OBJ_SET);
if (rc == GRN_SUCCESS) {
updated[i] = GNX_TRUE;
++count;
} else {
updated[i] = GNX_FALSE;
}
}
GRN_OBJ_FIN(ctx, &obj);
break;
}
// case GNX_GEO_POINT: {
// break;
// }
case GNX_TEXT: {
grn_obj obj;
GRN_TEXT_INIT(&obj, 0);
for (gnx_int i = 0; i < num_values; ++i) {
gnx_text text = static_cast<const gnx_text *>(values)[i];
GRN_TEXT_SET(ctx, &obj, text.data, text.size);
grn_rc rc = grn_obj_set_value(ctx, column, row_ids[i], &obj,
GRN_OBJ_SET);
if (rc == GRN_SUCCESS) {
updated[i] = GNX_TRUE;
++count;
} else {
updated[i] = GNX_FALSE;
}
}
GRN_OBJ_FIN(ctx, &obj);
break;
}
default: {
return GNX_NA_INT;
}
}
return count;
}

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
18 changes: 18 additions & 0 deletions go2/gnx/gnx.h
Expand Up @@ -45,6 +45,24 @@ gnx_bool gnx_set_value(grn_ctx *ctx, const char *table_name,
gnx_bool gnx_set_value2(grn_ctx *ctx,grn_obj *column, gnx_int row_id,
gnx_data_type value_type, const void *value);

gnx_int gnx_insert_rows(grn_ctx *ctx, const char *table_name,
gnx_int num_keys, gnx_data_type key_type,
const void *keys, gnx_int *row_ids,
gnx_bool *inserted);
gnx_int gnx_insert_rows2(grn_ctx *ctx, grn_obj *table,
gnx_int num_keys, gnx_data_type key_type,
const void *keys, gnx_int *row_ids,
gnx_bool *inserted);

gnx_int gnx_set_values(grn_ctx *ctx, const char *table_name,
const char *column_name, gnx_int num_values,
const gnx_int *row_ids, gnx_data_type value_type,
const void *values, gnx_bool *updated);
gnx_int gnx_set_values2(grn_ctx *ctx, grn_obj *table, grn_obj *column,
gnx_int num_values, const gnx_int *row_ids,
gnx_data_type value_type, const void *values,
gnx_bool *updated);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down

0 comments on commit 74d342a

Please sign in to comment.