null+****@clear*****
null+****@clear*****
2012年 3月 23日 (金) 17:27:42 JST
Kouhei Sutou 2012-03-23 17:27:42 +0900 (Fri, 23 Mar 2012) New Revision: 3af4a239f9ea34a1d0916623e46c496eca05854b Log: snip: use grn_obj mechanism fixes #1054 Modified files: include/groonga.h lib/db.c lib/db.h lib/snip.c lib/snip.h test/unit/util/test-snip.c Modified: include/groonga.h (+6 -0) =================================================================== --- include/groonga.h 2012-03-23 16:59:05 +0900 (5ddff92) +++ include/groonga.h 2012-03-23 17:27:42 +0900 (59eeea3) @@ -1964,6 +1964,8 @@ GRN_API grn_posting *grn_geo_cursor_next(grn_ctx *ctx, grn_obj *cursor); #define GRN_QUERY_COLUMN ':' #endif /* GRN_QUERY_COLUMN */ +/* grn_snip should be removed. + * TODO: 3.0 */ typedef struct _grn_snip grn_snip; typedef struct _grn_snip_mapping grn_snip_mapping; @@ -1975,11 +1977,15 @@ struct _grn_snip_mapping { #define GRN_SNIP_COPY_TAG (0x01<<1) #define GRN_SNIP_SKIP_LEADING_SPACES (0x01<<2) +/* grn_snip_open() should return grn_obj * instead. + * TODO: 3.0 */ GRN_API grn_snip *grn_snip_open(grn_ctx *ctx, int flags, unsigned int width, unsigned int max_results, const char *defaultopentag, unsigned int defaultopentag_len, const char *defaultclosetag, unsigned int defaultclosetag_len, grn_snip_mapping *mapping); +/* grn_snip_close() should be removed. Use grn_obj_close() instead. + * TODO: 3.0 */ GRN_API grn_rc grn_snip_close(grn_ctx *ctx, grn_snip *snip); GRN_API grn_rc grn_snip_add_cond(grn_ctx *ctx, grn_snip *snip, const char *keyword, unsigned int keyword_len, Modified: lib/db.c (+4 -0) =================================================================== --- lib/db.c 2012-03-23 16:59:05 +0900 (9467bef) +++ lib/db.c 2012-03-23 17:27:42 +0900 (3792f9a) @@ -25,6 +25,7 @@ #include "proc.h" #include "plugin_in.h" #include "geo.h" +#include "snip.h" #include "util.h" #include <string.h> #include <float.h> @@ -6918,6 +6919,9 @@ grn_obj_close(grn_ctx *ctx, grn_obj *obj) case GRN_ACCESSOR_VIEW : rc = grn_accessor_view_close(ctx, obj); break; + case GRN_SNIP : + rc = grn_snip_close_real(ctx, (grn_snip *)obj); + break; case GRN_CURSOR_TABLE_PAT_KEY : grn_pat_cursor_close(ctx, (grn_pat_cursor *)obj); break; Modified: lib/db.h (+3 -2) =================================================================== --- lib/db.h 2012-03-23 16:59:05 +0900 (fd4c4bf) +++ lib/db.h 2012-03-23 17:27:42 +0900 (845c22f) @@ -144,8 +144,9 @@ typedef struct { #define GRN_DB_OBJP(obj) \ (obj &&\ - (GRN_CURSOR_TABLE_HASH_KEY <= ((grn_db_obj *)obj)->header.type) &&\ - (((grn_db_obj *)obj)->header.type <= GRN_COLUMN_INDEX)) + ((GRN_SNIP == ((grn_db_obj *)obj)->header.type) ||\ + ((GRN_CURSOR_TABLE_HASH_KEY <= ((grn_db_obj *)obj)->header.type) &&\ + (((grn_db_obj *)obj)->header.type <= GRN_COLUMN_INDEX)))) #define GRN_OBJ_TABLEP(obj) \ (obj &&\ Modified: lib/snip.c (+24 -1) =================================================================== --- lib/snip.c 2012-03-23 16:59:05 +0900 (cfe958a) +++ lib/snip.c 2012-03-23 17:27:42 +0900 (8b51933) @@ -471,6 +471,17 @@ grn_snip_open(grn_ctx *ctx, int flags, unsigned int width, ret->tag_count = 0; ret->snip_count = 0; + GRN_DB_OBJ_SET_TYPE(ret, GRN_SNIP); + { + grn_obj *db; + grn_id id; + db = grn_ctx_db(ctx); + id = grn_obj_register(ctx, db, NULL, 0); + DB_OBJ(ret)->header.domain = GRN_ID_NIL; + DB_OBJ(ret)->range = GRN_ID_NIL; + grn_db_obj_init(ctx, db, id, DB_OBJ(ret)); + } + GRN_API_RETURN(ret); } @@ -491,8 +502,10 @@ exec_clean(grn_ctx *ctx, grn_snip *snip) return GRN_SUCCESS; } +/* It should be renamed to grn_snip_close() and marked as internal. + * TODO: 3.0 */ grn_rc -grn_snip_close(grn_ctx *ctx, grn_snip *snip) +grn_snip_close_real(grn_ctx *ctx, grn_snip *snip) { snip_cond *cond, *cond_end; if (!snip) { return GRN_INVALID_ARGUMENT; } @@ -519,6 +532,16 @@ grn_snip_close(grn_ctx *ctx, grn_snip *snip) GRN_API_RETURN(GRN_SUCCESS); } +/* Just for backward compatibility. It should be replaced with + * grn_snip_close_real() when groonga 3.0. + * TODO: 3.0 */ +grn_rc +grn_snip_close(grn_ctx *ctx, grn_snip *snip) +{ + return grn_obj_close(ctx, (grn_obj *)snip); +} + + grn_rc grn_snip_exec(grn_ctx *ctx, grn_snip *snip, const char *string, unsigned int string_len, unsigned int *nresults, unsigned int *max_tagged_len) Modified: lib/snip.h (+4 -0) =================================================================== --- lib/snip.h 2012-03-23 16:59:05 +0900 (4ad99b9) +++ lib/snip.h 2012-03-23 17:27:42 +0900 (f328af4) @@ -25,6 +25,8 @@ #include "str.h" #endif /* GRN_STR_H */ +#include "db.h" + #define ASIZE 256U #define MAX_SNIP_TAG_COUNT 512U #define MAX_SNIP_COND_COUNT 32U @@ -87,6 +89,7 @@ typedef struct struct _grn_snip { + grn_db_obj obj; grn_encoding encoding; int flags; size_t width; @@ -113,6 +116,7 @@ struct _grn_snip size_t max_tagged_len; }; +grn_rc grn_snip_close_real(grn_ctx *ctx, grn_snip *snip); grn_rc grn_snip_cond_init(grn_ctx *ctx, snip_cond *sc, const char *keyword, unsigned int keyword_len, grn_encoding enc, int flags); void grn_snip_cond_reinit(snip_cond *cond); Modified: test/unit/util/test-snip.c (+5 -5) =================================================================== --- test/unit/util/test-snip.c 2012-03-23 16:59:05 +0900 (925431e) +++ test/unit/util/test-snip.c 2012-03-23 17:27:42 +0900 (d0748a1) @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2; coding: utf-8 -*- */ /* - Copyright (C) 2008-2009 Kouhei Sutou <kou****@cozmi*****> + Copyright (C) 2008-2012 Kouhei Sutou <kou****@clear*****> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -217,7 +217,7 @@ void cut_teardown(void) { if (snip) { - grn_snip_close(&context, snip); + grn_obj_close(&context, (grn_obj *)snip); } if (keyword) { g_free(keyword); @@ -240,7 +240,7 @@ static grn_snip * open_snip(void) { if (snip) { - grn_snip_close(&context, snip); + grn_obj_close(&context, (grn_obj *)snip); } GRN_CTX_SET_ENCODING(&context, default_encoding); snip = grn_snip_open(&context, default_flags, @@ -460,7 +460,7 @@ test_exec_with_normalize(void) &n_results, &max_tagged_len)); cut_assert_equal_uint(0, n_results); - grn_snip_close(&context, snip); + grn_obj_close(&context, (grn_obj *)snip); snip = NULL; @@ -747,7 +747,7 @@ test_html_mapping_escape(void) void test_close_with_null(void) { - grn_test_assert_equal_rc(GRN_INVALID_ARGUMENT, grn_snip_close(&context, NULL)); + grn_test_assert_equal_rc(GRN_INVALID_ARGUMENT, grn_obj_close(&context, NULL)); } void