]> git.sesse.net Git - remoteglot-book/blobdiff - binloader.cpp
Switch value format to protobuf. Slightly smaller, easier to deal with extensions...
[remoteglot-book] / binloader.cpp
index e472364e47b234e8a3a0ed02f03c47c8d47a7582..fdad082502a21c10223ae50748551d9fb3678896 100644 (file)
 #include <memory>
 #include <string>
 #include <string.h>
-#include "count.h"
+#include "count.pb.h"
 #include "hash.h"
 
+#define DUMMY_TIMESTAMP 32503680000
+
 using namespace std;
 
 enum Result { WHITE = 0, DRAW, BLACK };
@@ -100,6 +102,7 @@ int main(int argc, char **argv)
        }
 
        printf("Writing SSTables...\n");
+       string buf;  // Keep allocated.
        for (int i = 0; i < num_buckets; ++i) {
                char filename[256];
                snprintf(filename, sizeof(filename), "%s.part%04d", argv[argc - 2], i);
@@ -111,26 +114,28 @@ int main(int argc, char **argv)
                for (size_t j = 0; j < elems[i].size(); ++j) {
                        const Element &e = elems[i][j];
                        if (e.result == WHITE) {
-                               ++c.white;
+                               c.set_white(c.white() + 1);
                        } else if (e.result == DRAW) {
-                               ++c.draw;
+                               c.set_draw(c.draw() + 1);
                        } else if (e.result == BLACK) {
-                               ++c.black;
+                               c.set_black(c.black() + 1);
                        }
                        if (e.white_elo >= 100 && e.black_elo >= 100) {
-                               c.sum_white_elo += e.white_elo;
-                               c.sum_black_elo += e.black_elo;
-                               ++c.num_elo;
+                               c.set_sum_white_elo(c.sum_white_elo() + e.white_elo);
+                               c.set_sum_black_elo(c.sum_black_elo() + e.black_elo);
+                               c.set_num_elo(c.num_elo() + 1);
                        }
-                       if (c.first_timestamp == DUMMY_TIMESTAMP ||
-                           e.timestamp < c.first_timestamp) {
-                               c.first_timestamp = e.timestamp;
-                               c.opening_num = e.opening_num;
+                       if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
+                               if (e.timestamp != DUMMY_TIMESTAMP) {
+                                       c.set_first_timestamp(e.timestamp);
+                               }
+                               c.set_opening_num(e.opening_num);
                        }
                        if (j == elems[i].size() - 1 || e.bpfen_and_move != elems[i][j + 1].bpfen_and_move) {
+                               c.SerializeToString(&buf);
                                mtbl_writer_add(mtbl,
                                        (const uint8_t *)e.bpfen_and_move.data(), e.bpfen_and_move.size(),
-                                       (const uint8_t *)&c, sizeof(c));
+                                       (const uint8_t *)buf.data(), buf.size());
                                c = Count();
                        }
                }