]> git.sesse.net Git - remoteglot-book/blob - binloader.cpp
Reduce binloader RAM requirements by over 40%.
[remoteglot-book] / binloader.cpp
1 //#define _GLIBCXX_PARALLEL
2
3 // Usage: ./binloader IN1 IN2 IN3 ... OUT NUM_BUCKETS
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 int main(int argc, char **argv)
49 {
50         int num_buckets = atoi(argv[argc - 1]);
51
52         vector<vector<Element>> elems;
53         elems.resize(num_buckets);
54
55         size_t num_elems = 0;
56         for (int i = 1; i < argc - 2; ++i) {
57                 FILE *fp = fopen(argv[i], "rb");
58                 if (fp == NULL) {
59                         perror(argv[i]);
60                         exit(1);
61                 }
62                 for ( ;; ) {
63                         int bpfen_len = getc(fp);
64                         if (bpfen_len == -1) {
65                                 break;
66                         }
67                 
68                         char *bpfen = arena.alloc(bpfen_len);
69                         if (fread(bpfen, bpfen_len, 1, fp) != 1) {
70                                 perror("fread()");
71                 //              exit(1);
72                                 break;
73                         }
74
75                         int r = getc(fp);
76                         if (r == -1) {
77                                 perror("getc()");
78                                 //exit(1);
79                                 break;
80                         }
81
82                         int opening_num, white_elo, black_elo, file_num;
83                         time_t timestamp;
84                         long start_position;
85                         if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
86                                 perror("fread()");
87                                 //exit(1);
88                                 break;
89                         }
90                         if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
91                                 perror("fread()");
92                                 //exit(1);
93                                 break;
94                         }
95                         if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
96                                 perror("fread()");
97                                 //exit(1);
98                                 break;
99                         }
100                         if (fread(&timestamp, sizeof(timestamp), 1, fp) != 1) {
101                                 perror("fread()");
102                                 //exit(1);
103                                 break;
104                         }
105                         if (fread(&file_num, sizeof(file_num), 1, fp) != 1) {
106                                 perror("fread()");
107                                 //exit(1);
108                                 break;
109                         }
110                         if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
111                                 perror("fread()");
112                                 //exit(1);
113                                 break;
114                         }
115
116
117                         char move[8];
118                         int l = getc(fp);
119                         if (l == -1) {
120                                 break;
121                         }
122                         if (l >= int(sizeof(move))) {
123                                 fprintf(stderr, "Overlong move (%d bytes)\n", l);
124                 //              exit(1);
125                                 break;
126                         }
127                         if (fread(&move[0], l, 1, fp) != 1) {
128                                 perror("fread()");
129                 //              exit(1);
130                                 break;
131                         }
132                         move[l] = 0;
133
134                         int bucket = hash_key_to_bucket(bpfen, bpfen_len, num_buckets);
135                         elems[bucket].emplace_back(Element {bpfen, bpfen_len, {}, Result(r), opening_num, white_elo, black_elo, timestamp, file_num, start_position});
136                         strcpy(elems[bucket].back().move, move);
137                         ++num_elems;
138                 }
139                 fclose(fp);
140
141                 printf("Read %ld elems\n", num_elems);
142         }
143
144         printf("Sorting...\n");
145         for (int i = 0; i < num_buckets; ++i) {
146                 sort(elems[i].begin(), elems[i].end());
147         }
148
149         printf("Writing SSTables...\n");
150         string buf;  // Keep allocated.
151         for (int i = 0; i < num_buckets; ++i) {
152                 char filename[256];
153                 snprintf(filename, sizeof(filename), "%s.part%04d", argv[argc - 2], i);
154
155                 mtbl_writer_options* wopt = mtbl_writer_options_init();
156                 mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
157                 mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
158                 Count c;
159                 unordered_set<string> moves;
160                 for (size_t j = 0; j < elems[i].size(); ++j) {
161                         const Element &e = elems[i][j];
162                         if (e.result == WHITE) {
163                                 c.set_white(c.white() + 1);
164                         } else if (e.result == DRAW) {
165                                 c.set_draw(c.draw() + 1);
166                         } else if (e.result == BLACK) {
167                                 c.set_black(c.black() + 1);
168                         }
169                         if (e.white_elo >= 100 && e.black_elo >= 100) {
170                                 c.set_sum_white_elo(c.sum_white_elo() + e.white_elo);
171                                 c.set_sum_black_elo(c.sum_black_elo() + e.black_elo);
172                                 c.set_num_elo(c.num_elo() + 1);
173                         }
174                         if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
175                                 if (e.timestamp != DUMMY_TIMESTAMP) {
176                                         c.set_first_timestamp(e.timestamp);
177                                 }
178                                 c.set_opening_num(e.opening_num);
179                                 c.set_pgn_file_num(e.file_num);
180                                 c.set_pgn_start_position(e.start_position);
181                         }
182                         if (!moves.count(e.move)) {
183                                 moves.insert(e.move);
184                                 c.add_move(e.move);
185                         }
186                         if (j == elems[i].size() - 1 ||
187                             e.bpfen_len != elems[i][j + 1].bpfen_len ||
188                             memcmp(e.bpfen, elems[i][j + 1].bpfen, e.bpfen_len) != 0) {
189                                 c.SerializeToString(&buf);
190                                 mtbl_writer_add(mtbl,
191                                         (const uint8_t *)e.bpfen, e.bpfen_len,
192                                         (const uint8_t *)buf.data(), buf.size());
193                                 c = Count();
194                                 moves.clear();
195                         }
196                 }
197                 mtbl_writer_destroy(&mtbl);
198         }
199 }