Kouhei Sutou
null+****@clear*****
Thu Aug 24 12:01:25 JST 2017
Kouhei Sutou 2017-08-24 12:01:25 +0900 (Thu, 24 Aug 2017) New Revision: 322e00b676ee406de325cdf5133370b094cd42cb https://github.com/groonga/groonga/commit/322e00b676ee406de325cdf5133370b094cd42cb Message: Add a new API to find a reference object for the target target New API: * grn_table_find_reference_object() Modified files: include/groonga/table.h lib/grn_db.h lib/table.c Modified: include/groonga/table.h (+2 -0) =================================================================== --- include/groonga/table.h 2017-08-24 12:00:49 +0900 (ea42700aa) +++ include/groonga/table.h 2017-08-24 12:01:25 +0900 (f9de43f46) @@ -239,6 +239,8 @@ GRN_API grn_rc grn_table_apply_expr(grn_ctx *ctx, grn_obj *output_column, grn_obj *expr); +GRN_API grn_id grn_table_find_reference_object(grn_ctx *ctx, grn_obj *table); + #ifdef __cplusplus } #endif Modified: lib/grn_db.h (+47 -0) =================================================================== --- lib/grn_db.h 2017-08-24 12:00:49 +0900 (570883c1e) +++ lib/grn_db.h 2017-08-24 12:01:25 +0900 (b270b000e) @@ -70,6 +70,53 @@ grn_bool grn_db_spec_unpack(grn_ctx *ctx, grn_obj *decoded_spec, const char *error_message_tag); +#define GRN_DB_SPEC_EACH_BEGIN(ctx, cursor, id, spec) do { \ + grn_obj *db = grn_ctx_db((ctx)); \ + grn_db *db_raw = (grn_db *)db; \ + grn_obj decoded_spec; \ + grn_io_win iw; \ + grn_bool iw_need_unref = GRN_FALSE; \ + GRN_OBJ_INIT(&decoded_spec, GRN_VECTOR, 0, GRN_DB_TEXT); \ + GRN_TABLE_EACH_BEGIN((ctx), db, cursor, id) { \ + void *encoded_spec; \ + uint32_t encoded_spec_size; \ + grn_bool success; \ + grn_obj_spec *spec; \ + \ + if (iw_need_unref) { \ + grn_ja_unref(ctx, &iw); \ + iw_need_unref = GRN_FALSE; \ + } \ + encoded_spec = grn_ja_ref((ctx), \ + db_raw->specs, \ + id, \ + &iw, \ + &encoded_spec_size); \ + if (!encoded_spec) { \ + continue; \ + } \ + iw_need_unref = GRN_TRUE; \ + \ + GRN_BULK_REWIND(&decoded_spec); \ + success = grn_db_spec_unpack(ctx, \ + id, \ + encoded_spec, \ + encoded_spec_size, \ + &spec, \ + &decoded_spec, \ + __FUNCTION__); \ + if (!success) { \ + continue; \ + } \ + +#define GRN_DB_SPEC_EACH_END(ctx, cursor) \ + } GRN_TABLE_EACH_END(ctx, cursor); \ + if (iw_need_unref) { \ + grn_ja_unref(ctx, &iw); \ + } \ + GRN_OBJ_FIN((ctx), &decoded_spec); \ +} while(GRN_FALSE) + void grn_db_init_from_env(void); GRN_API grn_rc grn_db_close(grn_ctx *ctx, grn_obj *db); Modified: lib/table.c (+48 -0) =================================================================== --- lib/table.c 2017-08-24 12:00:49 +0900 (f25ea7d5a) +++ lib/table.c 2017-08-24 12:01:25 +0900 (72a2f280b) @@ -72,3 +72,51 @@ grn_table_apply_expr(grn_ctx *ctx, GRN_API_RETURN(ctx->rc); } + +grn_id +grn_table_find_reference_object(grn_ctx *ctx, grn_obj *table) +{ + grn_id table_id; + grn_id reference_object_id = GRN_ID_NIL; + + GRN_API_ENTER; + + if (!grn_obj_is_table(ctx, table)) { + GRN_API_RETURN(GRN_ID_NIL); + } + + table_id = DB_OBJ(table)->id; + + GRN_DB_SPEC_EACH_BEGIN(ctx, cursor, id, spec) { + if (id == table_id) { + continue; + } + + switch (spec->header.type) { + case GRN_TABLE_HASH_KEY : + case GRN_TABLE_PAT_KEY : + case GRN_TABLE_DAT_KEY : + if (spec->header.domain == table_id) { + reference_object_id = id; + } + break; + case GRN_COLUMN_VAR_SIZE : + case GRN_COLUMN_FIX_SIZE : + if (spec->header.domain == table_id) { + break; + } + if (spec->range == table_id) { + reference_object_id = id; + } + break; + default : + break; + } + + if (reference_object_id != GRN_ID_NIL) { + break; + } + } GRN_DB_SPEC_EACH_END(ctx, cursor); + + GRN_API_RETURN(reference_object_id); +} -------------- next part -------------- HTML����������������������������...Télécharger