1 /*-------------------------------------------------------------------------
3 * Copyright (c) 2006-2015, NTT DATA Corporation
8 * Update Ludia functions so that they are available with PostgreSQL9.1.
9 * Author: NTT DATA Corporation
11 *-------------------------------------------------------------------------
16 #include <sys/types.h>
20 #include "catalog/pg_type.h"
23 #include "ludia_funcs.h"
24 #include "mb/pg_wchar.h"
26 #include "storage/fd.h"
27 #include "utils/builtins.h"
28 #include "utils/guc.h"
29 #include "miscadmin.h"
31 #if PG_VERSION_NUM >= 90300
32 #include "access/htup_details.h"
37 /* Last update date of ludia_funcs */
38 #define PGS2_LAST_UPDATE "2015.09.10"
42 static bool pgs2_enable_debug = false;
44 static char *pgs2_last_update = NULL;
45 static int norm_cache_limit = -1;
46 static bool escape_snippet_keyword = false;
48 #define SEN_NORMALIZE_FLAGS 0
49 #define SEN_MAX_N_EXPRS 32
51 /* upper limit for GUC variables measured in kilobytes of memory */
52 /* note that various places assume the byte size fits in a "long" variable */
53 #if SIZEOF_SIZE_T > 4 && SIZEOF_LONG > 4
54 #define MAX_KILOBYTES INT_MAX
56 #define MAX_KILOBYTES (INT_MAX / 1024)
59 #define ISBACKSLASHCHAR(x) (*(x) == '\\')
60 #define ISDOUBLEQUOTECHAR(x) (*(x) == '"')
61 #define ISSENNAOPSCHAR(x) (*(x) == '+' || *(x) == '-' || *(x) == ' ')
63 PG_FUNCTION_INFO_V1(pgs2snippet1);
64 PG_FUNCTION_INFO_V1(pgs2norm);
65 PG_FUNCTION_INFO_V1(pgs2textporter1);
66 PG_FUNCTION_INFO_V1(pgs2seninfo);
69 * The function prototypes are created as a part of PG_FUNCTION_INFO_V1
70 * macro since 9.4, and hence the declaration of the function prototypes
71 * here is necessary only for 9.3 or before.
73 #if PG_VERSION_NUM < 90400
74 Datum pgs2snippet1(PG_FUNCTION_ARGS);
75 Datum pgs2norm(PG_FUNCTION_ARGS);
76 Datum pgs2textporter1(PG_FUNCTION_ARGS);
77 Datum pgs2seninfo(PG_FUNCTION_ARGS);
80 static sen_encoding GetSennaEncoding(void);
81 static sen_query *GetSennaQuery(char *str, size_t len);
82 static bool EscapeSnippetKeyword(char **s, size_t *slen);
85 #define TEXTPORTER_TMPDIR "/tmp"
86 #define TEXTPORTER_MKSTEMP_UMASK 0177
87 #define TEXTPORTER_GROUPNAME "UTF-8"
88 #define TEXTPORTER_DEFLANGNAME "Japanese"
89 #define TEXTPORTER_BBIGENDIAN 1
90 #define TEXTPORTER_OPTION 0x00000020 /* DMC_GETTEXT_OPT_LF */
91 #define TEXTPORTER_OPTION_STRING "32"
92 #define TEXTPORTER_OPTION1 0x00010000 /* DMC_GETTEXT_OPT1_TXCONV */
93 #define TEXTPORTER_SIZE 0
94 #define TEXTPORTER_CSV_C 0
97 /* GUC variables for pgs2textpoter1 */
98 static int textporter_error = ERROR;
99 static unsigned int textporter_option = TEXTPORTER_OPTION;
102 * This variable is a dummy that doesn't do anything, except in some
103 * cases provides the value for SHOW to display. The real state is
104 * elsewhere and is kept in sync by assign_hooks.
106 static char *textporter_option_string;
108 static const struct config_enum_entry textporter_error_options[] = {
109 {"debug1", DEBUG1, false},
111 {"info", INFO, false},
112 {"notice", NOTICE, false},
113 {"warning", WARNING, false},
114 {"error", ERROR, false},
118 static void CleanupTextPorterTmpFiles(void);
120 static bool check_textporter_option(char **newval, void **extra, GucSource source);
121 static void assign_textporter_option(const char *newval, void *extra);
122 #endif /* TEXTPORTER */
133 /* Define custom GUC variable for debugging */
134 DefineCustomBoolVariable("ludia_funcs.enable_debug",
135 "Emit ludia_funcs debugging output.",
146 /* Can't be set in postgresql.conf */
147 DefineCustomStringVariable("ludia_funcs.last_update",
148 "Shows the last update date of ludia_funcs.",
153 GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE,
159 /* Define custom GUC variables */
160 DefineCustomEnumVariable("ludia_funcs.textporter_error",
161 "Sets the message levels that are emitted "
162 "when textporter fails.",
166 textporter_error_options,
173 DefineCustomStringVariable("ludia_funcs.textporter_option",
174 "Sets the option used to get text data "
177 &textporter_option_string,
178 TEXTPORTER_OPTION_STRING,
181 check_textporter_option,
182 assign_textporter_option,
185 /* Clean up remaining textporter temporary files */
186 CleanupTextPorterTmpFiles();
187 #endif /* TEXTPORTER */
190 * A value of 0 means no limit on the cache size. A value of -1 means
191 * that work_mem is used as the upper size limit of the cache.
193 DefineCustomIntVariable("ludia_funcs.norm_cache_limit",
194 "Sets the maximum memory to be used for caching "
195 "the result of pgs2norm()",
207 DefineCustomBoolVariable("ludia_funcs.escape_snippet_keyword",
208 "Escapes snippet keyword string.",
210 &escape_snippet_keyword,
218 EmitWarningsOnPlaceholders("ludia_funcs");
220 /* Initialize Senna */
222 if (rc != sen_success)
224 (errmsg("sen_init() failed: %d", rc)));
233 #define REMOVE_TMPFILE(path) \
235 if (unlink(path) != 0) \
237 (errcode_for_file_access(), \
238 errmsg("could not remove temporary file \"%s\": %m", path))); \
242 pgs2textporter1(PG_FUNCTION_ARGS)
244 char *appfile = text_to_cstring(PG_GETARG_TEXT_P(0));
245 char txtfile[] = TEXTPORTER_TMPDIR "/ludia_funcs_XXXXXX";
251 bool return_null = false;
254 /* Confirm that database encoding is UTF-8 */
260 * Generate a unique temporary filename where text data gotten
261 * from application file by TextPorter is stored temporarily.
262 * Set the permission of a temporary file to 0600 to ensure that
263 * only the owner of PostgreSQL server can read and write the file.
265 oumask = umask(TEXTPORTER_MKSTEMP_UMASK);
266 tmpfd = mkstemp(txtfile);
271 (errcode_for_file_access(),
272 errmsg("could not generate a unique temporary filename: %m")));
273 if (close(tmpfd) != 0)
275 (errcode_for_file_access(),
276 errmsg("could not close temporary file \"%s\": %m", txtfile)));
279 * Run TextPorter to read text data from application file (appfile)
280 * to temporary file (txtfile).
282 ret = ExecTextPorter((unsigned char *)appfile,
283 (unsigned char *)txtfile,
284 (unsigned char *)TEXTPORTER_GROUPNAME,
285 (unsigned char *)TEXTPORTER_DEFLANGNAME,
286 TEXTPORTER_BBIGENDIAN, textporter_option,
287 TEXTPORTER_OPTION1, TEXTPORTER_SIZE,
291 ereport(textporter_error,
292 (errmsg("could not get text from application file \"%s\"",
294 errdetail("DMC_GetText_V5() failed with errcode %d",
297 /* Return NULL if textporter_error is set to other than ERROR */
302 /* Read text data from temporary file to memory */
303 if (stat(txtfile, &statbuf))
305 (errcode_for_file_access(),
306 errmsg("could not stat file \"%s\": %m", txtfile)));
307 result = (text *) palloc(statbuf.st_size + VARHDRSZ);
309 fp = AllocateFile(txtfile, "r");
312 (errcode_for_file_access(),
313 errmsg("could not open file \"%s\": %m", txtfile)));
315 if (fread(VARDATA(result), 1, statbuf.st_size, fp) != statbuf.st_size ||
318 (errcode_for_file_access(),
319 errmsg("could not read file \"%s\": %m", txtfile)));
324 REMOVE_TMPFILE(txtfile);
329 REMOVE_TMPFILE(txtfile);
337 SET_VARSIZE(result, statbuf.st_size + VARHDRSZ);
339 PG_RETURN_TEXT_P(result);
343 * Clean up remaining textporter temporary files
346 CleanupTextPorterTmpFiles(void)
350 char path[MAXPGPATH];
352 tpdir = AllocateDir(TEXTPORTER_TMPDIR);
355 (errcode_for_file_access(),
356 errmsg("could not open textporter temporary file directory \"%s\": %m",
357 TEXTPORTER_TMPDIR)));
359 while ((tpde = ReadDir(tpdir, TEXTPORTER_TMPDIR)) != NULL)
361 if (strlen(tpde->d_name) == 18 &&
362 strncmp(tpde->d_name, "ludia_funcs_", 12) == 0)
364 snprintf(path, MAXPGPATH, TEXTPORTER_TMPDIR "/%s", tpde->d_name);
365 REMOVE_TMPFILE(path);
373 check_textporter_option(char **newval, void **extra, GucSource source)
377 unsigned int *myextra;
380 val = strtoul(*newval, &endptr, 0);
385 if (errno == ERANGE || val != (unsigned long) ((unsigned int) val))
387 GUC_check_errhint("Value exceeds unsigned integer range.");
391 /* Set up the "extra" struct actually used by assign_textporter_option */
392 myextra = (unsigned int *) malloc(sizeof(unsigned int));
395 GUC_check_errcode(ERRCODE_OUT_OF_MEMORY);
396 GUC_check_errmsg("out of memory");
399 *myextra = (unsigned int) val;
400 *extra = (void *) myextra;
406 assign_textporter_option(const char *newval, void *extra)
408 textporter_option = *((unsigned int *) extra);
411 #else /* TEXTPORTER */
414 pgs2textporter1(PG_FUNCTION_ARGS)
419 #endif /* TEXTPORTER */
422 GetSennaEncoding(void)
424 static sen_encoding encoding = sen_enc_default;
426 if (encoding == sen_enc_default)
428 if (GetDatabaseEncoding() == PG_UTF8)
429 encoding = sen_enc_utf8;
432 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
433 errmsg("does not support database encoding \"%s\"",
434 GetDatabaseEncodingName())));
440 * Escape the backslash and double quote characters in the given string.
442 * Return false if the given string has no character which needs to be
443 * escaped. Otherwise, return true. In this case, **s points the palloc'd
444 * space storing the escaped keyword string and *slen is set to the size
445 * of that string. The caller needs to free the palloc'd space.
448 EscapeSnippetKeyword(char **s, size_t *slen)
455 bool in_doublequote = false;
456 bool in_sennaops = false;
457 bool need_escape = false;
460 * Skip the heading double quote character because it always doesn't
461 * need to be interpreted as a character itself and be escaped.
462 * Note that we must not skip the heading character if it's not a
466 if (ISDOUBLEQUOTECHAR(sp))
470 * Check whether the snippet keyword string has a character which
471 * needs to be escaped.
473 while ((sp - *s) < *slen)
475 mblen = pg_mblen(sp);
478 * Backslash in the keyword always needs to be escaped.
480 if (ISBACKSLASHCHAR(sp))
488 if (ISSENNAOPSCHAR(sp))
491 in_doublequote = false;
496 * Double quote in the keyword needs to be escaped if
497 * any Senna search operators are to neither its right
506 if (ISDOUBLEQUOTECHAR(sp) && !in_sennaops)
507 in_doublequote = true;
508 if (!ISSENNAOPSCHAR(sp))
516 * Quick exit if the keyword has no character which needs to be
523 * Allocate the buffer space to store the escaped snippet keyword string.
524 * The maximum size of escaped string is double the input keyword size.
525 * The size reaches the maximum when every character in the input keyword
526 * needs to be escaped.
528 ep = escaped = (char *) palloc(*slen * 2);
531 * Copy the characters which have been passed through in the above loop
532 * and don't need to be escaped, into the buffer. If in_doublequote is
533 * true, we don't copy the double quote in the previous position into the
534 * buffer because it might still need to be escaped.
536 copylen = sp - *s - ((in_doublequote) ? 1 : 0);
537 memcpy(ep, *s, copylen);
541 * Construct the escaped snippet keyword string.
543 while ((sp - *s) < *slen)
545 mblen = pg_mblen(sp);
550 * dqchar indicates the previous character, that is a double
551 * quote. We assume here that a double quote is single-byte
554 char dqchar = *(sp - 1);
556 if (ISSENNAOPSCHAR(sp))
559 * Don't escape the double quote which is just before Senna
565 in_doublequote = false;
570 * Escape the double quote if no Senna operator is next to it.
575 if (ISDOUBLEQUOTECHAR(sp))
576 in_doublequote = true;
579 if (ISBACKSLASHCHAR(sp))
581 memcpy(ep, sp, mblen);
583 in_doublequote = false;
589 if (ISDOUBLEQUOTECHAR(sp))
592 * Don't escape the double quote which is just after Senna
598 in_doublequote = true;
602 if (ISBACKSLASHCHAR(sp))
605 * We don't check ISSENNAOPSCHAR() here. We handle Senna
606 * operator character as a character itself instead of
607 * an operator if it doesn't follow a double quote.
609 memcpy(ep, sp, mblen);
613 if (!ISSENNAOPSCHAR(sp))
620 /* Add the tailing double quote into the buffer */
628 if (pgs2_enable_debug)
630 char *tmp = pnstrdup(*s, *slen);
632 elog(LOG, "escaped snippet keyword: %s", tmp);
641 GetSennaQuery(char *str, size_t len)
643 static sen_query *query_cache = NULL;
644 static char *key_cache = NULL;
645 static size_t len_cache = 0;
646 static bool guc_cache = false;
648 sen_encoding encoding;
652 bool needfree = false;
655 * Return the cached Senna query if the same keyword has
656 * been used the last time.
658 if (key_cache != NULL &&
660 strncmp(key_cache, str, len) == 0 &&
661 escape_snippet_keyword == guc_cache)
664 if (pgs2_enable_debug)
666 char *tmp = pnstrdup(str, len);
668 elog(LOG, "GetSennaQuery(): quick exit: %s", tmp);
675 encoding = GetSennaEncoding();
680 (errcode(ERRCODE_OUT_OF_MEMORY),
681 errmsg("out of memory")));
684 * We always cache the unescaped keyword. Which enables us
685 * to check whether we can use the cached Senna query before
686 * escaping the keyword.
688 memcpy(key, str, len);
692 * If the keyword has been escaped, 'str' points to the
693 * newly-palloc'd space storing the escaped keyword. This
694 * space needs to be freed later.
696 if (escape_snippet_keyword)
697 needfree = EscapeSnippetKeyword(&str, &len);
699 query = sen_query_open(str, len, sen_sel_or, SEN_MAX_N_EXPRS,
705 (errmsg("sen_query_open() failed")));
708 if ((rest = sen_query_rest(query, NULL)) != 0)
710 (errmsg("too many expressions (%d)", rest)));
712 if (query_cache != NULL)
714 sen_query_close(query_cache);
721 guc_cache = escape_snippet_keyword;
730 pgs2snippet1(PG_FUNCTION_ARGS)
732 int flags = PG_GETARG_INT32(0);
733 uint32 width = PG_GETARG_UINT32(1);
734 uint32 max_results = PG_GETARG_UINT32(2);
735 text *opentags = PG_GETARG_TEXT_P(3);
736 text *closetags = PG_GETARG_TEXT_P(4);
737 int mapping = PG_GETARG_INT32(5);
738 text *keywords = PG_GETARG_TEXT_P(6);
739 text *document = PG_GETARG_TEXT_P(7);
741 sen_snip *snip = NULL;
742 const char *opentags_str = VARDATA_ANY(opentags);
743 const char *closetags_str = VARDATA_ANY(closetags);
744 char *keywords_str = VARDATA_ANY(keywords);
745 char *document_str = VARDATA_ANY(document);
746 uint32 opentags_len = VARSIZE_ANY_EXHDR(opentags);
747 uint32 closetags_len = VARSIZE_ANY_EXHDR(closetags);
748 uint32 keywords_len = VARSIZE_ANY_EXHDR(keywords);
749 uint32 document_len = VARSIZE_ANY_EXHDR(document);
751 uint32 max_tagged_len = 0;
754 uint32 result_len = 0;
755 bool return_null = false;
757 query = GetSennaQuery(keywords_str, keywords_len);
759 snip = sen_query_snip(query, flags, width, max_results, 1,
760 &opentags_str, &opentags_len,
761 &closetags_str, &closetags_len,
762 mapping == 0 ? NULL : (sen_snip_mapping *)-1);
765 (errmsg("sen_query_snip() failed")));
769 rc = sen_snip_exec(snip, document_str, document_len,
770 &nresults, &max_tagged_len);
771 if (rc != sen_success)
773 (errmsg("sen_snip_exec() failed: %d", rc)));
775 result = (text *) palloc(max_tagged_len + VARHDRSZ);
777 rc = sen_snip_get_result(snip, 0, VARDATA(result), &result_len);
778 if (rc == sen_invalid_argument)
780 else if (rc != sen_success)
782 (errmsg("sen_snip_get_result() failed: %d", rc)));
786 sen_snip_close(snip);
791 sen_snip_close(snip);
796 SET_VARSIZE(result, max_tagged_len + VARHDRSZ);
798 PG_RETURN_TEXT_P(result);
802 * Make sure there is enough space for 'needed' more bytes.
804 * Sets **buf to the allocated space which can store the needed bytes if OK,
805 * NULL if failed to enlarge the space because 'needed' is larger than 'maxlen'.
808 pgs2malloc(void **buf, long *buflen, long needed, long maxlen)
811 if (pgs2_enable_debug)
812 elog(LOG, "pgs2malloc(): buflen %ld, needed %ld, maxlen %ld",
813 *buflen, needed, maxlen);
816 if (*buf != NULL && *buflen >= needed && (*buflen <= maxlen || maxlen == 0))
817 return; /* got enough space already */
820 * Release the already-allocated space since it's too small to
821 * store the needed bytes or larger than the upper limit.
831 * Don't allocate any space if the needed space is larger than
834 if (needed > maxlen && maxlen != 0)
838 * Allocate the space for the needed bytes.
840 * We don't want to allocate just a little more space with each enlarge;
841 * for efficiency, double the buffer size each time it overflows.
842 * Actually, we might need to more than double it if 'needed' is big...
844 * We check whether '*buflen' overflows each cycle to avoid infinite loop.
847 while (*buflen < needed && *buflen != 0)
851 * Clamp to maxlen in case we went past it. Note we are assuming
852 * here that maxlen <= LONG_MAX/2, else the above loop could
853 * overflow. We will still have *buflen >= needed.
855 if (*buflen > maxlen && maxlen != 0)
858 /* Guard against out-of-range '*buflen' value */
861 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
862 errmsg("out of memory"),
863 errdetail("Cannot enlarge buffer by %ld more bytes.",
866 *buf = (void *) malloc(*buflen);
869 (errcode(ERRCODE_OUT_OF_MEMORY),
870 errmsg("out of memory")));
874 pgs2norm(PG_FUNCTION_ARGS)
876 text *str = PG_GETARG_TEXT_PP(0);
877 char *s = VARDATA_ANY(str);
878 long slen = VARSIZE_ANY_EXHDR(str);
886 * norm_cache is the cache memory storing both input and normalized strings
887 * as the result of pgs2norm(). norm_cache_size is the size of norm_cache
888 * and its upper limit is specified by norm_cache_limit parameter. norm_result
889 * is the pointer to the normalized string with the verlena header (i.e.,
890 * text type) stored in the latter half of the cache. norm_reslen is the size
891 * of norm_result. norm_slen is the size of the input string which is stored
892 * in the first half of the cache.
894 static char *norm_cache = NULL;
895 static long norm_cache_size = 0;
896 static long norm_slen = 0;
897 static char *norm_result = NULL;
898 static long norm_reslen = 0;
901 * Return the cached normalization result if the same string of
902 * the given one has been normalized the last time.
904 if (norm_cache != NULL &&
906 strncmp(norm_cache, s, slen) == 0)
909 if (pgs2_enable_debug)
911 char *tmp = text_to_cstring(str);
913 elog(LOG, "pgs2norm(): quick exit: %s", tmp);
918 PG_RETURN_TEXT_P(pnstrdup(norm_result, norm_reslen));
921 /* Confirm that database encoding is UTF-8 */
925 * Allocate the result buffer to store the normalized string. Since the size of
926 * normalized string can be larger than that of input one, the result buffer needs
927 * extra space. Problem is that, before calling sen_str_normalize, we need to
928 * allocate the result buffer but cannot know how large extra space is required.
929 * So we use RESULT_EXTRA_SIZE as the estimated size of extra space here.
931 #define RESULT_EXTRA_SIZE 64
932 buflen = slen + RESULT_EXTRA_SIZE;
935 result = (text *) palloc(buflen + VARHDRSZ);
937 #if defined(FAST_SENNA)
938 reslen = fast_sen_str_normalize(s, slen, VARDATA(result), buflen);
940 reslen = sen_str_normalize(s, slen, sen_enc_utf8,
942 VARDATA(result), buflen);
947 (errmsg("could not normalize the string")));
950 * If the result buffer size is too short to store the normalized string,
951 * we enlarge the buffer and retry the string normalization.
953 if (buflen <= reslen)
960 SET_VARSIZE(result, reslen + VARHDRSZ);
963 * Cache both input and normalized strings to accelerate the subsequent
964 * calls of pgs2norm() with the same input string. But we don't do that
965 * if the maximum allowed size of the cache is too small to store them.
967 needed = slen + reslen + VARHDRSZ;
968 maxlen = ((norm_cache_limit >= 0) ? norm_cache_limit : work_mem) * 1024L;
970 pgs2malloc((void **) &norm_cache, &norm_cache_size, needed, maxlen);
971 if (norm_cache != NULL)
973 /* Store the input string into the first half of the cache */
975 memcpy(norm_cache, s, slen);
978 * Store the normalized string with the varlena header (i.e., text type)
979 * into the latter half of the cache.
981 norm_result = norm_cache + slen;
982 norm_reslen = reslen + VARHDRSZ;
983 memcpy(norm_result, result, norm_reslen);
987 if (pgs2_enable_debug)
989 char *tmp = text_to_cstring(str);
991 elog(LOG, "pgs2norm(): complete (%s result cache): %s",
992 (norm_cache == NULL) ? "unset" : "set", tmp);
997 PG_RETURN_TEXT_P(result);
1001 * Report the version and configure options of Senna which
1002 * ludia_funcs depends on.
1005 pgs2seninfo(PG_FUNCTION_ARGS)
1007 char *version[MAXPGPATH];
1008 char *coptions[MAXPGPATH];
1015 * Get the version and configure options of Senna. Ignore the
1016 * return value of sen_info() because it always returns a success.
1018 sen_info((char **)&version, (char **)&coptions, NULL, NULL, NULL, NULL);
1021 * Construct a tuple descriptor for the result row. This must
1022 * match this function's ludia_funcs--x.x.sql entry.
1024 tupdesc = CreateTemplateTupleDesc(2, false);
1025 TupleDescInitEntry(tupdesc, (AttrNumber) 1,
1026 "version", TEXTOID, -1, 0);
1027 TupleDescInitEntry(tupdesc, (AttrNumber) 2,
1028 "configure_options", TEXTOID, -1, 0);
1029 tupdesc = BlessTupleDesc(tupdesc);
1032 values[0] = CStringGetTextDatum(*version);
1035 /* configure option */
1036 values[1] = CStringGetTextDatum(*coptions);
1039 tuple = heap_form_tuple(tupdesc, values, isnull);
1040 PG_RETURN_DATUM(HeapTupleGetDatum(tuple));