Skip to content

Commit

Permalink
Use pseudo column name macros instead of literal
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Dec 16, 2013
1 parent f920234 commit e906816
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 43 deletions.
72 changes: 51 additions & 21 deletions lib/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -4090,7 +4090,10 @@ grn_obj_get_accessor(grn_ctx *ctx, grn_obj *obj, const char *name, unsigned int
if (len < 2) { goto exit; }
switch (name[1]) {
case 'k' : /* key */
if (len != 4 || memcmp(name, "_key", 4)) { goto exit; }
if (len != GRN_COLUMN_NAME_KEY_LEN ||
memcmp(name, GRN_COLUMN_NAME_KEY, GRN_COLUMN_NAME_KEY_LEN)) {
goto exit;
}
for (rp = &res; !done; rp = &(*rp)->next) {
*rp = accessor_new(ctx);
(*rp)->obj = obj;
Expand Down Expand Up @@ -4132,7 +4135,10 @@ grn_obj_get_accessor(grn_ctx *ctx, grn_obj *obj, const char *name, unsigned int
}
break;
case 'i' : /* id */
if (len != 3 || memcmp(name, "_id", 3)) { goto exit; }
if (len != GRN_COLUMN_NAME_ID_LEN ||
memcmp(name, GRN_COLUMN_NAME_ID, GRN_COLUMN_NAME_ID_LEN)) {
goto exit;
}
for (rp = &res; !done; rp = &(*rp)->next) {
*rp = accessor_new(ctx);
(*rp)->obj = obj;
Expand Down Expand Up @@ -4167,7 +4173,10 @@ grn_obj_get_accessor(grn_ctx *ctx, grn_obj *obj, const char *name, unsigned int
}
break;
case 'v' : /* value */
if (len != 6 || memcmp(name, "_value", 6)) { goto exit; }
if (len != GRN_COLUMN_NAME_VALUE_LEN ||
memcmp(name, GRN_COLUMN_NAME_VALUE, GRN_COLUMN_NAME_VALUE_LEN)) {
goto exit;
}
for (rp = &res; !done; rp = &(*rp)->next) {
*rp = accessor_new(ctx);
(*rp)->obj = obj;
Expand Down Expand Up @@ -4215,7 +4224,10 @@ grn_obj_get_accessor(grn_ctx *ctx, grn_obj *obj, const char *name, unsigned int
}
break;
case 's' : /* score */
if (len != 6 || memcmp(name, "_score", 6)) { goto exit; }
if (len != GRN_COLUMN_NAME_SCORE_LEN ||
memcmp(name, GRN_COLUMN_NAME_SCORE, GRN_COLUMN_NAME_SCORE_LEN)) {
goto exit;
}
for (rp = &res; !done; rp = &(*rp)->next) {
*rp = accessor_new(ctx);
(*rp)->obj = obj;
Expand Down Expand Up @@ -4250,7 +4262,12 @@ grn_obj_get_accessor(grn_ctx *ctx, grn_obj *obj, const char *name, unsigned int
}
break;
case 'n' : /* nsubrecs */
if (len != 9 || memcmp(name, "_nsubrecs", 9)) { goto exit; }
if (len != GRN_COLUMN_NAME_NSUBRECS_LEN ||
memcmp(name,
GRN_COLUMN_NAME_NSUBRECS,
GRN_COLUMN_NAME_NSUBRECS_LEN)) {
goto exit;
}
for (rp = &res; !done; rp = &(*rp)->next) {
*rp = accessor_new(ctx);
(*rp)->obj = obj;
Expand Down Expand Up @@ -7997,19 +8014,19 @@ grn_column_name(grn_ctx *ctx, grn_obj *obj, char *namebuf, int buf_size)
for (a = (grn_accessor *)obj; a; a = a->next) {
switch (a->action) {
case GRN_ACCESSOR_GET_ID :
name = "_id";
name = GRN_COLUMN_NAME_ID;
break;
case GRN_ACCESSOR_GET_KEY :
name = "_key";
name = GRN_COLUMN_NAME_KEY;
break;
case GRN_ACCESSOR_GET_VALUE :
name = "_value";
name = GRN_COLUMN_NAME_VALUE;
break;
case GRN_ACCESSOR_GET_SCORE :
name = "_score";
name = GRN_COLUMN_NAME_SCORE;
break;
case GRN_ACCESSOR_GET_NSUBRECS :
name = "_nsubrecs";
name = GRN_COLUMN_NAME_NSUBRECS;
break;
case GRN_ACCESSOR_GET_COLUMN_VALUE :
case GRN_ACCESSOR_GET_DB_OBJ :
Expand Down Expand Up @@ -8050,23 +8067,29 @@ grn_column_name_(grn_ctx *ctx, grn_obj *obj, grn_obj *buf)
for (a = (grn_accessor *)obj; a; a = a->next) {
switch (a->action) {
case GRN_ACCESSOR_GET_ID :
GRN_TEXT_PUTS(ctx, buf, "_id");
GRN_TEXT_PUT(ctx, buf, GRN_COLUMN_NAME_ID, GRN_COLUMN_NAME_ID_LEN);
break;
case GRN_ACCESSOR_GET_KEY :
if (!a->next) {
GRN_TEXT_PUTS(ctx, buf, "_key");
GRN_TEXT_PUT(ctx, buf, GRN_COLUMN_NAME_KEY, GRN_COLUMN_NAME_KEY_LEN);
}
break;
case GRN_ACCESSOR_GET_VALUE :
if (!a->next) {
GRN_TEXT_PUTS(ctx, buf, "_value");
GRN_TEXT_PUT(ctx, buf,
GRN_COLUMN_NAME_VALUE,
GRN_COLUMN_NAME_VALUE_LEN);
}
break;
case GRN_ACCESSOR_GET_SCORE :
GRN_TEXT_PUTS(ctx, buf, "_score");
GRN_TEXT_PUT(ctx, buf,
GRN_COLUMN_NAME_SCORE,
GRN_COLUMN_NAME_SCORE_LEN);
break;
case GRN_ACCESSOR_GET_NSUBRECS :
GRN_TEXT_PUTS(ctx, buf, "_nsubrecs");
GRN_TEXT_PUT(ctx, buf,
GRN_COLUMN_NAME_NSUBRECS,
GRN_COLUMN_NAME_NSUBRECS_LEN);
break;
case GRN_ACCESSOR_GET_COLUMN_VALUE :
grn_column_name_(ctx, a->obj, buf);
Expand Down Expand Up @@ -9144,7 +9167,9 @@ grn_obj_columns(grn_ctx *ctx, grn_obj *table,
{
grn_obj *type = grn_ctx_at(ctx, table->header.domain);
if (GRN_OBJ_TABLEP(type)) {
grn_obj *ai = grn_obj_column(ctx, table, "_id", 3);
grn_obj *ai = grn_obj_column(ctx, table,
GRN_COLUMN_NAME_ID,
GRN_COLUMN_NAME_ID_LEN);
if (ai) {
if (ai->header.type == GRN_ACCESSOR) {
cols = grn_hash_create(ctx, NULL, sizeof(grn_id), 0,
Expand Down Expand Up @@ -9286,7 +9311,8 @@ grn_table_sort_key_from_str(grn_ctx *ctx, const char *str, unsigned int str_size
if ((k->key = grn_obj_column(ctx, table, p, r - p))) {
k++;
} else {
if (r - p == 6 && memcmp(p, "_score", 6) == 0) {
if (r - p == GRN_COLUMN_NAME_SCORE_LEN &&
memcmp(p, GRN_COLUMN_NAME_SCORE, GRN_COLUMN_NAME_SCORE_LEN) == 0) {
GRN_LOG(ctx, GRN_WARN,
"ignore invalid sort key: <%.*s>(<%.*s>)",
(int)(r - p), p, str_size, str);
Expand Down Expand Up @@ -9582,8 +9608,10 @@ bracket_close(grn_ctx *ctx, grn_loader *loader)
char *column_name = GRN_TEXT_VALUE(value);
unsigned int column_name_size = GRN_TEXT_LEN(value);
if (value->header.domain == GRN_DB_TEXT &&
(name_equal(column_name, column_name_size, KEY_NAME) ||
name_equal(column_name, column_name_size, ID_NAME))) {
(name_equal(column_name, column_name_size,
GRN_COLUMN_NAME_KEY) ||
name_equal(column_name, column_name_size,
GRN_COLUMN_NAME_ID))) {
if (loader->key_offset != -1) {
GRN_LOG(ctx, GRN_LOG_ERROR,
"duplicated key columns: <%.*s> at %d and <%.*s> at %i",
Expand Down Expand Up @@ -9719,8 +9747,10 @@ brace_close(grn_ctx *ctx, grn_loader *loader)
char *column_name = GRN_TEXT_VALUE(v);
unsigned int column_name_size = GRN_TEXT_LEN(v);
if (v->header.domain == GRN_DB_TEXT &&
(name_equal(column_name, column_name_size, KEY_NAME) ||
name_equal(column_name, column_name_size, ID_NAME))) {
(name_equal(column_name, column_name_size,
GRN_COLUMN_NAME_KEY) ||
name_equal(column_name, column_name_size,
GRN_COLUMN_NAME_ID))) {
if (key_column_name) {
GRN_LOG(ctx, GRN_LOG_ERROR, "duplicated key columns: %.*s and %.*s",
(int)GRN_TEXT_LEN(key_column_name),
Expand Down
3 changes: 0 additions & 3 deletions lib/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,6 @@ void grn_expr_pack(grn_ctx *ctx, grn_obj *buf, grn_obj *expr);
GRN_API grn_rc grn_expr_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *expr);
grn_obj *grn_expr_open(grn_ctx *ctx, grn_obj_spec *spec, const uint8_t *p, const uint8_t *pe);

#define KEY_NAME "_key"
#define ID_NAME "_id"

GRN_API void grn_load_(grn_ctx *ctx, grn_content_type input_type,
const char *table, unsigned int table_len,
const char *columns, unsigned int columns_len,
Expand Down
8 changes: 6 additions & 2 deletions lib/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,9 @@ grn_output_bulk(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
grn_obj *table = grn_ctx_at(ctx, bulk->header.domain);
grn_id id = GRN_RECORD_VALUE(bulk);
if (table && table->header.type != GRN_TABLE_NO_KEY) {
grn_obj *accessor = grn_obj_column(ctx, table, "_key", 4);
grn_obj *accessor = grn_obj_column(ctx, table,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
if (accessor) {
if (id == GRN_ID_NIL) {
grn_obj_reinit_for(ctx, &buf, accessor);
Expand Down Expand Up @@ -1263,7 +1265,9 @@ grn_output_table(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
grn_output_array_close(ctx, outbuf, output_type);
} else {
int i;
grn_obj *column = grn_obj_column(ctx, table, "_key", 4);
grn_obj *column = grn_obj_column(ctx, table,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
grn_table_cursor *tc = grn_table_cursor_open(ctx, table, NULL, 0, NULL, 0,
0, -1, GRN_CURSOR_ASCENDING);
grn_output_array_open(ctx, outbuf, output_type, "HIT", -1);
Expand Down
30 changes: 20 additions & 10 deletions lib/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,9 @@ proc_column_list(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_da
int column_list_size = -1;
#ifdef GRN_WITH_MESSAGE_PACK
column_list_size = 1; /* [header, (key), (COLUMNS)] */
if ((col = grn_obj_column(ctx, table, KEY_NAME, sizeof(KEY_NAME)-1))) {
if ((col = grn_obj_column(ctx, table,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN))) {
column_list_size++;
grn_obj_unlink(ctx, col);
}
Expand Down Expand Up @@ -1513,7 +1515,9 @@ proc_column_list(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_da
GRN_OUTPUT_CSTR("ShortText");
GRN_OUTPUT_ARRAY_CLOSE();
GRN_OUTPUT_ARRAY_CLOSE();
if ((col = grn_obj_column(ctx, table, KEY_NAME, sizeof(KEY_NAME)-1))) {
if ((col = grn_obj_column(ctx, table,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN))) {
int name_len;
char name_buf[GRN_TABLE_MAX_KEY_SIZE];
grn_id id;
Expand All @@ -1522,7 +1526,7 @@ proc_column_list(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_da
GRN_OUTPUT_ARRAY_OPEN("COLUMN", 8);
id = grn_obj_id(ctx, table);
GRN_OUTPUT_INT64(id);
GRN_OUTPUT_CSTR(KEY_NAME);
GRN_OUTPUT_CSTR(GRN_COLUMN_NAME_KEY);
GRN_OUTPUT_CSTR("");
GRN_OUTPUT_CSTR("");
grn_column_create_flags_to_text(ctx, &buf, 0);
Expand Down Expand Up @@ -2063,7 +2067,7 @@ dump_index_column_sources(grn_ctx *ctx, grn_obj *outbuf, grn_obj *column)
case GRN_TABLE_PAT_KEY:
case GRN_TABLE_DAT_KEY:
case GRN_TABLE_HASH_KEY:
GRN_TEXT_PUTS(ctx, outbuf, "_key");
GRN_TEXT_PUT(ctx, outbuf, GRN_COLUMN_NAME_KEY, GRN_COLUMN_NAME_KEY_LEN);
break;
default:
dump_column_name(ctx, outbuf, source);
Expand Down Expand Up @@ -2206,11 +2210,15 @@ dump_records(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table)
if (((table->header.type == GRN_TABLE_HASH_KEY ||
table->header.type == GRN_TABLE_PAT_KEY ||
table->header.type == GRN_TABLE_DAT_KEY) &&
GRN_TEXT_LEN(&column_name) == 3 &&
!memcmp(GRN_TEXT_VALUE(&column_name), "_id", 3)) ||
GRN_TEXT_LEN(&column_name) == GRN_COLUMN_NAME_ID_LEN &&
!memcmp(GRN_TEXT_VALUE(&column_name),
GRN_COLUMN_NAME_ID,
GRN_COLUMN_NAME_ID_LEN)) ||
(table->header.type == GRN_TABLE_NO_KEY &&
GRN_TEXT_LEN(&column_name) == 4 &&
!memcmp(GRN_TEXT_VALUE(&column_name), "_key", 4))) {
GRN_TEXT_LEN(&column_name) == GRN_COLUMN_NAME_KEY_LEN &&
!memcmp(GRN_TEXT_VALUE(&column_name),
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN))) {
continue;
}
GRN_PTR_PUT(ctx, &use_columns, columns[i]);
Expand Down Expand Up @@ -2254,8 +2262,10 @@ dump_records(grn_ctx *ctx, grn_obj *outbuf, grn_obj *table)
column = *((grn_obj **)GRN_BULK_HEAD(&use_columns) + j);
GRN_BULK_REWIND(&column_name);
grn_column_name_(ctx, column, &column_name);
if (GRN_TEXT_LEN(&column_name) == 6 &&
!memcmp(GRN_TEXT_VALUE(&column_name), "_value", 6)) {
if (GRN_TEXT_LEN(&column_name) == GRN_COLUMN_NAME_VALUE_LEN &&
!memcmp(GRN_TEXT_VALUE(&column_name),
GRN_COLUMN_NAME_VALUE,
GRN_COLUMN_NAME_VALUE_LEN)) {
is_value_column = 1;
} else {
is_value_column = 0;
Expand Down
8 changes: 6 additions & 2 deletions lib/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -2721,7 +2721,9 @@ grn_text_otoj(grn_ctx *ctx, grn_obj *bulk, grn_obj *obj, grn_obj_format *format)
if (table && table->header.type != GRN_TABLE_NO_KEY) {
/* todo : temporal patch. grn_table_at() is kinda costful... */
if (grn_table_at(ctx, table, id)) {
grn_obj *accessor = grn_obj_column(ctx, table, "_key", 4);
grn_obj *accessor = grn_obj_column(ctx, table,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
if (accessor) {
grn_obj_get_value(ctx, accessor, id, &buf);
grn_obj_unlink(ctx, accessor);
Expand Down Expand Up @@ -2952,7 +2954,9 @@ grn_text_otoj(grn_ctx *ctx, grn_obj *bulk, grn_obj *obj, grn_obj_format *format)
} else {
int i;
grn_id id;
grn_obj *column = grn_obj_column(ctx, obj, "_key", 4);
grn_obj *column = grn_obj_column(ctx, obj,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
grn_table_cursor *tc = grn_table_cursor_open(ctx, obj, NULL, 0, NULL, 0,
0, -1, GRN_CURSOR_ASCENDING);
GRN_TEXT_PUTC(ctx, bulk, '[');
Expand Down
12 changes: 9 additions & 3 deletions plugins/suggest/suggest.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ correct(grn_ctx *ctx, grn_obj *items, grn_obj *items_boost,
(similar_search_mode == GRN_SUGGEST_SEARCH_AUTO &&
max_score < frequency_threshold))) {
grn_obj *key, *index;
if ((key = grn_obj_column(ctx, items, CONST_STR_LEN("_key")))) {
if ((key = grn_obj_column(ctx, items,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN))) {
if (grn_column_index(ctx, key, GRN_OP_MATCH, &index, 1, NULL)) {
grn_select_optarg optarg;
memset(&optarg, 0, sizeof(grn_select_optarg));
Expand Down Expand Up @@ -445,8 +447,12 @@ correct(grn_ctx *ctx, grn_obj *items, grn_obj *items_boost,
GRN_EXPR_CREATE_FOR_QUERY(ctx, res, expr, var);
if (expr) {
grn_table_cursor *tc;
grn_obj *score = grn_obj_column(ctx, res, CONST_STR_LEN("_score"));
grn_obj *key = grn_obj_column(ctx, res, CONST_STR_LEN("_key"));
grn_obj *score = grn_obj_column(ctx, res,
GRN_COLUMN_NAME_SCORE,
GRN_COLUMN_NAME_SCORE_LEN);
grn_obj *key = grn_obj_column(ctx, res,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
grn_expr_append_obj(ctx, expr,
score,
GRN_OP_GET_VALUE, 1);
Expand Down
8 changes: 6 additions & 2 deletions src/suggest/groonga_suggest_learner.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ send_handler(void *zmq_send_sock, grn_ctx *ctx)
msgpack_pack_raw_body(&pk, name_buf, name_len);

msgpack_pack_raw(&pk, 4);
msgpack_pack_raw_body(&pk, CONST_STR_LEN("_key"));
msgpack_pack_raw_body(&pk,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
key_len = grn_table_cursor_get_key(ctx, tc, (void **)&key);
msgpack_pack_raw(&pk, key_len);
msgpack_pack_raw_body(&pk, key, key_len);
Expand Down Expand Up @@ -330,7 +332,9 @@ send_handler(void *zmq_send_sock, grn_ctx *ctx)
msgpack_pack_raw_body(&pk, name_buf, name_len);

msgpack_pack_raw(&pk, 4);
msgpack_pack_raw_body(&pk, CONST_STR_LEN("_key"));
msgpack_pack_raw_body(&pk,
GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN);
grn_table_cursor_get_key(ctx, tc, (void **)&key);
msgpack_pack_uint64(&pk, *key);

Expand Down

0 comments on commit e906816

Please sign in to comment.