[Groonga-commit] pgroonga/pgroonga at a4b9205 [master] Extract keyword table update code

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Sat Apr 23 23:11:05 JST 2016


Kouhei Sutou	2016-04-23 23:11:05 +0900 (Sat, 23 Apr 2016)

  New Revision: a4b9205c72be3b3a6c529b6e8cccbdc7f46571d8
  https://github.com/pgroonga/pgroonga/commit/a4b9205c72be3b3a6c529b6e8cccbdc7f46571d8

  Message:
    Extract keyword table update code

  Added files:
    src/pgrn_keywords.c
    src/pgrn_keywords.h
  Modified files:
    CMakeLists.txt
    Makefile
    src/pgrn_highlight_html.c
    src/pgroonga.c

  Modified: CMakeLists.txt (+1 -0)
===================================================================
--- CMakeLists.txt    2016-04-23 23:00:48 +0900 (520aa6c)
+++ CMakeLists.txt    2016-04-23 23:11:05 +0900 (596d756)
@@ -63,6 +63,7 @@ set(PGRN_SOURCES
   "src/pgrn_global.c"
   "src/pgrn_groonga.c"
   "src/pgrn_highlight_html.c"
+  "src/pgrn_keywords.c"
   "src/pgrn_jsonb.c"
   "src/pgrn_options.c"
   "src/pgrn_snippet_html.c"

  Modified: Makefile (+1 -0)
===================================================================
--- Makefile    2016-04-23 23:00:48 +0900 (b2e5e1d)
+++ Makefile    2016-04-23 23:11:05 +0900 (25f8a3d)
@@ -10,6 +10,7 @@ SRCS =						\
 	src/pgrn_global.c			\
 	src/pgrn_groonga.c			\
 	src/pgrn_highlight_html.c		\
+	src/pgrn_keywords.c			\
 	src/pgrn_jsonb.c			\
 	src/pgrn_options.c			\
 	src/pgrn_snippet_html.c			\

  Modified: src/pgrn_highlight_html.c (+2 -84)
===================================================================
--- src/pgrn_highlight_html.c    2016-04-23 23:00:48 +0900 (966eb4a)
+++ src/pgrn_highlight_html.c    2016-04-23 23:11:05 +0900 (f3dc80a)
@@ -3,14 +3,13 @@
 #include "pgrn_global.h"
 #include "pgrn_groonga.h"
 #include "pgrn_highlight_html.h"
+#include "pgrn_keywords.h"
 
 #include <catalog/pg_type.h>
-#include <utils/array.h>
 #include <utils/builtins.h>
 
 static grn_ctx *ctx = &PGrnContext;
 static grn_obj *keywordsTable = NULL;
-static grn_obj keywordIDs;
 
 PG_FUNCTION_INFO_V1(pgroonga_highlight_html);
 
@@ -25,10 +24,6 @@ PGrnInitializeHighlightHTML(void)
 					 keywordsTable,
 					 GRN_INFO_NORMALIZER,
 					 grn_ctx_get(ctx, "NormalizerAuto", -1));
-
-	GRN_RECORD_INIT(&keywordIDs,
-					GRN_OBJ_VECTOR,
-					grn_obj_id(ctx, keywordsTable));
 }
 
 void
@@ -37,87 +32,10 @@ PGrnFinalizeHighlightHTML(void)
 	if (!keywordsTable)
 		return;
 
-	GRN_OBJ_FIN(ctx, &keywordIDs);
-
 	grn_obj_close(ctx, keywordsTable);
 	keywordsTable = NULL;
 }
 
-static void
-PGrnKeywordsTableUpdate(ArrayType *keywords)
-{
-	{
-		int i, n;
-
-		GRN_BULK_REWIND(&keywordIDs);
-
-		n = ARR_DIMS(keywords)[0];
-		for (i = 1; i <= n; i++)
-		{
-			Datum keywordDatum;
-			text *keyword;
-			bool isNULL;
-			grn_id id;
-
-			keywordDatum = array_ref(keywords, 1, &i, -1, -1, false,
-									 'i', &isNULL);
-			if (isNULL)
-				continue;
-
-			keyword = DatumGetTextPP(keywordDatum);
-			id = grn_table_add(ctx, keywordsTable,
-							   VARDATA_ANY(keyword),
-							   VARSIZE_ANY_EXHDR(keyword),
-							   NULL);
-			if (id == GRN_ID_NIL)
-				continue;
-			GRN_RECORD_PUT(ctx, &keywordIDs, id);
-		}
-	}
-
-	{
-		grn_table_cursor *cursor;
-		grn_id id;
-		size_t nIDs;
-
-		cursor = grn_table_cursor_open(ctx,
-									   keywordsTable,
-									   NULL, 0,
-									   NULL, 0,
-									   0, -1, 0);
-		if (!cursor) {
-			ereport(ERROR,
-					(errcode(ERRCODE_OUT_OF_MEMORY),
-					 errmsg("pgroonga: "
-							"failed to create cursor for keywordsTable: %s",
-							ctx->errbuf)));
-		}
-
-		nIDs = GRN_BULK_VSIZE(&keywordIDs) / sizeof(grn_id);
-		while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL)
-		{
-			size_t i;
-			bool specified = false;
-
-			for (i = 0; i < nIDs; i++)
-			{
-				if (id == GRN_RECORD_VALUE_AT(&keywordIDs, i))
-				{
-					specified = true;
-					break;
-				}
-			}
-
-			if (specified)
-				continue;
-
-			grn_table_cursor_delete(ctx, cursor);
-		}
-
-		grn_table_cursor_close(ctx, cursor);
-	}
-}
-
 static text *
 PGrnHighlightHTML(text *target)
 {
@@ -193,7 +111,7 @@ pgroonga_highlight_html(PG_FUNCTION_ARGS)
 	ArrayType *keywords = PG_GETARG_ARRAYTYPE_P(1);
 	text *highlighted;
 
-	PGrnKeywordsTableUpdate(keywords);
+	PGrnKeywordsUpdateTable(keywords, keywordsTable);
 	highlighted = PGrnHighlightHTML(target);
 
 	PG_RETURN_TEXT_P(highlighted);

  Added: src/pgrn_keywords.c (+100 -0) 100644
===================================================================
--- /dev/null
+++ src/pgrn_keywords.c    2016-04-23 23:11:05 +0900 (6495cee)
@@ -0,0 +1,100 @@
+#include "pgroonga.h"
+
+#include "pgrn_global.h"
+#include "pgrn_groonga.h"
+#include "pgrn_keywords.h"
+
+#include <catalog/pg_type.h>
+#include <utils/builtins.h>
+
+static grn_ctx *ctx = &PGrnContext;
+static grn_obj keywordIDs;
+
+void
+PGrnInitializeKeywords(void)
+{
+	GRN_RECORD_INIT(&keywordIDs, GRN_OBJ_VECTOR, GRN_ID_NIL);
+}
+
+void
+PGrnFinalizeKeywords(void)
+{
+	GRN_OBJ_FIN(ctx, &keywordIDs);
+}
+
+void
+PGrnKeywordsUpdateTable(ArrayType *keywords, grn_obj *keywordsTable)
+{
+	{
+		int i, n;
+
+		GRN_BULK_REWIND(&keywordIDs);
+
+		n = ARR_DIMS(keywords)[0];
+		for (i = 1; i <= n; i++)
+		{
+			Datum keywordDatum;
+			text *keyword;
+			bool isNULL;
+			grn_id id;
+
+			keywordDatum = array_ref(keywords, 1, &i, -1, -1, false,
+									 'i', &isNULL);
+			if (isNULL)
+				continue;
+
+			keyword = DatumGetTextPP(keywordDatum);
+			id = grn_table_add(ctx, keywordsTable,
+							   VARDATA_ANY(keyword),
+							   VARSIZE_ANY_EXHDR(keyword),
+							   NULL);
+			if (id == GRN_ID_NIL)
+				continue;
+			GRN_RECORD_PUT(ctx, &keywordIDs, id);
+		}
+	}
+
+	{
+		grn_table_cursor *cursor;
+		grn_id id;
+		size_t nIDs;
+
+		cursor = grn_table_cursor_open(ctx,
+									   keywordsTable,
+									   NULL, 0,
+									   NULL, 0,
+									   0, -1, 0);
+		if (!cursor) {
+			ereport(ERROR,
+					(errcode(ERRCODE_OUT_OF_MEMORY),
+					 errmsg("pgroonga: "
+							"failed to create cursor for keywordsTable: %s",
+							ctx->errbuf)));
+		}
+
+		nIDs = GRN_BULK_VSIZE(&keywordIDs) / sizeof(grn_id);
+		while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL)
+		{
+			size_t i;
+			bool specified = false;
+
+			for (i = 0; i < nIDs; i++)
+			{
+				if (id == GRN_RECORD_VALUE_AT(&keywordIDs, i))
+				{
+					specified = true;
+					break;
+				}
+			}
+
+			if (specified)
+				continue;
+
+			grn_table_cursor_delete(ctx, cursor);
+		}
+
+		grn_table_cursor_close(ctx, cursor);
+	}
+}
+
+

  Added: src/pgrn_keywords.h (+12 -0) 100644
===================================================================
--- /dev/null
+++ src/pgrn_keywords.h    2016-04-23 23:11:05 +0900 (52ed791)
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <postgres.h>
+
+#include <groonga.h>
+
+#include <utils/array.h>
+
+void PGrnInitializeKeywords(void);
+void PGrnFinalizeKeywords(void);
+
+void PGrnKeywordsUpdateTable(ArrayType *keywords, grn_obj *keywordsTable);

  Modified: src/pgroonga.c (+5 -0)
===================================================================
--- src/pgroonga.c    2016-04-23 23:00:48 +0900 (f83f114)
+++ src/pgroonga.c    2016-04-23 23:11:05 +0900 (d51d863)
@@ -7,6 +7,7 @@
 #include "pgrn_global.h"
 #include "pgrn_groonga.h"
 #include "pgrn_highlight_html.h"
+#include "pgrn_keywords.h"
 #include "pgrn_jsonb.h"
 #include "pgrn_options.h"
 #include "pgrn_search.h"
@@ -263,6 +264,8 @@ PGrnOnProcExit(int code, Datum arg)
 
 		PGrnFinalizeHighlightHTML();
 
+		PGrnFinalizeKeywords();
+
 		PGrnFinalizeJSONB();
 
 		PGrnFinalizeMatchSequentialSearchData();
@@ -367,6 +370,8 @@ _PG_init(void)
 
 	PGrnInitializeJSONB();
 
+	PGrnInitializeKeywords();
+
 	PGrnInitializeHighlightHTML();
 }
 
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index