Skip to content

Commit

Permalink
sstables: Extract and use clustering_ranges_walker
Browse files Browse the repository at this point in the history
Extracted from mp_row_consumer.
  • Loading branch information
tgrabiec committed Mar 10, 2017
1 parent 88ccc99 commit 4750216
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 59 deletions.
128 changes: 128 additions & 0 deletions clustering_ranges_walker.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (C) 2017 ScyllaDB
*
* Modified by ScyllaDB
*/

/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "schema.hh"
#include "query-request.hh"
#include "streamed_mutation.hh"

// Utility for in-order checking of overlap with clustering ranges.
class clustering_ranges_walker {
const schema& _schema;
const query::clustering_row_ranges& _ranges;
query::clustering_row_ranges::const_iterator _current;
query::clustering_row_ranges::const_iterator _end;
bool _in_current = false;
public:
clustering_ranges_walker(const schema& s, const query::clustering_row_ranges& ranges)
: _schema(s)
, _ranges(ranges)
, _current(ranges.begin())
, _end(ranges.end())
{ }
clustering_ranges_walker(clustering_ranges_walker&& o) noexcept
: _schema(o._schema)
, _ranges(o._ranges)
, _current(o._current)
, _end(o._end)
, _in_current(o._in_current)
{ }
clustering_ranges_walker& operator=(clustering_ranges_walker&& o) {
if (this != &o) {
this->~clustering_ranges_walker();
new (this) clustering_ranges_walker(std::move(o));
}
return *this;
}

// Returns true if given position is contained.
// Must be called with monotonic positions.
// Idempotent.
bool advance_to(position_in_partition_view pos) {
position_in_partition::less_compare less(_schema);

while (_current != _end) {
if (!_in_current && _current->start()) {
position_in_partition_view range_start(position_in_partition_view::range_tag_t(), bound_view::from_range_start(*_current));
if (less(pos, range_start)) {
return false;
}
}
// All subsequent clustering keys are larger than the start of this
// range so there is no need to check that again.
_in_current = true;

if (!_current->end()) {
return true;
}

position_in_partition_view range_end(position_in_partition_view::range_tag_t(), bound_view::from_range_end(*_current));
if (less(pos, range_end)) {
return true;
}

++_current;
_in_current = false;
}

return false;
}

// Returns true if the range expressed by start and end (as in position_range) overlaps
// with clustering ranges.
// Must be called with monotonic start position. That position must also be greater than
// the last position passed to the other advance_to() overload.
// Idempotent.
bool advance_to(position_in_partition_view start, position_in_partition_view end) {
position_in_partition::less_compare less(_schema);

while (_current != _end) {
position_in_partition_view range_start(position_in_partition_view::range_tag_t(), bound_view::from_range_start(*_current));
if (less(end, range_start)) {
return false;
}

position_in_partition_view range_end(position_in_partition_view::range_tag_t(), bound_view::from_range_end(*_current));
if (less(start, range_end)) {
return true;
}

++_current;
_in_current = false;
}

return false;
}

// Returns true if advanced past all contained positions. Any later advance_to() until reset() will return false.
bool out_of_range() const {
return _current == _end;
}

// Resets the state of the walker so that advance_to() can be now called for new sequence of positions.
void reset() {
_current = _ranges.begin();
_in_current = false;
}
};
76 changes: 17 additions & 59 deletions sstables/partition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "index_reader.hh"
#include "counters.hh"
#include "utils/data_input.hh"
#include "clustering_ranges_walker.hh"

namespace sstables {

Expand Down Expand Up @@ -111,11 +112,9 @@ class mp_row_consumer : public row_consumer {
key_view _key;
const io_priority_class* _pc = nullptr;
const query::partition_slice& _slice;
bool _in_current_ck_range = false;
bool _out_of_range = false;
stdx::optional<query::clustering_key_filter_ranges> _ck_ranges;
query::clustering_row_ranges::const_iterator _current_ck_range;
query::clustering_row_ranges::const_iterator _ck_range_end;
stdx::optional<clustering_ranges_walker> _ck_ranges_walker;

bool _skip_partition = false;
bool _skip_clustering_row = false;
Expand Down Expand Up @@ -295,68 +294,25 @@ class mp_row_consumer : public row_consumer {
}
}

// Returns true if and only if the position is inside requested ranges.
// Assumes that this and the other advance_to() are called with monotonic positions.
// We rely on the fact that the first 'S' in SSTables stands for 'sorted'
// and the clustering row keys are always in an ascending order.
bool is_in_range(position_in_partition_view pos) {
position_in_partition::less_compare less(*_schema);

while (_current_ck_range != _ck_range_end) {
if (!_in_current_ck_range && _current_ck_range->start()) {
position_in_partition_view range_start(position_in_partition_view::range_tag_t(), bound_view::from_range_start(*_current_ck_range));
if (less(pos, range_start)) {
return false;
}
}
// All subsequent clustering keys are larger than the start of this
// range so there is no need to check that again.
_in_current_ck_range = true;

if (!_current_ck_range->end()) {
return true;
}

position_in_partition_view range_end(position_in_partition_view::range_tag_t(), bound_view::from_range_end(*_current_ck_range));
if (less(pos, range_end)) {
return true;
}

++_current_ck_range;
_in_current_ck_range = false;
}
_out_of_range = true;
return false;
void advance_to(position_in_partition_view pos) {
_skip_clustering_row = !pos.is_static_row() && !_ck_ranges_walker->advance_to(pos);
_out_of_range |= _ck_ranges_walker->out_of_range();
}

// Returns true if and only if the range tombstone is relevant for requested ranges.
// Assumes that this and is_in_range() are called with monotonic positions, except before
// the first call to is_in_range(), due to #1203.
bool is_tombstone_in_range(const range_tombstone& rt) {
position_in_partition::less_compare less(*_schema);
auto&& start = rt.position();
auto&& end = rt.end_position();

auto i = _current_ck_range; // Cannot advance _current_ck_range due to #1203
while (i != _ck_range_end) {
position_in_partition_view range_start(position_in_partition_view::range_tag_t(), bound_view::from_range_start(*i));
if (less(end, range_start)) {
return false;
}

position_in_partition_view range_end(position_in_partition_view::range_tag_t(), bound_view::from_range_end(*i));
if (less(start, range_end)) {
return true;
}

++i;
}
return false;
// Assumes that this and the other advance_to() are called with monotonic positions.
void advance_to(const range_tombstone& rt) {
_skip_clustering_row = !_ck_ranges_walker->advance_to(rt.position(), rt.end_position());
_out_of_range |= _ck_ranges_walker->out_of_range();
}

void set_up_ck_ranges(const partition_key& pk) {
_ck_ranges = query::clustering_key_filter_ranges::get_ranges(*_schema, _slice, pk);
_current_ck_range = _ck_ranges->begin();
_ck_range_end = _ck_ranges->end();
_in_current_ck_range = false;
_ck_ranges_walker = clustering_ranges_walker(*_schema, _ck_ranges->ranges());
_out_of_range = false;
_range_tombstones.reset();
_first_row_encountered = false;
Expand Down Expand Up @@ -428,7 +384,7 @@ class mp_row_consumer : public row_consumer {
ret = _skip_clustering_row ? proceed::yes : proceed::no;
flush();
}
_skip_clustering_row = !is_tombstone_in_range(rt);
advance_to(rt);
if (_out_of_range) {
ret = proceed::no;
}
Expand All @@ -440,6 +396,8 @@ class mp_row_consumer : public row_consumer {
// Part of workaround for #1203
if (!is_static && !_first_row_encountered) {
_first_row_encountered = true;
// from now on both range tombstones and rows should be in order
_ck_ranges_walker->reset();
}

position_in_partition::equal_compare eq(*_schema);
Expand All @@ -449,7 +407,7 @@ class mp_row_consumer : public row_consumer {
flush();
}
if (!_in_progress) {
_skip_clustering_row = !is_static && !is_in_range(static_cast<position_in_partition_view>(pos));
advance_to(pos);
if (_out_of_range) {
ret = proceed::no;
}
Expand Down Expand Up @@ -699,7 +657,7 @@ class mp_row_consumer : public row_consumer {
}
// Workaround for #1203
if (!_first_row_encountered) {
if (is_tombstone_in_range(rt)) {
if (_ck_ranges_walker->advance_to(rt_pos, rt.end_position())) {
_range_tombstones.apply(std::move(rt));
}
return proceed::yes;
Expand Down

0 comments on commit 4750216

Please sign in to comment.