Skip to content

Commit

Permalink
Make schema_builder constructible from schema
Browse files Browse the repository at this point in the history
* Make schema_builder use schema::raw_schema to ensure it carries the
same info
* Make it constructible from schema to allow modify-replace flows
  • Loading branch information
elcallio committed Jun 2, 2015
1 parent 20648b2 commit 3b6fc56
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 36 deletions.
45 changes: 45 additions & 0 deletions schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "utils/UUID_gen.hh"
#include "cql3/column_identifier.hh"
#include "schema.hh"
#include "schema_builder.hh"
#include <boost/algorithm/cxx11/any_of.hpp>


Expand Down Expand Up @@ -178,3 +179,47 @@ utils::UUID
generate_legacy_id(const sstring& ks_name, const sstring& cf_name) {
return utils::UUID_gen::get_name_UUID(ks_name + cf_name);
}

schema_builder::schema_builder(const sstring& ks_name, const sstring& cf_name,
std::experimental::optional<utils::UUID> id, data_type rct)
: _raw(id ? *id : utils::UUID_gen::get_time_UUID())
{
_raw._ks_name = ks_name;
_raw._cf_name = cf_name;
_raw._regular_column_name_type = rct;
}

schema_builder::schema_builder(const schema_ptr s)
: schema_builder(s->_raw)
{}

schema_builder::schema_builder(const schema::raw_schema& raw)
: _raw(raw)
{}

column_definition& schema_builder::find_column(const cql3::column_identifier& c) {
auto i = std::find_if(_raw._columns.begin(), _raw._columns.end(), [c](auto& p) {
return p.name() == c.name();
});
if (i != _raw._columns.end()) {
return *i;
}
throw std::invalid_argument(sprint("No such column %s", c.name()));
}

schema_builder& schema_builder::with_column(const column_definition& c) {
return with_column(bytes(c.name()), data_type(c.type), index_info(c.idx_info), column_kind(c.kind));
}

schema_builder& schema_builder::with_column(bytes name, data_type type, column_kind kind) {
return with_column(name, type, index_info(), kind);
}

schema_builder& schema_builder::with_column(bytes name, data_type type, index_info info, column_kind kind) {
_raw._columns.emplace_back(name, type, kind, info);
return *this;
}

schema_ptr schema_builder::build() {
return make_lw_shared<schema>(schema(_raw));
}
3 changes: 3 additions & 0 deletions schema.hh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public:
}
};

class schema_builder;

/*
* Effectively immutable.
* Not safe to access across cores because of shared_ptr's.
Expand Down Expand Up @@ -120,6 +122,7 @@ private:
lw_shared_ptr<compound_type<allow_prefixes::no>> _clustering_key_type;
lw_shared_ptr<compound_type<allow_prefixes::yes>> _clustering_key_prefix_type;

friend class schema_builder;
public:
typedef std::vector<column_definition> columns_type;
typedef typename columns_type::iterator iterator;
Expand Down
78 changes: 42 additions & 36 deletions schema_builder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,50 @@
#include "schema.hh"

struct schema_builder {
sstring _ks_name;
sstring _cf_name;
std::vector<schema::column> _partition_key;
std::vector<schema::column> _clustering_key;
std::vector<schema::column> _static_columns;
std::vector<schema::column> _regular_columns;
schema::raw_schema _raw;

schema_builder(const schema::raw_schema&);
public:
schema_builder(const sstring& ks_name, const sstring& cf_name)
: _ks_name(ks_name)
, _cf_name(cf_name)
{ }
schema_builder(const sstring& ks_name, const sstring& cf_name,
std::experimental::optional<utils::UUID> = { },
data_type regular_column_name_type = utf8_type);
schema_builder(const schema_ptr);

schema_builder& with_column(bytes name, data_type type, column_kind kind = column_kind::regular_column) {
switch (kind) {
case column_kind::partition_key:
_partition_key.emplace_back(schema::column{name, type});
break;
case column_kind::clustering_key:
_clustering_key.emplace_back(schema::column{name, type});
break;
case column_kind::static_column:
_static_columns.emplace_back(schema::column{name, type});
break;
case column_kind::regular_column:
_regular_columns.emplace_back(schema::column{name, type});
break;
};
return *this;
void set_uuid(const utils::UUID& id) {
_raw._id = id;
}

schema_ptr build() {
return make_lw_shared<schema>(schema({},
_ks_name,
_cf_name,
_partition_key,
_clustering_key,
_regular_columns,
_static_columns,
utf8_type));
const utils::UUID& uuid() const {
return _raw._id;
}
void set_regular_column_name_type(const data_type& t) {
_raw._regular_column_name_type = t;
}
const data_type& regular_column_name_type() const {
return _raw._regular_column_name_type;
}
const sstring& ks_name() const {
return _raw._ks_name;
}
const sstring& cf_name() const {
return _raw._cf_name;
}
void set_comment(const sstring& s) {
_raw._comment = s;
}
const sstring& comment() const {
return _raw._comment;
}
void set_default_time_to_live(gc_clock::duration t) {
_raw._default_time_to_live = t;
}
gc_clock::duration default_time_to_live() const {
return _raw._default_time_to_live;
}

column_definition& find_column(const cql3::column_identifier&);
schema_builder& with_column(const column_definition& c);
schema_builder& with_column(bytes name, data_type type, column_kind kind = column_kind::regular_column);
schema_builder& with_column(bytes name, data_type type, index_info info, column_kind kind = column_kind::regular_column);

schema_ptr build();
};

0 comments on commit 3b6fc56

Please sign in to comment.