susumu.yata
null+****@clear*****
Mon Oct 6 17:11:59 JST 2014
susumu.yata 2014-10-06 17:11:59 +0900 (Mon, 06 Oct 2014) New Revision: 6edd5bb944eba4ee9acb15433149485cd8fec9db https://github.com/groonga/grnxx/commit/6edd5bb944eba4ee9acb15433149485cd8fec9db Message: Separate implementations from grnxx::Table. Added files: lib/grnxx/impl/table.hpp Modified files: include/grnxx/column.hpp include/grnxx/table.hpp lib/grnxx/Makefile.am lib/grnxx/column.cpp lib/grnxx/impl/Makefile.am lib/grnxx/impl/db.cpp lib/grnxx/impl/db.hpp Renamed files: lib/grnxx/impl/table.cpp (from lib/grnxx/table.cpp) Modified: include/grnxx/column.hpp (+18 -5) =================================================================== --- include/grnxx/column.hpp 2014-10-06 15:36:59 +0900 (e51d251) +++ include/grnxx/column.hpp 2014-10-06 17:11:59 +0900 (457d8d1) @@ -5,6 +5,11 @@ #include "grnxx/types.hpp" namespace grnxx { +namespace impl { + +class Table; + +} // namespace impl class Column { public: @@ -12,7 +17,7 @@ class Column { // Return the table. Table *table() const { - return table_; + return reinterpret_cast<Table *>(table_); } // Return the name. StringCRef name() const { @@ -25,7 +30,7 @@ class Column { // Return the referenced (parent) table, or nullptr if the column is not a // reference column. Table *ref_table() const { - return ref_table_; + return reinterpret_cast<Table *>(ref_table_); } // Return whether the column has the key attribute or not. bool has_key_attribute() const { @@ -128,14 +133,22 @@ class Column { // TODO: This function should be hidden. // + // Return the referenced (parent) table, or nullptr if the column is not a + // reference column. + impl::Table *_ref_table() const { + return ref_table_; + } + + // TODO: This function should be hidden. + // // Replace references to "row_id" with NULL. virtual void clear_references(Int row_id); protected: - Table *table_; + impl::Table *table_; Name name_; DataType data_type_; - Table *ref_table_; + impl::Table *ref_table_; bool has_key_attribute_; Array<unique_ptr<Index>> indexes_; @@ -206,7 +219,7 @@ class Column { const StringCRef &name, Int *column_id) const; - friend class Table; + friend class impl::Table; }; } // namespace grnxx Modified: include/grnxx/table.hpp (+36 -132) =================================================================== --- include/grnxx/table.hpp 2014-10-06 15:36:59 +0900 (98bc281) +++ include/grnxx/table.hpp 2014-10-06 17:11:59 +0900 (2dbf2f3) @@ -5,50 +5,34 @@ #include "grnxx/types.hpp" namespace grnxx { -namespace impl { - -class DB; - -} // namespace impl class Table { public: - ~Table(); + Table() = default; // Return the owner DB. - DB *db() const { - return db_; - } + virtual DB *db() const = 0; // Return the name. - StringCRef name() const { - return name_.ref(); - } + virtual StringCRef name() const = 0; // Return the number of columns. - Int num_columns() const { - return columns_.size(); - } + virtual Int num_columns() const = 0; // Return the key column, or nullptr if the table has no key column. - Column *key_column() const { - return key_column_; - } + virtual Column *key_column() const = 0; // Return the number of rows. - Int num_rows() const { - return num_rows_; - } + virtual Int num_rows() const = 0; // Return the maximum row ID. - Int max_row_id() const { - return max_row_id_; - } + virtual Int max_row_id() const = 0; // Create a column with "name", "data_type", and "options". // // On success, returns a pointer to the column. // On failure, returns nullptr and stores error information into "*error" if // "error" != nullptr. - Column *create_column(Error *error, - const StringCRef &name, - DataType data_type, - const ColumnOptions &options = ColumnOptions()); + virtual Column *create_column( + Error *error, + const StringCRef &name, + DataType data_type, + const ColumnOptions &options = ColumnOptions()) = 0; // Remove a column named "name". // @@ -57,16 +41,16 @@ class Table { // "error" != nullptr. // // Note: Pointers to the removed column must not be used after deletion. - bool remove_column(Error *error, const StringCRef &name); + virtual bool remove_column(Error *error, const StringCRef &name) = 0; // Rename a column named "name" to "new_name". // // On success, returns true. // On failure, returns false and stores error information into "*error" if // "error" != nullptr. - bool rename_column(Error *error, - const StringCRef &name, - const StringCRef &new_name); + virtual bool rename_column(Error *error, + const StringCRef &name, + const StringCRef &new_name) = 0; // Change the order of columns. // @@ -79,9 +63,9 @@ class Table { // On success, returns true. // On failure, returns false and stores error information into "*error" if // "error" != nullptr. - bool reorder_column(Error *error, - const StringCRef &name, - const StringCRef &prev_name); + virtual bool reorder_column(Error *error, + const StringCRef &name, + const StringCRef &prev_name) = 0; // Get a column identified by "column_id". // @@ -90,16 +74,14 @@ class Table { // On success, returns a pointer to the column. // On failure, returns nullptr and stores error information into "*error" if // "error" != nullptr. - Column *get_column(Int column_id) const { - return columns_[column_id].get(); - } + virtual Column *get_column(Int column_id) const = 0; // Find a column named "name". // // On success, returns a pointer to the column. // On failure, returns nullptr and stores error information into "*error" if // "error" != nullptr. - Column *find_column(Error *error, const StringCRef &name) const; + virtual Column *find_column(Error *error, const StringCRef &name) const = 0; // Set the key attribute to the column named "name". // @@ -108,7 +90,7 @@ class Table { // On success, returns true. // On failure, returns false and stores error information into "*error" if // "error" != nullptr. - bool set_key_column(Error *error, const StringCRef &name); + virtual bool set_key_column(Error *error, const StringCRef &name) = 0; // Unset the key attribute of the key column. // @@ -117,7 +99,7 @@ class Table { // On success, returns true. // On failure, returns false and stores error information into "*error" if // "error" != nullptr. - bool unset_key_column(Error *error); + virtual bool unset_key_column(Error *error) = 0; // Insert a row. // @@ -136,24 +118,24 @@ class Table { // On success, returns true. // On failure, returns false and stores error information into "*error" if // "error" != nullptr. - bool insert_row(Error *error, - Int request_row_id, - const Datum &key, - Int *result_row_id); + virtual bool insert_row(Error *error, + Int request_row_id, + const Datum &key, + Int *result_row_id) = 0; // Remove a row identified by "row_id". // // On success, returns true. // On failure, returns false and stores error information into "*error" if // "error" != nullptr. - bool remove_row(Error *error, Int row_id); + virtual bool remove_row(Error *error, Int row_id) = 0; // Check the validity of a row. // // Returns true if "row_id" specifies a row in use. // Otherwise, returns false and stores error information into "*error" if // "error" != nullptr. - bool test_row(Error *error, Int row_id) const; + virtual bool test_row(Error *error, Int row_id) const = 0; // Find a row identified by "key". // @@ -162,16 +144,16 @@ class Table { // On success, returns the row ID. // On failure, returns NULL_ROW_ID and stores error information into // "*error" if "error" != nullptr. - Int find_row(Error *error, const Datum &key) const; + virtual Int find_row(Error *error, const Datum &key) const = 0; // Create a cursor to get records. // // On success, returns a pointer to the cursor. // On failure, returns nullptr and stores error information into "*error" if // "error" != nullptr. - unique_ptr<Cursor> create_cursor( + virtual unique_ptr<Cursor> create_cursor( Error *error, - const CursorOptions &options = CursorOptions()) const; + const CursorOptions &options = CursorOptions()) const = 0; // TODO: Grouping (drilldown). // @@ -182,91 +164,13 @@ class Table { // 失敗する状況としては,以下のようなものが挙げられる. // - オプションが不正である. // - リソースが確保できない. -// unique_ptr<Grouper> create_grouper( +// virtual unique_ptr<Grouper> create_grouper( // Error *error, // unique_ptr<Expression> &&expression, -// const GrouperOptions &options = GrouperOptions()) const; - - // TODO: This member function should be hidden. - // - // Append a referrer column. - // - // On success, returns true. - // On failure, returns false and stores error information into "*error" if - // "error" != nullptr. - bool append_referrer_column(Error *error, Column *column); - - // TODO: This member function should be hidden. - // - // Append a referrer column. - // - // On success, returns true. - // On failure, returns false and stores error information into "*error" if - // "error" != nullptr. - bool remove_referrer_column(Error *error, Column *column); - - private: - DB *db_; - Name name_; - Array<unique_ptr<Column>> columns_; - Array<Column *> referrer_columns_; - Column *key_column_; - Int num_rows_; - Int max_row_id_; - Array<UInt> bitmap_; - Array<Array<UInt>> bitmap_indexes_; - - // Create a new table. - // - // On success, returns a pointer to the table. - // On failure, returns nullptr and stores error information into "*error" if - // "error" != nullptr. - static unique_ptr<Table> create( - Error *error, - DB *db, - const StringCRef &name, - const TableOptions &options = TableOptions()); - - Table(); - - // Get the "i"-th bit from the bitmap. - bool get_bit(Int i) const { - return (bitmap_[i / 64] & (Int(1) << (i % 64))) != 0; - } - // Set 1 to the "i"-th bit of the bitmap and update bitmap indexes. - void set_bit(Int i); - // Set 0 to the "i"-th bit of the bitmap and update bitmap indexes. - void unset_bit(Int i); - // Find a zero-bit and return its position. - Int find_zero_bit() const; - // Reserve the "i"-th bit for the next row. - // - // On success, returns true. - // On failure, returns false and stores error information into "*error" if - // "error" != nullptr. - bool reserve_bit(Error *error, Int i); - - // Change the table name. - // - // On success, returns true. - // On failure, returns false and stores error information into "*error" if - // "error" != nullptr. - bool rename(Error *error, const StringCRef &new_name); - - // Return whether the table is removable or not. - bool is_removable(); - - // Find a column with its ID. - // - // On success, returns a pointer to the column. - // On failure, returns nullptr and stores error information into "*error" if - // "error" != nullptr. - Column *find_column_with_id(Error *error, - const StringCRef &name, - Int *column_id) const; +// const GrouperOptions &options = GrouperOptions()) const = 0; - friend class impl::DB; - friend class TableCursor; + protected: + virtual ~Table() = default; }; } // namespace grnxx Modified: lib/grnxx/Makefile.am (+0 -1) =================================================================== --- lib/grnxx/Makefile.am 2014-10-06 15:36:59 +0900 (5b2d1e2) +++ lib/grnxx/Makefile.am 2014-10-06 17:11:59 +0900 (6eba8e2) @@ -21,7 +21,6 @@ libgrnxx_la_SOURCES = \ name.cpp \ pipeline.cpp \ sorter.cpp \ - table.cpp \ types.cpp libgrnxx_includedir = ${includedir}/grnxx Modified: lib/grnxx/column.cpp (+5 -5) =================================================================== --- lib/grnxx/column.cpp 2014-10-06 15:36:59 +0900 (5e25e18) +++ lib/grnxx/column.cpp 2014-10-06 17:11:59 +0900 (f91c60b) @@ -3,8 +3,8 @@ #include "grnxx/column_impl.hpp" #include "grnxx/cursor.hpp" #include "grnxx/db.hpp" +#include "grnxx/impl/table.hpp" #include "grnxx/index.hpp" -#include "grnxx/table.hpp" #include <set> #include <unordered_set> @@ -185,7 +185,7 @@ bool Column::initialize_base(Error *error, const StringCRef &name, DataType data_type, const ColumnOptions &options) { - table_ = table; + table_ = static_cast<impl::Table *>(table); if (!name_.assign(error, name)) { return false; } @@ -196,7 +196,7 @@ bool Column::initialize_base(Error *error, if (!ref_table) { return false; } - ref_table_ = ref_table; + ref_table_ = static_cast<impl::Table *>(ref_table); } } return true; @@ -404,7 +404,7 @@ unique_ptr<ColumnImpl<Int>> ColumnImpl<Int>::create( return nullptr; } if (column->ref_table()) { - if (!column->ref_table()->append_referrer_column(error, column.get())) { + if (!column->ref_table_->append_referrer_column(error, column.get())) { return nullptr; } } @@ -935,7 +935,7 @@ unique_ptr<ColumnImpl<Vector<Int>>> ColumnImpl<Vector<Int>>::create( return nullptr; } if (column->ref_table()) { - if (!column->ref_table()->append_referrer_column(error, column.get())) { + if (!column->ref_table_->append_referrer_column(error, column.get())) { return nullptr; } } Modified: lib/grnxx/impl/Makefile.am (+4 -2) =================================================================== --- lib/grnxx/impl/Makefile.am 2014-10-06 15:36:59 +0900 (3353f76) +++ lib/grnxx/impl/Makefile.am 2014-10-06 17:11:59 +0900 (627068c) @@ -9,8 +9,10 @@ lib_LTLIBRARIES = libgrnxx_impl.la libgrnxx_impl_la_LDFLAGS = @AM_LTLDFLAGS@ libgrnxx_impl_la_SOURCES = \ - db.cpp + db.cpp \ + table.cpp libgrnxx_impl_includedir = ${includedir}/grnxx/impl libgrnxx_impl_include_HEADERS = \ - db.hpp + db.hpp \ + table.hpp Modified: lib/grnxx/impl/db.cpp (+10 -4) =================================================================== --- lib/grnxx/impl/db.cpp 2014-10-06 15:36:59 +0900 (7d0c66f) +++ lib/grnxx/impl/db.cpp 2014-10-06 17:11:59 +0900 (a15dbe3) @@ -1,7 +1,5 @@ #include "grnxx/impl/db.hpp" -#include "grnxx/table.hpp" - namespace grnxx { namespace impl { @@ -9,6 +7,10 @@ DB::DB() : tables_() {} DB::~DB() {} +Int DB::num_tables() const { + return tables_.size(); +} + Table *DB::create_table(Error *error, const StringCRef &name, const TableOptions &options) { @@ -91,8 +93,12 @@ bool DB::reorder_table(Error *error, return true; } +Table *DB::get_table(Int table_id) const { + return tables_[table_id].get(); +} + Table *DB::find_table(Error *error, const StringCRef &name) const { - for (Int table_id = 0; table_id < num_tables(); ++table_id) { + for (Int table_id = 0; table_id < tables_.size(); ++table_id) { if (name == tables_[table_id]->name()) { return tables_[table_id].get(); } @@ -113,7 +119,7 @@ bool DB::save(Error *error, Table *DB::find_table_with_id(Error *error, const StringCRef &name, Int *table_id) const { - for (Int i = 0; i < num_tables(); ++i) { + for (Int i = 0; i < tables_.size(); ++i) { if (name == tables_[i]->name()) { if (table_id != nullptr) { *table_id = i; Modified: lib/grnxx/impl/db.hpp (+3 -6) =================================================================== --- lib/grnxx/impl/db.hpp 2014-10-06 15:36:59 +0900 (f21acc2) +++ lib/grnxx/impl/db.hpp 2014-10-06 17:11:59 +0900 (18cdb29) @@ -2,6 +2,7 @@ #define GRNXX_IMPL_DB_HPP #include "grnxx/db.hpp" +#include "grnxx/impl/table.hpp" namespace grnxx { namespace impl { @@ -12,9 +13,7 @@ class DB : public grnxx::DB { DB(); ~DB(); - Int num_tables() const { - return tables_.size(); - } + Int num_tables() const; Table *create_table(Error *error, const StringCRef &name, @@ -26,10 +25,8 @@ class DB : public grnxx::DB { bool reorder_table(Error *error, const StringCRef &name, const StringCRef &prev_name); - Table *get_table(Int table_id) const { - return tables_[table_id].get(); - } + Table *get_table(Int table_id) const; Table *find_table(Error *error, const StringCRef &name) const; bool save(Error *error, Renamed: lib/grnxx/impl/table.cpp (+60 -28) 96% =================================================================== --- lib/grnxx/table.cpp 2014-10-06 15:36:59 +0900 (6777e8d) +++ lib/grnxx/impl/table.cpp 2014-10-06 17:11:59 +0900 (683541f) @@ -1,12 +1,13 @@ -#include "grnxx/table.hpp" +#include "grnxx/impl/table.hpp" #include "grnxx/column.hpp" #include "grnxx/cursor.hpp" -#include "grnxx/db.hpp" +#include "grnxx/impl/db.hpp" #include <iostream> // For debug. namespace grnxx { +namespace impl { // -- TableCursor -- @@ -36,6 +37,7 @@ class TableCursor : public Cursor { CursorResult reverse_read(Error *error, ArrayRef<Record> records); private: + const Table *table_; Int offset_left_; Int limit_left_; OrderType order_type_; @@ -93,6 +95,7 @@ unique_ptr<TableCursor> TableCursor::create(Error *error, TableCursor::TableCursor(const Table *table) : Cursor(table), + table_(table), offset_left_(), limit_left_(), order_type_(), @@ -208,8 +211,43 @@ CursorResult TableCursor::reverse_read(Error *, ArrayRef<Record> records) { // -- Table -- +Table::Table() + : db_(nullptr), + name_(), + columns_(), + referrer_columns_(), + key_column_(nullptr), + num_rows_(0), + max_row_id_(MIN_ROW_ID - 1), + bitmap_(), + bitmap_indexes_() {} + Table::~Table() {} +grnxx::DB *Table::db() const { + return db_; +} + +StringCRef Table::name() const { + return name_.ref(); +} + +Int Table::num_columns() const { + return columns_.size(); +} + +Column *Table::key_column() const { + return key_column_; +} + +Int Table::num_rows() const { + return num_rows_; +} + +Int Table::max_row_id() const { + return max_row_id_; +} + Column *Table::create_column(Error *error, const StringCRef &name, DataType data_type, @@ -248,7 +286,7 @@ bool Table::remove_column(Error *error, const StringCRef &name) { key_column_ = nullptr; } if (column->ref_table()) { - column->ref_table()->remove_referrer_column(nullptr, column); + column->_ref_table()->remove_referrer_column(nullptr, column); } columns_.erase(column_id); return true; @@ -301,6 +339,10 @@ bool Table::reorder_column(Error *error, return true; } +Column *Table::get_column(Int column_id) const { + return columns_[column_id].get(); +} + Column *Table::find_column(Error *error, const StringCRef &name) const { for (Int column_id = 0; column_id < num_columns(); ++column_id) { if (name == columns_[column_id]->name()) { @@ -494,6 +536,20 @@ unique_ptr<Table> Table::create(Error *error, return table; } +bool Table::rename(Error *error, const StringCRef &new_name) { + return name_.assign(error, new_name); +} + +bool Table::is_removable() { + // Referenced table (except self-reference) is not removable. + for (Int i = 0; i < referrer_columns_.size(); ++i) { + if (referrer_columns_[i]->table() != this) { + return false; + } + } + return true; +} + bool Table::append_referrer_column(Error *error, Column *column) { if (column->ref_table() != this) { GRNXX_ERROR_SET(error, INVALID_ARGUMENT, "Wrong column"); @@ -513,17 +569,6 @@ bool Table::remove_referrer_column(Error *error, Column *column) { return false; } -Table::Table() - : db_(nullptr), - name_(), - columns_(), - referrer_columns_(), - key_column_(nullptr), - num_rows_(0), - max_row_id_(MIN_ROW_ID - 1), - bitmap_(), - bitmap_indexes_() {} - void Table::set_bit(Int i) { bitmap_[i / 64] |= UInt(1) << (i % 64); if (bitmap_[i / 64] == ~UInt(0)) { @@ -602,20 +647,6 @@ bool Table::reserve_bit(Error *error, Int i) { return true; } -bool Table::rename(Error *error, const StringCRef &new_name) { - return name_.assign(error, new_name); -} - -bool Table::is_removable() { - // Referenced table (except self-reference) is not removable. - for (Int i = 0; i < referrer_columns_.size(); ++i) { - if (referrer_columns_[i]->table() != this) { - return false; - } - } - return true; -} - Column *Table::find_column_with_id(Error *error, const StringCRef &name, Int *column_id) const { @@ -632,4 +663,5 @@ Column *Table::find_column_with_id(Error *error, return nullptr; } +} // namespace impl } // namespace grnxx Added: lib/grnxx/impl/table.hpp (+137 -0) 100644 =================================================================== --- /dev/null +++ lib/grnxx/impl/table.hpp 2014-10-06 17:11:59 +0900 (668d548) @@ -0,0 +1,137 @@ +#ifndef GRNXX_IMPL_TABLE_HPP +#define GRNXX_IMPL_TABLE_HPP + +#include "grnxx/name.hpp" +#include "grnxx/db.hpp" +#include "grnxx/table.hpp" + +namespace grnxx { +namespace impl { + +class DB; + +class Table : public grnxx::Table { + public: + // Public API, see grnxx/table.hpp for details. + Table(); + ~Table(); + + grnxx::DB *db() const; + StringCRef name() const; + Int num_columns() const; + Column *key_column() const; + Int num_rows() const; + Int max_row_id() const; + + Column *create_column(Error *error, + const StringCRef &name, + DataType data_type, + const ColumnOptions &options = ColumnOptions()); + bool remove_column(Error *error, const StringCRef &name); + bool rename_column(Error *error, + const StringCRef &name, + const StringCRef &new_name); + bool reorder_column(Error *error, + const StringCRef &name, + const StringCRef &prev_name); + + Column *get_column(Int column_id) const; + Column *find_column(Error *error, const StringCRef &name) const; + + bool set_key_column(Error *error, const StringCRef &name); + bool unset_key_column(Error *error); + + bool insert_row(Error *error, + Int request_row_id, + const Datum &key, + Int *result_row_id); + bool remove_row(Error *error, Int row_id); + + bool test_row(Error *error, Int row_id) const; + Int find_row(Error *error, const Datum &key) const; + + unique_ptr<Cursor> create_cursor( + Error *error, + const CursorOptions &options = CursorOptions()) const; + + // Internal API. + + // Create a new table. + // + // On success, returns a pointer to the table. + // On failure, returns nullptr and stores error information into "*error" if + // "error" != nullptr. + static unique_ptr<Table> create( + Error *error, + DB *db, + const StringCRef &name, + const TableOptions &options = TableOptions()); + + // Change the table name. + // + // On success, returns true. + // On failure, returns false and stores error information into "*error" if + // "error" != nullptr. + bool rename(Error *error, const StringCRef &new_name); + + // Return whether the table is removable or not. + bool is_removable(); + + // Append a referrer column. + // + // On success, returns true. + // On failure, returns false and stores error information into "*error" if + // "error" != nullptr. + bool append_referrer_column(Error *error, Column *column); + + // Append a referrer column. + // + // On success, returns true. + // On failure, returns false and stores error information into "*error" if + // "error" != nullptr. + bool remove_referrer_column(Error *error, Column *column); + + private: + DB *db_; + Name name_; + Array<unique_ptr<Column>> columns_; + Array<Column *> referrer_columns_; + Column *key_column_; + Int num_rows_; + Int max_row_id_; + Array<UInt> bitmap_; + Array<Array<UInt>> bitmap_indexes_; + + // Get the "i"-th bit from the bitmap. + bool get_bit(Int i) const { + return (bitmap_[i / 64] & (Int(1) << (i % 64))) != 0; + } + // Set 1 to the "i"-th bit of the bitmap and update bitmap indexes. + void set_bit(Int i); + // Set 0 to the "i"-th bit of the bitmap and update bitmap indexes. + void unset_bit(Int i); + // Find a zero-bit and return its position. + Int find_zero_bit() const; + // Reserve the "i"-th bit for the next row. + // + // On success, returns true. + // On failure, returns false and stores error information into "*error" if + // "error" != nullptr. + bool reserve_bit(Error *error, Int i); + + // Find a column with its ID. + // + // On success, returns a pointer to the column. + // On failure, returns nullptr and stores error information into "*error" if + // "error" != nullptr. + Column *find_column_with_id(Error *error, + const StringCRef &name, + Int *column_id) const; + + friend class TableCursor; +}; + +} // namespace impl +} // namespace grnxx + +#endif // GRNXX_IMPL_TABLE_HPP -------------- next part -------------- HTML����������������������������...Download