susumu.yata
null+****@clear*****
Tue May 28 20:18:36 JST 2013
susumu.yata 2013-05-28 20:18:36 +0900 (Tue, 28 May 2013) New Revision: e4ad7a35225873be63ff13597cfaf682f4a26a8d https://github.com/groonga/grnxx/commit/e4ad7a35225873be63ff13597cfaf682f4a26a8d Message: Add grnxx::map::KeyArray. Added files: lib/grnxx/map/key_array.cpp lib/grnxx/map/key_array.hpp Modified files: lib/grnxx/map/Makefile.am test/test_map.cpp Modified: lib/grnxx/map/Makefile.am (+2 -0) =================================================================== --- lib/grnxx/map/Makefile.am 2013-05-28 20:11:16 +0900 (7050014) +++ lib/grnxx/map/Makefile.am 2013-05-28 20:18:36 +0900 (bcc0e80) @@ -6,6 +6,7 @@ libgrnxx_map_la_SOURCES = \ array_map.cpp \ bitmap.cpp \ bytes_store.cpp \ + key_array.cpp \ cursor_impl.cpp \ scanner_impl.cpp @@ -17,4 +18,5 @@ libgrnxx_map_include_HEADERS = \ cursor_impl.hpp \ header.hpp \ helper.hpp \ + key_array.hpp \ scanner_impl.hpp Added: lib/grnxx/map/key_array.cpp (+24 -0) 100644 =================================================================== --- /dev/null +++ lib/grnxx/map/key_array.cpp 2013-05-28 20:18:36 +0900 (6efd1ad) @@ -0,0 +1,24 @@ +/* + 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/map/key_array.hpp" + +namespace grnxx { +namespace map { + +} // namespace map +} // namespace grnxx Added: lib/grnxx/map/key_array.hpp (+113 -0) 100644 =================================================================== --- /dev/null +++ lib/grnxx/map/key_array.hpp 2013-05-28 20:18:36 +0900 (dcc24c0) @@ -0,0 +1,113 @@ +/* + 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_KEY_ARRAY_HPP +#define GRNXX_MAP_KEY_ARRAY_HPP + +#include "grnxx/features.hpp" + +#include "grnxx/array.hpp" +#include "grnxx/types.hpp" + +namespace grnxx { + +class Storage; + +namespace map { + +// Change the settings based on the key type. +template <typename T, size_t T_SIZE = sizeof(T)> +struct KeyArrayTraits { + // Map<T> has at most 2^40 different keys. + using ArrayType = Array<T>; +}; +template <typename T> +struct KeyArrayTraits<T, 1> { + // Map<T> has at most 2^8 different keys. + using ArrayType = Array<T, 256, 1, 1>; +}; +template <typename T> +struct KeyArrayTraits<T, 2> { + // Map<T> has at most 2^16 different keys. + using ArrayType = Array<T, 256, 256, 1>; +}; +template <typename T> +struct KeyArrayTraits<T, 4> { + // Map<T> has at most 2^32 different keys. + using ArrayType = Array<T, 65536, 256, 256>; +}; + +template <typename T> +class KeyArray { + public: + using ArrayImpl = typename KeyArrayTraits<T>::ArrayType; + using Key = typename ArrayImpl::Value; + using KeyArg = typename ArrayImpl::ValueArg; + + // Return true iff the array is valid. + explicit operator bool() const { + return static_cast<bool>(impl_); + } + + // Create an array. + bool create(Storage *storage, uint32_t storage_node_id) { + return impl_.create(storage, storage_node_id); + } + // Create an array with the default key. + bool create(Storage *storage, uint32_t storage_node_id, + KeyArg default_key) { + return impl_.create(storage, storage_node_id, default_key); + } + // Open an array. + bool open(Storage *storage, uint32_t storage_node_id) { + return impl_.open(storage, storage_node_id); + } + + // Unlink an array. + static bool unlink(Storage *storage, uint32_t storage_node_id) { + return ArrayImpl::unlink(storage, storage_node_id); + } + + // Return the storage node ID. + uint32_t storage_node_id() const { + return impl_.storage_node_id(); + } + + // Get a key. + // This function throws an exception on failure. + Key operator[](uint64_t key_id) { + return impl_[key_id]; + } + + // Get a key and return true on success. + // The key is assigned to "*key" iff "key" != nullptr. + bool get(uint64_t key_id, Key *key) { + return impl_.get(key_id, key); + } + // Set a key and return true on success. + bool set(uint64_t key_id, KeyArg key) { + return impl_.set(key_id, key); + } + + private: + ArrayImpl impl_; +}; + +} // namespace map +} // namespace grnxx + +#endif // GRNXX_MAP_KEY_ARRAY_HPP Modified: test/test_map.cpp (+1 -0) =================================================================== --- test/test_map.cpp 2013-05-28 20:11:16 +0900 (db88f08) +++ test/test_map.cpp 2013-05-28 20:18:36 +0900 (ba1c279) @@ -903,6 +903,7 @@ int main() { // TODO: test_bitmap(); test_bytes_store(); + // TODO: test_key_array(); test_map(); return 0; -------------- next part -------------- HTML����������������������������... Download