Skip to content

Commit

Permalink
dump: put index columns after reference columns
Browse files Browse the repository at this point in the history
Because index column may refer reference columns.
  • Loading branch information
kou committed Apr 3, 2015
1 parent 7d91e6d commit ed5f703
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 19 deletions.
64 changes: 45 additions & 19 deletions lib/proc.c
Expand Up @@ -2876,9 +2876,16 @@ reference_column_p(grn_ctx *ctx, grn_obj *column)
}
}

static int
index_column_p(grn_ctx *ctx, grn_obj *column)
{
return column->header.type == GRN_COLUMN_INDEX;
}

static void
dump_columns(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table,
grn_obj *pending_columns)
grn_obj *pending_reference_columns,
grn_obj *pending_index_columns)
{
grn_hash *columns;
columns = grn_hash_create(ctx, NULL, sizeof(grn_id), 0,
Expand All @@ -2895,7 +2902,9 @@ dump_columns(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table,
grn_obj *column;
if ((column = grn_ctx_at(ctx, *key))) {
if (reference_column_p(ctx, column)) {
GRN_PTR_PUT(ctx, pending_columns, column);
GRN_PTR_PUT(ctx, pending_reference_columns, column);
} else if (index_column_p(ctx, column)) {
GRN_PTR_PUT(ctx, pending_index_columns, column);
} else {
dump_column(ctx, outbuf, table, column);
grn_obj_unlink(ctx, column);
Expand Down Expand Up @@ -3107,7 +3116,8 @@ dump_records(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table)

static void
dump_table(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table,
grn_obj *pending_columns)
grn_obj *pending_reference_columns,
grn_obj *pending_index_columns)
{
grn_obj *domain = NULL, *range = NULL;
grn_obj_flags default_flags = GRN_OBJ_PERSISTENT;
Expand Down Expand Up @@ -3188,7 +3198,9 @@ dump_table(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table,
grn_obj_unlink(ctx, domain);
}

dump_columns(ctx, outbuf, table, pending_columns);
dump_columns(ctx, outbuf, table,
pending_reference_columns,
pending_index_columns);
}

/* can we move this to groonga.h? */
Expand All @@ -3201,21 +3213,40 @@ dump_table(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table,
}\
} while (0)

static void
dump_pending_columns(grn_ctx *ctx, grn_obj *outbuf, grn_obj *pending_columns)
{
size_t i, n_columns;

n_columns = GRN_BULK_VSIZE(pending_columns) / sizeof(grn_obj *);
for (i = 0; i < n_columns; i++) {
grn_obj *table, *column;

column = GRN_PTR_VALUE_AT(pending_columns, i);
table = grn_ctx_at(ctx, column->header.domain);
dump_column(ctx, outbuf, table, column);
grn_obj_unlink(ctx, column);
grn_obj_unlink(ctx, table);
}
}

static void
dump_schema(grn_ctx *ctx, grn_obj *outbuf)
{
grn_obj *db = ctx->impl->db;
grn_table_cursor *cur;
grn_id id;
grn_obj pending_columns;
grn_obj pending_reference_columns;
grn_obj pending_index_columns;

cur = grn_table_cursor_open(ctx, db, NULL, 0, NULL, 0, 0, -1,
GRN_CURSOR_BY_ID);
if (!cur) {
return;
}

GRN_PTR_INIT(&pending_columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
GRN_PTR_INIT(&pending_reference_columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
GRN_PTR_INIT(&pending_index_columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
while ((id = grn_table_cursor_next(ctx, cur)) != GRN_ID_NIL) {
grn_obj *object;

Expand All @@ -3225,7 +3256,9 @@ dump_schema(grn_ctx *ctx, grn_obj *outbuf)
case GRN_TABLE_PAT_KEY:
case GRN_TABLE_DAT_KEY:
case GRN_TABLE_NO_KEY:
dump_table(ctx, outbuf, object, &pending_columns);
dump_table(ctx, outbuf, object,
&pending_reference_columns,
&pending_index_columns);
break;
default:
break;
Expand All @@ -3241,18 +3274,11 @@ dump_schema(grn_ctx *ctx, grn_obj *outbuf)
}
grn_table_cursor_close(ctx, cur);

while (GRN_TRUE) {
grn_obj *table, *column;
GRN_PTR_POP(&pending_columns, column);
if (!column) {
break;
}
table = grn_ctx_at(ctx, column->header.domain);
dump_column(ctx, outbuf, table, column);
grn_obj_unlink(ctx, column);
grn_obj_unlink(ctx, table);
}
grn_obj_close(ctx, &pending_columns);
dump_pending_columns(ctx, outbuf, &pending_reference_columns);
grn_obj_close(ctx, &pending_reference_columns);

dump_pending_columns(ctx, outbuf, &pending_index_columns);
grn_obj_close(ctx, &pending_index_columns);
}

static void
Expand Down
@@ -0,0 +1,23 @@
table_create Bookmarks TABLE_HASH_KEY ShortText
[[0,0.0,0.0],true]
column_create Bookmarks title COLUMN_SCALAR ShortText
[[0,0.0,0.0],true]
table_create Users TABLE_HASH_KEY ShortText
[[0,0.0,0.0],true]
column_create Users name COLUMN_SCALAR ShortText
[[0,0.0,0.0],true]
column_create Users bookmark COLUMN_SCALAR Bookmarks
[[0,0.0,0.0],true]
table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
[[0,0.0,0.0],true]
column_create Terms users_name_index COLUMN_INDEX|WITH_POSITION Users name
[[0,0.0,0.0],true]
dump
table_create Bookmarks TABLE_HASH_KEY ShortText
column_create Bookmarks title COLUMN_SCALAR ShortText
table_create Users TABLE_HASH_KEY ShortText
column_create Users name COLUMN_SCALAR ShortText
table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
column_create Users bookmark COLUMN_SCALAR Bookmarks
column_create Terms users_name_index COLUMN_INDEX|WITH_POSITION Users name

@@ -0,0 +1,14 @@
table_create Bookmarks TABLE_HASH_KEY ShortText
column_create Bookmarks title COLUMN_SCALAR ShortText

table_create Users TABLE_HASH_KEY ShortText
column_create Users name COLUMN_SCALAR ShortText

column_create Users bookmark COLUMN_SCALAR Bookmarks

table_create Terms TABLE_PAT_KEY ShortText \
--default_tokenizer TokenBigram \
--normalizer NormalizerAuto
column_create Terms users_name_index COLUMN_INDEX|WITH_POSITION Users name

dump
@@ -0,0 +1,20 @@
table_create Bookmarks TABLE_HASH_KEY ShortText
[[0,0.0,0.0],true]
column_create Bookmarks title COLUMN_SCALAR ShortText
[[0,0.0,0.0],true]
table_create Users TABLE_HASH_KEY ShortText
[[0,0.0,0.0],true]
column_create Users name COLUMN_SCALAR ShortText
[[0,0.0,0.0],true]
column_create Users bookmark COLUMN_SCALAR Bookmarks
[[0,0.0,0.0],true]
column_create Bookmarks user COLUMN_SCALAR Users
[[0,0.0,0.0],true]
dump
table_create Bookmarks TABLE_HASH_KEY ShortText
column_create Bookmarks title COLUMN_SCALAR ShortText
table_create Users TABLE_HASH_KEY ShortText
column_create Users name COLUMN_SCALAR ShortText
column_create Bookmarks user COLUMN_SCALAR Users
column_create Users bookmark COLUMN_SCALAR Bookmarks

10 changes: 10 additions & 0 deletions test/command/suite/dump/schema/column/reference/each_other.test
@@ -0,0 +1,10 @@
table_create Bookmarks TABLE_HASH_KEY ShortText
column_create Bookmarks title COLUMN_SCALAR ShortText

table_create Users TABLE_HASH_KEY ShortText
column_create Users name COLUMN_SCALAR ShortText

column_create Users bookmark COLUMN_SCALAR Bookmarks
column_create Bookmarks user COLUMN_SCALAR Users

dump

0 comments on commit ed5f703

Please sign in to comment.