]> git.sesse.net Git - remoteglot-book/blob - binloader.cpp
Fix move numbering when navigating in history.
[remoteglot-book] / binloader.cpp
1 //#define _GLIBCXX_PARALLEL
2
3 // Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS NUM_POS_PER_SUBSHARD
4
5 #include <stdio.h>
6 #include <vector>
7 #include <mtbl.h>
8 #include <algorithm>
9 #include <utility>
10 #include <memory>
11 #include <string>
12 #include <unordered_set>
13 #include <string.h>
14 #include "count.pb.h"
15 #include "arena.h"
16 #include "hash.h"
17
18 #define DUMMY_TIMESTAMP 32503680000
19
20 using namespace std;
21
22 Arena arena;
23
24 enum Result { WHITE = 0, DRAW, BLACK };
25 struct Element {
26         char *bpfen;
27         int bpfen_len;
28         char move[8];   // Na1xc3+
29         Result result;
30         int opening_num, white_elo, black_elo;
31         int file_num;
32         time_t timestamp;
33         long start_position;
34
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);
38                 if (s < 0) {
39                         return true;
40                 } else if (s > 0) {
41                         return false;
42                 } else {
43                         return bpfen_len < other.bpfen_len;
44                 }
45         }
46 };
47
48 struct ShardData {
49         vector<Element> elems;
50         unique_ptr<Arena> arena;  // Used to allocate bpfen.
51         int num_written_subshards = 0;
52 };
53
54 void write_subshard(const char *basename, ShardData* shard, int bucket)
55 {
56         string buf;  // Keep allocated.
57         char filename[256];
58         snprintf(filename, sizeof(filename), "%s.part%04d.subshard%04d",
59                 basename, bucket, shard->num_written_subshards++);
60         printf("Writing SSTable %s...\n", filename);
61
62         sort(shard->elems.begin(), shard->elems.end());
63
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);
67         Count c;
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);
77                 }
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);
82                 }
83                 if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
84                         if (e.timestamp != DUMMY_TIMESTAMP) {
85                                 c.set_first_timestamp(e.timestamp);
86                         }
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);
90                 }
91                 if (!moves.count(e.move)) {
92                         moves.insert(e.move);
93                         c.add_move(e.move);
94                 }
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);
99                         mtbl_writer_add(mtbl,
100                                 (const uint8_t *)e.bpfen, e.bpfen_len,
101                                 (const uint8_t *)buf.data(), buf.size());
102                         c = Count();
103                         moves.clear();
104                 }
105         }
106         mtbl_writer_destroy(&mtbl);
107
108         shard->elems.clear();
109         shard->arena.reset(new Arena);
110 }
111
112 int main(int argc, char **argv)
113 {
114         int num_buckets = atoi(argv[argc - 2]);
115         size_t num_pos_per_subshard = atoi(argv[argc - 1]);  // 500000 is a reasonable value.
116
117         vector<ShardData> shards;
118         shards.resize(num_buckets);
119
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);
123         }
124
125         size_t num_elems = 0;
126         for (int i = 1; i < argc - 3; ++i) {
127                 FILE *fp;
128                 if (strcmp(argv[i], "-") == 0) {
129                         fp = stdin;
130                 } else {
131                         fp = fopen(argv[i], "rb");
132                         if (fp == NULL) {
133                                 perror(argv[i]);
134                                 exit(1);
135                         }
136                 }
137                 for ( ;; ) {
138                         char bpfen[256];
139
140                         int bpfen_len = getc(fp);
141                         if (bpfen_len == -1) {
142                                 break;
143                         }
144                         if (bpfen_len >= int(sizeof(bpfen))) {
145                                 fprintf(stderr, "Overlong BPFEN (%d bytes)\n", bpfen_len);
146                 //              exit(1);
147                                 break;
148                         }
149                         if (fread(bpfen, bpfen_len, 1, fp) != 1) {
150                                 perror("fread()");
151                 //              exit(1);
152                                 break;
153                         }
154
155                         int r = getc(fp);
156                         if (r == -1) {
157                                 perror("getc()");
158                                 //exit(1);
159                                 break;
160                         }
161
162                         int opening_num, white_elo, black_elo, file_num;
163                         time_t timestamp;
164                         long start_position;
165                         if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
166                                 perror("fread()");
167                                 //exit(1);
168                                 break;
169                         }
170                         if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
171                                 perror("fread()");
172                                 //exit(1);
173                                 break;
174                         }
175                         if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
176                                 perror("fread()");
177                                 //exit(1);
178                                 break;
179                         }
180                         if (fread(&timestamp, sizeof(timestamp), 1, fp) != 1) {
181                                 perror("fread()");
182                                 //exit(1);
183                                 break;
184                         }
185                         if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
186                                 perror("fread()");
187                                 //exit(1);
188                                 break;
189                         }
190                         if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
191                                 perror("fread()");
192                                 //exit(1);
193                                 break;
194                         }
195
196
197                         char move[8];
198                         int l = getc(fp);
199                         if (l == -1) {
200                                 break;
201                         }
202                         if (l >= int(sizeof(move))) {
203                                 fprintf(stderr, "Overlong move (%d bytes)\n", l);
204                 //              exit(1);
205                                 break;
206                         }
207                         if (fread(&move[0], l, 1, fp) != 1) {
208                                 perror("fread()");
209                 //              exit(1);
210                                 break;
211                         }
212                         move[l] = 0;
213
214                         int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
215                         Element e;
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);
228                         ++num_elems;
229
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);
233                         }
234                 }
235                 fclose(fp);
236
237                 printf("Read %ld elems\n", num_elems);
238         }
239
240         for (int i = 0; i < num_buckets; ++i) {
241                 write_subshard(argv[argc - 3], &shards[i], i);
242         }
243 }
244