Skip to content

Commit c941628

Browse files
committed
debug: wip
1 parent 2e07f8e commit c941628

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

lib/grn_hash.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ struct _grn_hash_header_large {
299299
grn_id garbages[GRN_HASH_MAX_KEY_SIZE_LARGE];
300300
};
301301

302-
struct _grn_hash_cursor_sorted_entires {
302+
struct _grn_hash_cursor_sorted_entries {
303303
grn_id *sorted_entries;
304304
uint32_t n_entries;
305-
uint32_t curr_idx;
305+
uint32_t curr;
306306
};
307307

308-
typedef struct _grn_hash_cursor_sorted_entires grn_hash_cursor_sorted_entires;
308+
typedef struct _grn_hash_cursor_sorted_entries grn_hash_cursor_sorted_entries;
309309

310310
struct _grn_hash_cursor {
311311
grn_db_obj obj;
@@ -315,7 +315,7 @@ struct _grn_hash_cursor {
315315
grn_id tail;
316316
unsigned int rest;
317317
int dir;
318-
grn_hash_cursor_sorted_entires *sorted_entries;
318+
grn_hash_cursor_sorted_entries *sorted_entries;
319319
};
320320

321321
/* deprecated */

lib/hash.c

+65-7
Original file line numberDiff line numberDiff line change
@@ -4334,12 +4334,60 @@ grn_hash_cursor_open_by_key(grn_ctx *ctx, grn_hash *hash,
43344334
const void *max, uint32_t max_size,
43354335
int offset, int limit, int flags)
43364336
{
4337+
int dir;
4338+
grn_hash_cursor *c;
4339+
if (!ctx || !hash) {
4340+
return NULL;
4341+
}
4342+
if (!(c = GRN_CALLOC(sizeof(grn_hash_cursor)))) {
4343+
return NULL;
4344+
}
4345+
GRN_DB_OBJ_SET_TYPE(c, GRN_CURSOR_TABLE_HASH_KEY);
4346+
c->ctx = ctx;
4347+
c->hash = hash;
4348+
c->obj.header.flags = (grn_obj_flags){flags};
4349+
c->obj.header.domain = GRN_ID_NIL;
4350+
c->dir = dir = 1; // TODO: support for decendin order.
4351+
4352+
// TODO: Is GRN_ARRAY_TINY really enough?
4353+
grn_array *sorted = grn_array_create(ctx, NULL, sizeof(grn_id), GRN_ARRAY_TINY);
4354+
if (!sorted) {
4355+
GRN_LOG(ctx,
4356+
GRN_LOG_ALERT,
4357+
"grn_hash_sort on grn_hash_cursor_open_by_key failed !");
4358+
grn_hash_close(ctx, hash);
4359+
return NULL;
4360+
}
4361+
grn_table_sort_optarg sort_opt = {0};
4362+
int n_sorted = grn_hash_sort(ctx, c->hash, limit, sorted, &sort_opt);
4363+
if (n_sorted == 0) {
4364+
grn_array_close(ctx, sorted);
4365+
return NULL;
4366+
}
4367+
4368+
size_t start_idx = (offset > 0) ? offset : 0;
4369+
grn_hash_cursor_sorted_entries *sorted_entries =
4370+
GRN_MALLOC(sizeof(grn_hash_cursor_sorted_entries));
4371+
if (!sorted_entries) {
4372+
grn_array_close(ctx, sorted);
4373+
return NULL;
4374+
}
4375+
sorted_entries->sorted_entries = (grn_id *)sorted;
4376+
sorted_entries->n_entries = grn_array_size(ctx, sorted);
4377+
sorted_entries->curr = start_idx;
4378+
c->sorted_entries = sorted_entries;
4379+
c->rest = (limit < 0) ? GRN_ARRAY_MAX : (unsigned int)limit;
4380+
4381+
return c;
43374382
}
43384383

43394384
void
43404385
grn_hash_cursor_close(grn_ctx *ctx, grn_hash_cursor *c)
43414386
{
43424387
GRN_ASSERT(c->ctx == ctx);
4388+
if (c->sorted_entries) {
4389+
GRN_FREE(c->sorted_entries);
4390+
}
43434391
GRN_FREE(c);
43444392
}
43454393

@@ -4358,7 +4406,7 @@ grn_hash_cursor_open(grn_ctx *ctx, grn_hash *hash,
43584406
return NULL;
43594407
}
43604408
if (!(c = GRN_CALLOC(sizeof(grn_hash_cursor)))) { return NULL; }
4361-
if ((flags & GRN_CURSOR_BY_KEY)) {
4409+
if (!(flags & GRN_CURSOR_BY_ID)) {
43624410
return grn_hash_cursor_open_by_key(ctx,
43634411
hash,
43644412
min,
@@ -4434,13 +4482,23 @@ grn_id
44344482
grn_hash_cursor_next(grn_ctx *ctx, grn_hash_cursor *c)
44354483
{
44364484
if (c && c->rest) {
4437-
while (c->curr_rec != c->tail) {
4438-
c->curr_rec = (grn_id)((int64_t)(c->curr_rec) + c->dir);
4439-
if (*c->hash->n_entries != HASH_CURR_MAX(c->hash)) {
4440-
if (!grn_hash_bitmap_at(ctx, c->hash, c->curr_rec)) { continue; }
4441-
}
4485+
if (c->sorted_entries) {
4486+
grn_hash_cursor_sorted_entries *se = c->sorted_entries;
4487+
if (se->curr >= se->n_entries)
4488+
return GRN_ID_NIL;
4489+
grn_id id = se->sorted_entries[se->curr];
4490+
se->curr++;
44424491
c->rest--;
4443-
return c->curr_rec;
4492+
return id;
4493+
} else {
4494+
while (c->curr_rec != c->tail) {
4495+
c->curr_rec = (grn_id)((int64_t)(c->curr_rec) + c->dir);
4496+
if (*c->hash->n_entries != HASH_CURR_MAX(c->hash)) {
4497+
if (!grn_hash_bitmap_at(ctx, c->hash, c->curr_rec)) { continue; }
4498+
}
4499+
c->rest--;
4500+
return c->curr_rec;
4501+
}
44444502
}
44454503
}
44464504
return GRN_ID_NIL;

0 commit comments

Comments
 (0)