Skip to content

Commit

Permalink
modified the signature of grn_token_open().
Browse files Browse the repository at this point in the history
* token.h (grn_token_open): fifth argument became 'add' instead of flags.
  • Loading branch information
daijiro committed May 12, 2009
1 parent 8453646 commit 6a71da5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
16 changes: 8 additions & 8 deletions lib/ii.c
Original file line number Diff line number Diff line change
Expand Up @@ -4201,7 +4201,7 @@ index_add(grn_ctx *ctx, grn_id rid, grn_obj *lexicon, grn_ii *ii, grn_vgram *vgr
grn_rc r, rc = GRN_SUCCESS;
grn_vgram_buf *sbuf = NULL;
if (!rid) { return GRN_INVALID_ARGUMENT; }
if (!(token = grn_token_open(ctx, lexicon, value, value_len, GRN_TABLE_ADD))) {
if (!(token = grn_token_open(ctx, lexicon, value, value_len, 1))) {
return GRN_NO_MEMORY_AVAILABLE;
}
if (vgram) { sbuf = grn_vgram_buf_open(value_len); }
Expand Down Expand Up @@ -4255,7 +4255,7 @@ index_del(grn_ctx *ctx, grn_id rid, grn_obj *lexicon, grn_ii *ii, grn_vgram *vgr
grn_ii_updspec **u;
grn_id tid, *tp;
if (!rid) { return GRN_INVALID_ARGUMENT; }
if (!(token = grn_token_open(ctx, lexicon, value, value_len, GRN_TOKEN_UPD))) {
if (!(token = grn_token_open(ctx, lexicon, value, value_len, 0))) {
return GRN_NO_MEMORY_AVAILABLE;
}
h = grn_hash_create(ctx, NULL, sizeof(grn_id), sizeof(grn_ii_updspec *), GRN_HASH_TINY);
Expand Down Expand Up @@ -4333,7 +4333,7 @@ grn_ii_update(grn_ctx *ctx, grn_ii *ii, grn_id rid, grn_vgram *vgram, unsigned i
goto exit;
}
for (j = newvalues->n_values, v = newvalues->values; j; j--, v++) {
if ((token = grn_token_open(ctx, lexicon, v->str, v->str_len, GRN_TABLE_ADD))) {
if ((token = grn_token_open(ctx, lexicon, v->str, v->str_len, 1))) {
while (!token->status) {
if ((tid = grn_token_next(ctx, token))) {
if (!grn_hash_add(ctx, new, &tid, sizeof(grn_id), (void **) &u, NULL)) {
Expand Down Expand Up @@ -4376,7 +4376,7 @@ grn_ii_update(grn_ctx *ctx, grn_ii *ii, grn_id rid, grn_vgram *vgram, unsigned i
goto exit;
}
for (j = oldvalues->n_values, v = oldvalues->values; j; j--, v++) {
if ((token = grn_token_open(ctx, lexicon, v->str, v->str_len, GRN_TOKEN_UPD))) {
if ((token = grn_token_open(ctx, lexicon, v->str, v->str_len, 0))) {
while (!token->status) {
if ((tid = grn_token_next(ctx, token))) {
if (!grn_hash_add(ctx, old, &tid, sizeof(grn_id), (void **) &u, NULL)) {
Expand Down Expand Up @@ -4442,7 +4442,7 @@ exit :

static grn_rc
grn_vector2updspecs(grn_ctx *ctx, grn_ii *ii, grn_id rid, unsigned int section,
grn_obj *in, grn_obj *out, grn_search_flags flags)
grn_obj *in, grn_obj *out, int add)
{
int j;
grn_id tid;
Expand All @@ -4454,7 +4454,7 @@ grn_vector2updspecs(grn_ctx *ctx, grn_ii *ii, grn_id rid, unsigned int section,
const char *head = GRN_BULK_HEAD(in->u.v.body);
for (j = in->u.v.n_sections, v = in->u.v.sections; j; j--, v++) {
if (v->length &&
(token = grn_token_open(ctx, lexicon, head + v->offset, v->length, flags))) {
(token = grn_token_open(ctx, lexicon, head + v->offset, v->length, add))) {
while (!token->status) {
if ((tid = grn_token_next(ctx, token))) {
if (!grn_hash_add(ctx, h, &tid, sizeof(grn_id), (void **) &u, NULL)) {
Expand Down Expand Up @@ -4541,7 +4541,7 @@ grn_ii_column_update(grn_ctx *ctx, grn_ii *ii, grn_id rid, unsigned int section,
GRN_LOG(ctx, GRN_LOG_ALERT, "grn_hash_create on grn_ii_update failed !");
rc = GRN_NO_MEMORY_AVAILABLE;
} else {
rc = grn_vector2updspecs(ctx, ii, rid, section, new_, new, GRN_TABLE_ADD);
rc = grn_vector2updspecs(ctx, ii, rid, section, new_, new, 1);
}
if (new_ != newvalue) { grn_obj_close(ctx, new_); }
if (rc) { goto exit; }
Expand Down Expand Up @@ -4591,7 +4591,7 @@ grn_ii_column_update(grn_ctx *ctx, grn_ii *ii, grn_id rid, unsigned int section,
GRN_LOG(ctx, GRN_LOG_ALERT, "grn_hash_create(ctx, NULL, old) on grn_ii_update failed!");
rc = GRN_NO_MEMORY_AVAILABLE;
} else {
rc = grn_vector2updspecs(ctx, ii, rid, section, old_, old, GRN_TOKEN_UPD);
rc = grn_vector2updspecs(ctx, ii, rid, section, old_, old, 0);
}
if (old_ != oldvalue) { grn_obj_close(ctx, old_); }
if (rc) { goto exit; }
Expand Down
25 changes: 11 additions & 14 deletions lib/token.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ ngram_next(grn_ctx *ctx, grn_obj *table, grn_proc_data *user_data,
if (tid && (len > 1 || r == p)) {
if (r != p && pos + len - 1 <= token->tail) { continue; }
p += strlen(key);
if (!*p && !(token->flags & GRN_TABLE_ADD)) { token->status = grn_token_done; }
if (!*p && !token->add) { token->status = grn_token_done; }
}
#endif /* PRE_DEFINED_UNSPLIT_WORDS */
if ((cl = grn_charlen_(ctx, (char *)r, (char *)e, token->encoding))) {
Expand Down Expand Up @@ -463,16 +463,15 @@ grn_token_fin(void)
}

grn_token *
grn_token_open(grn_ctx *ctx, grn_obj *table, const char *str, size_t str_len,
grn_search_flags flags)
grn_token_open(grn_ctx *ctx, grn_obj *table, const char *str, size_t str_len, int add)
{
grn_token *token;
grn_encoding encoding;
grn_obj *tokenizer;
if (grn_table_get_info(ctx, table, NULL, &encoding, &tokenizer)) { return NULL; }
if (!(token = GRN_MALLOC(sizeof(grn_token)))) { return NULL; }
token->table = table;
token->flags = flags;
token->add = add;
token->encoding = encoding;
token->tokenizer = tokenizer;
token->orig = str;
Expand Down Expand Up @@ -522,7 +521,7 @@ grn_token_next(grn_ctx *ctx, grn_token *token)
token->force_prefix = 0;
if (status & GRN_TOKEN_UNMATURED) {
if (status & GRN_TOKEN_OVERLAP) {
if (!(token->flags & GRN_TABLE_ADD)) { continue; }
if (!token->add) { continue; }
} else {
if (status & GRN_TOKEN_LAST) { token->force_prefix = 1; }
}
Expand All @@ -532,23 +531,23 @@ grn_token_next(grn_ctx *ctx, grn_token *token)
token->curr_size = token->orig_blen;
token->status = grn_token_done;
}
if (token->flags & GRN_TABLE_ADD) {
if (token->add) {
switch (table->header.type) {
case GRN_TABLE_PAT_KEY :
if (grn_io_lock(ctx, ((grn_pat *)table)->io, 10000000)) {
tid = GRN_ID_NIL;
} else {
tid = grn_pat_lookup(ctx, (grn_pat *)table, token->curr, token->curr_size,
NULL, &token->flags);
tid = grn_pat_add(ctx, (grn_pat *)table, token->curr, token->curr_size,
NULL, NULL);
grn_io_unlock(((grn_pat *)table)->io);
}
break;
case GRN_TABLE_HASH_KEY :
if (grn_io_lock(ctx, ((grn_hash *)table)->io, 10000000)) {
tid = GRN_ID_NIL;
} else {
tid = grn_hash_lookup(ctx, (grn_hash *)table, token->curr, token->curr_size,
NULL, &token->flags);
tid = grn_hash_add(ctx, (grn_hash *)table, token->curr, token->curr_size,
NULL, NULL);
grn_io_unlock(((grn_hash *)table)->io);
}
break;
Expand All @@ -563,12 +562,10 @@ grn_token_next(grn_ctx *ctx, grn_token *token)
} else {
switch (table->header.type) {
case GRN_TABLE_PAT_KEY :
tid = grn_pat_lookup(ctx, (grn_pat *)table, token->curr, token->curr_size,
NULL, &token->flags);
tid = grn_pat_get(ctx, (grn_pat *)table, token->curr, token->curr_size, NULL);
break;
case GRN_TABLE_HASH_KEY :
tid = grn_hash_lookup(ctx, (grn_hash *)table, token->curr, token->curr_size,
NULL, &token->flags);
tid = grn_hash_get(ctx, (grn_hash *)table, token->curr, token->curr_size, NULL);
break;
case GRN_TABLE_NO_KEY :
if (token->curr_size == sizeof(grn_id)) {
Expand Down
7 changes: 2 additions & 5 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,14 @@
extern "C" {
#endif

#define GRN_TOKEN_ADD 1
#define GRN_TOKEN_UPD 2

typedef struct {
grn_obj *table;
const unsigned char *orig;
const unsigned char *curr;
uint32_t orig_blen;
uint32_t curr_size;
int32_t pos;
grn_search_flags flags;
int32_t add;
uint8_t status;
uint8_t force_prefix;
grn_obj_flags table_flags;
Expand All @@ -76,7 +73,7 @@ grn_rc grn_token_init(void);
grn_rc grn_token_fin(void);

grn_token *grn_token_open(grn_ctx *ctx, grn_obj *table, const char *str,
size_t str_len, grn_search_flags flags);
size_t str_len, int add);

grn_id grn_token_next(grn_ctx *ctx, grn_token *ng);
grn_rc grn_token_close(grn_ctx *ctx, grn_token *ng);
Expand Down

0 comments on commit 6a71da5

Please sign in to comment.