From 618a3b10e22e7eeded59d463483297f999ce9316 Mon Sep 17 00:00:00 2001
From: Yunchih Chen <yunchih.cat@gmail.com>
Date: Sat, 28 Apr 2018 10:53:16 +0800
Subject: Add raw_size field into header

---
 lib/commit.c | 40 +++++++++++++++++++---------------------
 1 file changed, 19 insertions(+), 21 deletions(-)

(limited to 'lib/commit.c')

diff --git a/lib/commit.c b/lib/commit.c
index f2fbc06..53dc493 100644
--- a/lib/commit.c
+++ b/lib/commit.c
@@ -3,26 +3,27 @@
 #include <string.h>
 #include <zstd.h>
 
-static void nfl_commit_default(FILE *f, nfl_entry_t *store,
-                               uint32_t store_size);
-static void nfl_commit_lz4(FILE *f, nfl_entry_t *store, uint32_t store_size);
-static void nfl_commit_zstd(FILE *f, nfl_entry_t *store, uint32_t store_size);
-
-void nfl_commit_init() { /* TODO */
-}
-
-static void nfl_commit_default(FILE *f, nfl_entry_t *store,
+static void nfl_commit_default(FILE *f, nfl_header_t *header, nfl_entry_t *store,
                                uint32_t store_size) {
     uint32_t written;
+    header->raw_size = store_size;
+
+    // Write header
+    written = fwrite(header, 1, sizeof(nfl_header_t), f);
+    ERR(written != sizeof(nfl_header_t), strerror(errno));
+
+    // Write store
     written = fwrite(store, 1, store_size, f);
     ERR(written != store_size, strerror(errno));
 }
 
-static void nfl_commit_lz4(FILE *f, nfl_entry_t *store, uint32_t store_size) {
+static void nfl_commit_lz4(FILE *f, nfl_header_t *header, nfl_entry_t *store,
+                           uint32_t store_size) {
     /* TODO */
 }
 
-static void nfl_commit_zstd(FILE *f, nfl_entry_t *store, uint32_t store_size) {
+static void nfl_commit_zstd(FILE *f, nfl_header_t *header, nfl_entry_t *store,
+                            uint32_t store_size) {
     size_t const bufsize = ZSTD_compressBound(store_size);
     void *buf;
 
@@ -31,37 +32,34 @@ static void nfl_commit_zstd(FILE *f, nfl_entry_t *store, uint32_t store_size) {
     if (ZSTD_isError(csize))
         FATAL("zstd: %s \n", ZSTD_getErrorName(csize));
 
-    nfl_commit_default(f, buf, csize);
+    nfl_commit_default(f, header, buf, csize);
     free(buf);
 }
 
 void nfl_commit_worker(nfl_header_t *header, nfl_entry_t *store,
                        enum nfl_compression_t compression_opt,
+                       bool truncate,
                        const char *filename) {
     FILE *f;
-    uint32_t written;
+    const char *mode = truncate ? "wb" : "ab";
 
     debug("Comm worker #%u: commit to file %s\n", header->id, filename);
-    ERR((f = fopen(filename, "wb")) == NULL, strerror(errno));
-
-    // commit header
-    written = fwrite(header, 1, sizeof(nfl_header_t), f);
-    ERR(written != sizeof(nfl_header_t), strerror(errno));
+    ERR((f = fopen(filename, mode)) == NULL, strerror(errno));
 
     // commit store
     uint32_t store_size = sizeof(nfl_entry_t) * header->max_n_entries;
     switch (compression_opt) {
     case COMPRESS_NONE:
         debug("Comm worker #%u: commit without compression\n", header->id)
-            nfl_commit_default(f, store, store_size);
+            nfl_commit_default(f, header, store, store_size);
         break;
     case COMPRESS_LZ4:
         debug("Comm worker #%u: commit with compression algorithm: lz4",
-              header->id) nfl_commit_lz4(f, store, store_size);
+              header->id) nfl_commit_lz4(f, header, store, store_size);
         break;
     case COMPRESS_ZSTD:
         debug("Comm worker #%u: commit with compression algorithm: zstd",
-              header->id) nfl_commit_zstd(f, store, store_size);
+              header->id) nfl_commit_zstd(f, header, store, store_size);
         break;
     // Must not reach here ...
     default:
-- 
cgit