susumu.yata
null+****@clear*****
Mon Jun 17 14:05:41 JST 2013
susumu.yata 2013-06-17 14:05:41 +0900 (Mon, 17 Jun 2013) New Revision: e48df0dedab8814d03fc54082d0b45904e111164 https://github.com/groonga/grnxx/commit/e48df0dedab8814d03fc54082d0b45904e111164 Message: Update grnxx::map::ArrayMap to use grnxx::map::KeyStore. Removed files: lib/grnxx/map/array_map/bit_array.hpp lib/grnxx/map/array_map/key_array.hpp Modified files: lib/grnxx/map/array_map.cpp lib/grnxx/map/array_map.hpp lib/grnxx/map/array_map/Makefile.am lib/grnxx/map/array_map/dummy.cpp lib/grnxx/map/array_map/header.cpp lib/grnxx/map/array_map/header.hpp Modified: lib/grnxx/map/array_map.cpp (+29 -63) =================================================================== --- lib/grnxx/map/array_map.cpp 2013-06-14 12:34:13 +0900 (857aa3a) +++ lib/grnxx/map/array_map.cpp 2013-06-17 14:05:41 +0900 (1f6537a) @@ -34,8 +34,7 @@ ArrayMap<T>::ArrayMap() : storage_(nullptr), storage_node_id_(STORAGE_INVALID_NODE_ID), header_(nullptr), - keys_(), - bits_() {} + keys_() {} template <typename T> ArrayMap<T>::~ArrayMap() {} @@ -79,22 +78,22 @@ MapType ArrayMap<T>::type() const { template <typename T> int64_t ArrayMap<T>::max_key_id() const { - return header_->max_key_id; + return keys_->max_key_id(); } template <typename T> uint64_t ArrayMap<T>::num_keys() const { - return header_->num_keys; + return keys_->num_keys(); } template <typename T> bool ArrayMap<T>::get(int64_t key_id, Key *key) { - if ((key_id < MAP_MIN_KEY_ID) || (key_id > header_->max_key_id)) { + if ((key_id < MAP_MIN_KEY_ID) || (key_id > max_key_id())) { // Out of range. return false; } bool bit; - if (!bits_->get(key_id, &bit)) { + if (!keys_->get_bit(key_id, &bit)) { // Error. return false; } @@ -102,20 +101,19 @@ bool ArrayMap<T>::get(int64_t key_id, Key *key) { // Not found. return false; } - return keys_->get(key_id, key); + if (!keys_->get_key(key_id, key)) { + // Error. + return false; + } + return true; } template <typename T> bool ArrayMap<T>::unset(int64_t key_id) { - if (!get(key_id)) { + if (!keys_->unset(key_id)) { // Not found or error. return false; } - if (!bits_->set(key_id, false)) { - // Error. - return false; - } - --header_->num_keys; return true; } @@ -129,7 +127,7 @@ bool ArrayMap<T>::reset(int64_t key_id, KeyArg dest_key) { // Found. return false; } - if (!keys_->set(key_id, Helper<T>::normalize(dest_key))) { + if (!keys_->reset(key_id, Helper<T>::normalize(dest_key))) { // Error. return false; } @@ -139,15 +137,15 @@ bool ArrayMap<T>::reset(int64_t key_id, KeyArg dest_key) { template <typename T> bool ArrayMap<T>::find(KeyArg key, int64_t *key_id) { const Key normalized_key = map::Helper<T>::normalize(key); - for (int64_t i = MAP_MIN_KEY_ID; i <= header_->max_key_id; ++i) { + for (int64_t i = MAP_MIN_KEY_ID; i <= max_key_id(); ++i) { bool bit; - if (!bits_->get(i, &bit)) { + if (!keys_->get_bit(i, &bit)) { // Error. return false; } if (bit) { Key stored_key; - if (!keys_->get(i, &stored_key)) { + if (!keys_->get_key(i, &stored_key)) { // Error. return false; } @@ -166,16 +164,15 @@ bool ArrayMap<T>::find(KeyArg key, int64_t *key_id) { template <typename T> bool ArrayMap<T>::add(KeyArg key, int64_t *key_id) { const Key normalized_key = Helper<T>::normalize(key); - int64_t next_key_id = MAP_INVALID_KEY_ID; - for (int64_t i = MAP_MIN_KEY_ID; i <= header_->max_key_id; ++i) { + for (int64_t i = MAP_MIN_KEY_ID; i <= max_key_id(); ++i) { bool bit; - if (!bits_->get(i, &bit)) { + if (!keys_->get_bit(i, &bit)) { // Error. return false; } if (bit) { Key stored_key; - if (!keys_->get(i, &stored_key)) { + if (!keys_->get_key(i, &stored_key)) { // Error. return false; } @@ -186,37 +183,12 @@ bool ArrayMap<T>::add(KeyArg key, int64_t *key_id) { } return false; } - } else if (next_key_id == MAP_INVALID_KEY_ID) { - next_key_id = i; - } - } - if (next_key_id == MAP_INVALID_KEY_ID) { - next_key_id = header_->max_key_id + 1; - if (next_key_id > MAP_MAX_KEY_ID) { - GRNXX_ERROR() << "too many keys: next_key_id = " << next_key_id - << ", max_key_id = " << MAP_MAX_KEY_ID; - return false; } } - const uint64_t unit_id = next_key_id / bits_->unit_size(); - const uint64_t unit_bit = 1ULL << (next_key_id % bits_->unit_size()); - BitArrayUnit * const unit = bits_->get_unit(unit_id); - if (!unit) { - // Error. - return false; - } - if (!keys_->set(next_key_id, normalized_key)) { + if (!keys_->add(normalized_key, key_id)) { // Error. return false; } - *unit |= unit_bit; - if (next_key_id == (header_->max_key_id + 1)) { - ++header_->max_key_id; - } - ++header_->num_keys; - if (key_id) { - *key_id = next_key_id; - } return true; } @@ -227,11 +199,10 @@ bool ArrayMap<T>::remove(KeyArg key) { // Not found or error. return false; } - if (!bits_->set(key_id, false)) { + if (!keys_->unset(key_id)) { // Error. return false; } - --header_->num_keys; return true; } @@ -240,15 +211,15 @@ bool ArrayMap<T>::replace(KeyArg src_key, KeyArg dest_key, int64_t *key_id) { const Key normalized_src_key = Helper<T>::normalize(src_key); const Key normalized_dest_key = Helper<T>::normalize(dest_key); int64_t src_key_id = MAP_INVALID_KEY_ID; - for (int64_t i = MAP_MIN_KEY_ID; i <= header_->max_key_id; ++i) { + for (int64_t i = MAP_MIN_KEY_ID; i <= max_key_id(); ++i) { bool bit; - if (!bits_->get(i, &bit)) { + if (!keys_->get_bit(i, &bit)) { // Error. return false; } if (bit) { Key stored_key; - if (!keys_->get(i, &stored_key)) { + if (!keys_->get_key(i, &stored_key)) { // Error. return false; } @@ -266,7 +237,7 @@ bool ArrayMap<T>::replace(KeyArg src_key, KeyArg dest_key, int64_t *key_id) { // Not found. return false; } - if (!keys_->set(src_key_id, normalized_dest_key)) { + if (!keys_->reset(src_key_id, normalized_dest_key)) { // Error. return false; } @@ -278,9 +249,7 @@ bool ArrayMap<T>::replace(KeyArg src_key, KeyArg dest_key, int64_t *key_id) { template <typename T> bool ArrayMap<T>::truncate() { - header_->max_key_id = MAP_MIN_KEY_ID - 1; - header_->num_keys = 0; - return true; + return keys_->truncate(); } template <typename T> @@ -295,14 +264,12 @@ bool ArrayMap<T>::create_map(Storage *storage, uint32_t storage_node_id, storage_node_id_ = storage_node.id(); header_ = static_cast<Header *>(storage_node.body()); *header_ = Header(); - keys_.reset(KeyArray::create(storage, storage_node_id_)); - bits_.reset(BitArray::create(storage, storage_node_id_)); - if (!keys_ || !bits_) { + keys_.reset(KeyStore<T>::create(storage, storage_node_id_)); + if (!keys_) { storage->unlink_node(storage_node_id_); return false; } header_->keys_storage_node_id = keys_->storage_node_id(); - header_->bits_storage_node_id = bits_->storage_node_id(); return true; } @@ -320,9 +287,8 @@ bool ArrayMap<T>::open_map(Storage *storage, uint32_t storage_node_id) { } storage_node_id_ = storage_node_id; header_ = static_cast<Header *>(storage_node.body()); - keys_.reset(KeyArray::open(storage, header_->keys_storage_node_id)); - bits_.reset(BitArray::open(storage, header_->bits_storage_node_id)); - if (!keys_ || !bits_) { + keys_.reset(KeyStore<T>::open(storage, header_->keys_storage_node_id)); + if (!keys_) { return false; } return true; Modified: lib/grnxx/map/array_map.hpp (+2 -7) =================================================================== --- lib/grnxx/map/array_map.hpp 2013-06-14 12:34:13 +0900 (55678de) +++ lib/grnxx/map/array_map.hpp 2013-06-17 14:05:41 +0900 (b290e0c) @@ -23,8 +23,7 @@ #include <memory> #include "grnxx/map.hpp" -#include "grnxx/map/array_map/bit_array.hpp" -#include "grnxx/map/array_map/key_array.hpp" +#include "grnxx/map/key_store.hpp" #include "grnxx/types.hpp" namespace grnxx { @@ -41,9 +40,6 @@ struct Header; template <typename T> class ArrayMap : public Map<T> { using Header = array_map::Header; - using KeyArray = typename array_map::KeyArray<T>::Type; - using BitArray = typename array_map::BitArray<T>::Type; - using BitArrayUnit = typename BitArray::Unit; public: using Key = typename Map<T>::Key; @@ -78,8 +74,7 @@ class ArrayMap : public Map<T> { Storage *storage_; uint32_t storage_node_id_; Header *header_; - std::unique_ptr<KeyArray> keys_; - std::unique_ptr<BitArray> bits_; + std::unique_ptr<KeyStore<T>> keys_; bool create_map(Storage *storage, uint32_t storage_node_id, const MapOptions &options); Modified: lib/grnxx/map/array_map/Makefile.am (+1 -3) =================================================================== --- lib/grnxx/map/array_map/Makefile.am 2013-06-14 12:34:13 +0900 (c9b5701) +++ lib/grnxx/map/array_map/Makefile.am 2013-06-17 14:05:41 +0900 (79ecb61) @@ -8,7 +8,5 @@ libgrnxx_map_array_map_la_SOURCES = \ libgrnxx_map_array_map_includedir = ${includedir}/grnxx/map/array_map libgrnxx_map_array_map_include_HEADERS = \ - bit_array.hpp \ dummy.hpp \ - header.hpp \ - key_array.hpp + header.hpp Deleted: lib/grnxx/map/array_map/bit_array.hpp (+0 -62) 100644 =================================================================== --- lib/grnxx/map/array_map/bit_array.hpp 2013-06-14 12:34:13 +0900 (ab7cd3f) +++ /dev/null @@ -1,62 +0,0 @@ -/* - 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_MAP_ARRAY_MAP_BIT_ARRAY_HPP -#define GRNXX_MAP_ARRAY_MAP_BIT_ARRAY_HPP - -#include "grnxx/features.hpp" - -#include "grnxx/array.hpp" -#include "grnxx/types.hpp" - -namespace grnxx { -namespace map { -namespace array_map { - -// Change the array size based on the size of "T". -template <typename T, size_t T_SIZE = sizeof(T)> -struct BitArray; - -// Map<T> has at most 2^40 different keys. -template <typename T, size_t T_SIZE> -struct BitArray { - using Type = Array<bool, 65536, 4096, 4096>; -}; - -// Map<T> has at most 2^8 different keys. -template <typename T> -struct BitArray<T, 1> { - using Type = Array<bool, 256, 1, 1>; -}; - -// Map<T> has at most 2^16 different keys. -template <typename T> -struct BitArray<T, 2> { - using Type = Array<bool, 256, 256, 1>; -}; - -// Map<T> has at most 2^32 different keys. -template <typename T> -struct BitArray<T, 4> { - using Type = Array<bool, 16384, 512, 512>; -}; - -} // namespace array_map -} // namespace map -} // namespace grnxx - -#endif // GRNXX_MAP_ARRAY_MAP_BIT_ARRAY_HPP Modified: lib/grnxx/map/array_map/dummy.cpp (+0 -2) =================================================================== --- lib/grnxx/map/array_map/dummy.cpp 2013-06-14 12:34:13 +0900 (04c0d4f) +++ lib/grnxx/map/array_map/dummy.cpp 2013-06-17 14:05:41 +0900 (c4a196a) @@ -17,9 +17,7 @@ */ #include "grnxx/map/array_map/dummy.hpp" -#include "grnxx/map/array_map/bit_array.hpp" #include "grnxx/map/array_map/header.hpp" -#include "grnxx/map/array_map/key_array.hpp" namespace grnxx { namespace map { Modified: lib/grnxx/map/array_map/header.cpp (+1 -4) =================================================================== --- lib/grnxx/map/array_map/header.cpp 2013-06-14 12:34:13 +0900 (28a5644) +++ lib/grnxx/map/array_map/header.cpp 2013-06-17 14:05:41 +0900 (b25b4d1) @@ -25,10 +25,7 @@ namespace array_map { Header::Header() : map_type(MAP_ARRAY), - keys_storage_node_id(STORAGE_INVALID_NODE_ID), - bits_storage_node_id(STORAGE_INVALID_NODE_ID), - max_key_id(MAP_MIN_KEY_ID - 1), - num_keys(0) {} + keys_storage_node_id(STORAGE_INVALID_NODE_ID) {} } // namespace array_map } // namespace map Modified: lib/grnxx/map/array_map/header.hpp (+0 -3) =================================================================== --- lib/grnxx/map/array_map/header.hpp 2013-06-14 12:34:13 +0900 (5c67f6c) +++ lib/grnxx/map/array_map/header.hpp 2013-06-17 14:05:41 +0900 (fab8fa2) @@ -30,9 +30,6 @@ namespace array_map { struct Header { MapType map_type; uint32_t keys_storage_node_id; - uint32_t bits_storage_node_id; - int64_t max_key_id; - uint64_t num_keys; Header(); }; Deleted: lib/grnxx/map/array_map/key_array.hpp (+0 -70) 100644 =================================================================== --- lib/grnxx/map/array_map/key_array.hpp 2013-06-14 12:34:13 +0900 (399fcb3) +++ /dev/null @@ -1,70 +0,0 @@ -/* - 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_MAP_ARRAY_MAP_KEY_ARRAY_HPP -#define GRNXX_MAP_ARRAY_MAP_KEY_ARRAY_HPP - -#include "grnxx/features.hpp" - -#include "grnxx/array.hpp" -#include "grnxx/bytes.hpp" -#include "grnxx/map/bytes_array.hpp" -#include "grnxx/types.hpp" - -namespace grnxx { -namespace map { -namespace array_map { - -// Change the array size based on "T". -template <typename T, size_t T_SIZE = sizeof(T)> -struct KeyArray; - -// Map<T> has at most 2^40 different keys. -template <typename T, size_t T_SIZE> -struct KeyArray { - using Type = Array<T, 65536, 4096, 4096>; -}; - -// Map<T> has at most 2^8 different keys. -template <typename T> -struct KeyArray<T, 1> { - using Type = Array<T, 256, 1, 1>; -}; - -// Map<T> has at most 2^16 different keys. -template <typename T> -struct KeyArray<T, 2> { - using Type = Array<T, 256, 256, 1>; -}; - -// Map<T> has at most 2^32 different keys. -template <typename T> -struct KeyArray<T, 4> { - using Type = Array<T, 65536, 256, 256>; -}; - -// Map<T> has at most 2^40 different keys. -template <> -struct KeyArray<Bytes> { - using Type = BytesArray; -}; - -} // namespace array_map -} // namespace map -} // namespace grnxx - -#endif // GRNXX_MAP_ARRAY_MAP_KEY_ARRAY_HPP -------------- next part -------------- HTML����������������������������...Download