Skip to content

Instantly share code, notes, and snippets.

@naoa
Last active February 1, 2016 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoa/f42fd362497d31d71242 to your computer and use it in GitHub Desktop.
Save naoa/f42fd362497d31d71242 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <groonga.h>
int main()
{
grn_ctx ctx;
grn_obj *db, *table;
grn_id id;
const char *path = "score.grn";
grn_init();
grn_ctx_init(&ctx, 0);
db = grn_db_open(&ctx, path);
if (!db) { db = grn_db_create(&ctx, path, NULL); }
table = grn_table_create(&ctx, NULL, 0,
NULL,
GRN_OBJ_TABLE_HASH_KEY,
grn_ctx_at(&ctx, GRN_DB_SHORT_TEXT), NULL);
if ((id = grn_table_add(&ctx, table, "hoge", strlen("hoge"), NULL))) {
grn_obj *score_accessor;
grn_obj score_buffer;
score_accessor = grn_obj_column(&ctx, table,
GRN_COLUMN_NAME_SCORE,
GRN_COLUMN_NAME_SCORE_LEN);
GRN_FLOAT_INIT(&score_buffer, 0);
GRN_FLOAT_SET(&ctx, &score_buffer, 2.0);
grn_obj_set_value(&ctx, score_accessor, id, &score_buffer, GRN_OBJ_SET);
GRN_OBJ_FIN(&ctx, &score_buffer);
grn_obj_close(&ctx, score_accessor);
}
grn_p(&ctx, table);
/*
{
grn_table_cursor *tc;
if ((tc = grn_table_cursor_open(&ctx, table, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_BY_ID))) {
grn_obj *key_column = grn_obj_column(&ctx, table,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
grn_obj score_buffer;
GRN_FLOAT_INIT(&score_buffer, 0);
while ((id = grn_table_cursor_next(&ctx, tc))) {
GRN_BULK_REWIND(&score_buffer);
grn_obj_get_value(&ctx, key_column, id, &score_buffer);
printf("id=%d score=%f\n", id, GRN_FLOAT_VALUE(&score_buffer));
}
grn_obj_unlink(&ctx, key_column);
grn_obj_unlink(&ctx, &score_buffer);
grn_table_cursor_close(&ctx, tc);
}
}
*/
grn_obj_unlink(&ctx, table);
grn_obj_close(&ctx, db);
grn_ctx_fin(&ctx);
grn_fin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment