susumu.yata
null+****@clear*****
Thu Jul 17 10:28:49 JST 2014
susumu.yata 2014-07-17 10:28:49 +0900 (Thu, 17 Jul 2014) New Revision: 618a16abc271e76a74e19632530bf9c1b04cacf8 https://github.com/groonga/grnxx/commit/618a16abc271e76a74e19632530bf9c1b04cacf8 Message: Support Text. Modified files: lib/grnxx/column.cpp lib/grnxx/column_impl.hpp lib/grnxx/index.cpp lib/grnxx/table.cpp Modified: lib/grnxx/column.cpp (+81 -7) =================================================================== --- lib/grnxx/column.cpp 2014-07-16 16:34:16 +0900 (e934e81) +++ lib/grnxx/column.cpp 2014-07-17 10:28:49 +0900 (1179d17) @@ -88,9 +88,9 @@ unique_ptr<Column> Column::create(Error *error, case GEO_POINT_DATA: { return ColumnImpl<GeoPoint>::create(error, table, name, options); } -// case TEXT_DATA: { -// return ColumnImpl<Text>::create(error, table, name, options); -// } + case TEXT_DATA: { + return ColumnImpl<Text>::create(error, table, name, options); + } default: { // TODO: Other data types are not supported yet. GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet"); @@ -124,26 +124,27 @@ bool Column::rename(Error *error, String new_name) { } bool Column::is_removable() { - // TODO + // TODO: Reference column is not supported yet. return true; } bool Column::set_initial_key(Error *error, Int row_id, const Datum &key) { - // TODO + // TODO: Key column is not supported yet. GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet"); return false; } bool Column::set_default_value(Error *error, Int row_id) { - // TODO + // TODO: This function should be a pure virtual function. GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not suported yet"); return false; } void Column::unset(Int row_id) { + // TODO: This function should be a pure virtual function. } -// -- ColumnImpl -- +// -- ColumnImpl<T> -- template <typename T> bool ColumnImpl<T>::set(Error *error, Int row_id, const Datum &datum) { @@ -220,4 +221,77 @@ void ColumnImpl<T>::unset(Int row_id) { template <typename T> ColumnImpl<T>::ColumnImpl() : Column(), values_() {} +// -- ColumnImpl<Text> -- + +bool ColumnImpl<Text>::set(Error *error, Int row_id, const Datum &datum) { + if (datum.type() != TEXT_DATA) { + GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Wrong data type"); + return false; + } + if (!table_->test_row(error, row_id)) { + return false; + } + // Note that a Bool object does not have its own address. + Text value = datum.force_text(); + try { + std::string internal_value(value.data(), value.size()); + values_[row_id] = std::move(internal_value); + } catch (...) { + GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); + } + return true; +} + +bool ColumnImpl<Text>::get(Error *error, Int row_id, Datum *datum) const { + if (!table_->test_row(error, row_id)) { + return false; + } + *datum = Text(values_[row_id].data(), values_[row_id].size()); + return true; +} + +unique_ptr<ColumnImpl<Text>> ColumnImpl<Text>::create( + Error *error, + Table *table, + String name, + const ColumnOptions &options) { + unique_ptr<ColumnImpl> column(new (nothrow) ColumnImpl); + if (!column) { + GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); + return nullptr; + } + if (!column->initialize_base(error, table, name, TEXT_DATA, options)) { + return nullptr; + } + try { + column->values_.resize(table->max_row_id() + 1); + } catch (...) { + GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); + return nullptr; + } + return column; +} + +ColumnImpl<Text>::~ColumnImpl() {} + +bool ColumnImpl<Text>::set_default_value(Error *error, Int row_id) { + if (row_id >= values_.size()) { + try { + values_.resize(row_id + 1); + return true; + } catch (...) { + GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); + return false; + } + } + values_[row_id].clear(); + return true; +} + +void ColumnImpl<Text>::unset(Int row_id) { + values_[row_id].clear(); +} + +ColumnImpl<Text>::ColumnImpl() : Column(), values_() {} + } // namespace grnxx Modified: lib/grnxx/column_impl.hpp (+39 -0) =================================================================== --- lib/grnxx/column_impl.hpp 2014-07-16 16:34:16 +0900 (87c75b6) +++ lib/grnxx/column_impl.hpp 2014-07-17 10:28:49 +0900 (8fb6236) @@ -45,6 +45,45 @@ class ColumnImpl : public Column { ColumnImpl(); }; +template <> +class ColumnImpl<Text> : public Column { + 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<ColumnImpl> create(Error *error, + Table *table, + String name, + const ColumnOptions &options); + + ~ColumnImpl(); + + 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. + Text get(Int row_id) const { + return Text(values_[row_id].data(), values_[row_id].size()); + } + + protected: + // TODO: std::string should not be used. + std::vector<std::string> values_; + + ColumnImpl(); +}; + } // namespace grnxx #endif // GRNXX_COLUMN_BASE_HPP Modified: lib/grnxx/index.cpp (+3 -3) =================================================================== --- lib/grnxx/index.cpp 2014-07-16 16:34:16 +0900 (e6f760a) +++ lib/grnxx/index.cpp 2014-07-17 10:28:49 +0900 (c54edfe) @@ -9,7 +9,7 @@ Index::~Index() {} unique_ptr<Cursor> Index::create_cursor( Error *error, const CursorOptions &options) const { - // TODO + // TODO: Not supported yet. GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not supoprted yet"); return nullptr; } @@ -19,7 +19,7 @@ unique_ptr<Index> Index::create(Error *error, String name, IndexType type, const IndexOptions &options) { - // TODO + // TODO: Not supported yet. GRNXX_ERROR_SET(error, NOT_SUPPORTED_YET, "Not supoprted yet"); return nullptr; } @@ -29,7 +29,7 @@ bool Index::rename(Error *error, String new_name) { } bool Index::is_removable() { - // TODO + // TODO: Not supported yet. return true; } Modified: lib/grnxx/table.cpp (+1 -1) =================================================================== --- lib/grnxx/table.cpp 2014-07-16 16:34:16 +0900 (31f2527) +++ lib/grnxx/table.cpp 2014-07-17 10:28:49 +0900 (addf8cb) @@ -388,8 +388,8 @@ bool Table::insert_row(Error *error, return false; } } - // TODO: Fill other column values with the default values. if (next_row_id > max_row_id()) { + // Fill non-key column values with the default values. for (size_t column_id = 0; column_id < num_columns(); ++column_id) { if (columns_[column_id].get() != key_column()) { if (!columns_[column_id]->set_default_value(error, next_row_id)) { -------------- next part -------------- HTML����������������������������...Download