1 //#define _GLIBCXX_PARALLEL
3 // Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS NUM_POS_PER_SUBSHARD
12 #include <unordered_set>
18 #define DUMMY_TIMESTAMP 32503680000
24 enum Result { WHITE = 0, DRAW, BLACK };
28 char move[8]; // Na1xc3+
30 int opening_num, white_elo, black_elo;
35 bool operator< (const Element& other) const {
36 int shared_len = min(bpfen_len, other.bpfen_len);
37 int s = memcmp(bpfen, other.bpfen, shared_len);
43 return bpfen_len < other.bpfen_len;
49 vector<Element> elems;
50 unique_ptr<Arena> arena; // Used to allocate bpfen.
51 int num_written_subshards = 0;
54 void write_subshard(const char *basename, ShardData* shard, int bucket)
56 string buf; // Keep allocated.
58 snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
59 basename, bucket, shard->num_written_subshards++);
60 printf("Writing SSTable %s...\n", filename);
62 sort(shard->elems.begin(), shard->elems.end());
64 mtbl_writer_options* wopt = mtbl_writer_options_init();
65 mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
66 mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
68 unordered_set<string> moves;
69 for (size_t i = 0; i < shard->elems.size(); ++i) {
70 const Element &e = shard->elems[i];
71 if (e.result == WHITE) {
72 c.set_white(c.white() + 1);
73 } else if (e.result == DRAW) {
74 c.set_draw(c.draw() + 1);
75 } else if (e.result == BLACK) {
76 c.set_black(c.black() + 1);
78 if (e.white_elo >= 100 && e.black_elo >= 100) {
79 c.set_sum_white_elo(c.sum_white_elo() + e.white_elo);
80 c.set_sum_black_elo(c.sum_black_elo() + e.black_elo);
81 c.set_num_elo(c.num_elo() + 1);
83 if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
84 if (e.timestamp != DUMMY_TIMESTAMP) {
85 c.set_first_timestamp(e.timestamp);
87 c.set_opening_num(e.opening_num);
88 c.set_pgn_file_num(e.file_num);
89 c.set_pgn_start_position(e.start_position);
91 if (!moves.count(e.move)) {
95 if (i == shard->elems.size() - 1 ||
96 e.bpfen_len != shard->elems[i + 1].bpfen_len ||
97 memcmp(e.bpfen, shard->elems[i + 1].bpfen, e.bpfen_len) != 0) {
98 c.SerializeToString(&buf);
100 (const uint8_t *)e.bpfen, e.bpfen_len,
101 (const uint8_t *)buf.data(), buf.size());
106 mtbl_writer_destroy(&mtbl);
108 shard->elems.clear();
109 shard->arena.reset(new Arena);
112 int main(int argc, char **argv)
114 int num_buckets = atoi(argv[argc - 2]);
115 size_t num_pos_per_subshard = atoi(argv[argc - 1]); // 500000 is a reasonable value.
117 vector<ShardData> shards;
118 shards.resize(num_buckets);
120 for (int i = 0; i < num_buckets; ++i) {
121 shards[i].elems.reserve(num_pos_per_subshard);
122 shards[i].arena.reset(new Arena);
125 size_t num_elems = 0;
126 for (int i = 1; i < argc - 3; ++i) {
128 if (strcmp(argv[i], "-") == 0) {
131 fp = fopen(argv[i], "rb");
140 int bpfen_len = getc(fp);
141 if (bpfen_len == -1) {
144 if (bpfen_len >= int(sizeof(bpfen))) {
145 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
149 if (fread(bpfen, bpfen_len, 1, fp) != 1) {
162 int opening_num, white_elo, black_elo, file_num;
165 if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
170 if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
175 if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
180 if (fread(×tamp, sizeof(timestamp), 1, fp) != 1) {
185 if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
190 if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
202 if (l >= int(sizeof(move))) {
203 fprintf(stderr, "Overlong move (%d bytes)\n", l);
207 if (fread(&move[0], l, 1, fp) != 1) {
214 int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
216 e.bpfen = shards[bucket].arena->alloc(bpfen_len);
217 memcpy(e.bpfen, bpfen, bpfen_len);
218 e.bpfen_len = bpfen_len;
219 strcpy(e.move, move);
220 e.result = Result(r);
221 e.opening_num = opening_num;
222 e.white_elo = white_elo;
223 e.black_elo = black_elo;
224 e.file_num = file_num;
225 e.timestamp = timestamp;
226 e.start_position = start_position;
227 shards[bucket].elems.push_back(e);
230 if (shards[bucket].elems.size() >= num_pos_per_subshard) {
231 write_subshard(argv[argc - 3], &shards[bucket], bucket);
232 shards[bucket].elems.reserve(num_pos_per_subshard);
237 printf("Read %ld elems\n", num_elems);
240 for (int i = 0; i < num_buckets; ++i) {
241 write_subshard(argv[argc - 3], &shards[i], i);