null+****@clear*****
null+****@clear*****
2011年 11月 14日 (月) 16:27:41 JST
Daijiro MORI 2011-11-14 07:27:41 +0000 (Mon, 14 Nov 2011) New Revision: 299eadef584c852acd3316381d0d7345262ea727 Log: removed grn_ql_def_db_funcs() Modified files: lib/ctx.c lib/ql.c Modified: lib/ctx.c (+0 -1) =================================================================== --- lib/ctx.c 2011-11-13 12:06:48 +0000 (70d012a) +++ lib/ctx.c 2011-11-14 07:27:41 +0000 (1bf9db2) @@ -1920,7 +1920,6 @@ grn_ctx_use(grn_ctx *ctx, grn_obj *db) ctx->impl->db = db; if (db) { grn_obj buf; - if (ctx->impl->symbols) { grn_ql_def_db_funcs(ctx); } GRN_TEXT_INIT(&buf, 0); grn_obj_get_info(ctx, db, GRN_INFO_ENCODING, &buf); ctx->encoding = *(grn_encoding *)GRN_BULK_HEAD(&buf); Modified: lib/ql.c (+0 -521) =================================================================== --- lib/ql.c 2011-11-13 12:06:48 +0000 (1e38a27) +++ lib/ql.c 2011-11-14 07:27:41 +0000 (cf0327c) @@ -1920,63 +1920,6 @@ uvector2str(grn_ctx *ctx, grn_obj *obj, grn_obj *buf) } static grn_cell * -nf_tostring(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - grn_cell *o = F, *car; - POP(car, args); - switch (car->header.type) { - case GRN_UVECTOR : - { - grn_obj buf; - uint32_t size; - void *value = NULL; - GRN_TEXT_INIT(&buf, 0); - uvector2str(ctx, car->u.p.value, &buf); - if ((o = grn_cell_new(ctx))) { - if ((size = GRN_BULK_VSIZE(&buf))) { - if (!(value = GRN_MALLOC(size))) { return F; } - o->header.impl_flags |= GRN_OBJ_ALLOCATED; - memcpy(value, GRN_BULK_HEAD(&buf), size); - } - SETBULK(o, value, size); - } - grn_obj_close(ctx, &buf); - } - break; - } - return o; -} - -static grn_cell * -nf_toquery(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - grn_cell *o = F, *s; - POP(s, args); - if (BULKP(s)) { - /* TODO: operator, exprs, encoding */ - if (!(o = grn_obj_query(ctx, s->u.b.value, s->u.b.size, GRN_OP_AND, 32, ctx->encoding))) { - QLERR("query_obj_new failed"); - } - } - return o; -} - -static grn_cell * -nf_tosections(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - grn_cell *o = F, *s; - POP(s, args); - if (PAIRP(s)) { - grn_obj sections; - GRN_OBJ_INIT(§ions, GRN_VECTOR, 0, GRN_DB_TEXT); - list2vector(ctx, s, §ions); - GRN_CELL_NEW(ctx, o); - obj2cell(ctx, §ions, o); - } - return o; -} - -static grn_cell * ha_column(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) { char msg[STRBUF_SIZE]; @@ -2122,271 +2065,6 @@ grn_ql_obj_bind(grn_obj *obj, grn_cell *symbol) } } -static grn_cell * -nf_db(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - grn_obj *table; - char msg[STRBUF_SIZE]; - uint16_t msg_size; - grn_cell *car, *res = ctx->impl->code; - POP(car, args); - if (obj2str(car, msg, &msg_size)) { QLERR("invalid argument"); } - if (*msg == ':') { - switch (msg[1]) { - case 'c' : /* :clearlock */ - case 'C' : - { - grn_obj *obj; - void *key, *value; - uint32_t key_size; - grn_pat *keys = (grn_pat *)grn_db_keys(ctx->impl->db); - GRN_PAT_EACH(ctx, keys, id, &key, &key_size, &value, { - obj = grn_ctx_at(ctx, id); - grn_obj_clear_lock(ctx, obj); - }); - res = grn_obj_clear_lock(ctx, ctx->impl->db) ? F : T; - } - break; - case 'd' : /* :drop */ - case 'D' : - POP(car, args); - if (obj2str(car, msg, &msg_size)) { QLERR("invalid argument"); } - if (!(table = grn_ctx_get(ctx, msg, msg_size))) { QLERR("Invalid table"); } - grn_obj_remove(ctx, table); - grn_hash_delete(ctx, ctx->impl->symbols, msg, msg_size, NULL); - break; - case 'p' : /* :prefix-search */ - case 'P' : - { - grn_hash *r; - grn_pat *keys = (grn_pat *)grn_db_keys(ctx->impl->db); - POP(car, args); - if (obj2str(car, msg, &msg_size)) { QLERR("invalid argument"); } - if ((res = rec_obj_new(ctx, ctx->impl->db, NULL)) == F) { - QLERR("rec_obj_new failed."); - } - r = (grn_hash *)grn_ctx_at(ctx, res->u.o.id); - grn_pat_prefix_search(ctx, keys, msg, msg_size, r); - { - grn_id *rid; - GRN_HASH_EACH(ctx, r, id, &rid, NULL, NULL, { - uint32_t key_size; - const char *key = _grn_pat_key(ctx, keys, *rid, &key_size); - while (key_size--) { - if (*key++ == '.') { - grn_hash_delete(ctx, r, rid, sizeof(grn_id), NULL); - } - } - }); - } - } - break; - case 't' : /* :typedef */ - case 'T' : - { - uint32_t size; - grn_obj *type; - grn_cell *flag; - grn_obj_flags flags = 0; - POP(car, args); - POP(flag, args); - if (obj2str(flag, msg, &msg_size)) { QLERR("invalid argument"); } - flags = (*msg == 'v' || *msg == 'V') ? GRN_OBJ_KEY_VAR_SIZE : 0; - if (obj2str(car, msg, &msg_size)) { QLERR("invalid argument"); } - POP(car, args); - size = (uint32_t) IVALUE(car); - type = grn_type_create(ctx, msg, msg_size, flags, size); - if (!type) { QLERR("type_create failed"); } - if ((res = INTERN2(msg, msg_size)) != F) { - grn_ql_obj_bind(type, res); - } - } - break; - case '+' : /* :+ (iterator next) */ - { - grn_pat *keys = (grn_pat *)grn_db_keys(ctx->impl->db); - POP(res, args); - if (res->header.type == GRN_TYPE || - res->header.type == GRN_TABLE_PAT_KEY || - res->header.type == GRN_COLUMN_FIX_SIZE || - res->header.type == GRN_COLUMN_VAR_SIZE || - res->header.type == GRN_COLUMN_INDEX || - res->header.type == GRN_CELL_PSEUDO_COLUMN) { - grn_id id = res->u.o.id; - while ((id = grn_pat_next(ctx, keys, id))) { - if ((msg_size = grn_pat_get_key(ctx, keys, id, msg, STRBUF_SIZE))) { break; } - } - if (id == GRN_ID_NIL) { - res = F; - } else { - res = INTERN2(msg, msg_size); - } - } else { - res = F; - } - } - break; - case '\0' : /* : (iterator begin) */ - { - grn_id id = GRN_ID_NIL; - grn_pat *keys = (grn_pat *)grn_db_keys(ctx->impl->db); - while ((id = grn_pat_next(ctx, keys, id))) { - if ((msg_size = grn_pat_get_key(ctx, keys, id, msg, STRBUF_SIZE))) { break; } - } - if (id == GRN_ID_NIL) { - res = F; - } else { - res = INTERN2(msg, msg_size); - } - } - break; - } - } - return res; -} - -static grn_cell * -nf_table_(grn_ctx *ctx, grn_cell *args, const char *name, uint16_t name_size) -{ - grn_obj_flags flags = (name && name_size) ? GRN_OBJ_PERSISTENT : GRN_OBJ_TEMPORARY; - grn_encoding encoding = GRN_ENC_DEFAULT; - grn_obj *domain = grn_ctx_at(ctx, GRN_DB_SHORT_TEXT); - grn_cell *car; - grn_id tokenizer = GRN_DB_DELIMIT; - char msg[STRBUF_SIZE]; - uint16_t msg_size; - while (PAIRP(args)) { - POP(car, args); - switch (car->header.type) { - case GRN_TABLE_HASH_KEY : - case GRN_TABLE_PAT_KEY : - case GRN_TABLE_NO_KEY : - case GRN_TYPE : - domain = grn_ctx_at(ctx, car->u.o.id); - break; - default : - if (obj2str(car, msg, &msg_size)) { QLERR("invalid argument"); } - switch (*msg) { - case 'd' : - case 'D' : - switch (msg[2]) { - case 'l' : /* delimited */ - case 'L' : - /* tokenizer = GRN_DB_DELIMITED; */ - break; - case 'f' : /* default */ - case 'F' : - encoding = GRN_ENC_DEFAULT; - break; - } - break; - case 'e' : /* euc-jp */ - case 'E' : - encoding = GRN_ENC_EUC_JP; - break; - case 'k' : /* koi8r */ - case 'K' : - encoding = GRN_ENC_KOI8R; - break; - case 'l' : /* latin1 */ - case 'L' : - encoding = GRN_ENC_LATIN1; - break; - case 'm' : /* mecab */ - case 'M' : - tokenizer = GRN_DB_MECAB; - break; - case 'n' : - case 'N' : - switch (msg[1]) { - case 'g' : /* ngram */ - case 'G' : - tokenizer = GRN_DB_BIGRAM; - break; - case 'o' : /* normalize */ - case 'O' : - flags |= GRN_OBJ_KEY_NORMALIZE; - break; - default : - QLERR("ambiguous option %s", msg); - } - break; - case 'p' : - case 'P' : - switch (msg[1]) { - case 'a' : - case 'A' : - flags |= GRN_OBJ_TABLE_PAT_KEY; - break; - case 'e' : - case 'E' : - flags |= GRN_OBJ_PERSISTENT; - break; - default : - QLERR("ambiguous option %s", msg); - } - break; - case 's' : - case 'S' : - switch (msg[1]) { - case 'j' : /* shift-jis */ - case 'J' : - encoding = GRN_ENC_SJIS; - break; - case 'i' : /* with-sis */ - case 'I' : - flags |= GRN_OBJ_KEY_WITH_SIS; - break; - case 'u' : /* surrogate-key */ - case 'U' : - flags |= GRN_OBJ_TABLE_NO_KEY; - break; - default : - QLERR("ambiguous option %s", msg); - } - break; - case 'u' : /* utf8 */ - case 'U' : - encoding = GRN_ENC_UTF8; - break; - case 'v' : /* view */ - case 'V' : - /* todo */ - break; - default : - QLERR("illegal option"); - break; - } - } - } - { - grn_cell *r; - grn_encoding enc_ = GRN_CTX_GET_ENCODING(ctx); - if (encoding != GRN_ENC_DEFAULT) { GRN_CTX_SET_ENCODING(ctx, encoding); } - r = table_create(ctx, name, name_size, flags, domain, NULL, tokenizer); - GRN_CTX_SET_ENCODING(ctx, enc_); - return r; - } -} - -static grn_cell * -nf_ptable(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - char name[STRBUF_SIZE]; - uint16_t name_size; - grn_cell *car; - POP(car, args); - if (obj2str(car, name, &name_size)) { QLERR("invalid argument"); } - if (grn_ctx_get(ctx, name, name_size)) { return T; } - return nf_table_(ctx, args, name, name_size); -} - -static grn_cell * -nf_table(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - return nf_table_(ctx, args, NULL, 0); -} - #define PVALUE(obj,type) ((type *)((obj)->u.p.value)) inline static void @@ -2415,135 +2093,6 @@ grn_obj_patsnip_spec_close(grn_ctx *ctx, patsnip_spec *spec) } static grn_cell * -nf_snippet(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - /* args: (cond width @ int max_results @ int opentag1 @ bulk closetag1 @ bulk..) - cond: (keyword @ bulk..) or cond @ query or cond @ table */ - - grn_cell *res, *cur, *cond; - grn_snip *s; - unsigned int width = 100, max_results = 3; - if (!PAIRP(args)) { QLERR("cond expected"); } - POP(cond, args); - if (PAIRP(args)) { - POP(cur, args); - if (grn_obj2int(ctx, cur)) { QLERR("snippet failed (width expected)"); } - width = IVALUE(cur); - } - if (PAIRP(args)) { - POP(cur, args); - if (grn_obj2int(ctx, cur)) { QLERR("snipped failed (max_result expected)"); } - max_results = IVALUE(cur); - } - switch (cond->header.type) { - case GRN_CELL_LIST : - { - grn_cell *tags = args; - /* FIXME: mapping */ - if (!(s = grn_snip_open(ctx, GRN_SNIP_NORMALIZE, width, max_results, - NULL, 0, NULL, 0, (grn_snip_mapping *)-1))) { - QLERR("grn_snip_open failed"); - } - GRN_CELL_NEW(ctx, res); - snip_obj_bind(res, s); - s->flags |= GRN_SNIP_COPY_TAG; - while (PAIRP(cond)) { - char *ot = NULL, *ct = NULL; - uint32_t ot_l = 0, ct_l = 0; - grn_cell *kw; - POP(kw, cond); - if (!BULKP(kw)) { QLERR("snippet failed (invalid kw)"); } - if (!PAIRP(args)) { args = tags; } - POP(cur, args); - if (BULKP(cur)) { - ot = STRVALUE(cur); - ot_l = STRSIZE(cur); - } - POP(cur, args); - if (BULKP(cur)) { - ct = STRVALUE(cur); - ct_l = STRSIZE(cur); - } - if ((grn_snip_add_cond(ctx, s, kw->u.b.value, kw->u.b.size, ot, ot_l, ct, ct_l))) { - QLERR("grn_snip_add_cond failed"); - } - } - } - break; - case GRN_QUERY : - { - grn_cell *x; - grn_query *q; - unsigned int n_tags = 0; - const char **opentags, **closetags; - unsigned int *opentag_lens, *closetag_lens; - q = (grn_query *)cond->u.p.value; - for (x = args; PAIRP(x); x = CDR(x)) { n_tags++; } - if (!n_tags) { n_tags++; } - if (!(opentags = GRN_MALLOC((sizeof(char *) + sizeof(unsigned int)) * 2 * n_tags))) { - QLERR("malloc failed"); - } - closetags = &opentags[n_tags]; - opentag_lens = (unsigned int *)&closetags[n_tags]; - closetag_lens = &opentag_lens[n_tags]; - n_tags = 0; - for (x = args; PAIRP(x); x = CDR(x)) { - if (BULKP(CAR(x))) { - opentags[n_tags] = STRVALUE(CAR(x)); - opentag_lens[n_tags] = STRSIZE(CAR(x)); - if (PAIRP(CDR(x))) { - x = CDR(x); - if (BULKP(CAR(x))) { - closetags[n_tags] = STRVALUE(CAR(x)); - closetag_lens[n_tags] = STRSIZE(CAR(x)); - n_tags++; - } - } - } - } - if (!n_tags) { - n_tags++; - opentags[0] = NULL; - closetags[0] = NULL; - opentag_lens[0] = 0; - closetag_lens[0] = 0; - } - s = grn_query_snip(ctx, q, GRN_SNIP_NORMALIZE|GRN_SNIP_COPY_TAG, width, max_results, n_tags, - opentags, opentag_lens, closetags, closetag_lens, - (grn_snip_mapping *)-1); - GRN_FREE(opentags); - if (!s) { QLERR("grn_query_snip failed"); } - GRN_CELL_NEW(ctx, res); - snip_obj_bind(res, s); - } - break; - case GRN_TABLE_PAT_KEY : - { - patsnip_spec *spec; - grn_obj *table = grn_ctx_at(ctx, cond->u.o.id); - if (!table) { QLERR("table get failed."); } - GRN_CELL_NEW(ctx, res); - if (!(spec = GRN_MALLOC(sizeof(patsnip_spec)))) { - QLERR("patsnip_spec malloc failed"); - } - res->header.type = GRN_PATSNIP; - res->header.impl_flags = GRN_CELL_NATIVE|GRN_OBJ_ALLOCATED; - res->u.p.value = (grn_obj *)spec; - res->u.p.func = ha_snip; - spec->table = table; - spec->width = width; - spec->max_results = max_results; - spec->ce = column_exp_open(ctx, table, args, NIL); - } - break; - default : - QLERR("snippet failed. cond or query expected"); - break; - } - return res; -} - -static grn_cell * ha_snip(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) { /* args: (str @ bulk) */ @@ -3070,45 +2619,6 @@ disp_t(grn_ctx *ctx, grn_cell *obj, grn_obj *buf, int *f) } } -static grn_cell * -nf_disp(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - char str[STRBUF_SIZE]; - uint16_t str_size; - int f = 0; - grn_cell *val, *fmt; - POP(val, args); - POP(fmt, args); - if (!obj2str(fmt, str, &str_size)) { - switch (str[0]) { - case 'j' : /* json */ - case 'J' : - disp_j(ctx, val, ctx->impl->outbuf); - f = 1; - if (ERRP(ctx, GRN_WARN)) { return F; } - break; - case 's' : /* sexp */ - case 'S' : - break; - case 't' : /* tsv */ - case 'T' : - disp_t(ctx, val, ctx->impl->outbuf, &f); - if (ERRP(ctx, GRN_WARN)) { return F; } - break; - case 'x' : /* xml */ - case 'X' : - break; - } - } else { - QLERR("Few arguments"); - } - if (f) { - ctx->impl->output(ctx, GRN_CTX_MORE, ctx->impl->data.ptr); - if (ERRP(ctx, GRN_WARN)) { return F; } - } - return T; -} - inline static grn_cell * mk_atom(grn_ctx *ctx, char *str, unsigned int len) { @@ -3251,34 +2761,3 @@ json_read(grn_ctx *ctx, jctx *jc, int keyp) } } } - -static grn_cell * -nf_json_read(grn_ctx *ctx, grn_cell *args, grn_ql_co *co) -{ - grn_cell *car; - POP(car, args); // todo : delete when called with (()) - if (BULKP(car)) { - grn_cell *r; - jctx jc; - jc.encoding = ctx->encoding; - jc.cur = car->u.b.value; - jc.str_end = car->u.b.value + car->u.b.size; - if ((r = json_read(ctx, &jc, 0))) { return r; } - } - return F; -} - -grn_rc -grn_ql_def_db_funcs(grn_ctx *ctx) -{ - grn_ql_def_native_func(ctx, "<db>", nf_db); - grn_ql_def_native_func(ctx, "table", nf_table); - grn_ql_def_native_func(ctx, "ptable", nf_ptable); - grn_ql_def_native_func(ctx, "snippet", nf_snippet); - grn_ql_def_native_func(ctx, "disp", nf_disp); - grn_ql_def_native_func(ctx, "json-read", nf_json_read); - grn_ql_def_native_func(ctx, "x->query", nf_toquery); - grn_ql_def_native_func(ctx, "x->sections", nf_tosections); - grn_ql_def_native_func(ctx, "x->string", nf_tostring); - return GRN_SUCCESS; -}