[libdazzle] fuzzy: allow extracting the priority out of the match



commit aac5bb5a3537f054a4e17076f1d12a1923395e18
Author: Christian Hergert <chergert redhat com>
Date:   Sun Jun 4 21:11:53 2017 -0700

    fuzzy: allow extracting the priority out of the match
    
    This allows us to view the priority when visualizing the test result sets
    and that could be useful in other situations too.

 src/fuzzy/dzl-fuzzy-index-cursor.c  |   11 +++++++----
 src/fuzzy/dzl-fuzzy-index-match.c   |   27 +++++++++++++++++++++++++++
 src/fuzzy/dzl-fuzzy-index-match.h   |    1 +
 src/fuzzy/dzl-fuzzy-index-private.h |    3 ++-
 src/fuzzy/dzl-fuzzy-index.c         |    6 +++++-
 tests/test-desktop-index.c          |    5 +++--
 6 files changed, 45 insertions(+), 8 deletions(-)
---
diff --git a/src/fuzzy/dzl-fuzzy-index-cursor.c b/src/fuzzy/dzl-fuzzy-index-cursor.c
index 5815158..2219e6b 100644
--- a/src/fuzzy/dzl-fuzzy-index-cursor.c
+++ b/src/fuzzy/dzl-fuzzy-index-cursor.c
@@ -47,6 +47,7 @@ typedef struct
   const gchar *key;
   guint        document_id;
   gfloat       score;
+  guint        priority;
 } DzlFuzzyMatch;
 
 typedef struct
@@ -426,7 +427,8 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
                                                         item->lookaside_id,
                                                         &match.document_id,
                                                         &match.key,
-                                                        &penalty))
+                                                        &penalty,
+                                                        &match.priority))
                 continue;
 
               match.score = 1.0 / (strlen (match.key) + item->position) * penalty;
@@ -457,10 +459,10 @@ dzl_fuzzy_index_cursor_worker (GTask        *task,
                                                 lookaside_id,
                                                 &match.document_id,
                                                 &match.key,
-                                                &penalty))
+                                                &penalty,
+                                                &match.priority))
         continue;
 
-      match.score = 1.0 / (strlen (match.key) + score) * penalty;
 
       if (g_hash_table_lookup_extended (by_document,
                                         GUINT_TO_POINTER (match.document_id),
@@ -575,7 +577,7 @@ dzl_fuzzy_index_cursor_get_n_items (GListModel *model)
 
 static gpointer
 dzl_fuzzy_index_cursor_get_item (GListModel *model,
-                             guint       position)
+                                 guint       position)
 {
   DzlFuzzyIndexCursor *self = (DzlFuzzyIndexCursor *)model;
   g_autoptr(GVariant) document = NULL;
@@ -592,6 +594,7 @@ dzl_fuzzy_index_cursor_get_item (GListModel *model,
                        "document", document,
                        "key", match->key,
                        "score", match->score,
+                       "priority", match->priority,
                        NULL);
 }
 
diff --git a/src/fuzzy/dzl-fuzzy-index-match.c b/src/fuzzy/dzl-fuzzy-index-match.c
index 622acd7..f99205d 100644
--- a/src/fuzzy/dzl-fuzzy-index-match.c
+++ b/src/fuzzy/dzl-fuzzy-index-match.c
@@ -26,6 +26,7 @@ struct _DzlFuzzyIndexMatch
   GVariant *document;
   gchar    *key;
   gfloat    score;
+  guint     priority;
 };
 
 enum {
@@ -33,6 +34,7 @@ enum {
   PROP_DOCUMENT,
   PROP_KEY,
   PROP_SCORE,
+  PROP_PRIORITY,
   N_PROPS
 };
 
@@ -73,6 +75,10 @@ dzl_fuzzy_index_match_get_property (GObject    *object,
       g_value_set_string (value, self->key);
       break;
 
+    case PROP_PRIORITY:
+      g_value_set_uint (value, self->priority);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -100,6 +106,10 @@ dzl_fuzzy_index_match_set_property (GObject      *object,
       self->key = g_value_dup_string (value);
       break;
 
+    case PROP_PRIORITY:
+      self->priority = g_value_get_uint (value);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   }
@@ -129,6 +139,15 @@ dzl_fuzzy_index_match_class_init (DzlFuzzyIndexMatchClass *klass)
                          NULL,
                          (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_PRIORITY] =
+    g_param_spec_uint ("priority",
+                       "Priority",
+                       "The priority used when creating the index",
+                       0,
+                       255,
+                       0,
+                       (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
   properties [PROP_SCORE] =
     g_param_spec_float ("score",
                         "Score",
@@ -174,3 +193,11 @@ dzl_fuzzy_index_match_get_key (DzlFuzzyIndexMatch *self)
 
   return self->key;
 }
+
+guint
+dzl_fuzzy_index_match_get_priority (DzlFuzzyIndexMatch *self)
+{
+  g_return_val_if_fail (DZL_IS_FUZZY_INDEX_MATCH (self), 0);
+
+  return self->priority;
+}
diff --git a/src/fuzzy/dzl-fuzzy-index-match.h b/src/fuzzy/dzl-fuzzy-index-match.h
index 0f78815..bd5418f 100644
--- a/src/fuzzy/dzl-fuzzy-index-match.h
+++ b/src/fuzzy/dzl-fuzzy-index-match.h
@@ -30,6 +30,7 @@ G_DECLARE_FINAL_TYPE (DzlFuzzyIndexMatch, dzl_fuzzy_index_match, DZL, FUZZY_INDE
 const gchar *dzl_fuzzy_index_match_get_key      (DzlFuzzyIndexMatch *self);
 GVariant    *dzl_fuzzy_index_match_get_document (DzlFuzzyIndexMatch *self);
 gfloat       dzl_fuzzy_index_match_get_score    (DzlFuzzyIndexMatch *self);
+guint        dzl_fuzzy_index_match_get_priority (DzlFuzzyIndexMatch *self);
 
 G_END_DECLS
 
diff --git a/src/fuzzy/dzl-fuzzy-index-private.h b/src/fuzzy/dzl-fuzzy-index-private.h
index 2dbc8be..e56606a 100644
--- a/src/fuzzy/dzl-fuzzy-index-private.h
+++ b/src/fuzzy/dzl-fuzzy-index-private.h
@@ -29,7 +29,8 @@ gboolean     _dzl_fuzzy_index_resolve         (DzlFuzzyIndex  *self,
                                                guint           lookaside_id,
                                                guint          *document_id,
                                                const gchar   **key,
-                                               gfloat         *penalty);
+                                               gfloat         *penalty,
+                                               guint          *priority);
 
 G_END_DECLS
 
diff --git a/src/fuzzy/dzl-fuzzy-index.c b/src/fuzzy/dzl-fuzzy-index.c
index 50898ff..7cbfeb9 100644
--- a/src/fuzzy/dzl-fuzzy-index.c
+++ b/src/fuzzy/dzl-fuzzy-index.c
@@ -460,7 +460,8 @@ _dzl_fuzzy_index_resolve (DzlFuzzyIndex  *self,
                           guint           lookaside_id,
                           guint          *document_id,
                           const gchar   **key,
-                          gfloat         *penalty)
+                          gfloat         *penalty,
+                          guint          *priority)
 {
   const LookasideEntry *entry;
   const gchar *local_key = NULL;
@@ -490,6 +491,9 @@ _dzl_fuzzy_index_resolve (DzlFuzzyIndex  *self,
   if (document_id != NULL)
     *document_id = entry->document_id;
 
+  if (priority)
+    *priority = (entry->key_id & 0xFF000000) >> 24;
+
   if (penalty != NULL)
     {
       guint p = (entry->key_id & 0xFF000000) >> 24;
diff --git a/tests/test-desktop-index.c b/tests/test-desktop-index.c
index 8229a1b..e3d0424 100644
--- a/tests/test-desktop-index.c
+++ b/tests/test-desktop-index.c
@@ -57,9 +57,10 @@ query_cb (GObject      *object,
       escaped = g_markup_escape_text (title, -1);
       highlight = dzl_fuzzy_highlight (escaped, last_query, FALSE);
       escape_keyword = g_markup_escape_text (dzl_fuzzy_index_match_get_key (match), -1);
-      subtitle = g_strdup_printf ("%lf (%s)",
+      subtitle = g_strdup_printf ("%lf (%s) (priority %u)",
                                   dzl_fuzzy_index_match_get_score (match),
-                                  escape_keyword);
+                                  escape_keyword,
+                                  dzl_fuzzy_index_match_get_priority (match));
 
       suggestion = g_object_new (DZL_TYPE_SUGGESTION,
                                  "id", id,


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