[libdazzle] fuzzy: extract penalty when resolving the key and document



commit 2ff8cc3c03b9bc87031b81d384b7c1624eb485fe
Author: Christian Hergert <chergert redhat com>
Date:   Sun Jun 4 18:57:56 2017 -0700

    fuzzy: extract penalty when resolving the key and document

 src/fuzzy/dzl-fuzzy-index-cursor.c  |   12 +++++++-----
 src/fuzzy/dzl-fuzzy-index-private.h |    3 ++-
 src/fuzzy/dzl-fuzzy-index.c         |   25 +++++++++++++++++--------
 3 files changed, 26 insertions(+), 14 deletions(-)
---
diff --git a/src/fuzzy/dzl-fuzzy-index-cursor.c b/src/fuzzy/dzl-fuzzy-index-cursor.c
index ba0b022..e7fdc31 100644
--- a/src/fuzzy/dzl-fuzzy-index-cursor.c
+++ b/src/fuzzy/dzl-fuzzy-index-cursor.c
@@ -417,8 +417,8 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
       for (i = 0; i < lookup.tables_n_elements[0]; i++)
         {
           const DzlFuzzyIndexItem *item = &lookup.tables[0][i];
-          guint penalty = ((item->lookaside_id & 0xFF000000) >> 24) + 1;
           DzlFuzzyMatch match;
+          guint penalty;
 
           if (item->lookaside_id != last_id)
             {
@@ -427,10 +427,11 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
               if G_UNLIKELY (!_dzl_fuzzy_index_resolve (self->index,
                                                         item->lookaside_id,
                                                         &match.document_id,
-                                                        &match.key))
+                                                        &match.key,
+                                                        &penalty))
                 continue;
 
-              match.score = 1.0 / ((strlen (match.key) + item->position) * penalty);
+              match.score = 1.0 / (strlen (match.key) + item->position + penalty);
 
               g_array_append_val (self->matches, match);
             }
@@ -457,10 +458,11 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
       if G_UNLIKELY (!_dzl_fuzzy_index_resolve (self->index,
                                                 lookaside_id,
                                                 &match.document_id,
-                                                &match.key))
+                                                &match.key,
+                                                &penalty))
         continue;
 
-      match.score = 1.0 / ((strlen (match.key) + score) * penalty);
+      match.score = 1.0 / (strlen (match.key) + score + penalty);
 
       if (g_hash_table_lookup_extended (by_document,
                                         GUINT_TO_POINTER (match.document_id),
diff --git a/src/fuzzy/dzl-fuzzy-index-private.h b/src/fuzzy/dzl-fuzzy-index-private.h
index 217bafb..e324761 100644
--- a/src/fuzzy/dzl-fuzzy-index-private.h
+++ b/src/fuzzy/dzl-fuzzy-index-private.h
@@ -28,7 +28,8 @@ GVariant    *_dzl_fuzzy_index_lookup_document (DzlFuzzyIndex  *self,
 gboolean     _dzl_fuzzy_index_resolve         (DzlFuzzyIndex  *self,
                                                guint           lookaside_id,
                                                guint          *document_id,
-                                               const gchar   **key);
+                                               const gchar   **key,
+                                               guint          *penalty);
 
 G_END_DECLS
 
diff --git a/src/fuzzy/dzl-fuzzy-index.c b/src/fuzzy/dzl-fuzzy-index.c
index ef26aae..a690f95 100644
--- a/src/fuzzy/dzl-fuzzy-index.c
+++ b/src/fuzzy/dzl-fuzzy-index.c
@@ -459,9 +459,12 @@ gboolean
 _dzl_fuzzy_index_resolve (DzlFuzzyIndex  *self,
                           guint           lookaside_id,
                           guint          *document_id,
-                          const gchar   **key)
+                          const gchar   **key,
+                          guint          *penalty)
 {
   const LookasideEntry *entry;
+  const gchar *local_key = NULL;
+  guint key_id;
 
   g_assert (DZL_IS_FUZZY_INDEX (self));
   g_assert (document_id != NULL);
@@ -474,17 +477,23 @@ _dzl_fuzzy_index_resolve (DzlFuzzyIndex  *self,
 
   entry = &self->lookaside_raw [lookaside_id];
 
-  *document_id = entry->document_id;
+  /* The key_id has a mask with the priority as well */
+  key_id = entry->key_id & 0x00FFFFFF;
+  if G_UNLIKELY (key_id >= g_variant_n_children (self->keys))
+    return FALSE;
+
+  g_variant_get_child (self->keys, key_id, "&s", &local_key);
 
   if (key != NULL)
-    {
-      /* The key_id has a mask with the priority as well */
-      guint key_id = entry->key_id & 0x00FFFFFF;
+    *key = local_key;
 
-      if G_UNLIKELY (key_id >= g_variant_n_children (self->keys))
-        return FALSE;
+  if (document_id != NULL)
+    *document_id = entry->document_id;
 
-      g_variant_get_child (self->keys, key_id, "&s", key);
+  if (penalty != NULL)
+    {
+      *penalty = (entry->key_id & 0xFF000000) >> 24;
+      *penalty += strlen (local_key);
     }
 
   return TRUE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]