susumu.yata
null+****@clear*****
Thu May 2 15:15:22 JST 2013
susumu.yata 2013-05-02 15:15:22 +0900 (Thu, 02 May 2013) New Revision: 35c2a068708f63fe28acacb4dde26f36a30e07be https://github.com/groonga/grnxx/commit/35c2a068708f63fe28acacb4dde26f36a30e07be Message: Update grnxx::alpha::MapScan. Added files: lib/grnxx/alpha/map/scan.cpp lib/grnxx/alpha/map/scan.hpp Modified files: lib/grnxx/alpha/map.cpp lib/grnxx/alpha/map.hpp lib/grnxx/alpha/map/Makefile.am Modified: lib/grnxx/alpha/map.cpp (+9 -37) =================================================================== --- lib/grnxx/alpha/map.cpp 2013-05-01 13:09:58 +0900 (d28fbfc) +++ lib/grnxx/alpha/map.cpp 2013-05-02 15:15:22 +0900 (045ea4a) @@ -22,7 +22,7 @@ #include "grnxx/alpha/map/cursor.hpp" #include "grnxx/alpha/map/double_array.hpp" #include "grnxx/alpha/map/header.hpp" -#include "grnxx/charset.hpp" +#include "grnxx/alpha/map/scan.hpp" #include "grnxx/exception.hpp" #include "grnxx/slice.hpp" #include "grnxx/logger.hpp" @@ -324,20 +324,14 @@ MapCursor<Slice> *Map<Slice>::open_reverse_completion_cursor( } template <typename T> -MapScan *Map<T>::open_scan(T, const Charset *) { +MapScan<T> *Map<T>::open_scan(T, const Charset *) { // Not supported return nullptr; } template <> -MapScan *Map<Slice>::open_scan(Slice query, const Charset *charset) { - std::unique_ptr<MapScan> scan( - new (std::nothrow) MapScan(this, query, charset)); - if (!scan) { - GRNXX_ERROR() << "new grnxx::MapScan failed"; - GRNXX_THROW(); - } - return scan.release(); +MapScan<Slice> *Map<Slice>::open_scan(Slice query, const Charset *charset) { + return new (std::nothrow) map::Scan(this, query, charset); } template class Map<int8_t>; @@ -352,35 +346,13 @@ template class Map<double>; template class Map<GeoPoint>; template class Map<Slice>; -MapScan::~MapScan() {} +template <typename T> +MapScan<T>::MapScan() : offset_(0), size_(0), key_id_(-1), key_() {} -bool MapScan::next() { - offset_ += size_; - while (offset_ < query_.size()) { - const Slice query_left = query_.subslice(offset_, query_.size() - offset_); - if (map_->find_longest_prefix_match(query_left, &key_id_, &key_)) { - size_ = key_.size(); - return true; - } - // Move to the next character. - if (charset_) { - offset_ += charset_->get_char_size(query_left); - } else { - ++offset_; - } - } - size_ = 0; - return false; -} +template <typename T> +MapScan<T>::~MapScan() {} -MapScan::MapScan(Map<Slice> *map, const Slice &query, const Charset *charset) - : map_(map), - query_(query), - offset_(0), - size_(0), - key_id_(-1), - key_(), - charset_(charset) {} +template class MapScan<Slice>; } // namespace alpha } // namespace grnxx Modified: lib/grnxx/alpha/map.hpp (+34 -43) =================================================================== --- lib/grnxx/alpha/map.hpp 2013-05-01 13:09:58 +0900 (7799f63) +++ lib/grnxx/alpha/map.hpp 2013-05-02 15:15:22 +0900 (e3e561b) @@ -27,9 +27,9 @@ class Charset; namespace alpha { -template <typename T> class MapCursor; -class MapScan; template <typename T> class Map; +template <typename T> class MapCursor; +template <typename T> class MapScan; enum MapType : int32_t { MAP_UNKNOWN = 0, @@ -79,31 +79,6 @@ struct MapCursorOptions { }; template <typename T> -class MapCursor { - public: - MapCursor(); - virtual ~MapCursor(); - - // Move the cursor to the next key and return true on success. - virtual bool next(); - // Remove the current key and return true on success. - virtual bool remove(); - - // Return the ID of the current key. - int64_t key_id() const { - return key_id_; - } - // Return a reference to the current key. - T key() const { - return key_; - } - - protected: - int64_t key_id_; - T key_; -}; - -template <typename T> class Map { public: Map(); @@ -204,23 +179,44 @@ class Map { // Only for Slice. // Create a MapScan object to find keys in "query". - MapScan *open_scan(T query, const Charset *charset = nullptr); + virtual MapScan<T> *open_scan(T query, const Charset *charset = nullptr); }; -class MapScan { - friend class Map<Slice>; +template <typename T> +class MapCursor { + public: + MapCursor(); + virtual ~MapCursor(); + + // Move the cursor to the next key and return true on success. + virtual bool next(); + // Remove the current key and return true on success. + virtual bool remove(); + // Return the ID of the current key. + int64_t key_id() const { + return key_id_; + } + // Return a reference to the current key. + const T &key() const { + return key_; + } + + protected: + int64_t key_id_; + T key_; +}; + +template <typename T> +class MapScan { public: - ~MapScan(); + MapScan(); + virtual ~MapScan(); // Scan the rest of the query and return true iff a key is found (success). // On success, the found key is accessible via accessors. - bool next(); + virtual bool next() = 0; - // Return the query. - const Slice &query() const { - return query_; - } // Return the start position of the found key. uint64_t offset() const { return offset_; @@ -234,20 +230,15 @@ class MapScan { return key_id_; } // Return a reference to the found key. - const Slice &key() const { + const T &key() const { return key_; } protected: - Map<Slice> *map_; - Slice query_; uint64_t offset_; uint64_t size_; int64_t key_id_; - Slice key_; - const Charset *charset_; - - MapScan(Map<Slice> *map, const Slice &query, const Charset *charset); + T key_; }; } // namespace alpha Modified: lib/grnxx/alpha/map/Makefile.am (+4 -2) =================================================================== --- lib/grnxx/alpha/map/Makefile.am 2013-05-01 13:09:58 +0900 (01ee08b) +++ lib/grnxx/alpha/map/Makefile.am 2013-05-02 15:15:22 +0900 (ab16b6e) @@ -6,11 +6,13 @@ libgrnxx_alpha_map_la_SOURCES = \ array.cpp \ cursor.cpp \ double_array.cpp \ - double_array-slice.cpp + double_array-slice.cpp \ + scan.cpp libgrnxx_alpha_map_includedir = ${includedir}/grnxx/alpha/map libgrnxx_alpha_map_include_HEADERS = \ array.hpp \ cursor.hpp \ double_array.hpp \ - header.hpp + header.hpp \ + scan.hpp Added: lib/grnxx/alpha/map/scan.cpp (+57 -0) 100644 =================================================================== --- /dev/null +++ lib/grnxx/alpha/map/scan.cpp 2013-05-02 15:15:22 +0900 (8bcfb2e) @@ -0,0 +1,57 @@ +/* + Copyright (C) 2013 Brazil, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "grnxx/alpha/map/scan.hpp" + +#include "grnxx/charset.hpp" + +namespace grnxx { +namespace alpha { +namespace map { + +Scan::Scan(Map<Slice> *map, const Slice &query, const Charset *charset) + : MapScan<Slice>(), + map_(map), + query_(query), + charset_(charset) {} + +Scan::~Scan() {} + +bool Scan::next() { + this->offset_ += this->size_; + while (this->offset_ < query_.size()) { + const Slice query_left = + query_.subslice(this->offset_, query_.size() - this->offset_); + if (map_->find_longest_prefix_match(query_left, + &this->key_id_, &this->key_)) { + this->size_ = this->key_.size(); + return true; + } + // Move to the next character. + if (charset_) { + this->offset_ += charset_->get_char_size(query_left); + } else { + ++this->offset_; + } + } + this->size_ = 0; + return false; +} + +} // namespace map +} // namespace alpha +} // namespace grnxx Added: lib/grnxx/alpha/map/scan.hpp (+47 -0) 100644 =================================================================== --- /dev/null +++ lib/grnxx/alpha/map/scan.hpp 2013-05-02 15:15:22 +0900 (3e80527) @@ -0,0 +1,47 @@ +/* + Copyright (C) 2013 Brazil, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef GRNXX_ALPHA_MAP_SCAN_HPP +#define GRNXX_ALPHA_MAP_SCAN_HPP + +#include "grnxx/alpha/map.hpp" + +namespace grnxx { + +class Charset; + +namespace alpha { +namespace map { + +class Scan : public MapScan<Slice> { + public: + Scan(Map<Slice> *map, const Slice &query, const Charset *charset); + ~Scan(); + + bool next(); + + protected: + Map<Slice> *map_; + Slice query_; + const Charset *charset_; +}; + +} // namespace map +} // namespace alpha +} // namespace grnxx + +#endif // GRNXX_ALPHA_MAP_SCAN_HPP -------------- next part -------------- HTML����������������������������... Download