Skip to content

Commit

Permalink
ii: remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 4, 2016
1 parent 334d47a commit 5780995
Showing 1 changed file with 45 additions and 196 deletions.
241 changes: 45 additions & 196 deletions lib/ii.c
Expand Up @@ -4402,9 +4402,15 @@ grn_ii_cursor_set_min(grn_ctx *ctx, grn_ii_cursor *c, grn_id min)
}
}

grn_posting *
grn_ii_cursor_next(grn_ctx *ctx, grn_ii_cursor *c)
typedef struct {
grn_bool include_garbage;
} grn_ii_cursor_next_options;

static inline grn_posting *
grn_ii_cursor_next_internal(grn_ctx *ctx, grn_ii_cursor *c,
grn_ii_cursor_next_options *options)
{
const grn_bool include_garbage = options->include_garbage;
if (c->buf) {
for (;;) {
if (c->stat & CHUNK_USED) {
Expand Down Expand Up @@ -4565,20 +4571,32 @@ grn_ii_cursor_next(grn_ctx *ctx, grn_ii_cursor *c)
if (c->pc.rid) {
if (c->pc.rid < c->pb.rid) {
c->stat = CHUNK_USED;
if (c->pc.tf && c->pc.sid) { c->post = &c->pc; break; }
if (include_garbage || (c->pc.tf && c->pc.sid)) {
c->post = &c->pc;
break;
}
} else {
if (c->pb.rid < c->pc.rid) {
c->stat = BUFFER_USED;
if (c->pb.tf && c->pb.sid) { c->post = &c->pb; break; }
if (include_garbage || (c->pb.tf && c->pb.sid)) {
c->post = &c->pb;
break;
}
} else {
if (c->pb.sid) {
if (c->pc.sid < c->pb.sid) {
c->stat = CHUNK_USED;
if (c->pc.tf && c->pc.sid) { c->post = &c->pc; break; }
if (include_garbage || (c->pc.tf && c->pc.sid)) {
c->post = &c->pc;
break;
}
} else {
c->stat = BUFFER_USED;
if (c->pb.sid == c->pc.sid) { c->stat |= CHUNK_USED; }
if (c->pb.tf) { c->post = &c->pb; break; }
if (include_garbage || (c->pb.tf)) {
c->post = &c->pb;
break;
}
}
} else {
c->stat = CHUNK_USED;
Expand All @@ -4587,12 +4605,18 @@ grn_ii_cursor_next(grn_ctx *ctx, grn_ii_cursor *c)
}
} else {
c->stat = BUFFER_USED;
if (c->pb.tf && c->pb.sid) { c->post = &c->pb; break; }
if (include_garbage || (c->pb.tf && c->pb.sid)) {
c->post = &c->pb;
break;
}
}
} else {
if (c->pc.rid) {
c->stat = CHUNK_USED;
if (c->pc.tf && c->pc.sid) { c->post = &c->pc; break; }
if (include_garbage || (c->pc.tf && c->pc.sid)) {
c->post = &c->pc;
break;
}
} else {
c->post = NULL;
return NULL;
Expand All @@ -4615,6 +4639,15 @@ grn_ii_cursor_next(grn_ctx *ctx, grn_ii_cursor *c)
return c->post;
}

grn_posting *
grn_ii_cursor_next(grn_ctx *ctx, grn_ii_cursor *c)
{
grn_ii_cursor_next_options options = {
.include_garbage = GRN_FALSE
};
return grn_ii_cursor_next_internal(ctx, c, &options);
}

grn_posting *
grn_ii_cursor_next_pos(grn_ctx *ctx, grn_ii_cursor *c)
{
Expand Down Expand Up @@ -7274,200 +7307,16 @@ grn_ii_resolve_sel_and(grn_ctx *ctx, grn_hash *s, grn_operator op)
}
}

/* just for inspect */
static grn_posting *
grn_ii_cursor_next_all(grn_ctx *ctx, grn_ii_cursor *c)
{
if (c->buf) {
for (;;) {
if (c->stat & CHUNK_USED) {
for (;;) {
if (c->crp < c->cdp + c->cdf) {
uint32_t dgap = *c->crp++;
c->pc.rid += dgap;
if (dgap) { c->pc.sid = 0; }
if ((c->ii->header->flags & GRN_OBJ_WITH_SECTION)) {
c->pc.sid += 1 + *c->csp++;
} else {
c->pc.sid = 1;
}
c->cpp += c->pc.rest;
c->pc.rest = c->pc.tf = 1 + *c->ctp++;
if ((c->ii->header->flags & GRN_OBJ_WITH_WEIGHT)) {
c->pc.weight = *c->cwp++;
} else {
c->pc.weight = 0;
}
c->pc.pos = 0;
/*
{
static int count = 0;
int tf = c->pc.tf, pos = 0, *pp = (int *)c->cpp;
grn_obj buf;
GRN_TEXT_INIT(&buf, 0);
grn_text_itoa(ctx, &buf, c->pc.rid);
GRN_TEXT_PUTC(ctx, &buf, ':');
grn_text_itoa(ctx, &buf, c->pc.sid);
GRN_TEXT_PUTC(ctx, &buf, ':');
grn_text_itoa(ctx, &buf, c->pc.tf);
GRN_TEXT_PUTC(ctx, &buf, '(');
while (tf--) {
pos += *pp++;
count++;
grn_text_itoa(ctx, &buf, pos);
if (tf) { GRN_TEXT_PUTC(ctx, &buf, ':'); }
}
GRN_TEXT_PUTC(ctx, &buf, ')');
GRN_TEXT_PUTC(ctx, &buf, '\0');
GRN_LOG(ctx, GRN_LOG_DEBUG, "posting(%d):%s", count, GRN_TEXT_VALUE(&buf));
GRN_OBJ_FIN(ctx, &buf);
}
*/
} else {
if (c->curr_chunk <= c->nchunks) {
if (c->curr_chunk == c->nchunks) {
if (c->cp < c->cpe) {
grn_p_decv(ctx, c->cp, c->cpe - c->cp, c->rdv, c->ii->n_elements);
} else {
c->pc.rid = 0;
break;
}
} else {
uint8_t *cp;
grn_io_win iw;
uint32_t size = c->cinfo[c->curr_chunk].size;
if (size && (cp = WIN_MAP(c->ii->chunk, ctx, &iw,
c->cinfo[c->curr_chunk].segno, 0,
size, grn_io_rdonly))) {
grn_p_decv(ctx, cp, size, c->rdv, c->ii->n_elements);
grn_io_win_unmap(&iw);
} else {
c->pc.rid = 0;
break;
}
}
{
int j = 0;
c->cdf = c->rdv[j].data_size;
c->crp = c->cdp = c->rdv[j++].data;
if ((c->ii->header->flags & GRN_OBJ_WITH_SECTION)) {
c->csp = c->rdv[j++].data;
}
c->ctp = c->rdv[j++].data;
if ((c->ii->header->flags & GRN_OBJ_WITH_WEIGHT)) {
c->cwp = c->rdv[j++].data;
}
c->cpp = c->rdv[j].data;
}
c->prev_chunk_rid = c->pc.rid;
c->pc.rid = 0;
c->pc.sid = 0;
c->pc.rest = 0;
c->curr_chunk++;
continue;
} else {
c->pc.rid = 0;
}
}
break;
}
}
if (c->stat & BUFFER_USED) {
if (c->nextb) {
uint32_t lrid = c->pb.rid, lsid = c->pb.sid; /* for check */
buffer_rec *br = BUFFER_REC_AT(c->buf, c->nextb);
if (buffer_is_reused(ctx, c->ii, c)) {
GRN_LOG(ctx, GRN_LOG_DEBUG, "buffer reused(%d,%d)",
c->buffer_pseg, *c->ppseg);
// todo : rewind;
}
c->bp = GRN_NEXT_ADDR(br);
GRN_B_DEC(c->pb.rid, c->bp);
if ((c->ii->header->flags & GRN_OBJ_WITH_SECTION)) {
GRN_B_DEC(c->pb.sid, c->bp);
} else {
c->pb.sid = 1;
}
if (lrid > c->pb.rid || (lrid == c->pb.rid && lsid >= c->pb.sid)) {
ERR(GRN_FILE_CORRUPT, "brokend!! (%d:%d) -> (%d:%d) (%d->%d)",
lrid, lsid, c->pb.rid, c->pb.sid, c->buffer_pseg, *c->ppseg);
}
c->nextb = br->step;
GRN_B_DEC(c->pb.tf, c->bp);
if ((c->ii->header->flags & GRN_OBJ_WITH_WEIGHT)) {
GRN_B_DEC(c->pb.weight, c->bp);
} else {
c->pb.weight = 0;
}
c->pb.rest = c->pb.tf;
c->pb.pos = 0;
} else {
c->pb.rid = 0;
}
}
if (c->pb.rid) {
if (c->pc.rid) {
if (c->pc.rid < c->pb.rid) {
c->stat = CHUNK_USED;
c->post = &c->pc;
break;
} else {
if (c->pb.rid < c->pc.rid) {
c->stat = BUFFER_USED;
c->post = &c->pb;
break;
} else {
if (c->pb.sid) {
if (c->pc.sid < c->pb.sid) {
c->stat = CHUNK_USED;
c->post = &c->pc;
break;
} else {
c->stat = BUFFER_USED;
if (c->pb.sid == c->pc.sid) { c->stat |= CHUNK_USED; }
c->post = &c->pb;
break;
}
} else {
c->stat = CHUNK_USED;
}
}
}
} else {
c->stat = BUFFER_USED;
c->post = &c->pb;
break;
}
} else {
if (c->pc.rid) {
c->stat = CHUNK_USED;
c->post = &c->pc;
break;
} else {
c->post = NULL;
return NULL;
}
}
}
} else {
if (c->stat & SOLE_DOC_USED) {
c->post = NULL;
return NULL;
} else {
c->post = &c->pb;
c->stat |= SOLE_DOC_USED;
}
}
return c->post;
}

void
grn_ii_cursor_inspect(grn_ctx *ctx, grn_ii_cursor *c, grn_obj *buf)
{
grn_obj key_buf;
char key[GRN_TABLE_MAX_KEY_SIZE];
int key_size;
int i = 0;
grn_ii_cursor_next_options options = {
.include_garbage = GRN_TRUE
};

GRN_TEXT_PUTS(ctx, buf, " #<");
key_size = grn_table_get_key(ctx, c->ii->lexicon, c->id,
Expand All @@ -7478,7 +7327,7 @@ grn_ii_cursor_inspect(grn_ctx *ctx, grn_ii_cursor *c, grn_obj *buf)
GRN_OBJ_FIN(ctx, &key_buf);

GRN_TEXT_PUTS(ctx, buf, "\n elements:[\n ");
while (grn_ii_cursor_next_all(ctx, c)) {
while (grn_ii_cursor_next_internal(ctx, c, &options)) {
grn_posting *pos = c->post;
if (i > 0) {
GRN_TEXT_PUTS(ctx, buf, ",\n ");
Expand Down

0 comments on commit 5780995

Please sign in to comment.