Kouhei Sutou
null+****@clear*****
Fri Sep 23 16:42:46 JST 2016
Kouhei Sutou 2016-09-23 16:42:46 +0900 (Fri, 23 Sep 2016) New Revision: aff5d676e31c324b407493074e0cd9470452146a https://github.com/groonga/groonga/commit/aff5d676e31c324b407493074e0cd9470452146a Message: Add temporary open space It automatically closes newly opened tables and columns in the scope: grn_ctx_push_temporary_open_space(ctx); /* ... Open a new table (table1) by grn_ctx_at() ... */ /* ... Open a opened table (table2) by grn_ctx_at() ... */ grn_ctx_pop_temporary_open_space(ctx); /* The above call closes only table1. */ grn_ctx_merge_temporary_open_space() moves recorded newly opened tables and columns to the below space in stack: grn_ctx_push_temporary_open_space(ctx); /* ... Open a new table (table1) by grn_ctx_at() ... */ grn_ctx_push_temporary_open_space(ctx); /* ... Open a new table (table2) by grn_ctx_at() ... */ grn_ctx_merge_temporary_open_space(ctx); /* The above call closes nothing. */ grn_ctx_merge_temporary_open_space(ctx); /* The above call closes table1 and table2. */ Modified files: include/groonga/groonga.h lib/ctx.c lib/db.c lib/grn_ctx_impl.h Modified: include/groonga/groonga.h (+4 -0) =================================================================== --- include/groonga/groonga.h 2016-09-23 14:08:38 +0900 (aaf33c6) +++ include/groonga/groonga.h 2016-09-23 16:42:46 +0900 (42e5baf) @@ -247,6 +247,10 @@ GRN_API grn_ctx *grn_ctx_open(int flags); GRN_API grn_rc grn_ctx_close(grn_ctx *ctx); GRN_API grn_rc grn_ctx_set_finalizer(grn_ctx *ctx, grn_proc_func *func); +GRN_API grn_rc grn_ctx_push_temporary_open_space(grn_ctx *ctx); +GRN_API grn_rc grn_ctx_pop_temporary_open_space(grn_ctx *ctx); +GRN_API grn_rc grn_ctx_merge_temporary_open_space(grn_ctx *ctx); + GRN_API grn_encoding grn_get_default_encoding(void); GRN_API grn_rc grn_set_default_encoding(grn_encoding encoding); Modified: lib/ctx.c (+17 -0) =================================================================== --- lib/ctx.c 2016-09-23 14:08:38 +0900 (be9f6cd) +++ lib/ctx.c 2016-09-23 16:42:46 +0900 (ba31555) @@ -281,6 +281,9 @@ grn_ctx_impl_init(grn_ctx *ctx) grn_ctx_impl_mrb_init(ctx); + GRN_TEXT_INIT(&(ctx->impl->temporary_open_spaces.stack), 0); + ctx->impl->temporary_open_spaces.current = NULL; + return ctx->rc; } @@ -414,6 +417,20 @@ grn_ctx_fin(grn_ctx *ctx) if (ctx->impl->finalizer) { ctx->impl->finalizer(ctx, 0, NULL, &(ctx->user_data)); } + { + grn_obj *stack; + grn_obj *spaces; + unsigned int i, n_spaces; + + stack = &(ctx->impl->temporary_open_spaces.stack); + spaces = (grn_obj *)GRN_BULK_HEAD(stack); + n_spaces = GRN_BULK_VSIZE(stack) / sizeof(grn_obj); + for (i = 0; i < n_spaces; i++) { + grn_obj *space = spaces + (n_spaces - i - 1); + GRN_OBJ_FIN(ctx, space); + } + GRN_OBJ_FIN(ctx, stack); + } grn_ctx_impl_mrb_fin(ctx); grn_ctx_loader_clear(ctx); if (ctx->impl->parser) { Modified: lib/db.c (+110 -1) =================================================================== --- lib/db.c 2016-09-23 14:08:38 +0900 (ff97e0f) +++ lib/db.c 2016-09-23 16:42:46 +0900 (7358294) @@ -10407,6 +10407,25 @@ grn_ctx_at(grn_ctx *ctx, grn_id id) GRN_FUTEX_WAIT(&vp->ptr); } } + if (vp->ptr) { + switch (vp->ptr->header.type) { + case GRN_TABLE_HASH_KEY : + case GRN_TABLE_PAT_KEY : + case GRN_TABLE_DAT_KEY : + case GRN_TABLE_NO_KEY : + case GRN_COLUMN_FIX_SIZE : + case GRN_COLUMN_VAR_SIZE : + case GRN_COLUMN_INDEX : + { + grn_obj *space; + space = ctx->impl->temporary_open_spaces.current; + if (space) { + GRN_PTR_PUT(ctx, space, vp->ptr); + } + } + break; + } + } } res = vp->ptr; if (res && res->header.type == GRN_PROC) { @@ -10489,7 +10508,7 @@ grn_pvector_fin(grn_ctx *ctx, grn_obj *obj) unsigned int i, n_elements; n_elements = GRN_BULK_VSIZE(obj) / sizeof(grn_obj *); for (i = 0; i < n_elements; i++) { - grn_obj *element = GRN_PTR_VALUE_AT(obj, i); + grn_obj *element = GRN_PTR_VALUE_AT(obj, n_elements - i - 1); grn_obj_close(ctx, element); } } @@ -14820,3 +14839,93 @@ grn_ctx_get_all_token_filters(grn_ctx *ctx, grn_obj *token_filters_buffer) return grn_ctx_get_all_objects(ctx, token_filters_buffer, grn_obj_is_token_filter_proc); } + +grn_rc +grn_ctx_push_temporary_open_space(grn_ctx *ctx) +{ + grn_obj *stack; + grn_obj *space; + grn_obj buffer; + + GRN_API_ENTER; + + stack = &(ctx->impl->temporary_open_spaces.stack); + GRN_VOID_INIT(&buffer); + grn_bulk_write(ctx, stack, (const char *)&buffer, sizeof(grn_obj)); + space = ((grn_obj *)GRN_BULK_CURR(stack)) - 1; + GRN_PTR_INIT(space, GRN_OBJ_VECTOR | GRN_OBJ_OWN, GRN_ID_NIL); + + ctx->impl->temporary_open_spaces.current = space; + + GRN_API_RETURN(ctx->rc); +} + +grn_rc +grn_ctx_pop_temporary_open_space(grn_ctx *ctx) +{ + grn_obj *stack; + grn_obj *space; + + GRN_API_ENTER; + + stack = &(ctx->impl->temporary_open_spaces.stack); + if (GRN_BULK_EMPTYP(stack)) { + ERR(GRN_INVALID_ARGUMENT, + "[ctx][temporary-open-spaces][pop] too much pop"); + GRN_API_RETURN(ctx->rc); + } + + space = ctx->impl->temporary_open_spaces.current; + GRN_OBJ_FIN(ctx, space); + grn_bulk_truncate(ctx, stack, GRN_BULK_VSIZE(stack) - sizeof(grn_obj)); + + if (GRN_BULK_EMPTYP(stack)) { + space = NULL; + } else { + space = ((grn_obj *)GRN_BULK_CURR(stack)) - 1; + } + ctx->impl->temporary_open_spaces.current = space; + + GRN_API_RETURN(ctx->rc); +} + +grn_rc +grn_ctx_merge_temporary_open_space(grn_ctx *ctx) +{ + grn_obj *stack; + grn_obj *space; + grn_obj *next_space; + + GRN_API_ENTER; + + stack = &(ctx->impl->temporary_open_spaces.stack); + if (GRN_BULK_VSIZE(stack) < sizeof(grn_obj) * 2) { + ERR(GRN_INVALID_ARGUMENT, + "[ctx][temporary-open-spaces][merge] " + "merge requires at least two spaces"); + GRN_API_RETURN(ctx->rc); + } + + space = ctx->impl->temporary_open_spaces.current; + next_space = ctx->impl->temporary_open_spaces.current - 1; + { + unsigned int i, n_elements; + n_elements = GRN_BULK_VSIZE(space) / sizeof(grn_obj *); + for (i = 0; i < n_elements; i++) { + grn_obj *element = GRN_PTR_VALUE_AT(space, i); + GRN_PTR_PUT(ctx, next_space, element); + } + } + GRN_BULK_REWIND(space); + GRN_OBJ_FIN(ctx, space); + grn_bulk_truncate(ctx, stack, GRN_BULK_VSIZE(stack) - sizeof(grn_obj)); + + if (GRN_BULK_EMPTYP(stack)) { + space = NULL; + } else { + space = ((grn_obj *)GRN_BULK_CURR(stack)) - 1; + } + ctx->impl->temporary_open_spaces.current = space; + + GRN_API_RETURN(ctx->rc); +} Modified: lib/grn_ctx_impl.h (+5 -0) =================================================================== --- lib/grn_ctx_impl.h 2016-09-23 14:08:38 +0900 (ceeeeb4) +++ lib/grn_ctx_impl.h 2016-09-23 16:42:46 +0900 (5a29d5e) @@ -220,6 +220,11 @@ struct _grn_ctx_impl { unsigned int n_same_error_messages; grn_mrb_data mrb; + + struct { + grn_obj stack; + grn_obj *current; + } temporary_open_spaces; }; #ifdef __cplusplus -------------- next part -------------- HTML����������������������������...Download