]> git.sesse.net Git - remoteglot-book/blob - binlookup.cpp
Small cleanup.
[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 #include "hash.h"
11
12 using namespace std;
13
14 int main(int argc, char **argv)
15 {
16         int num_buckets = atoi(argv[2]);
17         mtbl_reader** mtbls = new mtbl_reader*[num_buckets];
18         const mtbl_source** srcs = new const mtbl_source*[num_buckets];
19         for (int i = 0; i < num_buckets; ++i) {
20                 mtbls[i] = NULL;
21                 srcs[i] = NULL;
22         }
23
24         while (!feof(stdin)) {
25                 char hex_bpfen[256];
26                 if (fgets(hex_bpfen, sizeof(hex_bpfen), stdin) == NULL) {
27                         break;
28                 }
29                 char *ptr = strchr(hex_bpfen, '\n');
30                 if (ptr != NULL) {
31                         *ptr = 0;
32                 }
33
34                 const int bpfen_len = strlen(hex_bpfen) / 2;
35                 uint8_t *bpfen = new uint8_t[bpfen_len];
36
37                 for (int i = 0; i < bpfen_len; ++i) {
38                         char x[3];
39                         x[0] = hex_bpfen[i * 2 + 0];
40                         x[1] = hex_bpfen[i * 2 + 1];
41                         x[2] = 0;
42                         int k;
43                         sscanf(x, "%02x", &k);
44                         bpfen[i] = k;
45                 }
46
47                 int bucket = hash_key_to_bucket((const char *)bpfen, bpfen_len, num_buckets);
48                 if (mtbls[bucket] == NULL) {
49                         char filename[256];
50                         snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], bucket);
51
52                         mtbls[bucket] = mtbl_reader_init(filename, NULL);
53                         srcs[bucket] = mtbl_reader_source(mtbls[bucket]);
54                 }
55
56                 mtbl_iter *it = mtbl_source_get(srcs[bucket], bpfen, bpfen_len);
57
58                 const uint8_t *key, *val;
59                 size_t len_key, len_val;
60
61                 if (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
62                         Count c;
63                         c.ParseFromArray(val, len_val);
64                         printf("%d %d %d %u %f %f %d %ld %d %ld",
65                                 c.white(), c.draw(), c.black(), c.opening_num(),
66                                 double(c.sum_white_elo()) / c.num_elo(),
67                                 double(c.sum_black_elo()) / c.num_elo(),
68                                 c.num_elo(), c.first_timestamp(),
69                                 c.pgn_file_num(),
70                                 c.pgn_start_position());
71                         for (int j = 0; j < c.move_size(); ++j) {
72                                 printf(" %s", c.move(j).c_str());
73                         }
74                         printf("\n");
75                 } else {
76                         printf("-\n");
77                 }
78                 fflush(stdout);
79                 mtbl_iter_destroy(&it);
80         }
81
82         for (int i = 0; i < num_buckets; ++i) {
83                 if (mtbls[i] != NULL) {
84                         mtbl_reader_destroy(&mtbls[i]);
85                 }
86         }
87 }