forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_upgrader.hh
110 lines (104 loc) · 4.1 KB
/
schema_upgrader.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (C) 2017-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include "mutation/mutation_fragment.hh"
#include "mutation/mutation_fragment_v2.hh"
#include "converting_mutation_partition_applier.hh"
// A StreamedMutationTransformer which transforms the stream to a different schema
class schema_upgrader {
schema_ptr _prev;
schema_ptr _new;
std::optional<reader_permit> _permit;
private:
row transform(row&& r, column_kind kind) {
row new_row;
r.for_each_cell([&] (column_id id, atomic_cell_or_collection& cell) {
const column_definition& col = _prev->column_at(kind, id);
const column_definition* new_col = _new->get_column_definition(col.name());
if (new_col) {
converting_mutation_partition_applier::append_cell(new_row, kind, *new_col, col, std::move(cell));
}
});
return new_row;
}
public:
schema_upgrader(schema_ptr s)
: _new(std::move(s))
{ }
schema_ptr operator()(schema_ptr old) {
_prev = std::move(old);
return _new;
}
mutation_fragment consume(static_row&& row) {
return mutation_fragment(*_new, std::move(*_permit), static_row(transform(std::move(row.cells()), column_kind::static_column)));
}
mutation_fragment consume(clustering_row&& row) {
return mutation_fragment(*_new, std::move(*_permit), clustering_row(row.key(), row.tomb(), row.marker(),
transform(std::move(row.cells()), column_kind::regular_column)));
}
mutation_fragment consume(range_tombstone&& rt) {
return mutation_fragment(*_new, std::move(*_permit), std::move(rt));
}
mutation_fragment consume(partition_start&& ph) {
return mutation_fragment(*_new, std::move(*_permit), std::move(ph));
}
mutation_fragment consume(partition_end&& eop) {
return mutation_fragment(*_new, std::move(*_permit), std::move(eop));
}
mutation_fragment operator()(mutation_fragment&& mf) {
_permit = mf.permit();
return std::move(mf).consume(*this);
}
};
// A StreamedMutationTransformer which transforms the stream to a different schema
class schema_upgrader_v2 {
schema_ptr _prev;
schema_ptr _new;
std::optional<reader_permit> _permit;
private:
row transform(row&& r, column_kind kind) {
row new_row;
r.for_each_cell([&] (column_id id, atomic_cell_or_collection& cell) {
const column_definition& col = _prev->column_at(kind, id);
const column_definition* new_col = _new->get_column_definition(col.name());
if (new_col) {
converting_mutation_partition_applier::append_cell(new_row, kind, *new_col, col, std::move(cell));
}
});
return new_row;
}
public:
schema_upgrader_v2(schema_ptr s)
: _new(std::move(s))
{ }
schema_ptr operator()(schema_ptr old) {
_prev = std::move(old);
return _new;
}
mutation_fragment_v2 consume(static_row&& row) {
return mutation_fragment_v2(*_new, std::move(*_permit), static_row(transform(std::move(row.cells()), column_kind::static_column)));
}
mutation_fragment_v2 consume(clustering_row&& row) {
return mutation_fragment_v2(*_new, std::move(*_permit), clustering_row(row.key(), row.tomb(), row.marker(),
transform(std::move(row.cells()), column_kind::regular_column)));
}
mutation_fragment_v2 consume(range_tombstone_change&& rt) {
return mutation_fragment_v2(*_new, std::move(*_permit), std::move(rt));
}
mutation_fragment_v2 consume(partition_start&& ph) {
return mutation_fragment_v2(*_new, std::move(*_permit), std::move(ph));
}
mutation_fragment_v2 consume(partition_end&& eop) {
return mutation_fragment_v2(*_new, std::move(*_permit), std::move(eop));
}
mutation_fragment_v2 operator()(mutation_fragment_v2&& mf) {
_permit = mf.permit();
return std::move(mf).consume(*this);
}
};
static_assert(StreamedMutationTranformer<schema_upgrader>);
static_assert(StreamedMutationTranformerV2<schema_upgrader_v2>);