Kouhei Sutou
null+****@clear*****
Sat Aug 27 21:49:12 JST 2016
Kouhei Sutou 2016-08-27 21:49:12 +0900 (Sat, 27 Aug 2016) New Revision: 7fbc97bb65af9841925b92aa4753e7dcc53bc178 https://github.com/pgroonga/pgroonga/commit/7fbc97bb65af9841925b92aa4753e7dcc53bc178 Message: Add pgroonga.flush Added files: expected/function/flush/jsonb.out expected/function/flush/multiple.out expected/function/flush/nonexistent.out expected/function/flush/single.out sql/function/flush/jsonb.sql sql/function/flush/multiple.sql sql/function/flush/nonexistent.sql sql/function/flush/single.sql src/pgrn_flush.c Modified files: .travis.yml Makefile pgroonga--1.1.0--1.1.1.sql pgroonga.sql src/pgrn_groonga.c src/pgrn_groonga.h src/pgroonga.h Modified: .travis.yml (+1 -0) =================================================================== --- .travis.yml 2016-08-27 21:33:40 +0900 (063755e) +++ .travis.yml 2016-08-27 21:49:12 +0900 (fac4e52) @@ -19,4 +19,5 @@ before_script: - sudo make install script: - rm -rf sql/jsonb + - rm -rf sql/function/flush/jsonb.sql - make installcheck || (cat regression.diffs; false) Modified: Makefile (+1 -0) =================================================================== --- Makefile 2016-08-27 21:33:40 +0900 (e4c60e4) +++ Makefile 2016-08-27 21:49:12 +0900 (1e942b8) @@ -7,6 +7,7 @@ SRCS = \ src/pgrn_column_name.c \ src/pgrn_convert.c \ src/pgrn_create.c \ + src/pgrn_flush.c \ src/pgrn_global.c \ src/pgrn_groonga.c \ src/pgrn_highlight_html.c \ Added: expected/function/flush/jsonb.out (+15 -0) 100644 =================================================================== --- /dev/null +++ expected/function/flush/jsonb.out 2016-08-27 21:49:12 +0900 (fd4c652) @@ -0,0 +1,15 @@ +CREATE TABLE logs ( + id int, + record jsonb +); +INSERT INTO logs VALUES (1, '{"message": "Hello World"}'); +INSERT INTO logs VALUES (2, '{"message": "This is a pen"}'); +INSERT INTO logs VALUES (3, '{"message": "Good-by World"}'); +CREATE INDEX pgroonga_index ON logs USING pgroonga (record); +SELECT pgroonga.flush('pgroonga_index'); + flush +------- + t +(1 row) + +DROP TABLE logs; Added: expected/function/flush/multiple.out (+19 -0) 100644 =================================================================== --- /dev/null +++ expected/function/flush/multiple.out 2016-08-27 21:49:12 +0900 (a0ebd48) @@ -0,0 +1,19 @@ +CREATE TABLE memos ( + id integer, + title text, + content text +); +INSERT INTO memos + VALUES (1, 'PostgreSQL', 'is a RDBMS.'); +INSERT INTO memos + VALUES (2, 'Groonga', 'is fast full text search engine.'); +INSERT INTO memos + VALUES (3, 'PGroonga', 'is a PostgreSQL extension that uses Groonga.'); +CREATE INDEX pgroonga_index ON memos USING pgroonga (title, content); +SELECT pgroonga.flush('pgroonga_index'); + flush +------- + t +(1 row) + +DROP TABLE memos; Added: expected/function/flush/nonexistent.out (+2 -0) 100644 =================================================================== --- /dev/null +++ expected/function/flush/nonexistent.out 2016-08-27 21:49:12 +0900 (a08efbe) @@ -0,0 +1,2 @@ +SELECT pgroonga.flush('pgroonga_index'); +ERROR: relation "pgroonga_index" does not exist Added: expected/function/flush/single.out (+15 -0) 100644 =================================================================== --- /dev/null +++ expected/function/flush/single.out 2016-08-27 21:49:12 +0900 (c8f8b3f) @@ -0,0 +1,15 @@ +CREATE TABLE memos ( + id integer, + content text +); +INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.'); +INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.'); +INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.'); +CREATE INDEX pgroonga_index ON memos USING pgroonga (content); +SELECT pgroonga.flush('pgroonga_index'); + flush +------- + t +(1 row) + +DROP TABLE memos; Modified: pgroonga--1.1.0--1.1.1.sql (+7 -0) =================================================================== --- pgroonga--1.1.0--1.1.1.sql 2016-08-27 21:33:40 +0900 (88cad6d) +++ pgroonga--1.1.0--1.1.1.sql 2016-08-27 21:49:12 +0900 (c2fbafc) @@ -4,3 +4,10 @@ CREATE FUNCTION pgroonga.match_positions_character(target text, keywords text[]) LANGUAGE C VOLATILE STRICT; + +CREATE FUNCTION pgroonga.flush(indexName cstring) + RETURNS bool + AS 'MODULE_PATHNAME', 'pgroonga_flush' + LANGUAGE C + VOLATILE + STRICT; Modified: pgroonga.sql (+7 -0) =================================================================== --- pgroonga.sql 2016-08-27 21:33:40 +0900 (053f859) +++ pgroonga.sql 2016-08-27 21:49:12 +0900 (2a1672e) @@ -58,6 +58,13 @@ CREATE FUNCTION pgroonga.query_extract_keywords(query text) VOLATILE STRICT; +CREATE FUNCTION pgroonga.flush(indexName cstring) + RETURNS bool + AS 'MODULE_PATHNAME', 'pgroonga_flush' + LANGUAGE C + VOLATILE + STRICT; + CREATE FUNCTION pgroonga.match_term(target text, term text) RETURNS bool AS 'MODULE_PATHNAME', 'pgroonga_match_term_text' Added: sql/function/flush/jsonb.sql (+14 -0) 100644 =================================================================== --- /dev/null +++ sql/function/flush/jsonb.sql 2016-08-27 21:49:12 +0900 (c5481d8) @@ -0,0 +1,14 @@ +CREATE TABLE logs ( + id int, + record jsonb +); + +INSERT INTO logs VALUES (1, '{"message": "Hello World"}'); +INSERT INTO logs VALUES (2, '{"message": "This is a pen"}'); +INSERT INTO logs VALUES (3, '{"message": "Good-by World"}'); + +CREATE INDEX pgroonga_index ON logs USING pgroonga (record); + +SELECT pgroonga.flush('pgroonga_index'); + +DROP TABLE logs; Added: sql/function/flush/multiple.sql (+18 -0) 100644 =================================================================== --- /dev/null +++ sql/function/flush/multiple.sql 2016-08-27 21:49:12 +0900 (4d93e2a) @@ -0,0 +1,18 @@ +CREATE TABLE memos ( + id integer, + title text, + content text +); + +INSERT INTO memos + VALUES (1, 'PostgreSQL', 'is a RDBMS.'); +INSERT INTO memos + VALUES (2, 'Groonga', 'is fast full text search engine.'); +INSERT INTO memos + VALUES (3, 'PGroonga', 'is a PostgreSQL extension that uses Groonga.'); + +CREATE INDEX pgroonga_index ON memos USING pgroonga (title, content); + +SELECT pgroonga.flush('pgroonga_index'); + +DROP TABLE memos; Added: sql/function/flush/nonexistent.sql (+1 -0) 100644 =================================================================== --- /dev/null +++ sql/function/flush/nonexistent.sql 2016-08-27 21:49:12 +0900 (301170c) @@ -0,0 +1 @@ +SELECT pgroonga.flush('pgroonga_index'); Added: sql/function/flush/single.sql (+14 -0) 100644 =================================================================== --- /dev/null +++ sql/function/flush/single.sql 2016-08-27 21:49:12 +0900 (079128e) @@ -0,0 +1,14 @@ +CREATE TABLE memos ( + id integer, + content text +); + +INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.'); +INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.'); +INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.'); + +CREATE INDEX pgroonga_index ON memos USING pgroonga (content); + +SELECT pgroonga.flush('pgroonga_index'); + +DROP TABLE memos; Added: src/pgrn_flush.c (+102 -0) 100644 =================================================================== --- /dev/null +++ src/pgrn_flush.c 2016-08-27 21:49:12 +0900 (d7943d1) @@ -0,0 +1,102 @@ +#include "pgroonga.h" + +#include "pgrn_global.h" +#include "pgrn_groonga.h" +#include "pgrn_jsonb.h" + +#include <storage/lockdefs.h> +#include <storage/lmgr.h> +#include <utils/builtins.h> + +static grn_ctx *ctx = &PGrnContext; + +PG_FUNCTION_INFO_V1(pgroonga_flush); + +/** + * pgroonga.flush(indexName cstring) : boolean + */ +Datum +pgroonga_flush(PG_FUNCTION_ARGS) +{ + Datum indexNameDatum = PG_GETARG_DATUM(0); + Datum indexOIDDatum; + Oid indexOID; + Relation index; + + indexOIDDatum = DirectFunctionCall1(regclassin, indexNameDatum); + if (!OidIsValid(indexOIDDatum)) + { + ereport(ERROR, + (errcode(ERRCODE_INVALID_NAME), + errmsg("pgroonga: nonexistent index name: <%s>", + DatumGetCString(indexNameDatum)))); + } + indexOID = DatumGetObjectId(indexOIDDatum); + + LockRelationOid(indexOID, AccessShareLock); + index = RelationIdGetRelation(indexOID); + if (!RelationIsValid(index)) + { + UnlockRelationOid(indexOID, AccessShareLock); + ereport(ERROR, + (errcode(ERRCODE_INVALID_NAME), + errmsg("pgroonga: failed to find index: <%s>", + DatumGetCString(indexNameDatum)))); + } + + PG_TRY(); + { + TupleDesc desc; + unsigned int i; + + PGrnFlushObject(PGrnLookupSourcesTable(index, ERROR), + true); + + desc = RelationGetDescr(index); + for (i = 0; i < desc->natts; i++) + { + Form_pg_attribute attribute; + + attribute = desc->attrs[i]; + if (PGrnAttributeIsJSONB(attribute->atttypid)) + { + PGrnFlushObject(PGrnJSONBLookupValuesTable(index, i, ERROR), + true); + PGrnFlushObject(PGrnJSONBLookupPathsTable(index, i, ERROR), + true); + PGrnFlushObject(PGrnJSONBLookupTypesTable(index, i, ERROR), + true); + + PGrnFlushObject( + PGrnJSONBLookupFullTextSearchLexicon(index, i, ERROR), + true); + PGrnFlushObject(PGrnJSONBLookupStringLexicon(index, i, ERROR), + true); + PGrnFlushObject(PGrnJSONBLookupNumberLexicon(index, i, ERROR), + true); + PGrnFlushObject(PGrnJSONBLookupBooleanLexicon(index, i, ERROR), + true); + PGrnFlushObject(PGrnJSONBLookupSizeLexicon(index, i, ERROR), + true); + } + else + { + PGrnFlushObject(PGrnLookupLexicon(index, i, ERROR), + true); + } + } + PGrnFlushObject(grn_ctx_db(ctx), false); + } + PG_CATCH(); + { + RelationClose(index); + UnlockRelationOid(indexOID, AccessShareLock); + PG_RE_THROW(); + } + PG_END_TRY(); + + RelationClose(index); + UnlockRelationOid(indexOID, AccessShareLock); + + PG_RETURN_BOOL(true); +} Modified: src/pgrn_groonga.c (+26 -0) =================================================================== --- src/pgrn_groonga.c 2016-08-27 21:33:40 +0900 (ba7b1c6) +++ src/pgrn_groonga.c 2016-08-27 21:49:12 +0900 (399c9e1) @@ -219,3 +219,29 @@ PGrnRemoveObject(const char *name) return false; } } + +void +PGrnFlushObject(grn_obj *object, bool recursive) +{ + grn_rc rc; + char name[GRN_TABLE_MAX_KEY_SIZE]; + int name_size; + + if (recursive) + { + rc = grn_obj_flush_recursive(ctx, object); + } + else + { + rc = grn_obj_flush(ctx, object); + } + if (rc == GRN_SUCCESS) + return; + + name_size = grn_obj_name(ctx, object, name, GRN_TABLE_MAX_KEY_SIZE); + ereport(ERROR, + (errcode(PGrnRCToPgErrorCode(rc)), + errmsg("pgroonga: failed to flush: <%.*s>: %s", + name_size, name, + ctx->errbuf))); +} Modified: src/pgrn_groonga.h (+2 -0) =================================================================== --- src/pgrn_groonga.h 2016-08-27 21:33:40 +0900 (247308e) +++ src/pgrn_groonga.h 2016-08-27 21:49:12 +0900 (8699ab6) @@ -36,3 +36,5 @@ grn_obj *PGrnCreateColumn(grn_obj *table, void PGrnIndexColumnSetSource(grn_obj *indexColumn, grn_obj *source); bool PGrnRemoveObject(const char *name); + +void PGrnFlushObject(grn_obj *object, bool recursive); Modified: src/pgroonga.h (+1 -0) =================================================================== --- src/pgroonga.h 2016-08-27 21:33:40 +0900 (d0b8fe7) +++ src/pgroonga.h 2016-08-27 21:49:12 +0900 (8a0094a) @@ -64,6 +64,7 @@ extern Datum PGDLLEXPORT pgroonga_highlight_html(PG_FUNCTION_ARGS); extern Datum PGDLLEXPORT pgroonga_match_positions_byte(PG_FUNCTION_ARGS); extern Datum PGDLLEXPORT pgroonga_match_positions_character(PG_FUNCTION_ARGS); extern Datum PGDLLEXPORT pgroonga_query_extract_keywords(PG_FUNCTION_ARGS); +extern Datum PGDLLEXPORT pgroonga_flush(PG_FUNCTION_ARGS); extern Datum PGDLLEXPORT pgroonga_match_term_text(PG_FUNCTION_ARGS); extern Datum PGDLLEXPORT pgroonga_match_term_text_array(PG_FUNCTION_ARGS); -------------- next part -------------- HTML����������������������������... 下載