Kouhei Sutou
null+****@clear*****
Thu Jul 6 11:59:55 JST 2017
Kouhei Sutou 2017-07-06 11:59:55 +0900 (Thu, 06 Jul 2017) New Revision: 922f84fb2e7cf07d7c54233e4ac5fdc108685c0d https://github.com/pgroonga/pgroonga/commit/922f84fb2e7cf07d7c54233e4ac5fdc108685c0d Message: query-expand: prefer PGroonga index Modified files: src/pgrn-query-expand.c src/pgroonga.c src/pgroonga.h Modified: src/pgrn-query-expand.c (+47 -20) =================================================================== --- src/pgrn-query-expand.c 2017-07-06 10:25:45 +0900 (16d2a71) +++ src/pgrn-query-expand.c 2017-07-06 11:59:55 +0900 (6c19ce4) @@ -206,22 +206,23 @@ PGrnFindSynonymsAttribute(const char *tableName, } static Relation -PGrnFindTargetIndex(Relation table, - const char *columnName, - size_t columnNameSize, - Oid opNo, - AttrNumber *indexAttributeNumber, - StrategyNumber *strategy) +PGrnFindTermIndex(Relation table, + const char *columnName, + size_t columnNameSize, + Oid opNo, + AttrNumber *indexAttributeNumber, + StrategyNumber *indexStrategy) { - Relation index = InvalidRelation; + Relation termIndex = InvalidRelation; + Relation preferedIndex = InvalidRelation; List *indexOIDList; ListCell *cell; indexOIDList = RelationGetIndexList(table); foreach(cell, indexOIDList) { + Relation index = InvalidRelation; Oid indexOID = lfirst_oid(cell); - bool isTargetIndex = false; int i; index = index_open(indexOID, NoLock); @@ -229,6 +230,7 @@ PGrnFindTargetIndex(Relation table, { const char *name = index->rd_att->attrs[i - 1]->attname.data; Oid opFamily; + StrategyNumber strategy; if (strlen(name) != columnNameSize) continue; @@ -237,24 +239,49 @@ PGrnFindTargetIndex(Relation table, continue; opFamily = index->rd_opfamily[i - 1]; - *strategy = get_op_opfamily_strategy(opNo, opFamily); - if (*strategy == InvalidStrategy) + strategy = get_op_opfamily_strategy(opNo, opFamily); + if (strategy == InvalidStrategy) continue; - *indexAttributeNumber = i; - isTargetIndex = true; + if (PGrnIndexIsPGroonga(index)) + { + preferedIndex = index; + *indexStrategy = strategy; + *indexAttributeNumber = i; + break; + } + + if (!RelationIsValid(termIndex)) + { + termIndex = index; + *indexStrategy = strategy; + *indexAttributeNumber = i; + } + break; } - if (isTargetIndex) + if (RelationIsValid(preferedIndex)) break; + if (termIndex == index) + continue; + index_close(index, NoLock); index = InvalidRelation; } list_free(indexOIDList); - return index; + if (RelationIsValid(preferedIndex)) + { + if (RelationIsValid(termIndex) && termIndex != preferedIndex) + index_close(termIndex, NoLock); + return preferedIndex; + } + else + { + return termIndex; + } } static AttrNumber @@ -335,12 +362,12 @@ pgroonga_query_expand(PG_FUNCTION_ARGS) VARDATA_ANY(synonymsColumnName), VARSIZE_ANY_EXHDR(synonymsColumnName)); - index = PGrnFindTargetIndex(currentData.table, - VARDATA_ANY(termColumnName), - VARSIZE_ANY_EXHDR(termColumnName), - opNo, - &(currentData.termAttributeNumber), - &(currentData.scanStrategy)); + index = PGrnFindTermIndex(currentData.table, + VARDATA_ANY(termColumnName), + VARSIZE_ANY_EXHDR(termColumnName), + opNo, + &(currentData.termAttributeNumber), + &(currentData.scanStrategy)); if (!index) currentData.termAttributeNumber = PGrnFindTermAttributeNumber(DatumGetCString(tableNameDatum), Modified: src/pgroonga.c (+14 -0) =================================================================== --- src/pgroonga.c 2017-07-06 10:25:45 +0900 (ec6bee9) +++ src/pgroonga.c 2017-07-06 11:59:55 +0900 (fed5c66) @@ -5013,3 +5013,17 @@ pgroonga_handler(PG_FUNCTION_ARGS) PG_RETURN_POINTER(routine); } #endif + +#ifdef PGRN_SUPPORT_CREATE_ACCESS_METHOD +bool +PGrnIndexIsPGroonga(Relation index) +{ + return index->rd_amroutine->ambuild == pgroonga_build_raw; +} +#else +bool +PGrnIndexIsPGroonga(Relation index) +{ + return false; +} +#endif Modified: src/pgroonga.h (+2 -0) =================================================================== --- src/pgroonga.h 2017-07-06 10:25:45 +0900 (e200137) +++ src/pgroonga.h 2017-07-06 11:59:55 +0900 (3ed6a73) @@ -2,6 +2,7 @@ #include <postgres.h> #include <fmgr.h> +#include <utils/rel.h> /* Default values */ #ifndef PGRN_DEFAULT_TOKENIZER @@ -73,3 +74,4 @@ #define PGrnIndexColumnName "index" extern bool PGrnGroongaInitialized; +bool PGrnIndexIsPGroonga(Relation index); -------------- next part -------------- HTML����������������������������...Download