]> git.sesse.net Git - remoteglot-book/blobdiff - binlookup.cpp
Switch value format to protobuf. Slightly smaller, easier to deal with extensions...
[remoteglot-book] / binlookup.cpp
index 363e8dc5e8dbe2c8b4e46bce3ebf8dec8085367d..3c969e2dc2701e72238b7f303bff1f00694aad2e 100644 (file)
@@ -6,13 +6,14 @@
 #include <memory>
 #include <string>
 #include <string.h>
-#include "count.h"
+#include "count.pb.h"
 
 using namespace std;
 
 int main(int argc, char **argv)
 {
-       const char *hex_prefix = argv[2];
+       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];
 
@@ -26,19 +27,26 @@ int main(int argc, char **argv)
                prefix[i] = k;
        }
 
-       mtbl_reader* mtbl = mtbl_reader_init(argv[1], NULL);
-       const mtbl_source *src = mtbl_reader_source(mtbl);
-               mtbl_iter *it = mtbl_source_get_prefix(src, prefix, prefix_len);
+       for (int i = 0; i < num_buckets; ++i) {
+               char filename[256];
+               snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], i);
 
-       const uint8_t *key, *val;
-       size_t len_key, len_val;
+               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);
 
-       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 %d %f %f\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);
+               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);
+                       Count c;
+                       c.ParseFromArray(val, len_val);
+                       printf("%s %d %d %d %u %f %f %d %ld\n", move.c_str(),
+                               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());
+               }
        }
 }