Skip to content

Commit

Permalink
dump records as arrays instead of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryo Onodera authored and daijiro committed Feb 4, 2010
1 parent 55259a2 commit 9f49300
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 79 deletions.
29 changes: 24 additions & 5 deletions lib/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,27 @@ dump_records(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table)
columns = (grn_obj **)GRN_BULK_HEAD(&columnbuf);
ncolumns = GRN_BULK_VSIZE(&columnbuf)/sizeof(grn_obj *);

GRN_TEXT_PUTC(ctx, outbuf, '[');
for (i = 0; i < ncolumns; i++) {
grn_obj buf;
GRN_TEXT_INIT(&buf, 0);
grn_column_name_(ctx, columns[i], &buf);
/* skips unnecessary columns */
if (((table->header.type == GRN_TABLE_HASH_KEY ||
table->header.type == GRN_TABLE_PAT_KEY) &&
!memcmp(GRN_TEXT_VALUE(&buf), "_id",
(GRN_TEXT_LEN(&buf) > 3) ? 3 : GRN_TEXT_LEN(&buf))) ||
(table->header.type == GRN_TABLE_NO_KEY &&
!memcmp(GRN_TEXT_VALUE(&buf), "_key",
(GRN_TEXT_LEN(&buf) > 4) ? 4 : GRN_TEXT_LEN(&buf)))) {
continue;
}
grn_text_otoj(ctx, outbuf, &buf, NULL);
grn_obj_unlink(ctx, &buf);
if (i + 1 < ncolumns) { GRN_TEXT_PUTC(ctx, outbuf, ','); }
}
GRN_TEXT_PUTS(ctx, outbuf, "],\n");

cursor = grn_table_cursor_open(ctx, table, NULL, 0, NULL, 0, 0, -1,
GRN_CURSOR_BY_ID);
for (i = 0; (id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL;
Expand All @@ -1180,15 +1201,15 @@ dump_records(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table)
if (table->header.type == GRN_TABLE_NO_KEY && old_id + 1 < id) {
grn_id current_id;
for (current_id = old_id + 1; current_id < id; current_id++) {
GRN_TEXT_PUTS(ctx, outbuf, "{},\n");
GRN_TEXT_PUTS(ctx, outbuf, "[],\n");
GRN_TEXT_PUTS(ctx, &delete_commands, "delete --table ");
dump_obj_name(ctx, &delete_commands, table);
GRN_TEXT_PUTS(ctx, &delete_commands, " --id ");
grn_text_lltoa(ctx, &delete_commands, current_id);
GRN_TEXT_PUTC(ctx, &delete_commands, '\n');
}
}
GRN_TEXT_PUTC(ctx, outbuf, '{');
GRN_TEXT_PUTC(ctx, outbuf, '[');
for (j = 0; j < ncolumns; j++) {
grn_id range;
GRN_TEXT_INIT(&buf, 0);
Expand All @@ -1209,9 +1230,7 @@ dump_records(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table)
} else {
is_value_column = 0;
}
grn_text_otoj(ctx, outbuf, &buf, NULL);
grn_obj_unlink(ctx, &buf);
GRN_TEXT_PUTC(ctx, outbuf, ':');
range = grn_obj_get_range(ctx, columns[j]);

switch (columns[j]->header.type) {
Expand Down Expand Up @@ -1260,7 +1279,7 @@ dump_records(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table)
}
if (j + 1 < ncolumns) { GRN_TEXT_PUTC(ctx, outbuf, ','); }
}
GRN_TEXT_PUTC(ctx, outbuf, '}');
GRN_TEXT_PUTC(ctx, outbuf, ']');
}
GRN_TEXT_PUTS(ctx, outbuf, "\n]\n");
GRN_TEXT_PUT(ctx, outbuf, GRN_TEXT_VALUE(&delete_commands),
Expand Down
14 changes: 8 additions & 6 deletions test/unit/core/test-dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ test_vector_column(gconstpointer data)
"column_create Table Column 1 %s\n"
"load --table Table\n"
"[\n"
"{\"_id\":1,\"Column\":%s}\n"
"[\"_id\",\"Column\"],\n"
"[1,%s]\n"
"]\n",
type_name,
gcut_data_get_string(data, "expected"));
Expand Down Expand Up @@ -464,10 +465,11 @@ test_unsequantial_records_in_table_with_keys(void)
grn_test_assert_dump("table_create Weekdays 0 ShortText\n"
"load --table Weekdays\n"
"[\n"
"{\"_key\":\"Sun\"},\n"
"{\"_key\":\"Mon\"},\n"
"{\"_key\":\"Wed\"},\n"
"{\"_key\":\"Thu\"},\n"
"{\"_key\":\"Sat\"}\n"
"[\"_key\"],\n"
"[\"Sun\"],\n"
"[\"Mon\"],\n"
"[\"Wed\"],\n"
"[\"Thu\"],\n"
"[\"Sat\"]\n"
"]\n");
}
Loading

0 comments on commit 9f49300

Please sign in to comment.