X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=binmerger.cpp;h=7e341592eec4d75c7f94e5702e53fd83926d26b0;hb=55481cd69d21b31bfdcc6e33853ea475ed16f622;hp=612a19b123dd9100056468776eb42436ca18a071;hpb=f7be5b3e074051364f605e13d7e937882c879d6f;p=remoteglot-book diff --git a/binmerger.cpp b/binmerger.cpp index 612a19b..7e34159 100644 --- a/binmerger.cpp +++ b/binmerger.cpp @@ -2,36 +2,63 @@ #include #include #include +#include #include #include -#include "count.h" +#include "count.pb.h" using namespace std; - void merge_count(void* userdata, const uint8_t *key, size_t len_key, const uint8_t *val0, size_t len_val0, const uint8_t *val1, size_t len_val1, uint8_t **merged_val, size_t *len_merged_val) { - assert(len_val0 == sizeof(Count)); - assert(len_val1 == sizeof(Count)); - - const Count* c0 = reinterpret_cast(val0); - const Count* c1 = reinterpret_cast(val1); - unique_ptr c((Count *)malloc(sizeof(Count))); // Needs to be with malloc, per merger spec. - - c->white = c0->white + c1->white; - c->draw = c0->draw + c1->draw; - c->black = c0->black + c1->black; - c->opening_num = c0->opening_num; // Arbitrary choice. - c->sum_white_elo = c0->sum_white_elo + c1->sum_white_elo; - c->sum_black_elo = c0->sum_black_elo + c1->sum_black_elo; - c->num_elo = c0->num_elo + c1->num_elo; - - *merged_val = reinterpret_cast(c.release()); - *len_merged_val = sizeof(Count); + Count c0, c1; + c0.ParseFromArray(val0, len_val0); + c1.ParseFromArray(val1, len_val1); + + Count c; + + c.set_white(c0.white() + c1.white()); + c.set_draw(c0.draw() + c1.draw()); + c.set_black(c0.black() + c1.black()); + c.set_sum_white_elo(c0.sum_white_elo() + c1.sum_white_elo()); + c.set_sum_black_elo(c0.sum_black_elo() + c1.sum_black_elo()); + c.set_num_elo(c0.num_elo() + c1.num_elo()); + if (c0.first_timestamp() <= c1.first_timestamp()) { + c.set_opening_num(c0.opening_num()); + if (c0.has_first_timestamp()) { + c.set_first_timestamp(c0.first_timestamp()); + } + c.set_pgn_start_position(c0.pgn_start_position()); + } else { + c.set_opening_num(c1.opening_num()); + if (c1.has_first_timestamp()) { + c.set_first_timestamp(c1.first_timestamp()); + } + c.set_pgn_start_position(c1.pgn_start_position()); + } + + // Merge the moves, with deduplication. + unordered_set moves; + for (int i = 0; i < c0.move_size(); ++i) { + moves.insert(c0.move(i)); + c.add_move(c0.move(i)); + } + for (int i = 0; i < c1.move_size(); ++i) { + if (!moves.count(c1.move(i))) { + c.add_move(c1.move(i)); + } + } + + static string buf; // Keep allocated. + c.SerializeToString(&buf); + + *merged_val = reinterpret_cast(malloc(buf.size())); + *len_merged_val = buf.size(); + memcpy(*merged_val, buf.data(), buf.size()); } int main(int argc, char **argv)