]> 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 98b29457e8a807eccb7806d0ab5d230ac6953468..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 };
@@ -20,6 +22,7 @@ struct Element {
        string bpfen_and_move;
        Result result;
        int opening_num, white_elo, black_elo;
+       time_t timestamp;
 
        bool operator< (const Element& other) const {
                return bpfen_and_move < other.bpfen_and_move;
@@ -62,6 +65,7 @@ int main(int argc, char **argv)
                        }
 
                        int opening_num, white_elo, black_elo;
+                       time_t timestamp;
                        if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
                                perror("fread()");
                                //exit(1);
@@ -77,9 +81,14 @@ int main(int argc, char **argv)
                                //exit(1);
                                break;
                        }
+                       if (fread(&timestamp, sizeof(timestamp), 1, fp) != 1) {
+                               perror("fread()");
+                               //exit(1);
+                               break;
+                       }
 
                        int bucket = hash_key_to_bucket(bpfen_and_move.data(), bpfen_and_move.size(), num_buckets);
-                       elems[bucket].emplace_back(Element {move(bpfen_and_move), Result(r), opening_num, white_elo, black_elo});
+                       elems[bucket].emplace_back(Element {move(bpfen_and_move), Result(r), opening_num, white_elo, black_elo, timestamp});
                        ++num_elems;
                }
                fclose(fp);
@@ -93,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);
@@ -104,22 +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);
                        }
-                       c.opening_num = e.opening_num;
                        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.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();
                        }
                }