susumu.yata
null+****@clear*****
Thu Dec 4 12:26:20 JST 2014
susumu.yata 2014-12-04 12:26:20 +0900 (Thu, 04 Dec 2014) New Revision: 96ad811aa1fc8272ae778b43d3879c0b1e617788 https://github.com/groonga/grnxx/commit/96ad811aa1fc8272ae778b43d3879c0b1e617788 Message: Update Column::contains/find_one() to use an index if available. (#125) Modified files: lib/grnxx/impl/column/scalar/float.cpp lib/grnxx/impl/column/scalar/int.cpp lib/grnxx/impl/column/scalar/text.cpp Modified: lib/grnxx/impl/column/scalar/float.cpp (+9 -3) =================================================================== --- lib/grnxx/impl/column/scalar/float.cpp 2014-12-04 11:24:48 +0900 (3ecb23c) +++ lib/grnxx/impl/column/scalar/float.cpp 2014-12-04 12:26:20 +0900 (ff62a2a) @@ -1,7 +1,7 @@ #include "grnxx/impl/column/scalar/float.hpp" #include "grnxx/impl/table.hpp" -//#include "grnxx/index.hpp" +#include "grnxx/impl/index.hpp" namespace grnxx { namespace impl { @@ -59,7 +59,10 @@ void Column<Float>::get(Int row_id, Datum *datum) const { } bool Column<Float>::contains(const Datum &datum) const { - // TODO: Use an index if exists. + // TODO: Choose the best index. + if (!indexes_.is_empty()) { + return indexes_[0]->contains(datum); + } Float value = parse_datum(datum); size_t valid_size = get_valid_size(); if (value.is_na()) { @@ -79,7 +82,10 @@ bool Column<Float>::contains(const Datum &datum) const { } Int Column<Float>::find_one(const Datum &datum) const { - // TODO: Use an index if exists. + // TODO: Choose the best index. + if (!indexes_.is_empty()) { + return indexes_[0]->find_one(datum); + } Float value = parse_datum(datum); size_t valid_size = get_valid_size(); if (value.is_na()) { Modified: lib/grnxx/impl/column/scalar/int.cpp (+9 -36) =================================================================== --- lib/grnxx/impl/column/scalar/int.cpp 2014-12-04 11:24:48 +0900 (a000dbd) +++ lib/grnxx/impl/column/scalar/int.cpp 2014-12-04 12:26:20 +0900 (1d88056) @@ -2,7 +2,7 @@ #include "grnxx/impl/db.hpp" #include "grnxx/impl/table.hpp" -//#include "grnxx/impl/index.hpp" +#include "grnxx/impl/index.hpp" #include <unordered_set> @@ -82,7 +82,10 @@ void Column<Int>::get(Int row_id, Datum *datum) const { } bool Column<Int>::contains(const Datum &datum) const { - // TODO: Use an index if exists. + // TODO: Choose the best index. + if (!indexes_.is_empty()) { + return indexes_[0]->contains(datum); + } Int value = parse_datum(datum); if (value.is_na()) { for (size_t i = 0; i < values_.size(); ++i) { @@ -101,7 +104,10 @@ bool Column<Int>::contains(const Datum &datum) const { } Int Column<Int>::find_one(const Datum &datum) const { - // TODO: Use an index if exists. + // TODO: Choose the best index. + if (!indexes_.is_empty()) { + return indexes_[0]->find_one(datum); + } Int value = parse_datum(datum); if (value.is_na()) { for (size_t i = 0; i < values_.size(); ++i) { @@ -117,39 +123,6 @@ Int Column<Int>::find_one(const Datum &datum) const { } } return Int::na(); - -// // TODO: Cursor should not be used because it takes time. -// // Also, cursor operations can fail due to memory allocation. -// Int value = datum.force_int(); -// if (indexes_.size() != 0) { -// return indexes_[0]->find_one(datum); -// } else { -// // TODO: A full scan takes time. -// // An index should be required for a key column. - -// // TODO: Functor-based inline callback may be better in this case, -// // because it does not require memory allocation. - -// // Scan the column to find "value". -// auto cursor = table_->create_cursor(nullptr); -// if (!cursor) { -// return NULL_ROW_ID; -// } -// Array<Record> records; -// for ( ; ; ) { -// auto result = cursor->read(nullptr, 1024, &records); -// if (!result.is_ok || result.count == 0) { -// return NULL_ROW_ID; -// } -// for (Int i = 0; i < result.count; ++i) { -// if (values_[records.get_row_id(i)] == value) { -// return records.get_row_id(i); -// } -// } -// records.clear(); -// } -// } -// return NULL_ROW_ID; } void Column<Int>::set_key_attribute() { Modified: lib/grnxx/impl/column/scalar/text.cpp (+9 -45) =================================================================== --- lib/grnxx/impl/column/scalar/text.cpp 2014-12-04 11:24:48 +0900 (e69995c) +++ lib/grnxx/impl/column/scalar/text.cpp 2014-12-04 12:26:20 +0900 (281429c) @@ -3,9 +3,8 @@ #include <cstring> #include <set> -#include "grnxx/impl/db.hpp" #include "grnxx/impl/table.hpp" -//#include "grnxx/impl/index.hpp" +#include "grnxx/impl/index.hpp" namespace grnxx { namespace impl { @@ -144,7 +143,10 @@ void Column<Text>::get(Int row_id, Datum *datum) const { } bool Column<Text>::contains(const Datum &datum) const { - // TODO: Use an index if exists. + // TODO: Choose the best index. + if (!indexes_.is_empty()) { + return indexes_[0]->contains(datum); + } Text value = parse_datum(datum); size_t valid_size = get_valid_size(); if (value.is_na()) { @@ -165,7 +167,10 @@ bool Column<Text>::contains(const Datum &datum) const { } Int Column<Text>::find_one(const Datum &datum) const { - // TODO: Use an index if exists. + // TODO: Choose the best index. + if (!indexes_.is_empty()) { + return indexes_[0]->find_one(datum); + } Text value = parse_datum(datum); size_t valid_size = get_valid_size(); if (value.is_na()) { @@ -185,47 +190,6 @@ Int Column<Text>::find_one(const Datum &datum) const { return Int::na(); } -//Int Column<Text>::find_one(const Datum &datum) const { -// // TODO: Cursor should not be used because it takes time. -// // Also, cursor operations can fail due to memory allocation. -// Text value = datum.force_text(); -// 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; -// } else { -// // TODO: A full scan takes time. -// // An index should be required for a key column. - -// // TODO: Functor-based inline callback may be better in this case, -// // because it does not require memory allocation. - -// // Scan the column to find "value". -// auto cursor = table_->create_cursor(nullptr); -// if (!cursor) { -// return NULL_ROW_ID; -// } -// Array<Record> records; -// for ( ; ; ) { -// auto result = cursor->read(nullptr, 1024, &records); -// if (!result.is_ok || result.count == 0) { -// return NULL_ROW_ID; -// } -// for (Int i = 0; i < result.count; ++i) { -// if (get(records.get_row_id(i)) == value) { -// return records.get_row_id(i); -// } -// } -// records.clear(); -// } -// } -// return NULL_ROW_ID; -//} - void Column<Text>::set_key_attribute() { if (is_key_) { throw "Key column"; // TODO -------------- next part -------------- HTML����������������������������...Download