Kouhei Sutou
null+****@clear*****
Wed Feb 11 21:48:02 JST 2015
Kouhei Sutou 2015-02-11 21:48:02 +0900 (Wed, 11 Feb 2015) New Revision: f4e8c6f77e9e20be4eed0c37f0336883e824367b https://github.com/pgroonga/pgroonga/commit/f4e8c6f77e9e20be4eed0c37f0336883e824367b Message: Add pgroonga.table_name() and pgroonga.command() Added files: expected/groonga/command.out sql/groonga/command.sql Modified files: Makefile pgroonga.c pgroonga.h pgroonga.sql Modified: Makefile (+3 -0) =================================================================== --- Makefile 2015-02-11 20:40:55 +0900 (8381527) +++ Makefile 2015-02-11 21:48:02 +0900 (f4f7625) @@ -38,6 +38,7 @@ installcheck: results/compare/integer/single/greater-than-equal installcheck: results/compare/integer/single/between installcheck: results/compare/integer/multiple/greater-than-equal installcheck: results/compare/integer/order_by_limit +installcheck: results/groonga results/full-text-search/text/single/contain: @mkdir -p $@ @@ -65,3 +66,5 @@ results/compare/integer/multiple/greater-than-equal: @mkdir -p $@ results/compare/integer/order_by_limit: @mkdir -p $@ +results/groonga: + @mkdir -p $@ Added: expected/groonga/command.out (+16 -0) 100644 =================================================================== --- /dev/null +++ expected/groonga/command.out 2015-02-11 21:48:02 +0900 (112cc71) @@ -0,0 +1,16 @@ +CREATE TABLE memos ( + content text +); +INSERT INTO memos VALUES ('PostgreSQL is a RDBMS.'); +INSERT INTO memos VALUES ('Groonga is fast full text search engine.'); +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 +-------------------------------------------------------------------------------------------------------------------------------------------------------------------- + [[[3],[["content","LongText"]],["PostgreSQL is a RDBMS."],["Groonga is fast full text search engine."],["PGroonga is a PostgreSQL extension that uses Groonga."]]] +(1 row) + +DROP TABLE memos; Modified: pgroonga.c (+56 -0) =================================================================== --- pgroonga.c 2015-02-11 20:40:55 +0900 (af36ea7) +++ pgroonga.c 2015-02-11 21:48:02 +0900 (4a79cfc) @@ -14,6 +14,7 @@ #include <miscadmin.h> #include <storage/ipc.h> #include <storage/lmgr.h> +#include <utils/builtins.h> #include <utils/lsyscache.h> #include <utils/selfuncs.h> @@ -69,6 +70,9 @@ typedef struct PGrnSearchData } PGrnSearchData; +PG_FUNCTION_INFO_V1(pgroonga_table_name); +PG_FUNCTION_INFO_V1(pgroonga_command); + PG_FUNCTION_INFO_V1(pgroonga_contain_text); PG_FUNCTION_INFO_V1(pgroonga_contain_bpchar); PG_FUNCTION_INFO_V1(pgroonga_match); @@ -597,6 +601,58 @@ UInt64ToCtid(uint64 key) return ctid; } +/** + * pgroonga.table_name(indexName cstring) : cstring + */ +Datum +pgroonga_table_name(PG_FUNCTION_ARGS) +{ + Datum indexNameDatum = PG_GETARG_DATUM(0); + Datum indexOidDatum; + Oid indexOid; + char tableName[GRN_TABLE_MAX_KEY_SIZE]; + char *copiedTableName; + + indexOidDatum = DirectFunctionCall1(to_regclass, indexNameDatum); + if (!indexOidDatum) + { + ereport(ERROR, + (errcode(ERRCODE_INVALID_NAME), + errmsg("unknown index name: <%s>", + DatumGetCString(indexNameDatum)))); + } + indexOid = DatumGetObjectId(indexOidDatum); + + snprintf(tableName, sizeof(tableName), PGrnIDsTableNameFormat, indexOid); + copiedTableName = pstrdup(tableName); + PG_RETURN_CSTRING(copiedTableName); +} + +/** + * pgroonga.command(groongaCommand text) : text + */ +Datum +pgroonga_command(PG_FUNCTION_ARGS) +{ + text *groongaCommand = PG_GETARG_TEXT_PP(0); + int flags; + char *copiedResult; + + grn_ctx_send(ctx, + VARDATA_ANY(groongaCommand), + VARSIZE_ANY_EXHDR(groongaCommand), 0); + grn_obj_reinit(ctx, &buffer, GRN_DB_TEXT, 0); + do { + char *chunk; + unsigned int chunkSize; + grn_ctx_recv(ctx, &chunk, &chunkSize, &flags); + GRN_TEXT_PUT(ctx, &buffer, chunk, chunkSize); + } while ((flags & GRN_CTX_MORE)); + + copiedResult = pnstrdup(GRN_TEXT_VALUE(&buffer), GRN_TEXT_LEN(&buffer)); + PG_RETURN_CSTRING(copiedResult); +} + static grn_bool pgroonga_contain_raw(const char *text, unsigned int text_size, const char *key, unsigned int key_size) Modified: pgroonga.h (+2 -0) =================================================================== --- pgroonga.h 2015-02-11 20:40:55 +0900 (eb946b7) +++ pgroonga.h 2015-02-11 21:48:02 +0900 (e3de601) @@ -45,6 +45,8 @@ /* in pgroonga.c */ extern void PGDLLEXPORT _PG_init(void); +extern Datum PGDLLEXPORT pgroonga_command(PG_FUNCTION_ARGS); + extern Datum PGDLLEXPORT pgroonga_contain_text(PG_FUNCTION_ARGS); extern Datum PGDLLEXPORT pgroonga_contain_bpchar(PG_FUNCTION_ARGS); extern Datum PGDLLEXPORT pgroonga_match(PG_FUNCTION_ARGS); Modified: pgroonga.sql (+14 -0) =================================================================== --- pgroonga.sql 2015-02-11 20:40:55 +0900 (435e5f2) +++ pgroonga.sql 2015-02-11 21:48:02 +0900 (7baa9ba) @@ -2,6 +2,20 @@ SET search_path = public; CREATE SCHEMA pgroonga; +CREATE FUNCTION pgroonga.table_name(indexName cstring) + RETURNS cstring + AS 'MODULE_PATHNAME', 'pgroonga_table_name' + LANGUAGE C + VOLATILE + STRICT; + +CREATE FUNCTION pgroonga.command(groongaCommand text) + RETURNS cstring + AS 'MODULE_PATHNAME', 'pgroonga_command' + LANGUAGE C + VOLATILE + STRICT; + CREATE FUNCTION pgroonga.contain(text, text) RETURNS bool AS 'MODULE_PATHNAME', 'pgroonga_contain_text' Added: sql/groonga/command.sql (+15 -0) 100644 =================================================================== --- /dev/null +++ sql/groonga/command.sql 2015-02-11 21:48:02 +0900 (b644852) @@ -0,0 +1,15 @@ +CREATE TABLE memos ( + content text +); + +INSERT INTO memos VALUES ('PostgreSQL is a RDBMS.'); +INSERT INTO memos VALUES ('Groonga is fast full text search engine.'); +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'); + +DROP TABLE memos; -------------- next part -------------- HTML����������������������������... Download