]> git.sesse.net Git - remoteglot-book/blobdiff - binloader.cpp
Fix move numbering when navigating in history.
[remoteglot-book] / binloader.cpp
index 5bf2502c6867610fbb963c91d63aaf3f717312c8..2ed60cbe78250ff34eba9f7c358968f90c4ddb97 100644 (file)
@@ -1,6 +1,6 @@
 //#define _GLIBCXX_PARALLEL
 
-// Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS
+// Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS NUM_POS_PER_SUBSHARD
 
 #include <stdio.h>
 #include <vector>
 #include <unordered_set>
 #include <string.h>
 #include "count.pb.h"
+#include "arena.h"
 #include "hash.h"
 
 #define DUMMY_TIMESTAMP 32503680000
 
 using namespace std;
 
+Arena arena;
+
 enum Result { WHITE = 0, DRAW, BLACK };
 struct Element {
-       string bpfen;
-       string move;
+       char *bpfen;
+       int bpfen_len;
+       char move[8];   // Na1xc3+
        Result result;
        int opening_num, white_elo, black_elo;
+       int file_num;
        time_t timestamp;
        long start_position;
 
        bool operator< (const Element& other) const {
-               return bpfen < other.bpfen;
+               int shared_len = min(bpfen_len, other.bpfen_len);
+               int s = memcmp(bpfen, other.bpfen, shared_len);
+               if (s < 0) {
+                       return true;
+               } else if (s > 0) {
+                       return false;
+               } else {
+                       return bpfen_len < other.bpfen_len;
+               }
        }
 };
 
+struct ShardData {
+       vector<Element> elems;
+       unique_ptr<Arena> arena;  // Used to allocate bpfen.
+       int num_written_subshards = 0;
+};
+
+void write_subshard(const char *basename, ShardData* shard, int bucket)
+{
+       string buf;  // Keep allocated.
+       char filename[256];
+       snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
+               basename, bucket, shard->num_written_subshards++);
+       printf("Writing SSTable %s...\n", filename);
+
+       sort(shard->elems.begin(), shard->elems.end());
+
+       mtbl_writer_options* wopt = mtbl_writer_options_init();
+       mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
+       mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
+       Count c;
+       unordered_set<string> moves;
+       for (size_t i = 0; i < shard->elems.size(); ++i) {
+               const Element &e = shard->elems[i];
+               if (e.result == WHITE) {
+                       c.set_white(c.white() + 1);
+               } else if (e.result == DRAW) {
+                       c.set_draw(c.draw() + 1);
+               } else if (e.result == BLACK) {
+                       c.set_black(c.black() + 1);
+               }
+               if (e.white_elo >= 100 && e.black_elo >= 100) {
+                       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);
+                       c.set_pgn_file_num(e.file_num);
+                       c.set_pgn_start_position(e.start_position);
+               }
+               if (!moves.count(e.move)) {
+                       moves.insert(e.move);
+                       c.add_move(e.move);
+               }
+               if (i == shard->elems.size() - 1 ||
+                   e.bpfen_len != shard->elems[i + 1].bpfen_len ||
+                   memcmp(e.bpfen, shard->elems[i + 1].bpfen, e.bpfen_len) != 0) {
+                       c.SerializeToString(&buf);
+                       mtbl_writer_add(mtbl,
+                               (const uint8_t *)e.bpfen, e.bpfen_len,
+                               (const uint8_t *)buf.data(), buf.size());
+                       c = Count();
+                       moves.clear();
+               }
+       }
+       mtbl_writer_destroy(&mtbl);
+
+       shard->elems.clear();
+       shard->arena.reset(new Arena);
+}
+
 int main(int argc, char **argv)
 {
-       int num_buckets = atoi(argv[argc - 1]);
+       int num_buckets = atoi(argv[argc - 2]);
+       size_t num_pos_per_subshard = atoi(argv[argc - 1]);  // 500000 is a reasonable value.
 
-       vector<vector<Element>> elems;
-       elems.resize(num_buckets);
+       vector<ShardData> shards;
+       shards.resize(num_buckets);
+
+       for (int i = 0; i < num_buckets; ++i) {
+               shards[i].elems.reserve(num_pos_per_subshard);
+               shards[i].arena.reset(new Arena);
+       }
 
        size_t num_elems = 0;
-       for (int i = 1; i < argc - 2; ++i) {
-               FILE *fp = fopen(argv[i], "rb");
-               if (fp == NULL) {
-                       perror(argv[i]);
-                       exit(1);
+       for (int i = 1; i < argc - 3; ++i) {
+               FILE *fp;
+               if (strcmp(argv[i], "-") == 0) {
+                       fp = stdin;
+               } else {
+                       fp = fopen(argv[i], "rb");
+                       if (fp == NULL) {
+                               perror(argv[i]);
+                               exit(1);
+                       }
                }
                for ( ;; ) {
-                       int l = getc(fp);
-                       if (l == -1) {
+                       char bpfen[256];
+
+                       int bpfen_len = getc(fp);
+                       if (bpfen_len == -1) {
+                               break;
+                       }
+                       if (bpfen_len >= int(sizeof(bpfen))) {
+                               fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
+               //              exit(1);
                                break;
                        }
-               
-                       string bpfen;
-                       bpfen.resize(l);
-                       if (fread(&bpfen[0], l, 1, fp) != 1) {
+                       if (fread(bpfen, bpfen_len, 1, fp) != 1) {
                                perror("fread()");
                //              exit(1);
                                break;
@@ -67,7 +159,7 @@ int main(int argc, char **argv)
                                break;
                        }
 
-                       int opening_num, white_elo, black_elo;
+                       int opening_num, white_elo, black_elo, file_num;
                        time_t timestamp;
                        long start_position;
                        if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
@@ -90,6 +182,11 @@ int main(int argc, char **argv)
                                //exit(1);
                                break;
                        }
+                       if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
+                               perror("fread()");
+                               //exit(1);
+                               break;
+                       }
                        if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
                                perror("fread()");
                                //exit(1);
@@ -97,77 +194,51 @@ int main(int argc, char **argv)
                        }
 
 
-                       l = getc(fp);
+                       char move[8];
+                       int l = getc(fp);
                        if (l == -1) {
                                break;
                        }
-                       string move;
-                       move.resize(l);
+                       if (l >= int(sizeof(move))) {
+                               fprintf(stderr, "Overlong move (%d bytes)\n", l);
+               //              exit(1);
+                               break;
+                       }
                        if (fread(&move[0], l, 1, fp) != 1) {
                                perror("fread()");
                //              exit(1);
                                break;
                        }
-
-                       int bucket = hash_key_to_bucket(bpfen.data(), bpfen.size(), num_buckets);
-                       elems[bucket].emplace_back(Element {std::move(bpfen), std::move(move), Result(r), opening_num, white_elo, black_elo, timestamp, start_position});
+                       move[l] = 0;
+
+                       int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
+                       Element e;
+                       e.bpfen = shards[bucket].arena->alloc(bpfen_len);
+                       memcpy(e.bpfen, bpfen, bpfen_len);
+                       e.bpfen_len = bpfen_len;
+                       strcpy(e.move, move);
+                       e.result = Result(r);
+                       e.opening_num = opening_num;
+                       e.white_elo = white_elo;
+                       e.black_elo = black_elo;
+                       e.file_num = file_num;
+                       e.timestamp = timestamp;
+                       e.start_position = start_position;
+                       shards[bucket].elems.push_back(e);
                        ++num_elems;
+
+                       if (shards[bucket].elems.size() >= num_pos_per_subshard) {
+                               write_subshard(argv[argc - 3], &shards[bucket], bucket);
+                               shards[bucket].elems.reserve(num_pos_per_subshard);
+                       }
                }
                fclose(fp);
 
                printf("Read %ld elems\n", num_elems);
        }
 
-       printf("Sorting...\n");
        for (int i = 0; i < num_buckets; ++i) {
-               sort(elems[i].begin(), elems[i].end());
-       }
-
-       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);
-
-               mtbl_writer_options* wopt = mtbl_writer_options_init();
-               mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
-               mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
-               Count c;
-               unordered_set<string> moves;
-               for (size_t j = 0; j < elems[i].size(); ++j) {
-                       const Element &e = elems[i][j];
-                       if (e.result == WHITE) {
-                               c.set_white(c.white() + 1);
-                       } else if (e.result == DRAW) {
-                               c.set_draw(c.draw() + 1);
-                       } else if (e.result == BLACK) {
-                               c.set_black(c.black() + 1);
-                       }
-                       if (e.white_elo >= 100 && e.black_elo >= 100) {
-                               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);
-                               c.set_pgn_start_position(e.start_position);
-                       }
-                       if (!moves.count(e.move)) {
-                               moves.insert(e.move);
-                               c.add_move(e.move);
-                       }
-                       if (j == elems[i].size() - 1 || e.bpfen != elems[i][j + 1].bpfen) {
-                               c.SerializeToString(&buf);
-                               mtbl_writer_add(mtbl,
-                                       (const uint8_t *)e.bpfen.data(), e.bpfen.size(),
-                                       (const uint8_t *)buf.data(), buf.size());
-                               c = Count();
-                               moves.clear();
-                       }
-               }
-               mtbl_writer_destroy(&mtbl);
+               write_subshard(argv[argc - 3], &shards[i], i);
        }
 }
+