susumu.yata
null+****@clear*****
Tue Jul 23 13:24:35 JST 2013
susumu.yata 2013-07-23 13:24:35 +0900 (Tue, 23 Jul 2013) New Revision: 7bcbb79ab2f6d548d7decb040e6a213eb000134d https://github.com/groonga/grnxx/commit/7bcbb79ab2f6d548d7decb040e6a213eb000134d Message: Update grnxx::Map and its implementations to use grnxx::map::CommonHeader. Modified files: lib/grnxx/map.cpp lib/grnxx/map/array_map.cpp lib/grnxx/map/array_map.hpp lib/grnxx/map/double_array.cpp lib/grnxx/map/double_array.hpp lib/grnxx/map/hash_table.cpp lib/grnxx/map/hash_table.hpp lib/grnxx/map/patricia.cpp lib/grnxx/map/patricia.hpp Modified: lib/grnxx/map.cpp (+9 -5) =================================================================== --- lib/grnxx/map.cpp 2013-07-23 13:23:05 +0900 (3667632) +++ lib/grnxx/map.cpp 2013-07-23 13:24:35 +0900 (b295088) @@ -26,10 +26,10 @@ #include "grnxx/storage.hpp" #include "grnxx/string_builder.hpp" #include "grnxx/map/array_map.hpp" +#include "grnxx/map/common_header.hpp" #include "grnxx/map/cursor_impl.hpp" #include "grnxx/map/double_array.hpp" #include "grnxx/map/hash_table.hpp" -#include "grnxx/map/header.hpp" #include "grnxx/map/helper.hpp" #include "grnxx/map/patricia.hpp" #include "grnxx/map/scanner_impl.hpp" @@ -98,9 +98,13 @@ Map<T> *Map<T>::open(Storage *storage, uint32_t storage_node_id) { throw LogicError(); } StorageNode storage_node = storage->open_node(storage_node_id); - const map::Header * const header = - static_cast<const map::Header *>(storage_node.body()); - switch (header->type) { + const map::CommonHeader * const common_header = + static_cast<const map::CommonHeader *>(storage_node.body()); + if (!*common_header) { + GRNXX_ERROR() << "wrong header: format = " << common_header->format(); + throw LogicError(); + } + switch (common_header->type()) { case MAP_ARRAY: { return map::ArrayMap<T>::open(storage, storage_node_id); } @@ -114,7 +118,7 @@ Map<T> *Map<T>::open(Storage *storage, uint32_t storage_node_id) { return map::HashTable<T>::open(storage, storage_node_id); } default: { - GRNXX_ERROR() << "invalid format: type = " << header->type; + GRNXX_ERROR() << "invalid format: type = " << common_header->type(); throw LogicError(); } } Modified: lib/grnxx/map/array_map.cpp (+30 -1) =================================================================== --- lib/grnxx/map/array_map.cpp 2013-07-23 13:23:05 +0900 (c4baf94) +++ lib/grnxx/map/array_map.cpp 2013-07-23 13:24:35 +0900 (46cba1f) @@ -23,12 +23,36 @@ #include "grnxx/exception.hpp" #include "grnxx/geo_point.hpp" #include "grnxx/logger.hpp" -#include "grnxx/map/array_map/header.hpp" +#include "grnxx/map/common_header.hpp" #include "grnxx/map/helper.hpp" #include "grnxx/storage.hpp" namespace grnxx { namespace map { +namespace { + +constexpr char FORMAT_STRING[] = "grnxx::map::ArrayMap"; + +} // namespace + +struct ArrayMapHeader { + CommonHeader common_header; + uint32_t keys_storage_node_id; + + // Initialize the member variables. + ArrayMapHeader(); + + // Return true iff the header seems to be correct. + explicit operator bool() const; +}; + +ArrayMapHeader::ArrayMapHeader() + : common_header(FORMAT_STRING, MAP_ARRAY), + keys_storage_node_id(STORAGE_INVALID_NODE_ID) {} + +ArrayMapHeader::operator bool() const { + return common_header.format() == FORMAT_STRING; +} template <typename T> ArrayMap<T>::ArrayMap() @@ -242,6 +266,11 @@ void ArrayMap<T>::open_map(Storage *storage, uint32_t storage_node_id) { } storage_node_id_ = storage_node_id; header_ = static_cast<Header *>(storage_node.body()); + if (!*header_) { + GRNXX_ERROR() << "wrong format: expected = " << FORMAT_STRING + << ", actual = " << header_->common_header.format(); + throw LogicError(); + } keys_.reset(KeyStore<T>::open(storage, header_->keys_storage_node_id)); } Modified: lib/grnxx/map/array_map.hpp (+2 -5) =================================================================== --- lib/grnxx/map/array_map.hpp 2013-07-23 13:23:05 +0900 (a37e2c3) +++ lib/grnxx/map/array_map.hpp 2013-07-23 13:24:35 +0900 (e924c0a) @@ -31,15 +31,12 @@ namespace grnxx { class Storage; namespace map { -namespace array_map { -struct Header; - -} // namespace array_map +struct ArrayMapHeader; template <typename T> class ArrayMap : public Map<T> { - using Header = array_map::Header; + using Header = ArrayMapHeader; public: using Key = typename Map<T>::Key; Modified: lib/grnxx/map/double_array.cpp (+53 -0) =================================================================== --- lib/grnxx/map/double_array.cpp 2013-07-23 13:23:05 +0900 (2ec87ec) +++ lib/grnxx/map/double_array.cpp 2013-07-23 13:24:35 +0900 (5a6b0e6) @@ -25,6 +25,7 @@ #include "grnxx/intrinsic.hpp" #include "grnxx/logger.hpp" #include "grnxx/map/bytes_pool.hpp" +#include "grnxx/map/common_header.hpp" #include "grnxx/map/double_array/block.hpp" #include "grnxx/map/double_array/entry.hpp" #include "grnxx/map/double_array/header.hpp" @@ -36,6 +37,8 @@ namespace grnxx { namespace map { namespace { +constexpr char FORMAT_STRING[] = "grnxx::map::DoubleArray"; + using double_array::BLOCK_MAX_FAILURE_COUNT; using double_array::BLOCK_MAX_LEVEL; using double_array::BLOCK_INVALID_ID; @@ -51,6 +54,51 @@ constexpr uint64_t ROOT_NODE_ID = 0; } // namespace +struct DoubleArrayHeader { + CommonHeader common_header; + int64_t max_key_id; + uint64_t num_keys; + uint32_t nodes_storage_node_id; + uint32_t siblings_storage_node_id; + uint32_t blocks_storage_node_id; + uint32_t entries_storage_node_id; + uint32_t pool_storage_node_id; + uint64_t next_key_id; + uint64_t num_blocks; + uint64_t num_phantoms; + uint64_t num_zombies; + uint64_t latest_blocks[BLOCK_MAX_LEVEL + 1]; + + // Initialize the member variables. + DoubleArrayHeader(); + + // Return true iff the header seems to be correct. + explicit operator bool() const; +}; + +DoubleArrayHeader::DoubleArrayHeader() + : common_header(FORMAT_STRING, MAP_DOUBLE_ARRAY), + max_key_id(MAP_MIN_KEY_ID - 1), + num_keys(0), + nodes_storage_node_id(STORAGE_INVALID_NODE_ID), + siblings_storage_node_id(STORAGE_INVALID_NODE_ID), + blocks_storage_node_id(STORAGE_INVALID_NODE_ID), + entries_storage_node_id(STORAGE_INVALID_NODE_ID), + pool_storage_node_id(STORAGE_INVALID_NODE_ID), + next_key_id(MAP_MIN_KEY_ID), + num_blocks(0), + num_phantoms(0), + num_zombies(0), + latest_blocks() { + for (uint64_t i = 0; i <= BLOCK_MAX_LEVEL; ++i) { + latest_blocks[i] = BLOCK_INVALID_ID; + } +} + +DoubleArrayHeader::operator bool() const { + return common_header.format() == FORMAT_STRING; +} + template <typename T> Map<T> *DoubleArray<T>::create(Storage *, uint32_t, const MapOptions &) { GRNXX_ERROR() << "invalid combination"; @@ -463,6 +511,11 @@ void DoubleArray<Bytes>::open_map(Storage *storage, uint32_t storage_node_id) { throw LogicError(); } header_ = static_cast<Header *>(storage_node.body()); + if (!*header_) { + GRNXX_ERROR() << "wrong format: expected = " << FORMAT_STRING + << ", actual = " << header_->common_header.format(); + throw LogicError(); + } nodes_.reset(NodeArray::open(storage, header_->nodes_storage_node_id)); siblings_.reset( SiblingArray::open(storage, header_->siblings_storage_node_id)); Modified: lib/grnxx/map/double_array.hpp (+3 -7) =================================================================== --- lib/grnxx/map/double_array.hpp 2013-07-23 13:23:05 +0900 (ba9803c) +++ lib/grnxx/map/double_array.hpp 2013-07-23 13:24:35 +0900 (28f28bb) @@ -40,12 +40,6 @@ namespace map { class BytesPool; -namespace double_array { - -struct Header; - -} // namespace double_array - enum DoubleArrayResult { DOUBLE_ARRAY_FOUND, DOUBLE_ARRAY_NOT_FOUND, @@ -53,6 +47,8 @@ enum DoubleArrayResult { DOUBLE_ARRAY_FAILED }; +struct DoubleArrayHeader; + template <typename T> class DoubleArray { public: @@ -63,6 +59,7 @@ class DoubleArray { template <> class DoubleArray<Bytes> : public Map<Bytes> { + using Header = DoubleArrayHeader; using Node = double_array::Node; using Block = double_array::Block; using Entry = double_array::Entry; @@ -78,7 +75,6 @@ class DoubleArray<Bytes> : public Map<Bytes> { static constexpr uint64_t ENTRY_ARRAY_SIZE = 1ULL << 40; public: - using Header = double_array::Header; using Key = typename Map<Bytes>::Key; using KeyArg = typename Map<Bytes>::KeyArg; using Cursor = typename Map<Bytes>::Cursor; Modified: lib/grnxx/map/hash_table.cpp (+35 -1) =================================================================== --- lib/grnxx/map/hash_table.cpp 2013-07-23 13:23:05 +0900 (e72b030) +++ lib/grnxx/map/hash_table.cpp 2013-07-23 13:24:35 +0900 (d3504c9) @@ -25,8 +25,8 @@ #include "grnxx/intrinsic.hpp" #include "grnxx/lock.hpp" #include "grnxx/logger.hpp" +#include "grnxx/map/common_header.hpp" #include "grnxx/map/hash_table/hash.hpp" -#include "grnxx/map/hash_table/header.hpp" #include "grnxx/map/helper.hpp" #include "grnxx/mutex.hpp" #include "grnxx/storage.hpp" @@ -35,6 +35,8 @@ namespace grnxx { namespace map { namespace { +constexpr char FORMAT_STRING[] = "grnxx::map::HashTable"; + constexpr int64_t TABLE_ENTRY_UNUSED = -1; constexpr int64_t TABLE_ENTRY_REMOVED = -2; @@ -45,6 +47,33 @@ using Hash = hash_table::Hash<T>; } // namespace +struct HashTableHeader { + CommonHeader common_header; + uint32_t key_ids_storage_node_id; + uint32_t old_key_ids_storage_node_id; + uint32_t keys_storage_node_id; + uint64_t num_key_ids; + Mutex mutex; + + // Initialize the member variables. + HashTableHeader(); + + // Return true iff the header seems to be correct. + explicit operator bool() const; +}; + +HashTableHeader::HashTableHeader() + : common_header(FORMAT_STRING, MAP_HASH_TABLE), + key_ids_storage_node_id(STORAGE_INVALID_NODE_ID), + old_key_ids_storage_node_id(STORAGE_INVALID_NODE_ID), + keys_storage_node_id(STORAGE_INVALID_NODE_ID), + num_key_ids(0), + mutex() {} + +HashTableHeader::operator bool() const { + return common_header.format() == FORMAT_STRING; +} + template <typename T> HashTable<T>::HashTable() : storage_(nullptr), @@ -311,6 +340,11 @@ void HashTable<T>::open_map(Storage *storage, uint32_t storage_node_id) { } storage_node_id_ = storage_node_id; header_ = static_cast<Header *>(storage_node.body()); + if (!*header_) { + GRNXX_ERROR() << "wrong format: expected = " << FORMAT_STRING + << ", actual = " << header_->common_header.format(); + throw LogicError(); + } key_ids_.reset(KeyIDArray::open(storage, header_->key_ids_storage_node_id)); keys_.reset(KeyStore<T>::open(storage, header_->keys_storage_node_id)); } Modified: lib/grnxx/map/hash_table.hpp (+2 -5) =================================================================== --- lib/grnxx/map/hash_table.hpp 2013-07-23 13:23:05 +0900 (927b3a4) +++ lib/grnxx/map/hash_table.hpp 2013-07-23 13:24:35 +0900 (cefba41) @@ -31,15 +31,12 @@ namespace grnxx { class Storage; namespace map { -namespace hash_table { -struct Header; - -} // namespace hash_table +struct HashTableHeader; template <typename T> class HashTable : public Map<T> { - using Header = hash_table::Header; + using Header = HashTableHeader; using KeyIDArray = Array<int64_t>; public: Modified: lib/grnxx/map/patricia.cpp (+39 -2) =================================================================== --- lib/grnxx/map/patricia.cpp 2013-07-23 13:23:05 +0900 (ebb77f6) +++ lib/grnxx/map/patricia.cpp 2013-07-23 13:24:35 +0900 (9d911df) @@ -23,6 +23,7 @@ #include "grnxx/exception.hpp" #include "grnxx/geo_point.hpp" #include "grnxx/logger.hpp" +#include "grnxx/map/common_header.hpp" #include "grnxx/map/hash_table/hash.hpp" #include "grnxx/map/helper.hpp" #include "grnxx/map/patricia/header.hpp" @@ -32,6 +33,8 @@ namespace grnxx { namespace map { namespace { +constexpr char FORMAT_STRING[] = "grnxx::map::Patricia"; + constexpr uint64_t ROOT_NODE_ID = 0; using patricia::NODE_INVALID_OFFSET; @@ -43,6 +46,32 @@ using patricia::NODE_TERMINAL; } // namespace +struct PatriciaHeader { + CommonHeader common_header; + MapType map_type; + uint64_t next_node_id; + uint32_t nodes_storage_node_id; + uint32_t keys_storage_node_id; + uint32_t cache_storage_node_id; + + // Initialize the member variables. + PatriciaHeader(); + + // Return true iff the header seems to be correct. + explicit operator bool() const; +}; + +PatriciaHeader::PatriciaHeader() + : common_header(FORMAT_STRING, MAP_PATRICIA), + next_node_id(2), + nodes_storage_node_id(STORAGE_INVALID_NODE_ID), + keys_storage_node_id(STORAGE_INVALID_NODE_ID), + cache_storage_node_id(STORAGE_INVALID_NODE_ID) {} + +PatriciaHeader::operator bool() const { + return common_header.format() == FORMAT_STRING; +} + template <typename T> Patricia<T>::Patricia() : storage_(nullptr), @@ -479,7 +508,11 @@ void Patricia<T>::open_map(Storage *storage, uint32_t storage_node_id) { } storage_node_id_ = storage_node_id; header_ = static_cast<Header *>(storage_node.body()); - // TODO: Check the format. + if (!*header_) { + GRNXX_ERROR() << "wrong format: expected = " << FORMAT_STRING + << ", actual = " << header_->common_header.format(); + throw LogicError(); + } nodes_.reset(NodeArray::open(storage, header_->nodes_storage_node_id)); keys_.reset(KeyStore<T>::open(storage, header_->keys_storage_node_id)); } @@ -1350,7 +1383,11 @@ void Patricia<Bytes>::open_map(Storage *storage, uint32_t storage_node_id) { } storage_node_id_ = storage_node_id; header_ = static_cast<Header *>(storage_node.body()); - // TODO: Check the format. + if (!*header_) { + GRNXX_ERROR() << "wrong format: expected = " << FORMAT_STRING + << ", actual = " << header_->common_header.format(); + throw LogicError(); + } nodes_.reset(NodeArray::open(storage, header_->nodes_storage_node_id)); keys_.reset(KeyStore<Bytes>::open(storage, header_->keys_storage_node_id)); cache_.reset(Cache::open(storage, header_->cache_storage_node_id)); Modified: lib/grnxx/map/patricia.hpp (+3 -6) =================================================================== --- lib/grnxx/map/patricia.hpp 2013-07-23 13:23:05 +0900 (c91e88b) +++ lib/grnxx/map/patricia.hpp 2013-07-23 13:24:35 +0900 (d0b3e9a) @@ -34,19 +34,16 @@ namespace grnxx { class Storage; namespace map { -namespace patricia { -struct Header; - -} // namespace patricia +struct PatriciaHeader; template <typename T> class Patricia : public Map<T> { + using Header = PatriciaHeader; using Node = patricia::Node; using NodeArray = Array<Node, 65536, 8192>; public: - using Header = patricia::Header; using Key = typename Map<T>::Key; using KeyArg = typename Map<T>::KeyArg; using Cursor = typename Map<T>::Cursor; @@ -92,12 +89,12 @@ class Patricia : public Map<T> { template <> class Patricia<Bytes> : public Map<Bytes> { + using Header = PatriciaHeader; using Node = patricia::Node; using NodeArray = Array<Node, 65536, 8192>; using Cache = Array<int64_t>; public: - using Header = patricia::Header; using Key = typename Map<Bytes>::Key; using KeyArg = typename Map<Bytes>::KeyArg; using Cursor = typename Map<Bytes>::Cursor; -------------- next part -------------- HTML����������������������������...Download