diff --git a/schema.cc b/schema.cc index 05b3fd57fdf7..0ef8a59a6b66 100644 --- a/schema.cc +++ b/schema.cc @@ -5,6 +5,7 @@ #include "utils/UUID_gen.hh" #include "cql3/column_identifier.hh" #include "schema.hh" +#include "schema_builder.hh" #include @@ -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 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(_raw)); +} diff --git a/schema.hh b/schema.hh index 9e2eb546d8b1..f8da21806d1d 100644 --- a/schema.hh +++ b/schema.hh @@ -85,6 +85,8 @@ public: } }; +class schema_builder; + /* * Effectively immutable. * Not safe to access across cores because of shared_ptr's. @@ -120,6 +122,7 @@ private: lw_shared_ptr> _clustering_key_type; lw_shared_ptr> _clustering_key_prefix_type; + friend class schema_builder; public: typedef std::vector columns_type; typedef typename columns_type::iterator iterator; diff --git a/schema_builder.hh b/schema_builder.hh index d3d12659c9ef..fb5f905c875d 100644 --- a/schema_builder.hh +++ b/schema_builder.hh @@ -7,44 +7,50 @@ #include "schema.hh" struct schema_builder { - sstring _ks_name; - sstring _cf_name; - std::vector _partition_key; - std::vector _clustering_key; - std::vector _static_columns; - std::vector _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 = { }, + 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({}, - _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(); };