susumu.yata
null+****@clear*****
Tue Dec 16 10:43:44 JST 2014
susumu.yata 2014-11-17 17:43:13 +0900 (Mon, 17 Nov 2014) New Revision: 432ef99e38dda37e5278e4df9ccc3a6e861cde8a https://github.com/groonga/grnxx/commit/432ef99e38dda37e5278e4df9ccc3a6e861cde8a Message: Add Column<Vector<Float>>. (#110) Modified files: lib/grnxx/impl/column/base.cpp lib/grnxx/impl/column/vector.hpp lib/grnxx/impl/column/vector/Makefile.am lib/grnxx/impl/column/vector/float.cpp lib/grnxx/impl/column/vector/float.hpp Modified: lib/grnxx/impl/column/base.cpp (+4 -4) =================================================================== --- lib/grnxx/impl/column/base.cpp 2014-11-17 17:32:46 +0900 (efec40f) +++ lib/grnxx/impl/column/base.cpp 2014-11-17 17:43:13 +0900 (d9c3347) @@ -207,10 +207,10 @@ std::unique_ptr<ColumnBase> ColumnBase::create( column.reset(new impl::Column<Vector<Int>>(table, name, options)); break; } -// case FLOAT_VECTOR_DATA: { -// column.reset(new impl::Column<Vector<Float>>(table, name, options)); -// break; -// } + case FLOAT_VECTOR_DATA: { + column.reset(new impl::Column<Vector<Float>>(table, name, options)); + break; + } // case GEO_POINT_VECTOR_DATA: { // column.reset(new impl::Column<Vector<GeoPoint>>(table, name, options)); // break; Modified: lib/grnxx/impl/column/vector.hpp (+1 -1) =================================================================== --- lib/grnxx/impl/column/vector.hpp 2014-11-17 17:32:46 +0900 (76c7d66) +++ lib/grnxx/impl/column/vector.hpp 2014-11-17 17:43:13 +0900 (2a03705) @@ -2,7 +2,7 @@ #define GRNXX_IMPL_COLUMN_VECTOR_HPP #include "grnxx/impl/column/vector/bool.hpp" -//#include "grnxx/impl/column/vector/float.hpp" +#include "grnxx/impl/column/vector/float.hpp" //#include "grnxx/impl/column/vector/geo_point.hpp" #include "grnxx/impl/column/vector/int.hpp" //#include "grnxx/impl/column/vector/text.hpp" Modified: lib/grnxx/impl/column/vector/Makefile.am (+1 -1) =================================================================== --- lib/grnxx/impl/column/vector/Makefile.am 2014-11-17 17:32:46 +0900 (c176333) +++ lib/grnxx/impl/column/vector/Makefile.am 2014-11-17 17:43:13 +0900 (3b2e8da) @@ -10,9 +10,9 @@ libgrnxx_impl_column_vector_la_LDFLAGS = @AM_LTLDFLAGS@ libgrnxx_impl_column_vector_la_SOURCES = \ bool.cpp \ + float.cpp \ int.cpp -# float.cpp \ # geo_point.cpp \ # text.cpp Modified: lib/grnxx/impl/column/vector/float.cpp (+129 -65) =================================================================== --- lib/grnxx/impl/column/vector/float.cpp 2014-11-17 17:32:46 +0900 (0802911) +++ lib/grnxx/impl/column/vector/float.cpp 2014-11-17 17:43:13 +0900 (ca0f932) @@ -1,97 +1,161 @@ -#include "grnxx/impl/column/column_vector_float.hpp" +#include "grnxx/impl/column/vector/float.hpp" + +#include <cstring> -#include "grnxx/cursor.hpp" #include "grnxx/impl/db.hpp" #include "grnxx/impl/table.hpp" +//#include "grnxx/impl/index.hpp" namespace grnxx { namespace impl { -bool Column<Vector<Float>>::set(Error *error, Int row_id, const Datum &datum) { - if (datum.type() != FLOAT_VECTOR_DATA) { - GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Wrong data type"); - return false; +Column<Vector<Float>>::Column(Table *table, + const String &name, + const ColumnOptions &options) + : ColumnBase(table, name, FLOAT_VECTOR_DATA), + headers_(), + bodies_() {} + +Column<Vector<Float>>::~Column() {} + +void Column<Vector<Float>>::set(Int row_id, const Datum &datum) { + Vector<Float> new_value = parse_datum(datum); + if (!table_->test_row(row_id)) { + throw "Invalid row ID"; // TODO } - if (!table_->test_row(error, row_id)) { - return false; + if (new_value.is_na()) { + unset(row_id); + return; } - Vector<Float> value = datum.force_float_vector(); - if (value.size() == 0) { - headers_[row_id] = 0; - return true; + Vector<Float> old_value = get(row_id); + if ((old_value == new_value).is_true()) { + return; } - Int offset = bodies_.size(); - if (value.size() < 0xFFFF) { - if (!bodies_.resize(error, offset + value.size())) { - return false; - } - for (Int i = 0; i < value.size(); ++i) { - bodies_[offset + i] = value[i]; - } - headers_[row_id] = (offset << 16) | value.size(); + if (!old_value.is_na()) { + // TODO: Remove the old value from indexes. +// for (size_t i = 0; i < num_indexes(); ++i) { +// indexes_[i]->remove(row_id, old_value); +// } + } + size_t value_id = row_id.value(); + if (value_id >= headers_.size()) { + headers_.resize(value_id + 1, na_header()); + } + // TODO: Insert the new value into indexes. +// for (size_t i = 0; i < num_indexes(); ++i) try { +// indexes_[i]->insert(row_id, datum)) { +// } catch (...) { +// for (size_t j = 0; j < i; ++i) { +// indexes_[j]->remove(row_id, datum); +// } +// throw; +// } + // TODO: Error handling. + size_t offset = bodies_.size(); + size_t size = new_value.size().value(); + uint64_t header; + if (size < 0xFFFF) { + bodies_.resize(offset + size); + std::memcpy(&bodies_[offset], new_value.data(), sizeof(Float) * size); + header = (offset << 16) | size; } else { // The size of a long vector is stored in front of the body. - if (!bodies_.resize(error, offset + 1 + value.size())) { - return false; - } - Int size_for_copy = value.size(); - std::memcpy(&bodies_[offset], &size_for_copy, sizeof(Int)); - for (Int i = 0; i < value.size(); ++i) { - bodies_[offset + 1 + i] = value[i]; + if ((offset % sizeof(uint64_t)) != 0) { + offset += sizeof(uint64_t) - (offset % sizeof(uint64_t)); } - headers_[row_id] = (offset << 16) | 0xFFFF; + bodies_.resize(offset + sizeof(uint64_t) + size); + *reinterpret_cast<uint64_t *>(&bodies_[offset]) = size; + std::memcpy(&bodies_[offset + sizeof(uint64_t)], + new_value.data(), sizeof(Float) * size); + header = (offset << 16) | 0xFFFF; } - return true; + headers_[value_id] = header; } -bool Column<Vector<Float>>::get(Error *error, Int row_id, Datum *datum) const { - if (!table_->test_row(error, row_id)) { - return false; +void Column<Vector<Float>>::get(Int row_id, Datum *datum) const { + size_t value_id = row_id.value(); + if (value_id >= headers_.size()) { + *datum = Vector<Float>::na(); + } else { + // TODO + *datum = get(row_id); } - *datum = get(row_id); - return true; } -unique_ptr<Column<Vector<Float>>> Column<Vector<Float>>::create( - Error *error, - Table *table, - const StringCRef &name, - const ColumnOptions &options) { - unique_ptr<Column> column(new (nothrow) Column); - if (!column) { - GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); - return nullptr; - } - if (!column->initialize_base(error, table, name, - FLOAT_VECTOR_DATA, options)) { - return nullptr; - } - if (!column->headers_.resize(error, table->max_row_id() + 1, 0)) { - return nullptr; +bool Column<Vector<Float>>::contains(const Datum &datum) const { + // TODO: Use an index if exists. + Vector<Float> value = parse_datum(datum); + if (value.is_na()) { + for (size_t i = 0; i < headers_.size(); ++i) { + if (headers_[i] == na_header()) { + return true; + } + } + } else { + for (size_t i = 0; i < headers_.size(); ++i) { + // TODO: Improve this. + if ((get(Int(i)) == value).is_true()) { + return true; + } + } } - return column; + return false; } -Column<Vector<Float>>::~Column() {} - -bool Column<Vector<Float>>::set_default_value(Error *error, Int row_id) { - if (row_id >= headers_.size()) { - if (!headers_.resize(error, row_id + 1)) { - return false; +Int Column<Vector<Float>>::find_one(const Datum &datum) const { + // TODO: Use an index if exists. + Vector<Float> value = parse_datum(datum); + if (value.is_na()) { + for (size_t i = 0; i < headers_.size(); ++i) { + if (headers_[i] == na_header()) { + return Int(i); + } + } + } else { + for (size_t i = 0; i < headers_.size(); ++i) { + // TODO: Improve this. + if ((get(Int(i)) == value).is_true()) { + return Int(i); + } } } - headers_[row_id] = 0; - return true; + return Int::na(); } void Column<Vector<Float>>::unset(Int row_id) { - headers_[row_id] = 0; + Vector<Float> value = get(row_id); + if (!value.is_na()) { + // TODO: Update indexes if exist. +// for (size_t i = 0; i < num_indexes(); ++i) { +// indexes_[i]->remove(row_id, value); +// } + headers_[row_id.value()] = na_header(); + } } -Column<Vector<Float>>::Column() - : ColumnBase(), - headers_(), - bodies_() {} +void Column<Vector<Float>>::read(ArrayCRef<Record> records, + ArrayRef<Vector<Float>> values) const { + if (records.size() != values.size()) { + throw "Data size conflict"; // TODO + } + for (size_t i = 0; i < records.size(); ++i) { + values.set(i, get(records[i].row_id)); + } +} + +Vector<Float> Column<Vector<Float>>::parse_datum(const Datum &datum) { + switch (datum.type()) { + case NA_DATA: { + return Vector<Float>::na(); + } + case FLOAT_VECTOR_DATA: { + return datum.as_float_vector(); + } + default: { + throw "Wrong data type"; // TODO + } + } +} } // namespace impl } // namespace grnxx Modified: lib/grnxx/impl/column/vector/float.hpp (+97 -29) =================================================================== --- lib/grnxx/impl/column/vector/float.hpp 2014-11-17 17:32:46 +0900 (58417ed) +++ lib/grnxx/impl/column/vector/float.hpp 2014-11-17 17:43:13 +0900 (0dce527) @@ -1,68 +1,136 @@ #ifndef GRNXX_IMPL_COLUMN_VECTOR_FLOAT_HPP #define GRNXX_IMPL_COLUMN_VECTOR_FLOAT_HPP -#include "grnxx/impl/column/column.hpp" +#include <limits> +#include <cstdint> + +#include "grnxx/impl/column/base.hpp" namespace grnxx { namespace impl { +template <typename T> class Column; + template <> class Column<Vector<Float>> : public ColumnBase { public: - // -- Public API -- + // -- Public API (grnxx/column.hpp) -- - bool set(Error *error, Int row_id, const Datum &datum); - bool get(Error *error, Int row_id, Datum *datum) const; + Column(Table *table, const String &name, const ColumnOptions &options); + ~Column(); - // -- Internal API -- + void set(Int row_id, const Datum &datum); + void get(Int row_id, Datum *datum) const; - // Create a new column. - // - // Returns a pointer to the column on success. - // On failure, returns nullptr and stores error information into "*error" if - // "error" != nullptr. - static unique_ptr<Column> create(Error *error, - Table *table, - const StringCRef &name, - const ColumnOptions &options); + bool contains(const Datum &datum) const; + Int find_one(const Datum &datum) const; - ~Column(); + // -- Internal API (grnxx/impl/column/base.hpp) -- - bool set_default_value(Error *error, Int row_id); void unset(Int row_id); - // Return a value identified by "row_id". + // -- Internal API -- + + // Return a value. + // + // If "row_id" is valid, returns the stored value. + // If "row_id" is invalid, returns N/A. // - // Assumes that "row_id" is valid. Otherwise, the result is undefined. + // TODO: Vector cannot reuse allocated memory because of this interface. Vector<Float> get(Int row_id) const { - Int size = static_cast<Int>(headers_[row_id] & 0xFFFF); + size_t value_id = row_id.value(); + if (value_id >= headers_.size()) { + return Vector<Float>::na(); + } + if (headers_[value_id] == na_header()) { + return Vector<Float>::na(); + } + size_t size = headers_[value_id] & 0xFFFF; if (size == 0) { return Vector<Float>(nullptr, 0); } - Int offset = static_cast<Int>(headers_[row_id] >> 16); + size_t offset = headers_[value_id] >> 16; if (size < 0xFFFF) { return Vector<Float>(&bodies_[offset], size); } else { // The size of a long vector is stored in front of the body. - std::memcpy(&size, &bodies_[offset], sizeof(Int)); + size = *reinterpret_cast<const uint64_t *>(&bodies_[offset]); return Vector<Float>(&bodies_[offset + 1], size); } } - // Read values. - void read(ArrayCRef<Record> records, ArrayRef<Vector<Float>> values) const { - for (Int i = 0; i < records.size(); ++i) { - values.set(i, get(records.get_row_id(i))); - } - } + // + // On failure, throws an exception. + void read(ArrayCRef<Record> records, ArrayRef<Vector<Float>> values) const; private: - Array<UInt> headers_; + Array<uint64_t> headers_; Array<Float> bodies_; - Column(); + static constexpr uint64_t na_header() { + return std::numeric_limits<uint64_t>::max(); + } + + static Vector<Float> parse_datum(const Datum &datum); }; +//template <> +//class Column<Vector<Float>> : public ColumnBase { +// public: +// // -- Public API -- + +// bool set(Error *error, Int row_id, const Datum &datum); +// bool get(Error *error, Int row_id, Datum *datum) const; + +// // -- Internal API -- + +// // Create a new column. +// // +// // Returns a pointer to the column on success. +// // On failure, returns nullptr and stores error information into "*error" if +// // "error" != nullptr. +// static unique_ptr<Column> create(Error *error, +// Table *table, +// const StringCRef &name, +// const ColumnOptions &options); + +// ~Column(); + +// bool set_default_value(Error *error, Int row_id); +// void unset(Int row_id); + +// // Return a value identified by "row_id". +// // +// // Assumes that "row_id" is valid. Otherwise, the result is undefined. +// Vector<Float> get(Int row_id) const { +// Int size = static_cast<Int>(headers_[row_id] & 0xFFFF); +// if (size == 0) { +// return Vector<Float>(nullptr, 0); +// } +// Int offset = static_cast<Int>(headers_[row_id] >> 16); +// if (size < 0xFFFF) { +// return Vector<Float>(&bodies_[offset], size); +// } else { +// // The size of a long vector is stored in front of the body. +// std::memcpy(&size, &bodies_[offset], sizeof(Int)); +// return Vector<Float>(&bodies_[offset + 1], size); +// } +// } + +// // Read values. +// void read(ArrayCRef<Record> records, ArrayRef<Vector<Float>> values) const { +// for (Int i = 0; i < records.size(); ++i) { +// values.set(i, get(records.get_row_id(i))); +// } +// } + +// private: +// Array<UInt> headers_; +// Array<Float> bodies_; + +// Column(); +//}; + } // namespace impl } // namespace grnxx -------------- next part -------------- HTML����������������������������...Download