Kouhei Sutou
null+****@clear*****
Sun Apr 24 00:56:53 JST 2016
Kouhei Sutou 2016-04-24 00:56:53 +0900 (Sun, 24 Apr 2016) New Revision: dcc859721bf1a8498775c13324848905f9bb8bfa https://github.com/pgroonga/pgroonga/commit/dcc859721bf1a8498775c13324848905f9bb8bfa Message: AddAdd pgroonga.query_extract_keywords Added files: expected/function/query-extract-keywords/and-not.out expected/function/query-extract-keywords/multiple.out expected/function/query-extract-keywords/one.out sql/function/query-extract-keywords/and-not.sql sql/function/query-extract-keywords/multiple.sql sql/function/query-extract-keywords/one.sql src/pgrn_query_extract_keywords.c src/pgrn_query_extract_keywords.h Modified files: CMakeLists.txt Makefile pgroonga--1.0.6--1.0.7.sql pgroonga.sql src/pgroonga.c Modified: CMakeLists.txt (+1 -0) =================================================================== --- CMakeLists.txt 2016-04-24 00:52:04 +0900 (c0cd25a) +++ CMakeLists.txt 2016-04-24 00:56:53 +0900 (363a753) @@ -67,6 +67,7 @@ set(PGRN_SOURCES "src/pgrn_jsonb.c" "src/pgrn_match_positions_byte.c" "src/pgrn_options.c" + "src/pgrn_query_extract_keywords.c" "src/pgrn_snippet_html.c" "src/pgrn_value.c" "src/pgrn_variables.c" Modified: Makefile (+1 -0) =================================================================== --- Makefile 2016-04-24 00:52:04 +0900 (00acf9e) +++ Makefile 2016-04-24 00:56:53 +0900 (c349991) @@ -14,6 +14,7 @@ SRCS = \ src/pgrn_jsonb.c \ src/pgrn_match_positions_byte.c \ src/pgrn_options.c \ + src/pgrn_query_extract_keywords.c \ src/pgrn_snippet_html.c \ src/pgrn_value.c \ src/pgrn_variables.c \ Added: expected/function/query-extract-keywords/and-not.out (+6 -0) 100644 =================================================================== --- /dev/null +++ expected/function/query-extract-keywords/and-not.out 2016-04-24 00:56:53 +0900 (e4e92f5) @@ -0,0 +1,6 @@ +SELECT pgroonga.query_extract_keywords('Groonga -MySQL PostgreSQL'); + query_extract_keywords +------------------------ + {PostgreSQL,Groonga} +(1 row) + Added: expected/function/query-extract-keywords/multiple.out (+6 -0) 100644 =================================================================== --- /dev/null +++ expected/function/query-extract-keywords/multiple.out 2016-04-24 00:56:53 +0900 (da6b101) @@ -0,0 +1,6 @@ +SELECT pgroonga.query_extract_keywords('Groonga PostgreSQL'); + query_extract_keywords +------------------------ + {PostgreSQL,Groonga} +(1 row) + Added: expected/function/query-extract-keywords/one.out (+6 -0) 100644 =================================================================== --- /dev/null +++ expected/function/query-extract-keywords/one.out 2016-04-24 00:56:53 +0900 (f18c1e2) @@ -0,0 +1,6 @@ +SELECT pgroonga.query_extract_keywords('Groonga'); + query_extract_keywords +------------------------ + {Groonga} +(1 row) + Modified: pgroonga--1.0.6--1.0.7.sql (+7 -0) =================================================================== --- pgroonga--1.0.6--1.0.7.sql 2016-04-24 00:52:04 +0900 (2bba1ef) +++ pgroonga--1.0.6--1.0.7.sql 2016-04-24 00:56:53 +0900 (da189fe) @@ -11,3 +11,10 @@ CREATE FUNCTION pgroonga.match_positions_byte(target text, keywords text[]) LANGUAGE C VOLATILE STRICT; + +CREATE FUNCTION pgroonga.query_extract_keywords(query text) + RETURNS text[] + AS 'MODULE_PATHNAME', 'pgroonga_query_extract_keywords' + LANGUAGE C + VOLATILE + STRICT; Modified: pgroonga.sql (+7 -0) =================================================================== --- pgroonga.sql 2016-04-24 00:52:04 +0900 (5018fd3) +++ pgroonga.sql 2016-04-24 00:56:53 +0900 (8d87520) @@ -44,6 +44,13 @@ CREATE FUNCTION pgroonga.match_positions_byte(target text, keywords text[]) VOLATILE STRICT; +CREATE FUNCTION pgroonga.query_extract_keywords(query text) + RETURNS text[] + AS 'MODULE_PATHNAME', 'pgroonga_query_extract_keywords' + 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/query-extract-keywords/and-not.sql (+1 -0) 100644 =================================================================== --- /dev/null +++ sql/function/query-extract-keywords/and-not.sql 2016-04-24 00:56:53 +0900 (b749d57) @@ -0,0 +1 @@ +SELECT pgroonga.query_extract_keywords('Groonga -MySQL PostgreSQL'); Added: sql/function/query-extract-keywords/multiple.sql (+1 -0) 100644 =================================================================== --- /dev/null +++ sql/function/query-extract-keywords/multiple.sql 2016-04-24 00:56:53 +0900 (f9fd933) @@ -0,0 +1 @@ +SELECT pgroonga.query_extract_keywords('Groonga PostgreSQL'); Added: sql/function/query-extract-keywords/one.sql (+1 -0) 100644 =================================================================== --- /dev/null +++ sql/function/query-extract-keywords/one.sql 2016-04-24 00:56:53 +0900 (1c1d611) @@ -0,0 +1 @@ +SELECT pgroonga.query_extract_keywords('Groonga'); Added: src/pgrn_query_extract_keywords.c (+140 -0) 100644 =================================================================== --- /dev/null +++ src/pgrn_query_extract_keywords.c 2016-04-24 00:56:53 +0900 (0160f19) @@ -0,0 +1,140 @@ +#include "pgroonga.h" + +#include "pgrn_global.h" +#include "pgrn_groonga.h" +#include "pgrn_query_extract_keywords.h" + +#include <catalog/pg_type.h> +#include <utils/array.h> +#include <utils/builtins.h> + +static grn_ctx *ctx = &PGrnContext; +static grn_obj *table = NULL; +static grn_obj *textColumn = NULL; + +PG_FUNCTION_INFO_V1(pgroonga_query_extract_keywords); + +void +PGrnInitializeQueryExtractKeywords(void) +{ + table = grn_table_create(ctx, NULL, 0, NULL, + GRN_OBJ_TABLE_NO_KEY, + NULL, + NULL); + textColumn = grn_column_create(ctx, + table, + "text", strlen("text"), + NULL, + GRN_OBJ_COLUMN_SCALAR, + grn_ctx_at(ctx, GRN_DB_TEXT)); +} + +void +PGrnFinalizeQueryExtractKeywords(void) +{ + if (textColumn) + { + grn_obj_close(ctx, textColumn); + textColumn = NULL; + } + + if (table) + { + grn_obj_close(ctx, table); + table = NULL; + } +} + +static ArrayType * +PGrnQueryExtractKeywords(text *query) +{ + grn_obj *expression; + grn_obj *variable; + grn_expr_flags flags = + GRN_EXPR_SYNTAX_QUERY | GRN_EXPR_ALLOW_LEADING_NOT; + grn_rc rc; + ArrayType *keywords; + + GRN_EXPR_CREATE_FOR_QUERY(ctx, table, expression, variable); + if (!expression) + { + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("pgroonga: query_extract_keywords: " + "failed to create expression: %s", + ctx->errbuf))); + } + + rc = grn_expr_parse(ctx, + expression, + VARDATA_ANY(query), + VARSIZE_ANY_EXHDR(query), + textColumn, + GRN_OP_MATCH, + GRN_OP_AND, + flags); + if (rc != GRN_SUCCESS) + { + char message[GRN_CTX_MSGSIZE]; + grn_strncpy(message, GRN_CTX_MSGSIZE, + ctx->errbuf, GRN_CTX_MSGSIZE); + + grn_obj_close(ctx, expression); + ereport(ERROR, + (errcode(PGrnRCToPgErrorCode(rc)), + errmsg("pgroonga: query_extract_keywords: " + "failed to parse expression: %s", + message))); + } + + { + size_t i, nKeywords; + grn_obj extractedKeywords; + Datum *elements; + int dims[1]; + int lbs[1]; + + GRN_PTR_INIT(&extractedKeywords, GRN_OBJ_VECTOR, GRN_ID_NIL); + grn_expr_get_keywords(ctx, expression, &extractedKeywords); + nKeywords = GRN_BULK_VSIZE(&extractedKeywords) / sizeof(grn_obj *); + elements = palloc(sizeof(Datum) * nKeywords); + for (i = 0; i < nKeywords; i++) { + grn_obj *extractedKeyword; + text *keyword; + + extractedKeyword = GRN_PTR_VALUE_AT(&extractedKeywords, i); + keyword = cstring_to_text_with_len(GRN_TEXT_VALUE(extractedKeyword), + GRN_TEXT_LEN(extractedKeyword)); + elements[i] = PointerGetDatum(keyword); + } + dims[0] = nKeywords; + lbs[0] = 1; + keywords = construct_md_array(elements, + NULL, + 1, + dims, + lbs, + TEXTOID, + -1, + false, + 'i'); + + GRN_OBJ_FIN(ctx, &extractedKeywords); + } + + return keywords; +} + +/** + * pgroonga.query_extract_keywords(query text) : text[] + */ +Datum +pgroonga_query_extract_keywords(PG_FUNCTION_ARGS) +{ + text *query = PG_GETARG_TEXT_PP(0); + ArrayType *keywords; + + keywords = PGrnQueryExtractKeywords(query); + + PG_RETURN_POINTER(keywords); +} Added: src/pgrn_query_extract_keywords.h (+4 -0) 100644 =================================================================== --- /dev/null +++ src/pgrn_query_extract_keywords.h 2016-04-24 00:56:53 +0900 (a27c25c) @@ -0,0 +1,4 @@ +#pragma once + +void PGrnInitializeQueryExtractKeywords(void); +void PGrnFinalizeQueryExtractKeywords(void); Modified: src/pgroonga.c (+5 -0) =================================================================== --- src/pgroonga.c 2016-04-24 00:52:04 +0900 (5d1219f) +++ src/pgroonga.c 2016-04-24 00:56:53 +0900 (2f2915e) @@ -11,6 +11,7 @@ #include "pgrn_jsonb.h" #include "pgrn_match_positions_byte.h" #include "pgrn_options.h" +#include "pgrn_query_extract_keywords.h" #include "pgrn_search.h" #include "pgrn_value.h" #include "pgrn_variables.h" @@ -263,6 +264,8 @@ PGrnOnProcExit(int code, Datum arg) { grn_obj *db; + PGrnFinalizeQueryExtractKeywords(); + PGrnFinalizeMatchPositionsByte(); PGrnFinalizeHighlightHTML(); @@ -378,6 +381,8 @@ _PG_init(void) PGrnInitializeHighlightHTML(); PGrnInitializeMatchPositionsByte(); + + PGrnInitializeQueryExtractKeywords(); } static grn_id -------------- next part -------------- HTML����������������������������...Télécharger