diff options
author | Yunchih Chen <yunchih.cat@gmail.com> | 2018-03-05 09:38:15 +0800 |
---|---|---|
committer | Yunchih Chen <yunchih.cat@gmail.com> | 2018-03-05 09:41:39 +0800 |
commit | 71f6b20e99e03c16bf9bf8c5d509ade84b6d5db4 (patch) | |
tree | acbdacf51d6403a44e03bcdbf43448db2d0098f5 /commit.c | |
parent | 87e6bdc58160b85664c43ebc731a07ae8bceed79 (diff) | |
download | nfcollect-71f6b20e99e03c16bf9bf8c5d509ade84b6d5db4.tar nfcollect-71f6b20e99e03c16bf9bf8c5d509ade84b6d5db4.tar.gz nfcollect-71f6b20e99e03c16bf9bf8c5d509ade84b6d5db4.tar.bz2 nfcollect-71f6b20e99e03c16bf9bf8c5d509ade84b6d5db4.tar.lz nfcollect-71f6b20e99e03c16bf9bf8c5d509ade84b6d5db4.tar.xz nfcollect-71f6b20e99e03c16bf9bf8c5d509ade84b6d5db4.tar.zst nfcollect-71f6b20e99e03c16bf9bf8c5d509ade84b6d5db4.zip |
Implement zstandard commit compression
Diffstat (limited to 'commit.c')
-rw-r--r-- | commit.c | 41 |
1 files changed, 39 insertions, 2 deletions
@@ -1,9 +1,47 @@ #include <errno.h> #include <string.h> +#include <zstd.h> #include "commit.h" +static void nfl_commit_default(FILE* f, nflog_entry_t* store, uint32_t store_size); +static void nfl_commit_lz4(FILE* f, nflog_entry_t* store, uint32_t store_size); +static void nfl_commit_zstd(FILE* f, nflog_entry_t* store, uint32_t store_size); + +typedef void (*nflog_commit_run_table_t)(FILE* f, nflog_entry_t* store, uint32_t size); +static const nflog_commit_run_table_t commit_run_table[] = { + nfl_commit_default, + nfl_commit_lz4, + nfl_commit_zstd +}; + void nfl_commit_init() { + /* TODO */ +} + +static void nfl_commit_default(FILE* f, nflog_entry_t* store, uint32_t store_size) { + uint32_t written; + written = fwrite(store, 1, store_size, f); + ERR(written != store_size, strerror(errno)); +} +static void nfl_commit_lz4(FILE* f, nflog_entry_t* store, uint32_t store_size) { + /* TODO */ +} + +static void nfl_commit_zstd(FILE* f, nflog_entry_t* store, uint32_t store_size) { + size_t const bufsize = ZSTD_compressBound(store_size); + void* buf; + + ERR((buf = malloc(bufsize)), NULL); + + size_t const csize = ZSTD_compress(buf, bufsize, store, store_size, 1); + if (ZSTD_isError(csize)) { + fprintf(stderr, "zstd error: %s \n", ZSTD_getErrorName(csize)); + exit(8); + } + + nfl_commit_default(f, buf, bufsize); + free(buf); } void nfl_commit_worker(nflog_header_t* header, nflog_entry_t* store, const char* filename) { @@ -19,8 +57,7 @@ void nfl_commit_worker(nflog_header_t* header, nflog_entry_t* store, const char* // commit store uint32_t store_size = sizeof(nflog_entry_t) * header->max_n_entries; - written = fwrite(store, 1, store_size, f); - ERR(written != store_size, strerror(errno)); + commit_run_table[header->compression_opt](f, store, store_size); // Do fsync ? fclose(f); |