]> git.sesse.net Git - remoteglot-book/blob - binloader.cpp
Use the new pgn-extract partitioning to get the file offsets right.
[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 "hash.h"
16
17 #define DUMMY_TIMESTAMP 32503680000
18
19 using namespace std;
20
21 enum Result { WHITE = 0, DRAW, BLACK };
22 struct Element {
23         string bpfen;
24         string move;
25         Result result;
26         int opening_num, white_elo, black_elo;
27         time_t timestamp;
28         long start_position;
29
30         bool operator< (const Element& other) const {
31                 return bpfen < other.bpfen;
32         }
33 };
34
35 int main(int argc, char **argv)
36 {
37         int num_buckets = atoi(argv[argc - 1]);
38
39         vector<vector<Element>> elems;
40         elems.resize(num_buckets);
41
42         size_t num_elems = 0;
43         for (int i = 1; i < argc - 2; ++i) {
44                 FILE *fp = fopen(argv[i], "rb");
45                 if (fp == NULL) {
46                         perror(argv[i]);
47                         exit(1);
48                 }
49                 for ( ;; ) {
50                         int l = getc(fp);
51                         if (l == -1) {
52                                 break;
53                         }
54                 
55                         string bpfen;
56                         bpfen.resize(l);
57                         if (fread(&bpfen[0], l, 1, fp) != 1) {
58                                 perror("fread()");
59                 //              exit(1);
60                                 break;
61                         }
62
63                         int r = getc(fp);
64                         if (r == -1) {
65                                 perror("getc()");
66                                 //exit(1);
67                                 break;
68                         }
69
70                         int opening_num, white_elo, black_elo;
71                         time_t timestamp;
72                         long start_position;
73                         if (fread(&white_elo, sizeof(white_elo), 1, fp) != 1) {
74                                 perror("fread()");
75                                 //exit(1);
76                                 break;
77                         }
78                         if (fread(&black_elo, sizeof(black_elo), 1, fp) != 1) {
79                                 perror("fread()");
80                                 //exit(1);
81                                 break;
82                         }
83                         if (fread(&opening_num, sizeof(opening_num), 1, fp) != 1) {
84                                 perror("fread()");
85                                 //exit(1);
86                                 break;
87                         }
88                         if (fread(&timestamp, sizeof(timestamp), 1, fp) != 1) {
89                                 perror("fread()");
90                                 //exit(1);
91                                 break;
92                         }
93                         if (fread(&start_position, sizeof(start_position), 1, fp) != 1) {
94                                 perror("fread()");
95                                 //exit(1);
96                                 break;
97                         }
98
99
100                         l = getc(fp);
101                         if (l == -1) {
102                                 break;
103                         }
104                         string move;
105                         move.resize(l);
106                         if (fread(&move[0], l, 1, fp) != 1) {
107                                 perror("fread()");
108                 //              exit(1);
109                                 break;
110                         }
111
112                         int bucket = hash_key_to_bucket(bpfen.data(), bpfen.size(), num_buckets);
113                         elems[bucket].emplace_back(Element {std::move(bpfen), std::move(move), Result(r), opening_num, white_elo, black_elo, timestamp, start_position});
114                         ++num_elems;
115                 }
116                 fclose(fp);
117
118                 printf("Read %ld elems\n", num_elems);
119         }
120
121         printf("Sorting...\n");
122         for (int i = 0; i < num_buckets; ++i) {
123                 sort(elems[i].begin(), elems[i].end());
124         }
125
126         printf("Writing SSTables...\n");
127         string buf;  // Keep allocated.
128         for (int i = 0; i < num_buckets; ++i) {
129                 char filename[256];
130                 snprintf(filename, sizeof(filename), "%s.part%04d", argv[argc - 2], i);
131
132                 mtbl_writer_options* wopt = mtbl_writer_options_init();
133                 mtbl_writer_options_set_compression(wopt, MTBL_COMPRESSION_SNAPPY);
134                 mtbl_writer* mtbl = mtbl_writer_init(filename, wopt);
135                 Count c;
136                 unordered_set<string> moves;
137                 for (size_t j = 0; j < elems[i].size(); ++j) {
138                         const Element &e = elems[i][j];
139                         if (e.result == WHITE) {
140                                 c.set_white(c.white() + 1);
141                         } else if (e.result == DRAW) {
142                                 c.set_draw(c.draw() + 1);
143                         } else if (e.result == BLACK) {
144                                 c.set_black(c.black() + 1);
145                         }
146                         if (e.white_elo >= 100 && e.black_elo >= 100) {
147                                 c.set_sum_white_elo(c.sum_white_elo() + e.white_elo);
148                                 c.set_sum_black_elo(c.sum_black_elo() + e.black_elo);
149                                 c.set_num_elo(c.num_elo() + 1);
150                         }
151                         if (!c.has_first_timestamp() || e.timestamp < c.first_timestamp()) {
152                                 if (e.timestamp != DUMMY_TIMESTAMP) {
153                                         c.set_first_timestamp(e.timestamp);
154                                 }
155                                 c.set_opening_num(e.opening_num);
156                                 c.set_pgn_start_position(e.start_position);
157                         }
158                         if (!moves.count(e.move)) {
159                                 moves.insert(e.move);
160                                 c.add_move(e.move);
161                         }
162                         if (j == elems[i].size() - 1 || e.bpfen != elems[i][j + 1].bpfen) {
163                                 c.SerializeToString(&buf);
164                                 mtbl_writer_add(mtbl,
165                                         (const uint8_t *)e.bpfen.data(), e.bpfen.size(),
166                                         (const uint8_t *)buf.data(), buf.size());
167                                 c = Count();
168                                 moves.clear();
169                         }
170                 }
171                 mtbl_writer_destroy(&mtbl);
172         }
173 }