Browse Source

Initial graph codec streaming framework.

Stefano Cossu 4 years ago
parent
commit
6db8ed893f
2 changed files with 64 additions and 24 deletions
  1. 19 18
      include/codec_base.h
  2. 45 6
      src/codec_nt.c

+ 19 - 18
include/codec_base.h

@@ -4,6 +4,9 @@
 #include "graph.h"
 
 
+typedef struct codec_iter_t LSUP_CodecIterator;
+
+
 /** @brief Term encoder callback type.
  *
  * @param[in] term Single term handle.
@@ -26,25 +29,15 @@ typedef LSUP_rc (*term_dec_fn_t)(
         const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term);
 
 
-/** @brief Triple encoder callback type.
+/** @brief Graph encoder callback type.
  *
  * @return LSUP_OK on successful encoding; <0 for other errors.
  */
-typedef LSUP_rc (*trp_enc_fn_t)(
-        const LSUP_Triple trp[], const LSUP_NSMap *nsm, char **rep);
-
-
-/** TODO
- */
-typedef LSUP_rc (*trp_dec_fn_t)(
-        const char *rep, const LSUP_NSMap *nsm, LSUP_Triple **trp);
+typedef LSUP_CodecIterator * (*gr_codec_init_fn_t)(const LSUP_Graph *gr);
 
+typedef LSUP_rc (*gr_codec_iter_fn_t)(LSUP_CodecIterator *it);
 
-/** @brief Graph encoder callback type.
- *
- * @return LSUP_OK on successful encoding; <0 for other errors.
- */
-typedef LSUP_rc (*gr_enc_fn_t)(const LSUP_Graph *gr, char **rep);
+typedef void (*gr_codec_done_fn_t)(LSUP_CodecIterator *it);
 
 
 /** TODO
@@ -72,12 +65,20 @@ typedef struct codec_t {
     char                name[16];       // Name of the codec.
     char                mimetype[32];   // MIME type associated with the codec.
     char                extension[8];   // Serialized file extension.
+
+    // Term encoding and decoding.
     term_enc_fn_t       term_encoder;   // Term encoder function.
     term_dec_fn_t       term_decoder;   // Term decoder function.
-    trp_enc_fn_t        trp_encoder;    // Triple encoder function.
-    trp_dec_fn_t        trp_decoder;    // Triple decoder function.
-    gr_enc_fn_t         gr_encoder;     // Graph encoder function.
-    gr_dec_fn_t         gr_decoder;     // Graph decoder function.
+
+    // Graph encoding.
+    gr_codec_init_fn_t  gr_encode_init; // Graph encoder initialization.
+    gr_codec_iter_fn_t  gr_encode_iter; // Graph encoder initialization.
+    gr_codec_done_fn_t  gr_encode_done; // Graph encoder initialization.
+
+    // Graph decoding.
+    gr_codec_init_fn_t  gr_decode_init; // Graph decoder initialization.
+    gr_codec_iter_fn_t  gr_decode_iter; // Graph decoder initialization.
+    gr_codec_done_fn_t  gr_decode_done; // Graph decoder initialization.
 } LSUP_Codec;
 
 #endif

+ 45 - 6
src/codec_nt.c

@@ -17,6 +17,15 @@
 #define XSD_STRING "http://www.w3.org/2001/XMLSchema#string"
 
 
+typedef struct codec_iter_t {
+    char *              rep;        // String representation of a RDF fragment.
+    LSUP_Triple *       trp;        // RDF fragment being encoded or decoded.
+    LSUP_NSMap *        nsm;        // Namespace map.
+    size_t              cur;        // Internal cursor.
+    LSUP_rc             rc;         // Internal return code.
+} CodecIterator;
+
+
 /* * * Static prototypes. * * */
 
 static LSUP_rc escape_lit (const char *in, char **out_p);
@@ -95,30 +104,60 @@ nt_to_term (const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term)
 }
 
 
-static LSUP_rc
-gr_to_nt (const LSUP_Graph *gr, char **rep)
+static CodecIterator *
+gr_to_nt_init (const LSUP_Graph *gr)
 {
     // TODO
-    return LSUP_NOT_IMPL_ERR;
+    return NULL;
 }
 
 
 static LSUP_rc
-nt_to_gr (const char *rep, LSUP_Graph **gr)
+gr_to_nt_iter (CodecIterator *it) {
+    // TODO
+    return LSUP_NOT_IMPL_ERR;
+}
+
+
+static void
+gr_to_nt_done (CodecIterator *it) {
+    free (it);
+}
+
+
+static CodecIterator *
+nt_to_gr_init (const LSUP_Graph *gr)
 {
+    // TODO
+    return NULL;
+}
+
+
+static LSUP_rc
+nt_to_gr_iter (CodecIterator *it) {
     // TODO
     return LSUP_NOT_IMPL_ERR;
 }
 
 
+static void
+nt_to_gr_done (CodecIterator *it) {
+    free (it);
+}
+
+
 const LSUP_Codec nt_codec = {
     .name           = "N-Triples",
     .mimetype       = "application/n-triples",
     .extension      = "nt",
     .term_encoder   = term_to_nt,
     .term_decoder   = nt_to_term,
-    .gr_encoder     = gr_to_nt,
-    .gr_decoder     = nt_to_gr,
+    .gr_encode_init = gr_to_nt_init,
+    .gr_encode_iter = gr_to_nt_iter,
+    .gr_encode_done = gr_to_nt_done,
+    .gr_decode_init = nt_to_gr_init,
+    .gr_decode_iter = nt_to_gr_iter,
+    .gr_decode_done = nt_to_gr_done,
 };