Introduce partition_slice_builder
This commit is contained in:
@@ -520,6 +520,7 @@ urchin_core = (['database.cc',
|
||||
'streaming/messages/outgoing_file_message.cc',
|
||||
'streaming/messages/incoming_file_message.cc',
|
||||
'gc_clock.cc',
|
||||
'partition_slice_builder.cc',
|
||||
]
|
||||
+ [Antlr3Grammar('cql3/Cql.g')]
|
||||
+ [Thrift('interface/cassandra.thrift', 'Cassandra')]
|
||||
|
||||
105
partition_slice_builder.cc
Normal file
105
partition_slice_builder.cc
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Cloudius Systems, Ltd.
|
||||
*/
|
||||
|
||||
#include <boost/range/adaptor/transformed.hpp>
|
||||
#include <boost/range/algorithm/copy.hpp>
|
||||
#include <boost/range/algorithm_ext/push_back.hpp>
|
||||
|
||||
#include "partition_slice_builder.hh"
|
||||
|
||||
partition_slice_builder::partition_slice_builder(const schema& schema)
|
||||
: _schema(schema)
|
||||
{
|
||||
_options.set<query::partition_slice::option::send_partition_key>();
|
||||
_options.set<query::partition_slice::option::send_clustering_key>();
|
||||
_options.set<query::partition_slice::option::send_timestamp_and_expiry>();
|
||||
}
|
||||
|
||||
query::partition_slice
|
||||
partition_slice_builder::build() {
|
||||
std::vector<query::clustering_range> ranges;
|
||||
if (_row_ranges) {
|
||||
ranges = std::move(*_row_ranges);
|
||||
} else {
|
||||
ranges.emplace_back(query::clustering_range::make_open_ended_both_sides());
|
||||
}
|
||||
|
||||
std::vector<column_id> static_columns;
|
||||
if (_static_columns) {
|
||||
static_columns = std::move(*_static_columns);
|
||||
} else {
|
||||
boost::range::push_back(static_columns,
|
||||
_schema.static_columns() | boost::adaptors::transformed(std::mem_fn(&column_definition::id)));
|
||||
}
|
||||
|
||||
std::vector<column_id> regular_columns;
|
||||
if (_regular_columns) {
|
||||
regular_columns = std::move(*_regular_columns);
|
||||
} else {
|
||||
boost::range::push_back(regular_columns,
|
||||
_schema.regular_columns() | boost::adaptors::transformed(std::mem_fn(&column_definition::id)));
|
||||
}
|
||||
|
||||
return {
|
||||
std::move(ranges),
|
||||
std::move(static_columns),
|
||||
std::move(regular_columns),
|
||||
std::move(_options)
|
||||
};
|
||||
}
|
||||
|
||||
partition_slice_builder&
|
||||
partition_slice_builder::with_range(query::clustering_range range) {
|
||||
if (!_row_ranges) {
|
||||
_row_ranges = std::vector<query::clustering_range>();
|
||||
}
|
||||
_row_ranges->emplace_back(std::move(range));
|
||||
return *this;
|
||||
}
|
||||
|
||||
partition_slice_builder&
|
||||
partition_slice_builder::with_no_regular_columns() {
|
||||
_regular_columns = std::vector<column_id>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
partition_slice_builder&
|
||||
partition_slice_builder::with_regular_column(bytes name) {
|
||||
if (!_regular_columns) {
|
||||
_regular_columns = std::vector<column_id>();
|
||||
}
|
||||
|
||||
const column_definition* def = _schema.get_column_definition(name);
|
||||
if (!def) {
|
||||
throw std::runtime_error(sprint("No such column: %s", _schema.regular_column_name_type()->to_string(name)));
|
||||
}
|
||||
if (!def->is_regular()) {
|
||||
throw std::runtime_error(sprint("Column is not regular: %s", _schema.regular_column_name_type()->to_string(name)));
|
||||
}
|
||||
_regular_columns->push_back(def->id);
|
||||
return *this;
|
||||
}
|
||||
|
||||
partition_slice_builder&
|
||||
partition_slice_builder::with_no_static_columns() {
|
||||
_static_columns = std::vector<column_id>();
|
||||
return *this;
|
||||
}
|
||||
|
||||
partition_slice_builder&
|
||||
partition_slice_builder::with_static_column(bytes name) {
|
||||
if (!_static_columns) {
|
||||
_static_columns = std::vector<column_id>();
|
||||
}
|
||||
|
||||
const column_definition* def = _schema.get_column_definition(name);
|
||||
if (!def) {
|
||||
throw std::runtime_error(sprint("No such column: %s", utf8_type->to_string(name)));
|
||||
}
|
||||
if (!def->is_static()) {
|
||||
throw std::runtime_error(sprint("Column is not static: %s", utf8_type->to_string(name)));
|
||||
}
|
||||
_static_columns->push_back(def->id);
|
||||
return *this;
|
||||
}
|
||||
38
partition_slice_builder.hh
Normal file
38
partition_slice_builder.hh
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Cloudius Systems, Ltd.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <experimental/optional>
|
||||
#include <vector>
|
||||
|
||||
#include "query-request.hh"
|
||||
#include "schema.hh"
|
||||
|
||||
//
|
||||
// Fluent builder for query::partition_slice.
|
||||
//
|
||||
// Selects everything by default, unless restricted. Each property can be
|
||||
// restricted separately. For example, by default all static columns are
|
||||
// selected, but if with_static_column() is called then only that column will
|
||||
// be included. Still, all regular columns and the whole clustering range will
|
||||
// be selected (unless restricted).
|
||||
//
|
||||
class partition_slice_builder {
|
||||
std::experimental::optional<std::vector<column_id>> _regular_columns;
|
||||
std::experimental::optional<std::vector<column_id>> _static_columns;
|
||||
std::experimental::optional<std::vector<query::clustering_range>> _row_ranges;
|
||||
const schema& _schema;
|
||||
query::partition_slice::option_set _options;
|
||||
public:
|
||||
partition_slice_builder(const schema& schema);
|
||||
|
||||
partition_slice_builder& with_static_column(bytes name);
|
||||
partition_slice_builder& with_no_static_columns();
|
||||
partition_slice_builder& with_regular_column(bytes name);
|
||||
partition_slice_builder& with_no_regular_columns();
|
||||
partition_slice_builder& with_range(query::clustering_range range);
|
||||
|
||||
query::partition_slice build();
|
||||
};
|
||||
Reference in New Issue
Block a user