]> git.sesse.net Git - remoteglot-book/blob - binlookup.cpp
Switch value format to protobuf. Slightly smaller, easier to deal with extensions...
[remoteglot-book] / binlookup.cpp
1 #include <stdio.h>
2 #include <vector>
3 #include <mtbl.h>
4 #include <algorithm>
5 #include <utility>
6 #include <memory>
7 #include <string>
8 #include <string.h>
9 #include "count.pb.h"
10
11 using namespace std;
12
13 int main(int argc, char **argv)
14 {
15         int num_buckets = atoi(argv[2]);
16         const char *hex_prefix = argv[3];
17         const int prefix_len = strlen(hex_prefix) / 2;
18         uint8_t *prefix = new uint8_t[prefix_len];
19
20         for (int i = 0; i < prefix_len; ++i) {
21                 char x[3];
22                 x[0] = hex_prefix[i * 2 + 0];
23                 x[1] = hex_prefix[i * 2 + 1];
24                 x[2] = 0;
25                 int k;
26                 sscanf(x, "%02x", &k);
27                 prefix[i] = k;
28         }
29
30         for (int i = 0; i < num_buckets; ++i) {
31                 char filename[256];
32                 snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], i);
33
34                 mtbl_reader* mtbl = mtbl_reader_init(filename, NULL);
35                 const mtbl_source *src = mtbl_reader_source(mtbl);
36                 mtbl_iter *it = mtbl_source_get_prefix(src, prefix, prefix_len);
37
38                 const uint8_t *key, *val;
39                 size_t len_key, len_val;
40
41                 while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
42                         string move((char *)(key + prefix_len), len_key - prefix_len);
43                         Count c;
44                         c.ParseFromArray(val, len_val);
45                         printf("%s %d %d %d %u %f %f %d %ld\n", move.c_str(),
46                                 c.white(), c.draw(), c.black(), c.opening_num(),
47                                 double(c.sum_white_elo()) / c.num_elo(),
48                                 double(c.sum_black_elo()) / c.num_elo(),
49                                 c.num_elo(), c.first_timestamp());
50                 }
51         }
52 }