Kouhei Sutou
null+****@clear*****
Fri Feb 13 22:46:39 JST 2015
Kouhei Sutou 2015-02-13 22:46:39 +0900 (Fri, 13 Feb 2015) New Revision: 22d86857d27cc5699500a5c2ad1cfd4fbf01dee2 https://github.com/pgroonga/pgroonga/commit/22d86857d27cc5699500a5c2ad1cfd4fbf01dee2 Message: pgroonga.command: add envelope to ensure valid JSON Modified files: expected/groonga/command.out pgroonga.c sql/groonga/command.sql Modified: expected/groonga/command.out (+3 -2) =================================================================== --- expected/groonga/command.out 2015-02-11 21:53:18 +0900 (112cc71) +++ expected/groonga/command.out 2015-02-13 22:46:39 +0900 (cbffc30) @@ -7,8 +7,9 @@ INSERT INTO memos VALUES ('PGroonga is a PostgreSQL extension that uses Groonga. CREATE INDEX pgroonga_index ON memos USING pgroonga (content); SELECT pgroonga.command('select ' || pgroonga.table_name('pgroonga_index') || - ' --output_columns content'); - command + ' --output_columns content')::json->>1 + AS body; + body -------------------------------------------------------------------------------------------------------------------------------------------------------------------- [[[3],[["content","LongText"]],["PostgreSQL is a RDBMS."],["Groonga is fast full text search engine."],["PGroonga is a PostgreSQL extension that uses Groonga."]]] (1 row) Modified: pgroonga.c (+28 -2) =================================================================== --- pgroonga.c 2015-02-11 21:53:18 +0900 (fcb3e93) +++ pgroonga.c 2015-02-13 22:46:39 +0900 (5eebbc8) @@ -93,6 +93,9 @@ PG_FUNCTION_INFO_V1(pgroonga_options); static grn_ctx grnContext; static grn_ctx *ctx = &grnContext; static grn_obj buffer; +static grn_obj headBuffer; +static grn_obj bodyBuffer; +static grn_obj footBuffer; static grn_obj inspectBuffer; static const char * @@ -161,6 +164,9 @@ PGrnOnProcExit(int code, Datum arg) grn_obj *db; GRN_OBJ_FIN(ctx, &inspectBuffer); + GRN_OBJ_FIN(ctx, &footBuffer); + GRN_OBJ_FIN(ctx, &bodyBuffer); + GRN_OBJ_FIN(ctx, &headBuffer); GRN_OBJ_FIN(ctx, &buffer); db = grn_ctx_db(ctx); @@ -294,6 +300,9 @@ _PG_init(void) on_proc_exit(PGrnOnProcExit, 0); GRN_VOID_INIT(&buffer); + GRN_TEXT_INIT(&headBuffer, 0); + GRN_TEXT_INIT(&bodyBuffer, 0); + GRN_TEXT_INIT(&footBuffer, 0); GRN_TEXT_INIT(&inspectBuffer, 0); PGrnEnsureDatabase(); @@ -641,14 +650,31 @@ pgroonga_command(PG_FUNCTION_ARGS) grn_ctx_send(ctx, VARDATA_ANY(groongaCommand), VARSIZE_ANY_EXHDR(groongaCommand), 0); - grn_obj_reinit(ctx, &buffer, GRN_DB_TEXT, 0); + GRN_BULK_REWIND(&bodyBuffer); do { char *chunk; unsigned int chunkSize; grn_ctx_recv(ctx, &chunk, &chunkSize, &flags); - GRN_TEXT_PUT(ctx, &buffer, chunk, chunkSize); + GRN_TEXT_PUT(ctx, &bodyBuffer, chunk, chunkSize); } while ((flags & GRN_CTX_MORE)); + GRN_BULK_REWIND(&headBuffer); + GRN_BULK_REWIND(&footBuffer); + grn_output_envelope(ctx, + ctx->rc, + &headBuffer, + &bodyBuffer, + &footBuffer, + NULL, + 0); + + grn_obj_reinit(ctx, &buffer, GRN_DB_TEXT, 0); + GRN_TEXT_PUT(ctx, &buffer, + GRN_TEXT_VALUE(&headBuffer), GRN_TEXT_LEN(&headBuffer)); + GRN_TEXT_PUT(ctx, &buffer, + GRN_TEXT_VALUE(&bodyBuffer), GRN_TEXT_LEN(&bodyBuffer)); + GRN_TEXT_PUT(ctx, &buffer, + GRN_TEXT_VALUE(&footBuffer), GRN_TEXT_LEN(&footBuffer)); result = cstring_to_text_with_len(GRN_TEXT_VALUE(&buffer), GRN_TEXT_LEN(&buffer)); PG_RETURN_TEXT_P(result); Modified: sql/groonga/command.sql (+2 -1) =================================================================== --- sql/groonga/command.sql 2015-02-11 21:53:18 +0900 (b644852) +++ sql/groonga/command.sql 2015-02-13 22:46:39 +0900 (742202a) @@ -10,6 +10,7 @@ CREATE INDEX pgroonga_index ON memos USING pgroonga (content); SELECT pgroonga.command('select ' || pgroonga.table_name('pgroonga_index') || - ' --output_columns content'); + ' --output_columns content')::json->>1 + AS body; DROP TABLE memos; -------------- next part -------------- HTML����������������������������... Download