Created
August 5, 2018 13:06
-
-
Save msh5/7c8cf799897808d36ea5b4cb342f3d2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <assert.h> | |
#include <groonga/groonga.h> | |
#define TABLE_NAME "table" | |
int main (int argc, char **argv) { | |
// ライブラリを初期化 | |
assert(grn_init() == GRN_SUCCESS); | |
// データベースを作成 | |
grn_obj *db = grn_db_create(ctx, NULL, NULL); | |
assert(db != NULL); | |
// コンテキストを取得 | |
grn_ctx *ctx = grn_ctx_open(0); | |
assert(ctx != NULL); | |
// テーブルを作成 | |
grn_obj *value_type = grn_ctx_at(ctx, GRN_DB_UINT32); | |
assert(value_type != NULL); | |
grn_obj *table = grn_table_create(ctx, TABLE_NAME, strlen(TABLE_NAME), NULL, GRN_OBJ_TEMPORARY | GRN_OBJ_TABLE_NO_KEY, NULL, value_type); | |
assert(table != NULL); | |
// レコードを作成 | |
grn_id record_id = grn_table_add(ctx, table, NULL, 0, NULL); | |
assert(record_id != GRN_ID_NIL); | |
// レコードに値を設定しておく | |
grn_obj record_value; | |
GRN_UINT32_INIT(&record_value, 0); | |
GRN_UINT32_SET(ctx, &record_value, 100); | |
assert(GRN_UINT32_VALUE(&record_value) == 100); | |
grn_obj_set_value(ctx, table, record_id, &record_value, GRN_OBJ_SET); | |
GRN_BULK_REWIND(&record_value); | |
// テーブルを一旦クローズ | |
grn_obj_unlink(ctx, table); | |
// テーブルをオープン | |
table = grn_ctx_get(ctx, TABLE_NAME, strlen(TABLE_NAME)); | |
assert(table != NULL); | |
// 同じIDのレコードから設定した値が取得できることを確認 | |
grn_obj_get_value(ctx, table, record_id, &record_value); | |
assert(GRN_UINT32_VALUE(&record_value) == 100); | |
// リソースを破棄 | |
grn_obj_unlink(ctx, &record_value); | |
grn_obj_unlink(ctx, db); | |
grn_obj_unlink(ctx, table); | |
assert(grn_ctx_close(ctx) == GRN_SUCCESS); | |
// ライブラリの終了処理 | |
assert(grn_fin() == GRN_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment