db: Add virtual tables interface

This change introduces the basic interface we expect each virtual
table to implement. More specific implementations will then expand
upon it if needed.
This commit is contained in:
Juliusz Stasiewicz
2021-03-24 12:34:24 +01:00
committed by Piotr Wojtczak
parent 8333d66d4e
commit 61a0314952
4 changed files with 124 additions and 0 deletions

View File

@@ -434,6 +434,7 @@ set(scylla_sources
db/sstables-format-selector.cc
db/system_distributed_keyspace.cc
db/system_keyspace.cc
db/virtual_table.cc
db/view/row_locking.cc
db/view/view.cc
db/view/view_update_generator.cc

View File

@@ -841,6 +841,7 @@ scylla_core = (['database.cc',
'cql3/variable_specifications.cc',
'db/consistency_level.cc',
'db/system_keyspace.cc',
'db/virtual_table.cc',
'db/system_distributed_keyspace.cc',
'db/size_estimates_virtual_reader.cc',
'db/schema_tables.cc',

64
db/virtual_table.cc Normal file
View File

@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Modified by ScyllaDB
* Copyright (C) 2021 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/>.
*/
#include "db/virtual_table.hh"
namespace db {
void virtual_table::set_cell(row& cr, const bytes& column_name, data_value value) {
auto ts = api::new_timestamp();
auto cdef = schema()->get_column_definition(column_name);
if (!cdef) {
throw_with_backtrace<std::runtime_error>(format("column not found: {}", column_name));
}
if (!value.is_null()) {
cr.apply(*cdef, atomic_cell::make_live(*cdef->type, ts, value.serialize_nonnull()));
}
}
bool virtual_table::this_shard_owns(const dht::decorated_key& dk) const {
return dht::shard_of(*_s, dk.token()) == this_shard_id();
}
bool virtual_table::contains_key(const dht::partition_range& pr, const dht::decorated_key& dk) const {
return pr.contains(dk, dht::ring_position_comparator(*_s));
}
}

58
db/virtual_table.hh Normal file
View File

@@ -0,0 +1,58 @@
/*
* Copyright 2020 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 "mutation_reader.hh"
#include "schema.hh"
#include "database_fwd.hh"
namespace db {
class virtual_table {
protected:
schema_ptr _s;
database* _db = nullptr; // Always valid when attached to a database.
protected: // opt-ins
// If set to true, the implementation ensures that produced data
// only contains partitions owned by the current shard.
// Implementations can do this by checking the result of this_shard_owns().
// If set to false, data will be filtered out automatically.
bool _shard_aware = false;
protected:
void set_cell(row&, const bytes& column_name, data_value);
bool contains_key(const dht::partition_range&, const dht::decorated_key&) const;
bool this_shard_owns(const dht::decorated_key&) const;
public:
explicit virtual_table(schema_ptr s) : _s(std::move(s)) {}
const schema_ptr& schema() const { return _s; }
// Keep this object alive as long as the returned mutation_source is alive.
virtual mutation_source as_mutation_source() = 0;
void set_database(database& db) { _db = &db; }
};
}