1 /*
2   Copyright (C) 2021  The Blosc Developers <blosc@blosc.org>
3   https://blosc.org
4   License: BSD 3-Clause (see LICENSE.txt)
5 
6   Example program demonstrating use of the Blosc filter from C code.
7 
8   To compile this program:
9 
10   $ gcc -O zstd_dict.c -o zstd_dict -lblosc2
11 
12   To run:
13 
14   $ ./zstd_dict
15   TODO ...
16 
17 */
18 
19 #include <stdio.h>
20 #include <assert.h>
21 #include <time.h>
22 #include "blosc2.h"
23 
24 #define KB  1024.
25 #define MB  (1024*KB)
26 #define GB  (1024*MB)
27 
28 #define CHUNKSIZE (200 * 1000)
29 #define NCHUNKS 20
30 #define NTHREADS 4
31 
32 
main(void)33 int main(void) {
34   static int64_t data[CHUNKSIZE];
35   static int64_t data_dest[CHUNKSIZE];
36   const int32_t isize = CHUNKSIZE * sizeof(int64_t);
37   int dsize = 0;
38   int64_t nbytes, cbytes;
39   blosc2_schunk* schunk;
40   int i;
41   int32_t nchunk;
42   int32_t nchunks;
43   blosc_timestamp_t last, current;
44   double ttotal;
45 
46   printf("Blosc version info: %s (%s)\n",
47          BLOSC_VERSION_STRING, BLOSC_VERSION_DATE);
48 
49   /* Initialize the Blosc compressor */
50   blosc_init();
51 
52   /* Create a super-chunk container */
53   blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
54   cparams.typesize = 8;
55   //cparams.filters[0] = BLOSC_DELTA;
56   cparams.compcode = BLOSC_ZSTD;
57   cparams.use_dict = 1;
58   //cparams.clevel = 7;
59   cparams.blocksize = 1024 * 4;  // a page size
60   //cparams.blocksize = 1024 * 32;
61   cparams.nthreads = NTHREADS;
62   blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;
63   dparams.nthreads = NTHREADS;
64   blosc2_storage storage = {.cparams=&cparams, .dparams=&dparams};
65   schunk = blosc2_schunk_new(&storage);
66 
67   blosc_set_timestamp(&last);
68   for (nchunk = 0; nchunk < NCHUNKS; nchunk++) {
69     for (i = 0; i < CHUNKSIZE; i++) {
70       data[i] = i + (int64_t)nchunk * CHUNKSIZE;
71     }
72     nchunks = blosc2_schunk_append_buffer(schunk, data, isize);
73     assert(nchunks == nchunk + 1);
74   }
75   /* Gather some info */
76   nbytes = schunk->nbytes;
77   cbytes = schunk->cbytes;
78   blosc_set_timestamp(&current);
79   ttotal = blosc_elapsed_secs(last, current);
80   printf("Compression ratio: %.1f MB -> %.1f MB (%.1fx)\n",
81          nbytes / MB, cbytes / MB, (1. * nbytes) / cbytes);
82   printf("Compression time: %.3g s, %.1f MB/s\n",
83          ttotal, nbytes / (ttotal * MB));
84 
85   /* Retrieve and decompress the chunks (0-based count) */
86   blosc_set_timestamp(&last);
87   for (nchunk = NCHUNKS - 1; nchunk >= 0; nchunk--) {
88     dsize = blosc2_schunk_decompress_chunk(schunk, nchunk, data_dest, isize);
89   }
90   if (dsize < 0) {
91     printf("Decompression error.  Error code: %d\n", dsize);
92     return dsize;
93   }
94   blosc_set_timestamp(&current);
95   ttotal = blosc_elapsed_secs(last, current);
96   printf("Decompression time: %.3g s, %.1f MB/s\n",
97          ttotal, nbytes / (ttotal * MB));
98 
99   /* Check integrity of the first chunk */
100   for (i = 0; i < CHUNKSIZE; i++) {
101     if (data_dest[i] != (int64_t)i) {
102       printf("Decompressed data differs from original %d, %ld!\n",
103              i, (long)data_dest[i]);
104       return -1;
105     }
106   }
107 
108   printf("Successful roundtrip!\n");
109 
110   /* Free resources */
111   blosc2_schunk_free(schunk);
112   blosc_destroy();
113 
114   return 0;
115 }
116