]> git.sesse.net Git - remoteglot-book/blob - binloader.cpp
Fix a field ordering messup caused by the memory saving changes.
[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                         Element e;
136                         e.bpfen = bpfen;
137                         e.bpfen_len = bpfen_len;
138                         strcpy(e.move, move);
139                         e.result = Result(r);
140                         e.opening_num = opening_num;
141                         e.white_elo = white_elo;
142                         e.black_elo = black_elo;
143                         e.file_num = file_num;
144                         e.timestamp = timestamp;
145                         e.start_position = start_position;
146                         elems[bucket].push_back(e);
147                         ++num_elems;
148                 }
149                 fclose(fp);
150
151                 printf("Read %ld elems\n", num_elems);
152         }
153
154         printf("Sorting...\n");
155         for (int i = 0; i < num_buckets; ++i) {
156                 sort(elems[i].begin(), elems[i].end());
157         }
158
159         printf("Writing SSTables...\n");
160         string buf;  // Keep allocated.
161         for (int i = 0; i < num_buckets; ++i) {
162                 char filename[256];
163                 snprintf(filename, sizeof(filename), "%s.part%04d", argv[argc - 2], i);
164
165                 mtbl_writer_options* wopt = mtbl_writer_options_init();
166                 mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
167                 mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
168                 Count c;
169                 unordered_set<string> moves;
170                 for (size_t j = 0; j < elems[i].size(); ++j) {
171                         const Element &e = elems[i][j];
172                         if (e.result == WHITE) {
173                                 c.set_white(c.white() + 1);
174                         } else if (e.result == DRAW) {
175                                 c.set_draw(c.draw() + 1);
176                         } else if (e.result == BLACK) {
177                                 c.set_black(c.black() + 1);
178                         }
179                         if (e.white_elo >= 100 && e.black_elo >= 100) {
180                                 c.set_sum_white_elo(c.sum_white_elo() + e.white_elo);
181                                 c.set_sum_black_elo(c.sum_black_elo() + e.black_elo);
182                                 c.set_num_elo(c.num_elo() + 1);
183                         }
184                         if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
185                                 if (e.timestamp != DUMMY_TIMESTAMP) {
186                                         c.set_first_timestamp(e.timestamp);
187                                 }
188                                 c.set_opening_num(e.opening_num);
189                                 c.set_pgn_file_num(e.file_num);
190                                 c.set_pgn_start_position(e.start_position);
191                         }
192                         if (!moves.count(e.move)) {
193                                 moves.insert(e.move);
194                                 c.add_move(e.move);
195                         }
196                         if (j == elems[i].size() - 1 ||
197                             e.bpfen_len != elems[i][j + 1].bpfen_len ||
198                             memcmp(e.bpfen, elems[i][j + 1].bpfen, e.bpfen_len) != 0) {
199                                 c.SerializeToString(&buf);
200                                 mtbl_writer_add(mtbl,
201                                         (const uint8_t *)e.bpfen, e.bpfen_len,
202                                         (const uint8_t *)buf.data(), buf.size());
203                                 c = Count();
204                                 moves.clear();
205                         }
206                 }
207                 mtbl_writer_destroy(&mtbl);
208         }
209 }