X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=binlookup.cpp;h=286e831ff02ada183f1901cc7d73b66f44b1b227;hb=04d675c5a4a867b2c15e2ef64d5179353fd1489e;hp=afc446c73a7749ef49a9838f3cc17f1d1b1d93b9;hpb=e9b5b1e5b285542f44f0c52ee0b470b22ac7381b;p=remoteglot-book diff --git a/binlookup.cpp b/binlookup.cpp index afc446c..286e831 100644 --- a/binlookup.cpp +++ b/binlookup.cpp @@ -6,11 +6,39 @@ #include #include #include +#include #include "count.pb.h" +#include "merge_count.h" #include "hash.h" using namespace std; +string read_hex_line(FILE *fp) +{ + 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 (size_t i = 0; i < ret.size(); ++i) { + char x[3]; + x[0] = hex[i * 2 + 0]; + x[1] = hex[i * 2 + 1]; + x[2] = 0; + int k; + sscanf(x, "%02x", &k); + ret[i] = k; + } + return ret; +} + int main(int argc, char **argv) { int num_buckets = atoi(argv[2]); @@ -22,29 +50,14 @@ int main(int argc, char **argv) } while (!feof(stdin)) { - char hex_bpfen[256]; - if (fgets(hex_bpfen, sizeof(hex_bpfen), stdin) == NULL) { + string bpfen = read_hex_line(stdin); + if (bpfen.empty()) { break; } - char *ptr = strchr(hex_bpfen, '\n'); - if (ptr != NULL) { - *ptr = 0; - } - - const int bpfen_len = strlen(hex_bpfen) / 2; - uint8_t *bpfen = new uint8_t[bpfen_len]; - - for (int i = 0; i < bpfen_len; ++i) { - char x[3]; - x[0] = hex_bpfen[i * 2 + 0]; - x[1] = hex_bpfen[i * 2 + 1]; - x[2] = 0; - int k; - sscanf(x, "%02x", &k); - bpfen[i] = k; - } + string prev_pos_hash = read_hex_line(stdin); - int bucket = hash_key_to_bucket((const char *)bpfen, bpfen_len, num_buckets); + 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); @@ -53,18 +66,32 @@ int main(int argc, char **argv) srcs[bucket] = mtbl_reader_source(mtbls[bucket]); } - mtbl_iter *it = mtbl_source_get(srcs[bucket], bpfen, bpfen_len); + // 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_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)) { - Count c; - c.ParseFromArray(val, len_val); - printf("%d %d %d %u %f %f %d %ld %d %ld", - c.white(), c.draw(), c.black(), c.opening_num(), - double(c.sum_white_elo()) / c.num_elo(), - double(c.sum_black_elo()) / c.num_elo(), + 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()); @@ -72,6 +99,8 @@ int main(int argc, char **argv) printf(" %s", c.move(j).c_str()); } printf("\n"); + } else { + printf("-\n"); } fflush(stdout); mtbl_iter_destroy(&it);