ID facet term with map - persists inside a session
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 2 Jul 2015 13:44:55 +0000 (15:44 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 2 Jul 2015 13:44:55 +0000 (15:44 +0200)
Untested

src/client.c
src/session.c
src/session.h

index e25bd2d..e446632 100644 (file)
@@ -521,7 +521,7 @@ static void client_report_facets(struct client *cl, ZOOM_resultset rs)
                                 ZOOM_facet_field_get_term(facets[facet_idx],
                                                           term_idx, &freq);
                             if (term)
-                                add_facet(se, p, term, freq);
+                                add_facet(se, p, term, freq, cl);
                         }
                         break;
                     }
@@ -1349,7 +1349,7 @@ static void ccl_quote_map_term(CCL_bibset ccl_map, WRBUF w,
     }
 }
 
-static int apply_limit(struct session_database *sdb,
+static int apply_limit(struct client *cl,
                        facet_limits_t facet_limits,
                        WRBUF w_pqf, CCL_bibset ccl_map,
                        struct conf_service *service)
@@ -1358,6 +1358,7 @@ static int apply_limit(struct session_database *sdb,
     int i = 0;
     const char *name;
     const char *value;
+    struct session_database *sdb = client_get_database(cl);
 
     NMEM nmem_tmp = nmem_create();
     for (i = 0; (name = facet_limits_get(facet_limits, i, &value)); i++)
@@ -1377,6 +1378,13 @@ static int apply_limit(struct session_database *sdb,
                 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
                                       &num, 1, '\\', 1);
 
+                for (i = 0; i < num; i++)
+                {
+                    const char *id = session_lookup_id_facet(cl->session,
+                                                             cl, p, values[i]);
+                    if (id)
+                        values[i] = nmem_strdup(nmem_tmp, id);
+                }
                 nmem_strsplit_escape2(nmem_tmp, ",", s->value, &cvalues,
                                       &cnum, 1, '\\', 1);
 
@@ -1503,7 +1511,7 @@ int client_parse_query(struct client *cl, const char *query,
         wrbuf_puts(w_pqf, " ");
     }
 
-    if (apply_limit(sdb, facet_limits, w_pqf, ccl_map, service))
+    if (apply_limit(cl, facet_limits, w_pqf, ccl_map, service))
     {
         ccl_qual_rm(&ccl_map);
         return -2;
index e7d5b9c..6b95835 100644 (file)
@@ -203,7 +203,43 @@ static void session_normalize_facet(struct session *s,
     run_icu(s, icu_chain_id, value, facet_wrbuf, display_wrbuf);
 }
 
-void add_facet(struct session *s, const char *type, const char *value, int count)
+struct facet_id {
+    char *client_id;
+    char *type;
+    char *id;
+    char *term;
+    struct facet_id *next;
+};
+
+static void session_add_id_facet(struct session *s, struct client *cl,
+                          const char *type,
+                          const char *id,
+                          const char *term)
+{
+    struct facet_id *t = nmem_malloc(s->session_nmem, sizeof(*t));
+
+    t->client_id = nmem_strdup(s->session_nmem, client_get_id(cl));
+    t->type = nmem_strdup(s->session_nmem, type);
+    t->id = nmem_strdup(s->session_nmem, id);
+    t->term = nmem_strdup(s->session_nmem, term);
+    t->next = s->facet_id_list;
+    s->facet_id_list = t;
+}
+
+
+const char *session_lookup_id_facet(struct session *s, struct client *cl,
+                                    const char *type,
+                                    const char *term)
+{
+    struct facet_id *t = s->facet_id_list;
+    for (; t; t = t->next)
+        if (!strcmp(client_get_id(cl), t->id) &&
+            !strcmp(t->type, type) && !strcmp(t->term, term))
+            return t->id;
+    return 0;
+}
+
+void add_facet(struct session *s, const char *type, const char *value, int count, struct client *cl)
 {
     WRBUF facet_wrbuf = wrbuf_alloc();
     WRBUF display_wrbuf = wrbuf_alloc();
@@ -243,6 +279,8 @@ void add_facet(struct session *s, const char *type, const char *value, int count
         }
         termlist_insert((*tp)->termlist, wrbuf_cstr(display_wrbuf),
                         wrbuf_cstr(facet_wrbuf), id, id_len, count);
+        if (id)
+            session_add_id_facet(s, cl, type, id, wrbuf_cstr(display_wrbuf));
     }
     wrbuf_destroy(facet_wrbuf);
     wrbuf_destroy(display_wrbuf);
@@ -1045,6 +1083,7 @@ struct session *new_session(NMEM nmem, struct conf_service *service,
     session->clients_cached = 0;
     session->settings_modified = 0;
     session->session_nmem = nmem;
+    session->facet_id_list = 0;
     session->nmem = nmem_create();
     session->databases = 0;
     session->sorted_results = 0;
@@ -2405,15 +2444,15 @@ static int ingest_to_cluster(struct client *cl,
                     char year[64];
                     sprintf(year, "%d", rec_md->data.number.max);
 
-                    add_facet(se, (char *) type, year, term_factor);
+                    add_facet(se, (char *) type, year, term_factor, cl);
                     if (rec_md->data.number.max != rec_md->data.number.min)
                     {
                         sprintf(year, "%d", rec_md->data.number.min);
-                        add_facet(se, (char *) type, year, term_factor);
+                        add_facet(se, (char *) type, year, term_factor, cl);
                     }
                 }
                 else
-                    add_facet(se, type, wrbuf_cstr(wrbuf_disp), term_factor);
+                    add_facet(se, type, wrbuf_cstr(wrbuf_disp), term_factor, cl);
             }
         }
         else
index 153c6ad..49e5338 100644 (file)
@@ -113,6 +113,7 @@ struct session {
     facet_limits_t facet_limits;
     int clients_starting;
     struct reclist_sortparms *sorted_results;
+    struct facet_id *facet_id_list;
 };
 
 struct statistics {
@@ -192,11 +193,15 @@ int ingest_record(struct client *cl, const char *rec, int record_no, NMEM nmem);
 int ingest_xml_record(struct client *cl, xmlDoc *xdoc,
                       int record_no, NMEM nmem, int cached_copy);
 void session_alert_watch(struct session *s, int what);
-void add_facet(struct session *s, const char *type, const char *value, int count);
+void add_facet(struct session *s, const char *type, const char *value, int count, struct client *cl);
 
 int session_check_cluster_limit(struct session *se, struct record_cluster *rec);
 
 void perform_termlist(struct http_channel *c, struct session *se, const char *name, int num, int version);
+
+const char *session_lookup_id_facet(struct session *s, struct client *cl,
+                                    const char *type, const char *term);
+
 void session_log(struct session *s, int level, const char *fmt, ...)
 #ifdef __GNUC__
     __attribute__ ((format (printf, 3, 4)))