susumu.yata
null+****@clear*****
Tue Sep 30 17:46:29 JST 2014
susumu.yata 2014-09-30 17:46:29 +0900 (Tue, 30 Sep 2014) New Revision: 9751ef513e5ef75be2314023623d0a8355e52e61 https://github.com/groonga/grnxx/commit/9751ef513e5ef75be2314023623d0a8355e52e61 Message: Add Index::contains() and Index::find_one(). Modified files: include/grnxx/index.hpp lib/grnxx/column.cpp lib/grnxx/index.cpp Modified: include/grnxx/index.hpp (+12 -0) =================================================================== --- include/grnxx/index.hpp 2014-09-30 17:10:57 +0900 (e37ca53) +++ include/grnxx/index.hpp 2014-09-30 17:46:29 +0900 (e79d76e) @@ -82,6 +82,18 @@ class Index { return type_; } + // Check if "datum" is registered or not. + // + // If registered, returns true. + // Otherwise, returns false. + virtual bool contains(const Datum &datum) const; + + // Search the index for "datum". + // + // On success, returns the row ID of one of the matched values. + // On failure, returns NULL_ROW_ID. + virtual Int find_one(const Datum &datum) const; + // Create a cursor to get records. // // On success, returns a pointer to the cursor. Modified: lib/grnxx/column.cpp (+1 -7) =================================================================== --- lib/grnxx/column.cpp 2014-09-30 17:10:57 +0900 (627b826) +++ lib/grnxx/column.cpp 2014-09-30 17:46:29 +0900 (ca1c5e6) @@ -519,13 +519,7 @@ Int ColumnImpl<Int>::find_one(const Datum &datum) const { // Also, cursor operations can fail due to memory allocation. Int value = datum.force_int(); if (indexes_.size() != 0) { - auto cursor = indexes_[0]->find(nullptr, value); - Array<Record> records; - auto result = cursor->read(nullptr, 1, &records); - if (!result.is_ok || (result.count == 0)) { - return NULL_ROW_ID; - } - return true; + return indexes_[0]->find_one(datum); } else { // TODO: A full scan takes time. // An index should be required for a key column. Modified: lib/grnxx/index.cpp (+18 -0) =================================================================== --- lib/grnxx/index.cpp 2014-09-30 17:10:57 +0900 (6c8e604) +++ lib/grnxx/index.cpp 2014-09-30 17:46:29 +0900 (6ac6930) @@ -12,6 +12,24 @@ namespace grnxx { Index::~Index() {} +bool Index::contains(const Datum &datum) const { + return find_one(datum) != NULL_ROW_ID; +} + +Int Index::find_one(const Datum &datum) const { + // TODO: This function should not fail, so Cursor should not be used. + auto cursor = find(nullptr, datum); + if (!cursor) { + return NULL_ROW_ID; + } + Array<Record> records; + auto result = cursor->read(nullptr, 1, &records); + if (!result.is_ok || (result.count == 0)) { + return NULL_ROW_ID; + } + return records[0].row_id; +} + unique_ptr<Cursor> Index::find( Error *error, const Datum &, -------------- next part -------------- HTML����������������������������...Download