summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarlos Garnacho <carlosg@gnome.org>2023-03-02 14:41:12 +0100
committerCarlos Garnacho <carlosg@gnome.org>2023-03-02 16:16:12 +0100
commita4f9885a15d845203f777c87e7109471d216c3d0 (patch)
treee39da4cffa9ca72c5cfcd3b20b092d3b7a3c9e2b /src
parent45c4695f4207919c4a9ada1d2758ec3c27ff8739 (diff)
downloadtracker-a4f9885a15d845203f777c87e7109471d216c3d0.tar.gz
libtracker-common: Convert unicode parser to a GModule
These are big and hairy dependencies, probably unwanted if anybody wishes dbus connections but not local ones. Making them a GModule means they are only loaded when strictly needed. Closes: https://gitlab.gnome.org/GNOME/tracker/-/issues/396
Diffstat (limited to 'src')
-rw-r--r--src/libtracker-common/meson.build44
-rw-r--r--src/libtracker-common/tracker-common.h1
-rw-r--r--src/libtracker-common/tracker-parser-libicu.c1
-rw-r--r--src/libtracker-common/tracker-parser.c255
4 files changed, 291 insertions, 10 deletions
diff --git a/src/libtracker-common/meson.build b/src/libtracker-common/meson.build
index 639588fa5..96124437b 100644
--- a/src/libtracker-common/meson.build
+++ b/src/libtracker-common/meson.build
@@ -6,17 +6,42 @@ tracker_common_sources = [
'tracker-file-utils.c',
'tracker-term-utils.c',
'tracker-utils.c',
- 'tracker-parser-utils.c',
- 'tracker-language.c',
+ 'tracker-parser.c',
]
if unicode_library_name == 'icu'
- tracker_common_sources += 'tracker-parser-libicu.c'
+ libtracker_parser_libicu = shared_module('tracker-parser-libicu',
+ 'tracker-parser-libicu.c',
+ 'tracker-parser-utils.c',
+ 'tracker-language.c',
+ dependencies: [gobject,libstemmer, icu_uc, icu_i18n],
+ c_args: tracker_c_args + [
+ '-include', join_paths(build_root, 'config.h'),
+ '-DMODULE',
+ ],
+ include_directories: [configinc, srcinc],
+ install: true,
+ install_dir: tracker_internal_libs_dir,
+ name_suffix: 'so',
+ )
else
- tracker_common_sources += 'tracker-parser-libunistring.c'
+ libtracker_parser_libunistring = shared_module('tracker-parser-libunistring',
+ 'tracker-parser-libunistring.c',
+ 'tracker-parser-utils.c',
+ 'tracker-language.c',
+ dependencies: [gobject,libstemmer, libunistring],
+ c_args: tracker_c_args + [
+ '-include', join_paths(build_root, 'config.h'),
+ '-DMODULE',
+ ],
+ include_directories: [configinc, srcinc],
+ install: true,
+ install_dir: tracker_internal_libs_dir,
+ name_suffix: 'so',
+ )
endif
-tracker_common_dependencies = [glib, gio, gio_unix, libmath, libstemmer]
+tracker_common_dependencies = [glib, gio, gio_unix, libmath]
if build_machine.system() == 'openbsd'
libkvm = meson.get_compiler('c').find_library('kvm')
@@ -25,8 +50,11 @@ endif
libtracker_common = static_library('tracker-common',
tracker_common_sources,
- dependencies: tracker_common_dependencies + [unicode_library],
- c_args: tracker_c_args,
+ dependencies: [tracker_common_dependencies, gmodule],
+ c_args: [
+ '-DPRIVATE_LIBDIR="@0@"'.format(tracker_internal_libs_dir),
+ '-DBUILDROOT="@0@"'.format(meson.build_root()),
+ ] + tracker_c_args,
include_directories: [configinc, srcinc],
gnu_symbol_visibility: 'hidden',
)
@@ -35,6 +63,6 @@ commoninc = include_directories('.')
tracker_common_dep = declare_dependency(
link_with: libtracker_common,
- dependencies: tracker_common_dependencies + [unicode_library],
+ dependencies: [tracker_common_dependencies, gmodule],
include_directories: [configinc, srcinc, commoninc],
)
diff --git a/src/libtracker-common/tracker-common.h b/src/libtracker-common/tracker-common.h
index 0a5c361b7..d04e2b083 100644
--- a/src/libtracker-common/tracker-common.h
+++ b/src/libtracker-common/tracker-common.h
@@ -31,7 +31,6 @@
#include "tracker-date-time.h"
#include "tracker-debug.h"
#include "tracker-file-utils.h"
-#include "tracker-language.h"
#include "tracker-parser.h"
#include "tracker-term-utils.h"
#include "tracker-utils.h"
diff --git a/src/libtracker-common/tracker-parser-libicu.c b/src/libtracker-common/tracker-parser-libicu.c
index c0e4cec98..8795af7cf 100644
--- a/src/libtracker-common/tracker-parser-libicu.c
+++ b/src/libtracker-common/tracker-parser-libicu.c
@@ -766,7 +766,6 @@ tracker_collation_init (void)
/* Get locale! */
locale = setlocale (LC_COLLATE, NULL);
- TRACKER_NOTE (COLLATION, g_message ("[ICU collation] Initializing collator for locale '%s'", locale));
collator = ucol_open (locale, &status);
if (!collator) {
g_warning ("[ICU collation] Collator for locale '%s' cannot be created: %s",
diff --git a/src/libtracker-common/tracker-parser.c b/src/libtracker-common/tracker-parser.c
new file mode 100644
index 000000000..aaaed58de
--- /dev/null
+++ b/src/libtracker-common/tracker-parser.c
@@ -0,0 +1,255 @@
+/*
+ * Copyright (C) 2023, Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * Author: Carlos Garnacho <carlosg@gnome.org>
+ */
+
+#include "config.h"
+
+#include <gio/gio.h>
+#include <gmodule.h>
+
+#include "tracker-parser.h"
+
+#include "tracker-debug.h"
+
+static TrackerParser * (*parser_new) (void);
+static void (*parser_free) (TrackerParser *parser);
+static void (*parser_reset) (TrackerParser *parser,
+ const gchar *txt,
+ gint txt_size,
+ guint max_word_length,
+ gboolean enable_stemmer,
+ gboolean enable_unaccent,
+ gboolean ignore_stop_words,
+ gboolean ignore_reserved_words,
+ gboolean ignore_numbers);
+static const gchar * (*parser_next) (TrackerParser *parser,
+ gint *position,
+ gint *byte_offset_start,
+ gint *byte_offset_end,
+ gboolean *stop_word,
+ gint *word_length);
+static gpointer (*collation_init) (void);
+static void (*collation_shutdown) (gpointer collator);
+static gint (*collation_utf8) (gpointer collator,
+ gint len1,
+ gconstpointer str1,
+ gint len2,
+ gconstpointer str2);
+static gunichar2 * (*util_tolower) (const gunichar2 *input,
+ gsize len,
+ gsize *len_out);
+static gunichar2 * (*util_toupper) (const gunichar2 *input,
+ gsize len,
+ gsize *len_out);
+static gunichar2 * (*util_casefold) (const gunichar2 *input,
+ gsize len,
+ gsize *len_out);
+static gunichar2 * (*util_normalize) (const gunichar2 *input,
+ GNormalizeMode mode,
+ gsize len,
+ gsize *len_out);
+static gunichar2 * (*util_unaccent) (const gunichar2 *input,
+ gsize len,
+ gsize *len_out);
+
+static void
+ensure_init_parser (void)
+{
+ static GModule *module = NULL;
+
+ if (module == NULL) {
+ const gchar *modules[] = {
+
+ "libtracker-parser-libicu.so",
+ "libtracker-parser-libunistring.so"
+ };
+ gchar *module_path;
+ guint i;
+
+ g_assert (g_module_supported ());
+
+ for (i = 0; i < G_N_ELEMENTS (modules); i++) {
+ if (g_strcmp0 (g_get_current_dir (), BUILDROOT) == 0) {
+ /* Detect in-build runtime of this code, this may happen
+ * building introspection information or running tests.
+ * We want the in-tree modules to be loaded then.
+ */
+ module_path = g_strdup_printf (BUILDROOT "/src/libtracker-common/%s", modules[i]);
+ } else {
+ module_path = g_strdup_printf (PRIVATE_LIBDIR "/%s", modules[i]);
+ }
+
+ module = g_module_open (module_path,
+ G_MODULE_BIND_LAZY |
+ G_MODULE_BIND_LOCAL);
+ g_free (module_path);
+
+ if (module)
+ break;
+ }
+
+ g_assert (module != NULL);
+
+ if (!g_module_symbol (module, "tracker_parser_new", (gpointer *) &parser_new) ||
+ !g_module_symbol (module, "tracker_parser_free", (gpointer *) &parser_free) ||
+ !g_module_symbol (module, "tracker_parser_reset", (gpointer *) &parser_reset) ||
+ !g_module_symbol (module, "tracker_parser_next", (gpointer *) &parser_next) ||
+ !g_module_symbol (module, "tracker_collation_init", (gpointer *) &collation_init) ||
+ !g_module_symbol (module, "tracker_collation_shutdown", (gpointer *) &collation_shutdown) ||
+ !g_module_symbol (module, "tracker_collation_utf8", (gpointer *) &collation_utf8) ||
+ !g_module_symbol (module, "tracker_parser_tolower", (gpointer *) &util_tolower) ||
+ !g_module_symbol (module, "tracker_parser_toupper", (gpointer *) &util_toupper) ||
+ !g_module_symbol (module, "tracker_parser_casefold", (gpointer *) &util_casefold) ||
+ !g_module_symbol (module, "tracker_parser_normalize", (gpointer *) &util_normalize) ||
+ !g_module_symbol (module, "tracker_parser_unaccent", (gpointer *) &util_unaccent)) {
+ g_printerr ("Could not initialize parser functions: %s\n",
+ g_module_error ());
+ }
+
+ TRACKER_NOTE (COLLATION, g_message ("Initialized collator %s", g_module_name (module)));
+
+ g_module_make_resident (module);
+ g_module_close (module);
+ }
+}
+
+TrackerParser *
+tracker_parser_new (void)
+{
+ ensure_init_parser ();
+
+ return parser_new ();
+}
+
+void
+tracker_parser_free (TrackerParser *parser)
+{
+ parser_free (parser);
+}
+
+void
+tracker_parser_reset (TrackerParser *parser,
+ const gchar *txt,
+ gint txt_size,
+ guint max_word_length,
+ gboolean enable_stemmer,
+ gboolean enable_unaccent,
+ gboolean ignore_stop_words,
+ gboolean ignore_reserved_words,
+ gboolean ignore_numbers)
+{
+ parser_reset (parser, txt, txt_size,
+ max_word_length,
+ enable_stemmer,
+ enable_unaccent,
+ ignore_stop_words,
+ ignore_reserved_words,
+ ignore_numbers);
+}
+
+const gchar *
+tracker_parser_next (TrackerParser *parser,
+ gint *position,
+ gint *byte_offset_start,
+ gint *byte_offset_end,
+ gboolean *stop_word,
+ gint *word_length)
+{
+ return parser_next (parser, position,
+ byte_offset_start,
+ byte_offset_end,
+ stop_word,
+ word_length);
+}
+
+gpointer
+tracker_collation_init (void)
+{
+ ensure_init_parser ();
+
+ return collation_init ();
+}
+
+void
+tracker_collation_shutdown (gpointer collator)
+{
+ collation_shutdown (collator);
+}
+
+gint
+tracker_collation_utf8 (gpointer collator,
+ gint len1,
+ gconstpointer str1,
+ gint len2,
+ gconstpointer str2)
+{
+ return collation_utf8 (collator, len1, str1, len2, str2);
+}
+
+gunichar2 *
+tracker_parser_tolower (const gunichar2 *input,
+ gsize len,
+ gsize *len_out)
+{
+ ensure_init_parser ();
+
+ return util_tolower (input, len, len_out);
+}
+
+gunichar2 *
+tracker_parser_toupper (const gunichar2 *input,
+ gsize len,
+ gsize *len_out)
+{
+ ensure_init_parser ();
+
+ return util_toupper (input, len, len_out);
+}
+
+gunichar2 *
+tracker_parser_casefold (const gunichar2 *input,
+ gsize len,
+ gsize *len_out)
+{
+ ensure_init_parser ();
+
+ return util_casefold (input, len, len_out);
+}
+
+gunichar2 *
+tracker_parser_normalize (const gunichar2 *input,
+ GNormalizeMode mode,
+ gsize len,
+ gsize *len_out)
+{
+ ensure_init_parser ();
+
+ return util_normalize (input, mode, len, len_out);
+}
+
+gunichar2 *
+tracker_parser_unaccent (const gunichar2 *input,
+ gsize len,
+ gsize *len_out)
+{
+ ensure_init_parser ();
+
+ return util_unaccent (input, len, len_out);
+}