Skip to content

Commit

Permalink
mutation: Make mutation equality comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrabiec committed May 6, 2015
1 parent 87e00d9 commit 04846ed
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 36 deletions.
8 changes: 8 additions & 0 deletions mutation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ void mutation::update_column(row& row, const column_definition& def, atomic_cell
merge_column(def, i->second, value);
}
}

bool mutation::operator==(const mutation& m) const {
return _dk.equal(*_schema, m._dk) && _p.equal(*_schema, m._p);
}

bool mutation::operator!=(const mutation& m) const {
return !(*this == m);
}
2 changes: 2 additions & 0 deletions mutation.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public:
const mutation_partition& partition() const { return _p; }
mutation_partition& partition() { return _p; }
const utils::UUID& column_family_id() const { return _schema->id(); }
bool operator==(const mutation&) const;
bool operator!=(const mutation&) const;
private:
static void update_column(row& row, const column_definition& def, atomic_cell_or_collection&& value);
friend std::ostream& operator<<(std::ostream& os, const mutation& m);
Expand Down
47 changes: 47 additions & 0 deletions mutation_partition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,50 @@ operator<<(std::ostream& os, const mutation_partition& mp) {
mp._tombstone, ::join(", ", mp._row_tombstones), mp._static_row,
::join(", ", mp._rows));
}

static bool
rows_equal(const schema& s, const row& r1, const row& r2) {
return std::equal(r1.begin(), r1.end(), r2.begin(), r2.end(),
[] (const row::value_type& c1, const row::value_type& c2) {
return c1.first == c2.first && c1.second.serialize() == c2.second.serialize();
});
}

bool
deletable_row::equal(const schema& s, const deletable_row& other) const {
if (t != other.t || created_at != other.created_at) {
return false;
}
return rows_equal(s, cells, other.cells);
}

bool
rows_entry::equal(const schema& s, const rows_entry& other) const {
return key().equal(s, other.key()) && row().equal(s, other.row());
}

bool
row_tombstones_entry::equal(const schema& s, const row_tombstones_entry& other) const {
return prefix().equal(s, other.prefix()) && t() == other.t();
}

bool mutation_partition::equal(const schema& s, const mutation_partition& p) const {
if (_tombstone != p._tombstone) {
return false;
}

if (!std::equal(_rows.begin(), _rows.end(), p._rows.begin(), p._rows.end(),
[&s] (const rows_entry& e1, const rows_entry& e2) { return e1.equal(s, e2); }
)) {
return false;
}

if (!std::equal(_row_tombstones.begin(), _row_tombstones.end(),
p._row_tombstones.begin(), p._row_tombstones.end(),
[&s] (const row_tombstones_entry& e1, const row_tombstones_entry& e2) { return e1.equal(s, e2); }
)) {
return false;
}

return rows_equal(s, _static_row, p._static_row);
}
7 changes: 7 additions & 0 deletions mutation_partition.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
#include "atomic_cell.hh"
#include "query-result-writer.hh"

// FIXME: Encapsulate
using row = std::map<column_id, atomic_cell_or_collection>;

std::ostream& operator<<(std::ostream& os, const row::value_type& rv);
std::ostream& operator<<(std::ostream& os, const row& r);

// FIXME: Encapsulate
struct deletable_row final {
tombstone t;
api::timestamp_type created_at = api::missing_timestamp;
Expand All @@ -29,6 +31,7 @@ struct deletable_row final {
}

friend std::ostream& operator<<(std::ostream& os, const deletable_row& dr);
bool equal(const schema& s, const deletable_row& other) const;
};

class row_tombstones_entry : public boost::intrusive::set_base_hook<> {
Expand Down Expand Up @@ -86,6 +89,7 @@ public:
}

friend std::ostream& operator<<(std::ostream& os, const row_tombstones_entry& rte);
bool equal(const schema& s, const row_tombstones_entry& other) const;
};

class rows_entry : public boost::intrusive::set_base_hook<> {
Expand Down Expand Up @@ -154,6 +158,7 @@ public:
return delegating_compare<Comparator>(std::move(c));
}
friend std::ostream& operator<<(std::ostream& os, const rows_entry& re);
bool equal(const schema& s, const rows_entry& other) const;
};

namespace db {
Expand Down Expand Up @@ -209,4 +214,6 @@ public:
friend std::ostream& operator<<(std::ostream& os, const mutation_partition& mp);
boost::iterator_range<rows_type::const_iterator> range(const schema& schema, const query::range<clustering_key_prefix>& r) const;
void query(const schema& s, const query::partition_slice& slice, uint32_t limit, query::result::partition_writer& pw) const;
public:
bool equal(const schema& s, const mutation_partition&) const;
};
36 changes: 0 additions & 36 deletions tests/urchin/serializer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,42 +143,6 @@ static atomic_cell make_atomic_cell(bytes value) {
return atomic_cell::make_live(0, ttl_opt{}, std::move(value));
}

inline bool operator==(const deletable_row& r1, const deletable_row& r2) {
return r1.t == r2.t && r1.cells == r2.cells;
}

inline bool operator==(const partition_key& m1, const partition_key& m2) {
const bytes_view& b1 = m1;
const bytes_view& b2 = m2;
return b1 == b2;
}

inline bool operator==(const rows_entry& r1, const rows_entry& r2) {
return r1.row() == r2.row();
}

inline bool operator!=(const rows_entry& r1, const rows_entry& r2) {
return !(r1 == r2);
}

// TODO: not complete... meh...
inline bool operator==(const mutation_partition& cp1, const mutation_partition& cp2) {
static schema dummy({}, "", "", {}, {}, {}, {}, utf8_type);
auto& p1 = const_cast<mutation_partition&>(cp1);
auto& p2 = const_cast<mutation_partition&>(cp2);
return p1.static_row() == p2.static_row()
&& p1.range(dummy, query::range<clustering_key_prefix>::make_open_ended_both_sides())
== p2.range(dummy, query::range<clustering_key_prefix>::make_open_ended_both_sides())
;
}

inline bool operator==(const mutation& m1, const mutation& m2) {
return m1.schema().get() == m2.schema().get()
&& m1.key() == m2.key()
&& m1.partition() == m2.partition()
;
}

SEASTAR_TEST_CASE(test_mutation){
auto s = make_lw_shared(schema({}, some_keyspace, some_column_family,
{{"p1", utf8_type}}, {{"c1", int32_type}}, {{"r1", int32_type}}, {}, utf8_type));
Expand Down

0 comments on commit 04846ed

Please sign in to comment.