From 39dbb4048391a7e7f131210c021dd43bf34739cf Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Sun, 22 Mar 2015 16:55:27 +0200 Subject: [PATCH] cql3: convert lists::setter to C++ --- cql3/lists.hh | 89 +++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/cql3/lists.hh b/cql3/lists.hh index 32c1697913..810b17c67d 100644 --- a/cql3/lists.hh +++ b/cql3/lists.hh @@ -27,6 +27,7 @@ #include "cql3/abstract_marker.hh" #include "to_string.hh" +#include "utils/UUID_gen.hh" namespace cql3 { @@ -203,6 +204,7 @@ public: os << "]"; return os.str(); } + friend class lists; }; /** * Basically similar to a Value, but with some non-pure function (that need @@ -317,26 +319,27 @@ public: } } } +#endif - public static class Setter extends Operation - { - public Setter(ColumnDefinition column, Term t) - { - super(column, t); + class setter : public operation { + public: + setter(column_definition& column, shared_ptr t) + : operation(column, std::move(t)) { } - public void execute(ByteBuffer rowKey, ColumnFamily cf, Composite prefix, UpdateParameters params) throws InvalidRequestException - { - if (column.type.isMultiCell()) - { + virtual void execute(mutation& m, const exploded_clustering_prefix& prefix, const update_parameters& params) override { + if (column.type->is_multi_cell()) { // delete + append + // FIXME: + warn(unimplemented::cause::COLLECTIONS); +#if 0 CellName name = cf.getComparator().create(prefix, column); cf.addAtom(params.makeTombstoneForOverwrite(name.slice())); - } - Appender.doAppend(t, cf, prefix, column, params); - } - } #endif + } + do_append(_t, m, prefix, column, params); + } + }; class setter_by_index : public operation { shared_ptr _idx; @@ -408,37 +411,47 @@ public: assert column.type.isMultiCell() : "Attempted to append to a frozen list"; doAppend(t, cf, prefix, column, params); } + } +#endif - static void doAppend(Term t, ColumnFamily cf, Composite prefix, ColumnDefinition column, UpdateParameters params) throws InvalidRequestException - { - Term.Terminal value = t.bind(params.options); - Lists.Value listValue = (Lists.Value)value; - if (column.type.isMultiCell()) - { - // If we append null, do nothing. Note that for Setter, we've - // already removed the previous value so we're good here too - if (value == null) - return; - - List toAdd = listValue.elements; - for (int i = 0; i < toAdd.size(); i++) - { - ByteBuffer uuid = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes()); - cf.addColumn(params.makeColumn(cf.getComparator().create(prefix, column, uuid), toAdd.get(i))); - } + static void do_append(shared_ptr t, + mutation& m, + const exploded_clustering_prefix& prefix, + const column_definition& column, + const update_parameters& params) { + auto&& value = t->bind(params._options); + auto&& list_value = dynamic_pointer_cast(value); + auto&& ltype = dynamic_pointer_cast(column.type); + if (column.type->is_multi_cell()) { + // If we append null, do nothing. Note that for Setter, we've + // already removed the previous value so we're good here too + if (!value) { + return; } - else - { - // for frozen lists, we're overwriting the whole cell value - CellName name = cf.getComparator().create(prefix, column); - if (value == null) - cf.addAtom(params.makeTombstone(name)); - else - cf.addColumn(params.makeColumn(name, listValue.getWithProtocolVersion(Server.CURRENT_VERSION))); + + auto&& to_add = list_value->_elements; + collection_type_impl::mutation appended; + appended.reserve(to_add.size()); + for (auto&& e : to_add) { + auto uuid1 = utils::UUID_gen::get_time_UUID_bytes(); + auto uuid = bytes(reinterpret_cast(uuid1.data()), uuid1.size()); + appended.emplace_back(std::move(uuid), params.make_cell(e)); + } + m.set_cell(prefix, column, ltype->serialize_mutation_form(appended)); + } else { + // for frozen lists, we're overwriting the whole cell value + if (!value) { + m.set_cell(prefix, column, params.make_dead_cell()); + } else { + auto&& to_add = list_value->_elements; + auto&& newv = collection_mutation::one{list_type_impl::pack(to_add.begin(), to_add.end(), to_add.size(), + serialization_format::internal())}; + m.set_cell(prefix, column, atomic_cell_or_collection::from_collection_mutation(std::move(newv))); } } } +#if 0 public static class Prepender extends Operation { public Prepender(ColumnDefinition column, Term t)