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