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