]> git.sesse.net Git - remoteglot-book/blob - binmerger.cpp
Update .gitignore.
[remoteglot-book] / binmerger.cpp
1 #include <stdio.h>
2 #include <mtbl.h>
3 #include <memory>
4 #include <string>
5 #include <string.h>
6 #include <assert.h>
7 #include "count.h"
8
9 using namespace std;
10
11
12 void merge_count(void* userdata,
13                  const uint8_t *key, size_t len_key,
14                  const uint8_t *val0, size_t len_val0,
15                  const uint8_t *val1, size_t len_val1,
16                  uint8_t **merged_val, size_t *len_merged_val)
17 {
18         assert(len_val0 == sizeof(Count));
19         assert(len_val1 == sizeof(Count));
20
21         const Count* c0 = reinterpret_cast<const Count*>(val0);
22         const Count* c1 = reinterpret_cast<const Count*>(val1);
23         unique_ptr<Count> c((Count *)malloc(sizeof(Count)));  // Needs to be with malloc, per merger spec.
24
25         c->white = c0->white + c1->white;
26         c->draw = c0->draw + c1->draw;
27         c->black = c0->black + c1->black;
28         c->opening_num = c0->opening_num;  // Arbitrary choice.
29         c->sum_white_elo = c0->sum_white_elo + c1->sum_white_elo;
30         c->sum_black_elo = c0->sum_black_elo + c1->sum_black_elo;
31         c->num_elo = c0->num_elo + c1->num_elo;
32
33         *merged_val = reinterpret_cast<uint8_t *>(c.release());
34         *len_merged_val = sizeof(Count);
35 }
36
37 int main(int argc, char **argv)
38 {
39         mtbl_merger_options* mopt = mtbl_merger_options_init();
40         mtbl_merger_options_set_merge_func(mopt, merge_count, NULL);
41         mtbl_merger* merger = mtbl_merger_init(mopt);
42
43         for (int i = 1; i < argc - 1; ++i) {
44                 mtbl_reader* mtbl = mtbl_reader_init(argv[i], NULL);
45                 mtbl_merger_add_source(merger, mtbl_reader_source(mtbl));
46         }
47
48         mtbl_writer_options* wopt = mtbl_writer_options_init();
49         mtbl_writer_options_set_block_size(wopt, 65536);
50         mtbl_writer* writer = mtbl_writer_init(argv[argc - 1], wopt);
51         mtbl_source_write(mtbl_merger_source(merger), writer);
52         mtbl_writer_destroy(&writer);
53 }