Skip to content

Commit

Permalink
Fixed a problem in grn_view_sort() ignoring the limit parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
daijiro committed Apr 23, 2010
1 parent 6d4b9a2 commit 0ee6c2c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/db.c
Expand Up @@ -1476,6 +1476,7 @@ grn_view_cursor_open(grn_ctx *ctx, grn_obj *view,
offset += v->offset;
while (offset--) { if (!grn_view_cursor_next(ctx, vc)) { break; } }
vc->rest = (limit < 0) ? GRN_ID_MAX : limit;
if (v->limit < vc->rest) { vc->rest = v->limit; }
return vc;
}
GRN_FREE(vc);
Expand Down Expand Up @@ -6137,6 +6138,7 @@ grn_view_sort(grn_ctx *ctx, grn_obj *table, int offset, int limit,
rv->keys = keys;
rv->n_keys = n_keys;
rv->offset = offset;
rv->limit = limit;
for (ks = keys, kd =keys_; ks < ke ; ks++, kd++) { kd->flags = ks->flags; }
GRN_HASH_EACH(ctx, th, id, &tp, NULL, NULL, {
grn_hash_get_key(ctx, rh, id, &rid, sizeof(grn_id));
Expand All @@ -6151,6 +6153,7 @@ grn_view_sort(grn_ctx *ctx, grn_obj *table, int offset, int limit,
}
});
GRN_FREE(keys_);
if (i > limit) { i = limit; }
}
return i;
}
Expand Down
1 change: 1 addition & 0 deletions lib/db.h
Expand Up @@ -136,6 +136,7 @@ typedef struct {
grn_table_sort_key *keys;
int n_keys;
int offset;
int limit;
} grn_view;

#define GRN_OBJ_TMP_OBJECT 0x80000000
Expand Down
30 changes: 30 additions & 0 deletions test/unit/core/test-view-operations.c
Expand Up @@ -188,6 +188,36 @@ test_sort(void)
cut_assert_equal_int(limit, n_records);
}

void
test_sort_offset(void)
{
grn_obj *result;
grn_table_sort_key keys[1];
gint offset, limit, n_records;

result = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_VIEW, NULL, NULL);
grn_view_add(context, result,
grn_table_create(context, NULL, 0, NULL, GRN_TABLE_NO_KEY,
NULL, users));
grn_view_add(context, result,
grn_table_create(context, NULL, 0, NULL, GRN_TABLE_NO_KEY,
NULL, dogs));

keys[0].key = grn_obj_column(context, entries, "_key", strlen("_key"));
keys[0].flags = GRN_TABLE_SORT_DESC;
offset = 1;
limit = 2;
n_records = grn_table_sort(context, entries, offset, limit, result,
keys, sizeof(keys[0]) / sizeof(keys));
grn_test_assert_equal_view(context,
gcut_take_new_list_string("taro",
"pochi",
NULL),
result,
"_key");
cut_assert_equal_int(limit, n_records);
}

static grn_obj *
query(const gchar *string)
{
Expand Down

0 comments on commit 0ee6c2c

Please sign in to comment.