]> git.sesse.net Git - remoteglot-book/blob - binlookup.cpp
Indent fix.
[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                 string prev_pos_hash = read_hex_line(stdin);
58
59                 string searchkey = bpfen + prev_pos_hash;
60                 int bucket = hash_key_to_bucket(searchkey.data(), searchkey.size(), num_buckets);
61                 if (mtbls[bucket] == NULL) {
62                         char filename[256];
63                         snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], bucket);
64
65                         mtbls[bucket] = mtbl_reader_init(filename, NULL);
66                         srcs[bucket] = mtbl_reader_source(mtbls[bucket]);
67                 }
68
69                 // Give back the hash of the position to be kind to the user
70                 uint16_t board_hash = util::Hash32(bpfen.data(), bpfen.size());
71                 printf("%d\n", board_hash);
72
73                 mtbl_iter *it = mtbl_source_get_prefix(srcs[bucket], (const uint8_t *)searchkey.data(), searchkey.size());
74
75                 const uint8_t *key, *val;
76                 size_t len_key, len_val;
77
78                 Count c;
79                 bool has_c = false;
80                 while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) {
81                         if (has_c) {
82                                 Count tmpc;
83                                 tmpc.ParseFromArray(val, len_val);
84                                 c = merge_count(c, tmpc);
85                         } else {
86                                 c.ParseFromArray(val, len_val);
87                                 has_c = true;
88                         }
89                 }
90
91                 if (has_c) {
92                         printf("%d %d %d %d %u %ld %ld %d %ld %d %ld",
93                                 c.white(), c.draw(), c.black(), c.computer(),
94                                 c.opening_num(), c.sum_white_elo(), c.sum_black_elo(),
95                                 c.num_elo(), c.first_timestamp(),
96                                 c.pgn_file_num(),
97                                 c.pgn_start_position());
98                         for (int j = 0; j < c.move_size(); ++j) {
99                                 printf(" %s", c.move(j).c_str());
100                         }
101                         printf("\n");
102                 } else {
103                         printf("-\n");
104                 }
105                 fflush(stdout);
106                 mtbl_iter_destroy(&it);
107         }
108
109         for (int i = 0; i < num_buckets; ++i) {
110                 if (mtbls[i] != NULL) {
111                         mtbl_reader_destroy(&mtbls[i]);
112                 }
113         }
114 }