X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=binlookup.cpp;h=286e831ff02ada183f1901cc7d73b66f44b1b227;hb=04d675c5a4a867b2c15e2ef64d5179353fd1489e;hp=b3a0488b0672e240c93bff1fc1c3d450c9fbd6a9;hpb=8eb9465efeb8813c93bfab2a8f385549f45bd827;p=remoteglot-book diff --git a/binlookup.cpp b/binlookup.cpp index b3a0488..286e831 100644 --- a/binlookup.cpp +++ b/binlookup.cpp @@ -6,46 +6,109 @@ #include #include #include -#include "count.h" +#include +#include "count.pb.h" +#include "merge_count.h" +#include "hash.h" using namespace std; -int main(int argc, char **argv) +string read_hex_line(FILE *fp) { - int num_buckets = atoi(argv[2]); - const char *hex_prefix = argv[3]; - const int prefix_len = strlen(hex_prefix) / 2; - uint8_t *prefix = new uint8_t[prefix_len]; + char hex[256]; + if (fgets(hex, sizeof(hex), fp) == NULL) { + return ""; + } + char *ptr = strchr(hex, '\n'); + if (ptr != NULL) { + *ptr = 0; + } + + string ret; + ret.resize(strlen(hex) / 2); - for (int i = 0; i < prefix_len; ++i) { + for (size_t i = 0; i < ret.size(); ++i) { char x[3]; - x[0] = hex_prefix[i * 2 + 0]; - x[1] = hex_prefix[i * 2 + 1]; + x[0] = hex[i * 2 + 0]; + x[1] = hex[i * 2 + 1]; x[2] = 0; int k; sscanf(x, "%02x", &k); - prefix[i] = k; + ret[i] = k; } + return ret; +} +int main(int argc, char **argv) +{ + int num_buckets = atoi(argv[2]); + mtbl_reader** mtbls = new mtbl_reader*[num_buckets]; + const mtbl_source** srcs = new const mtbl_source*[num_buckets]; for (int i = 0; i < num_buckets; ++i) { - char filename[256]; - snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], i); + mtbls[i] = NULL; + srcs[i] = NULL; + } + + while (!feof(stdin)) { + string bpfen = read_hex_line(stdin); + if (bpfen.empty()) { + break; + } + string prev_pos_hash = read_hex_line(stdin); + + string searchkey = bpfen + prev_pos_hash; + int bucket = hash_key_to_bucket(searchkey.data(), searchkey.size(), num_buckets); + if (mtbls[bucket] == NULL) { + char filename[256]; + snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], bucket); + + mtbls[bucket] = mtbl_reader_init(filename, NULL); + srcs[bucket] = mtbl_reader_source(mtbls[bucket]); + } + + // Give back the hash of the position to be kind to the user + uint16_t board_hash = util::Hash32(bpfen.data(), bpfen.size()); + printf("%d\n", board_hash); - mtbl_reader* mtbl = mtbl_reader_init(filename, NULL); - const mtbl_source *src = mtbl_reader_source(mtbl); - mtbl_iter *it = mtbl_source_get_prefix(src, prefix, prefix_len); + mtbl_iter *it = mtbl_source_get_prefix(srcs[bucket], (const uint8_t *)searchkey.data(), searchkey.size()); const uint8_t *key, *val; size_t len_key, len_val; + Count c; + bool has_c = false; while (mtbl_iter_next(it, &key, &len_key, &val, &len_val)) { - string move((char *)(key + prefix_len), len_key - prefix_len); - const Count* c = (Count *)val; - printf("%s %d %d %d %u %f %f %d %ld\n", move.c_str(), - c->white, c->draw, c->black, c->opening_num, - float(c->sum_white_elo) / c->num_elo, - float(c->sum_black_elo) / c->num_elo, - c->num_elo, c->first_timestamp); + if (has_c) { + Count tmpc; + tmpc.ParseFromArray(val, len_val); + c = merge_count(c, tmpc); + } else { + c.ParseFromArray(val, len_val); + has_c = true; + } + } + + if (has_c) { + printf("%d %d %d %d %u %ld %ld %d %ld %d %ld", + c.white(), c.draw(), c.black(), c.computer(), + c.opening_num(), c.sum_white_elo(), c.sum_black_elo(), + c.num_elo(), c.first_timestamp(), + c.pgn_file_num(), + c.pgn_start_position()); + for (int j = 0; j < c.move_size(); ++j) { + printf(" %s", c.move(j).c_str()); + } + printf("\n"); + } else { + printf("-\n"); + } + fflush(stdout); + mtbl_iter_destroy(&it); + } + + for (int i = 0; i < num_buckets; ++i) { + if (mtbls[i] != NULL) { + mtbl_reader_destroy(&mtbls[i]); } } }