[Groonga-commit] groonga/grnxx at b090482 [master] Use grnxx::CommonHeader.

Back to archive index

susumu.yata null+****@clear*****
Wed Jul 24 10:37:29 JST 2013


susumu.yata	2013-07-24 10:37:29 +0900 (Wed, 24 Jul 2013)

  New Revision: b090482271b150167a434f11420a6ab0d140b48a
  https://github.com/groonga/grnxx/commit/b090482271b150167a434f11420a6ab0d140b48a

  Message:
    Use grnxx::CommonHeader.

  Modified files:
    lib/grnxx/map/bytes_pool.cpp

  Modified: lib/grnxx/map/bytes_pool.cpp (+19 -1)
===================================================================
--- lib/grnxx/map/bytes_pool.cpp    2013-07-23 14:42:15 +0900 (d00b89c)
+++ lib/grnxx/map/bytes_pool.cpp    2013-07-24 10:37:29 +0900 (ce0e546)
@@ -20,6 +20,7 @@
 #include <cstring>
 #include <new>
 
+#include "grnxx/common_header.hpp"
 #include "grnxx/exception.hpp"
 #include "grnxx/logger.hpp"
 #include "grnxx/storage.hpp"
@@ -29,6 +30,8 @@ namespace grnxx {
 namespace map {
 namespace {
 
+constexpr char FORMAT_STRING[] = "grnxx::map::BytesPool";
+
 constexpr uint64_t POOL_SIZE       = 1ULL << 48;
 
 constexpr uint32_t MAX_PAGE_ID     = (POOL_SIZE / BytesPool::page_size()) - 1;
@@ -37,6 +40,7 @@ constexpr uint32_t INVALID_PAGE_ID = MAX_PAGE_ID + 1;
 }  // namespace
 
 struct BytesPoolHeader {
+  grnxx::CommonHeader common_header;
   uint64_t next_offset;
   uint32_t max_page_id;
   uint32_t latest_empty_page_id;
@@ -45,11 +49,16 @@ struct BytesPoolHeader {
   uint32_t page_headers_storage_node_id;
   uint32_t reserved;
 
+  // Initialize the member variables.
   BytesPoolHeader();
+
+  // Return true iff the header seems to be correct.
+  explicit operator bool() const;
 };
 
 BytesPoolHeader::BytesPoolHeader()
-    : next_offset(0),
+    : common_header(FORMAT_STRING),
+      next_offset(0),
       max_page_id(0),
       latest_empty_page_id(INVALID_PAGE_ID),
       latest_idle_page_id(INVALID_PAGE_ID),
@@ -57,6 +66,10 @@ BytesPoolHeader::BytesPoolHeader()
       page_headers_storage_node_id(STORAGE_INVALID_NODE_ID),
       reserved(0) {}
 
+BytesPoolHeader::operator bool() const {
+  return common_header.format() == FORMAT_STRING;
+}
+
 StringBuilder &operator<<(StringBuilder &builder, BytesPoolPageStatus status) {
   switch (status) {
     case BYTES_POOL_PAGE_ACTIVE: {
@@ -262,6 +275,11 @@ void BytesPool::open_pool(Storage *storage, uint32_t storage_node_id) {
   StorageNode storage_node = storage->open_node(storage_node_id);
   storage_node_id_ = storage_node.id();
   header_ = static_cast<BytesPoolHeader *>(storage_node.body());
+  if (!*header_) {
+    GRNXX_ERROR() << "wrong format: expected = " << FORMAT_STRING
+                  << ", actual = " << header_->common_header.format();
+    throw LogicError();
+  }
   pool_.reset(Pool::open(storage, header_->pool_storage_node_id));
   page_headers_.reset(
       PageHeaderArray::open(storage, header_->page_headers_storage_node_id));
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index