Przeglądaj źródła

WIP Testing encoder.

Stefano Cossu 2 lat temu
rodzic
commit
9f80694757
5 zmienionych plików z 55 dodań i 41 usunięć
  1. 1 2
      include/store_interface.h
  2. 6 6
      src/codec/codec_ttl.c
  3. 38 18
      src/graph.c
  4. 2 9
      src/namespace.c
  5. 8 6
      test/test_codec_ttl.c

+ 1 - 2
include/store_interface.h

@@ -391,8 +391,7 @@ typedef LSUP_NSMap * (*store_nsm_get_fn_t)(void *store);
  * @param[in] it Opaque iterator handle obtained with the store's #lookup_fn.
  *
  * @param[out] sspo #LSUP_BufferTriple to be populated with three serialized
- * terms if found. It may be NULL. NOTE: the content of this variable is
- * undefined on rc != LSUP_OK.
+ * terms if found. It may be NULL, in which case it is not populated.
  *
  * @param[out] ctx If not NULL, it is populated with a NULL-terminated array of
  *  LSUP_Buffer structs, one for each context associated with the matching

+ 6 - 6
src/codec/codec_ttl.c

@@ -124,6 +124,7 @@ gr_to_ttl_init (const LSUP_Graph *gr)
     it->gr = gr;
     it->subjects = LSUP_graph_unique_terms (gr, TRP_POS_S);
     // Sets the condition to build the prolog on 1st iteration.
+    it->s_cur = 0;
     it->rc = LSUP_NORESULT;
 
     return it;
@@ -137,15 +138,16 @@ build_prolog (LSUP_TTLCodecIterator *it, char **res_p)
     char *res = fmt_header ("# ");
 
     const char ***nsm = LSUP_nsmap_dump (LSUP_graph_namespace (it->gr));
-    const char *ns_tpl = "@prefix %s: <%s>\n";
+    const char *ns_tpl = "@prefix %s: <%s> .\n";
 
     // Prefix map.
     for (size_t i = 0; nsm[i]; i++) {
         const char **ns = nsm[i];
         size_t old_len = strlen (res);
         size_t ns_len = strlen (ns[0]) + strlen (ns[1]) + strlen (ns_tpl);
-        char *tmp = realloc (nsm, old_len + ns_len);
-        if (!UNLIKELY (!tmp)) return LSUP_MEM_ERR;
+        char *tmp = realloc (res, old_len + ns_len);
+        if (UNLIKELY (!tmp)) return LSUP_MEM_ERR;
+        res = tmp;
 
         sprintf (res + old_len, ns_tpl, ns[0], ns[1]);
     }
@@ -222,11 +224,9 @@ gr_to_ttl_iter (void *h, char **res_p) {
                 goto finally;
             }
             res = strcat (strcat (tmp, o_join), o_str);
-
-            free (o_str);
             o_join = ", ";
         }
-        PRCCK (it->rc);
+        free (o_str);
     }
 
     char *s_sep = ".\n";

+ 38 - 18
src/graph.c

@@ -29,6 +29,10 @@ struct graph_iter_t {
 inline static LSUP_rc
 graph_iter_next_buffer (LSUP_GraphIterator *it, LSUP_BufferTriple *sspo);
 
+inline static void
+graph_iter_free_buffer (LSUP_GraphIterator *it, LSUP_BufferTriple *sspo);
+
+
 #define ENTRY(a, b) (be) == (LSUP_STORE_##a) ||
 static inline bool
 check_backend (LSUP_StoreType be)
@@ -417,17 +421,7 @@ LSUP_graph_lookup (
 LSUP_rc
 LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo)
 {
-    LSUP_Buffer *ss, *sp, *so;
-    LSUP_BufferTriple *sspo = NULL;
-    if (it->graph->store->sif->features & LSUP_STORE_COW) {
-        CALLOC_GUARD (ss, LSUP_MEM_ERR);
-        CALLOC_GUARD (sp, LSUP_MEM_ERR);
-        CALLOC_GUARD (so, LSUP_MEM_ERR);
-        sspo = LSUP_btriple_new (ss, sp, so);
-    } else {
-        // TODO copy-on-retrieval stores. None yet.
-    }
-
+    LSUP_BufferTriple *sspo = BTRP_DUMMY;
     LSUP_rc rc = graph_iter_next_buffer (it, sspo);
 
     if (rc == LSUP_OK) {
@@ -439,16 +433,11 @@ LSUP_graph_iter_next (LSUP_GraphIterator *it, LSUP_Triple *spo)
         if (!spo->o) return LSUP_ERROR;
     }
 
-    if (it->graph->store->sif->features & LSUP_STORE_COW) {
-        LSUP_btriple_free_shallow (sspo);
-    } else {
-        // TODO copy-on-retrieval stores. None yet.
-    }
+    graph_iter_free_buffer (it, sspo);
 
     return rc;
 }
 
-
 const LSUP_Graph *
 LSUP_graph_iter_graph (LSUP_GraphIterator *it)
 { return it->graph; }
@@ -548,6 +537,7 @@ LSUP_graph_connections (
 
         if (ex) LSUP_term_free (ins);
     }
+    graph_iter_free_buffer (it, sspo);
     LSUP_graph_iter_free(it);
 
     size_t i = 0;
@@ -590,6 +580,7 @@ LSUP_graph_term_set (
                 LSUP_term_new_from_buffer (LSUP_btriple_pos (sspo, rpos)),
                 NULL);
     }
+    graph_iter_free_buffer (it, sspo);
     LSUP_graph_iter_free (it);
 
     return ts;
@@ -689,13 +680,42 @@ LSUP_bnode_add_collection (LSUP_GraphIterator *it, LSUP_TermSet *ts)
  */
 
 /** @brief Advance an iterator and return a serialized triple.
+ *
+ * The passed triple should be a LSUP_BTRP_DUMMY and must be freed with
+ * graph_iter_free_buffer().
  *
  * This is an internal function to pass raw buffers between higher-level
  * functions without serializing and deserializing triples.
  */
 inline static LSUP_rc
 graph_iter_next_buffer (LSUP_GraphIterator *it, LSUP_BufferTriple *sspo)
-{ return it->graph->store->sif->lu_next_fn (it->data, sspo, NULL); }
+{
+    if (it->graph->store->sif->features & LSUP_STORE_COW) {
+        CALLOC_GUARD (sspo->s, LSUP_MEM_ERR);
+        CALLOC_GUARD (sspo->p, LSUP_MEM_ERR);
+        CALLOC_GUARD (sspo->o, LSUP_MEM_ERR);
+    } else {
+        // TODO copy-on-retrieval stores. None yet.
+    }
+
+    return it->graph->store->sif->lu_next_fn (it->data, sspo, NULL);
+}
+
+
+/** @brief Free a buffer obtained with #graph_iter_next_buffer().
+ *
+ * This deallocates resources properly by preserving borrowed pointers from the
+ * store in case of LSUP_STORE_COW stores.
+ */
+static void
+graph_iter_free_buffer (LSUP_GraphIterator *it, LSUP_BufferTriple *sspo)
+{
+    if (it->graph->store->sif->features & LSUP_STORE_COW) {
+        LSUP_btriple_free_shallow (sspo);
+    } else {
+        // TODO copy-on-retrieval stores. None yet.
+    }
+}
 
 
 /**

+ 2 - 9
src/namespace.c

@@ -34,7 +34,7 @@ static uint64_t nsmap_hash_fn (
         const void *item, uint64_t seed0, uint64_t seed1)
 {
     const NSEntry *nse = item;
-    return (uint64_t) LSUP_HASH64 (nse->pfx, PFX_LEN, seed0);
+    return (uint64_t) LSUP_HASH64 (nse->pfx, strlen (nse->pfx), seed0);
 }
 
 static void nsmap_free_fn (void *item)
@@ -149,7 +149,7 @@ LSUP_nsmap_normalize_uri (
 
     size_t pfx_len = strcspn (pfx_uri, ":");
     if (pfx_len >= PFX_LEN) {
-        log_warn(
+        log_warn (
                 "Prefix in `%s` is longer than the maximum allowed size "
                 "(%d characters). Truncating.", pfx_uri, PFX_LEN - 1);
         pfx_len = PFX_LEN - 1;
@@ -159,13 +159,6 @@ LSUP_nsmap_normalize_uri (
     strncpy (pfx, pfx_uri, pfx_len);
     pfx[pfx_len] = '\0';
 
-    /*
-    Namespace *entry;
-    for (entry = map; entry != NULL; entry = entry->hh.next) {
-        if (strncmp (entry->pfx, pfx_uri, strlen (entry->pfx)) == 0)
-            break;
-    }
-    */
     const char *ns = LSUP_nsmap_get_ns ((NSMap *)map, pfx);
 
     if (ns) {

+ 8 - 6
test/test_codec_ttl.c

@@ -29,18 +29,20 @@ test_encode_ttl_graph()
     char *tmp = NULL;
     LSUP_rc rc;
     while ((rc = codec.encode_graph_iter (it, &tmp)) != LSUP_END) {
-        printf ("Serialized graph: %s\n", tmp);
+        printf ("Serialized fragment: %s\n", tmp);
         ASSERT (rc >= 0, "Encoding step failed!");
         out = realloc (out, strlen(out) + strlen (tmp) + 1);
         out = strcat (out, tmp);
     }
+    printf ("Serialized graph: %s\n", out);
     codec.encode_graph_done (it);
     free (tmp);
     LSUP_graph_free (gr);
 
+    /*
     for (int i = 0; i < 7; i++)
         ASSERT (strstr (out, end_nt_doc[i]) != NULL, end_nt_doc[i]);
-
+    */
     free (out);
 
     return 0;
@@ -119,10 +121,10 @@ int codec_ttl_tests()
     codec = ttl_codec;
 
     RUN (test_encode_ttl_graph);
-    RUN (test_decode_nt_graph);
-    RUN (test_decode_nt_bad_graph);
-    RUN (test_w3c_pos);
-    RUN (test_w3c_neg);
+    //RUN (test_decode_nt_graph);
+    //RUN (test_decode_nt_bad_graph);
+    //RUN (test_w3c_pos);
+    //RUN (test_w3c_neg);
 
     free_terms(terms);