X-Git-Url: https://git.sesse.net/?p=remoteglot-book;a=blobdiff_plain;f=binlookup.cpp;h=afc446c73a7749ef49a9838f3cc17f1d1b1d93b9;hp=083941de3fdb663aea033ea823ff1aabf310e294;hb=b94d3db60ee33f550078c313560af3718d304ee6;hpb=2a322a4ad576d87535ce8a479f7a516da9457e80 diff --git a/binlookup.cpp b/binlookup.cpp index 083941d..afc446c 100644 --- a/binlookup.cpp +++ b/binlookup.cpp @@ -6,46 +6,80 @@ #include #include #include -#include "count.h" +#include "count.pb.h" +#include "hash.h" using namespace std; int main(int argc, char **argv) { 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]; - - for (int i = 0; i < prefix_len; ++i) { - char x[3]; - x[0] = hex_prefix[i * 2 + 0]; - x[1] = hex_prefix[i * 2 + 1]; - x[2] = 0; - int k; - sscanf(x, "%02x", &k); - prefix[i] = k; + 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) { + mtbls[i] = NULL; + srcs[i] = NULL; } - for (int i = 0; i < num_buckets; ++i) { - char filename[256]; - snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], i); + while (!feof(stdin)) { + char hex_bpfen[256]; + if (fgets(hex_bpfen, sizeof(hex_bpfen), stdin) == NULL) { + 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; + } - 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); + int bucket = hash_key_to_bucket((const char *)bpfen, bpfen_len, 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]); + } + + mtbl_iter *it = mtbl_source_get(srcs[bucket], bpfen, bpfen_len); const uint8_t *key, *val; size_t len_key, len_val; 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\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); + 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(), + 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"); + } + fflush(stdout); + mtbl_iter_destroy(&it); + } + + for (int i = 0; i < num_buckets; ++i) { + if (mtbls[i] != NULL) { + mtbl_reader_destroy(&mtbls[i]); } } }