Files
scylladb/query-result-set.hh
Kefu Chai f5b05cf981 treewide: use defaulted operator!=() and operator==()
in C++20, compiler generate operator!=() if the corresponding
operator==() is already defined, the language now understands
that the comparison is symmetric in the new standard.

fortunately, our operator!=() is always equivalent to
`! operator==()`, this matches the behavior of the default
generated operator!=(). so, in this change, all `operator!=`
are removed.

in addition to the defaulted operator!=, C++20 also brings to us
the defaulted operator==() -- it is able to generated the
operator==() if the member-wise lexicographical comparison.
under some circumstances, this is exactly what we need. so,
in this change, if the operator==() is also implemented as
a lexicographical comparison of all memeber variables of the
class/struct in question, it is implemented using the default
generated one by removing its body and mark the function as
`default`. moreover, if the class happen to have other comparison
operators which are implemented using lexicographical comparison,
the default generated `operator<=>` is used in place of
the defaulted `operator==`.

sometimes, we fail to mark the operator== with the `const`
specifier, in this change, to fulfil the need of C++ standard,
and to be more correct, the `const` specifier is added.

also, to generate the defaulted operator==, the operand should
be `const class_name&`, but it is not always the case, in the
class of `version`, we use `version` as the parameter type, to
fulfill the need of the C++ standard, the parameter type is
changed to `const version&` instead. this does not change
the semantic of the comparison operator. and is a more idiomatic
way to pass non-trivial struct as function parameters.

please note, because in C++20, both operator= and operator<=> are
symmetric, some of the operators in `multiprecision` are removed.
they are the symmetric form of the another variant. if they were
not removed, compiler would, for instance, find ambiguous
overloaded operator '=='.

this change is a cleanup to modernize the code base with C++20
features.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes #13687
2023-04-27 10:24:46 +03:00

132 lines
3.8 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <seastar/core/shared_ptr.hh>
#include "types/types.hh"
#include "schema/schema.hh"
#include <optional>
#include <stdexcept>
class mutation;
namespace query {
class result;
class no_value : public std::runtime_error {
public:
using runtime_error::runtime_error;
};
class non_null_data_value {
data_value _v;
public:
explicit non_null_data_value(data_value&& v);
operator const data_value&() const {
return _v;
}
};
inline bool operator==(const non_null_data_value& x, const non_null_data_value& y) {
return static_cast<const data_value&>(x) == static_cast<const data_value&>(y);
}
// Result set row is a set of cells that are associated with a row
// including regular column cells, partition keys, as well as static values.
class result_set_row {
schema_ptr _schema;
const std::unordered_map<sstring, non_null_data_value> _cells;
public:
result_set_row(schema_ptr schema, std::unordered_map<sstring, non_null_data_value>&& cells)
: _schema{schema}
, _cells{std::move(cells)}
{ }
// Look up a deserialized row cell value by column name
const data_value*
get_data_value(const sstring& column_name) const {
auto it = _cells.find(column_name);
if (it == _cells.end()) {
return nullptr;
}
return &static_cast<const data_value&>(it->second);
}
// Look up a deserialized row cell value by column name
template<typename T>
std::optional<T>
get(const sstring& column_name) const {
if (const auto *value = get_ptr<T>(column_name)) {
return std::optional(*value);
}
return std::nullopt;
}
template<typename T>
const T*
get_ptr(const sstring& column_name) const {
const auto *value = get_data_value(column_name);
if (value == nullptr) {
return nullptr;
}
return &value_cast<T>(*value);
}
// throws no_value on error
template<typename T>
const T& get_nonnull(const sstring& column_name) const {
auto v = get_ptr<std::remove_reference_t<T>>(column_name);
if (v) {
return *v;
}
throw no_value(column_name);
}
const std::unordered_map<sstring, non_null_data_value>& cells() const { return _cells; }
friend inline bool operator==(const result_set_row& x, const result_set_row& y) = default;
friend std::ostream& operator<<(std::ostream& out, const result_set_row& row);
};
// Result set is an in-memory representation of query results in
// deserialized format. To obtain a result set, use the result_set_builder
// class as a visitor to query_result::consume() function.
class result_set {
schema_ptr _schema;
std::vector<result_set_row> _rows;
public:
static result_set from_raw_result(schema_ptr, const partition_slice&, const result&);
result_set(schema_ptr s, std::vector<result_set_row>&& rows)
: _schema(std::move(s)), _rows{std::move(rows)}
{ }
explicit result_set(const mutation&);
bool empty() const {
return _rows.empty();
}
// throws std::out_of_range on error
const result_set_row& row(size_t idx) const {
if (idx >= _rows.size()) {
throw std::out_of_range("no such row in result set: " + std::to_string(idx));
}
return _rows[idx];
}
const std::vector<result_set_row>& rows() const {
return _rows;
}
const schema_ptr& schema() const {
return _schema;
}
friend inline bool operator==(const result_set& x, const result_set& y);
friend std::ostream& operator<<(std::ostream& out, const result_set& rs);
};
inline bool operator==(const result_set& x, const result_set& y) {
return x._rows == y._rows;
}
}