[Groonga-commit] groonga/grnxx at 483b109 [master] Update grnxx::Array to use grnxx::Exception.

Back to archive index

susumu.yata null+****@clear*****
Wed Jul 3 17:38:48 JST 2013


susumu.yata	2013-07-03 17:38:48 +0900 (Wed, 03 Jul 2013)

  New Revision: 483b109922b8a4184e7b6c4bba09258674e6c788
  https://github.com/groonga/grnxx/commit/483b109922b8a4184e7b6c4bba09258674e6c788

  Message:
    Update grnxx::Array to use grnxx::Exception.

  Modified files:
    lib/grnxx/array.hpp
    lib/grnxx/array_impl.cpp
    lib/grnxx/array_impl.hpp
    lib/grnxx/map/hash_table/key_id_array.hpp
    test/test_array.cpp

  Modified: lib/grnxx/array.hpp (+7 -25)
===================================================================
--- lib/grnxx/array.hpp    2013-07-03 17:03:53 +0900 (d80cd9f)
+++ lib/grnxx/array.hpp    2013-07-03 17:38:48 +0900 (45168d5)
@@ -62,9 +62,7 @@ class Array {
   // Create an array.
   static Array *create(Storage *storage, uint32_t storage_node_id) {
     std::unique_ptr<Array> array(create_instance());
-    if (!array->impl_.create(storage, storage_node_id)) {
-      return nullptr;
-    }
+    array->impl_.create(storage, storage_node_id);
     return array.release();
   }
 
@@ -72,18 +70,14 @@ class Array {
   static Array *create(Storage *storage, uint32_t storage_node_id,
                        ValueArg default_value) {
     std::unique_ptr<Array> array(create_instance());
-    if (!array->impl_.create(storage, storage_node_id, default_value)) {
-      return nullptr;
-    }
+    array->impl_.create(storage, storage_node_id, default_value);
     return array.release();
   }
 
   // Open an array.
   static Array *open(Storage *storage, uint32_t storage_node_id) {
     std::unique_ptr<Array> array(create_instance());
-    if (!array->impl_.open(storage, storage_node_id)) {
-      return nullptr;
-    }
+    array->impl_.open(storage, storage_node_id);
     return array.release();
   }
 
@@ -173,9 +167,7 @@ class Array<bool, PAGE_SIZE_IN_BITS, TABLE_SIZE, SECONDARY_TABLE_SIZE> {
   // Create an array.
   static Array *create(Storage *storage, uint32_t storage_node_id) {
     std::unique_ptr<Array> array(create_instance());
-    if (!array->units_.create(storage, storage_node_id)) {
-      return nullptr;
-    }
+    array->units_.create(storage, storage_node_id);
     return array.release();
   }
 
@@ -183,19 +175,15 @@ class Array<bool, PAGE_SIZE_IN_BITS, TABLE_SIZE, SECONDARY_TABLE_SIZE> {
   static Array *create(Storage *storage, uint32_t storage_node_id,
                        ValueArg default_value) {
     std::unique_ptr<Array> array(create_instance());
-    if (!array->units_.create(storage, storage_node_id,
-                              default_value ? ~Unit(0) : Unit(0))) {
-      return nullptr;
-    }
+    array->units_.create(storage, storage_node_id,
+                         default_value ? ~Unit(0) : Unit(0));
     return array.release();
   }
 
   // Open an array.
   static Array *open(Storage *storage, uint32_t storage_node_id) {
     std::unique_ptr<Array> array(create_instance());
-    if (!array->units_.open(storage, storage_node_id)) {
-      return nullptr;
-    }
+    array->units_.open(storage, storage_node_id);
     return array.release();
   }
 
@@ -235,9 +223,6 @@ class Array<bool, PAGE_SIZE_IN_BITS, TABLE_SIZE, SECONDARY_TABLE_SIZE> {
   bool get(uint64_t value_id, Value *value) {
     const uint64_t unit_id = value_id / UNIT_SIZE;
     const Unit * const page = get_page(unit_id / PAGE_SIZE);
-    if (!page) {
-      return false;
-    }
     if (value) {
       *value = (page[unit_id % PAGE_SIZE] &
                 (Unit(1) << (value_id % UNIT_SIZE))) != 0;
@@ -251,9 +236,6 @@ class Array<bool, PAGE_SIZE_IN_BITS, TABLE_SIZE, SECONDARY_TABLE_SIZE> {
   bool set(uint64_t value_id, ValueArg value) {
     const uint64_t unit_id = value_id / UNIT_SIZE;
     Unit * const page = get_page(unit_id / PAGE_SIZE);
-    if (!page) {
-      return false;
-    }
     if (value) {
       page[unit_id % PAGE_SIZE] |= Unit(1) << (value_id % UNIT_SIZE);
     } else {

  Modified: lib/grnxx/array_impl.cpp (+108 -171)
===================================================================
--- lib/grnxx/array_impl.cpp    2013-07-03 17:03:53 +0900 (07bacb8)
+++ lib/grnxx/array_impl.cpp    2013-07-03 17:38:48 +0900 (862a119)
@@ -17,9 +17,9 @@
 */
 #include "grnxx/array_impl.hpp"
 
-#include <cstring>
 #include <new>
 
+#include "grnxx/exception.hpp"
 #include "grnxx/lock.hpp"
 #include "grnxx/logger.hpp"
 #include "grnxx/storage.hpp"
@@ -64,79 +64,67 @@ Array1D::Array1D()
 
 Array1D::~Array1D() {}
 
-bool Array1D::create(Storage *storage, uint32_t storage_node_id,
+void Array1D::create(Storage *storage, uint32_t storage_node_id,
                      uint64_t value_size, uint64_t page_size,
                      const void *default_value, FillPage fill_page) {
   if (!storage) {
     GRNXX_ERROR() << "invalid argument: storage = nullptr";
-    return false;
+    throw LogicError();
   }
   StorageNode storage_node =
       storage->create_node(storage_node_id, sizeof(ArrayHeader));
-  if (!storage_node) {
-    return false;
-  }
   storage_node_id_ = storage_node.id();
-  header_ = static_cast<ArrayHeader *>(storage_node.body());
-  *header_ = ArrayHeader();
-  header_->value_size = value_size;
-  header_->page_size = page_size;
-  StorageNode page_node =
-      storage->create_node(storage_node_id_, value_size * page_size);
-  if (!page_node) {
+  try {
+    header_ = static_cast<ArrayHeader *>(storage_node.body());
+    *header_ = ArrayHeader();
+    header_->value_size = value_size;
+    header_->page_size = page_size;
+    StorageNode page_node =
+        storage->create_node(storage_node_id_, value_size * page_size);
+    header_->page_storage_node_id = page_node.id();
+    page_ = page_node.body();
+    if (default_value) {
+      header_->has_default_value = true;
+      fill_page(page_, default_value);
+    }
+  } catch (...) {
     storage->unlink_node(storage_node_id_);
-    return false;
+    throw;
   }
-  header_->page_storage_node_id = page_node.id();
-  page_ = page_node.body();
-  if (default_value) {
-    header_->has_default_value = true;
-    fill_page(page_, default_value);
-  }
-  return true;
 }
 
-bool Array1D::open(Storage *storage, uint32_t storage_node_id,
+void Array1D::open(Storage *storage, uint32_t storage_node_id,
                    uint64_t value_size, uint64_t page_size) {
   if (!storage) {
     GRNXX_ERROR() << "invalid argument: storage = nullptr";
-    return false;
+    throw LogicError();
   }
   StorageNode storage_node = storage->open_node(storage_node_id);
-  if (!storage_node) {
-    return false;
-  }
   if (storage_node.size() < sizeof(ArrayHeader)) {
     GRNXX_ERROR() << "invalid format: node_size = " << storage_node.size()
                   << ", header_size = " << sizeof(ArrayHeader);
-    return false;
+    throw LogicError();
   }
   storage_node_id_ = storage_node.id();
   header_ = static_cast<ArrayHeader *>(storage_node.body());
   if (header_->value_size != value_size) {
     GRNXX_ERROR() << "parameter conflict: value_size = " << value_size
                   << ", stored_value_size = " << header_->value_size;
-    return false;
+    throw LogicError();
   }
   if (header_->page_size != page_size) {
     GRNXX_ERROR() << "parameter conflict: page_size = " << page_size
                   << ", stored_page_size = " << header_->page_size;
-    return false;
+    throw LogicError();
   }
   StorageNode page_node = storage->open_node(header_->page_storage_node_id);
-  if (!page_node) {
-    return false;
-  }
   page_ = page_node.body();
-  return true;
 }
 
 bool Array1D::unlink(Storage *storage, uint32_t storage_node_id,
                      uint64_t value_size, uint64_t page_size) {
   Array1D array;
-  if (!array.open(storage, storage_node_id, value_size, page_size)) {
-    return false;
-  }
+  array.open(storage, storage_node_id, value_size, page_size);
   return storage->unlink_node(storage_node_id);
 }
 
@@ -152,13 +140,13 @@ Array2D::Array2D()
 
 Array2D::~Array2D() {}
 
-bool Array2D::create(Storage *storage, uint32_t storage_node_id,
+void Array2D::create(Storage *storage, uint32_t storage_node_id,
                      uint64_t value_size, uint64_t page_size,
                      uint64_t table_size,
                      const void *default_value, FillPage fill_page) {
   if (!storage) {
     GRNXX_ERROR() << "invalid argument: storage = nullptr";
-    return nullptr;
+    throw LogicError();
   }
   storage_ = storage;
   uint64_t storage_node_size = sizeof(ArrayHeader);
@@ -167,108 +155,95 @@ bool Array2D::create(Storage *storage, uint32_t storage_node_id,
   }
   StorageNode storage_node =
       storage->create_node(storage_node_id, storage_node_size);
-  if (!storage_node) {
-    return false;
-  }
   storage_node_id_ = storage_node.id();
-  header_ = static_cast<ArrayHeader *>(storage_node.body());
-  *header_ = ArrayHeader();
-  header_->value_size = value_size;
-  header_->page_size = page_size;
-  header_->table_size = table_size;
-  if (default_value) {
-    header_->has_default_value = true;
-    default_value_ = header_ + 1;
-    std::memcpy(default_value_, default_value, value_size);
-    fill_page_ = fill_page;
-  }
-  StorageNode table_node =
-      storage->create_node(storage_node_id_, sizeof(uint32_t) * table_size);
-  if (!table_node) {
-    storage->unlink_node(storage_node_id_);
-    return false;
-  }
-  header_->table_storage_node_id = table_node.id();
-  table_ = static_cast<uint32_t *>(table_node.body());
-  for (uint64_t i = 0; i < table_size; ++i) {
-    table_[i] = STORAGE_INVALID_NODE_ID;
-  }
-  table_cache_.reset(new (std::nothrow) void *[table_size]);
-  if (!table_cache_) {
-    GRNXX_ERROR() << "new void *[] failed: size = " << table_size;
+  try {
+    header_ = static_cast<ArrayHeader *>(storage_node.body());
+    *header_ = ArrayHeader();
+    header_->value_size = value_size;
+    header_->page_size = page_size;
+    header_->table_size = table_size;
+    if (default_value) {
+      header_->has_default_value = true;
+      default_value_ = header_ + 1;
+      std::memcpy(default_value_, default_value, value_size);
+      fill_page_ = fill_page;
+    }
+    StorageNode table_node =
+        storage->create_node(storage_node_id_, sizeof(uint32_t) * table_size);
+    header_->table_storage_node_id = table_node.id();
+    table_ = static_cast<uint32_t *>(table_node.body());
+    for (uint64_t i = 0; i < table_size; ++i) {
+      table_[i] = STORAGE_INVALID_NODE_ID;
+    }
+    table_cache_.reset(new (std::nothrow) void *[table_size]);
+    if (!table_cache_) {
+      GRNXX_ERROR() << "new void *[] failed: size = " << table_size;
+      throw MemoryError();
+    }
+    for (uint64_t i = 0; i < table_size; ++i) {
+      table_cache_[i] = nullptr;
+    }
+  } catch (...) {
     storage->unlink_node(storage_node_id_);
-    return false;
-  }
-  for (uint64_t i = 0; i < table_size; ++i) {
-    table_cache_[i] = nullptr;
+    throw;
   }
-  return true;
 }
 
-bool Array2D::open(Storage *storage, uint32_t storage_node_id,
+void Array2D::open(Storage *storage, uint32_t storage_node_id,
                    uint64_t value_size, uint64_t page_size,
                    uint64_t table_size, FillPage fill_page) {
   if (!storage) {
     GRNXX_ERROR() << "invalid argument: storage = nullptr";
-    return false;
+    throw LogicError();
   }
   storage_ = storage;
   StorageNode storage_node = storage->open_node(storage_node_id);
-  if (!storage_node) {
-    return false;
-  }
   if (storage_node.size() < sizeof(ArrayHeader)) {
     GRNXX_ERROR() << "invalid format: node_size = " << storage_node.size()
                   << ", header_size = " << sizeof(ArrayHeader);
-    return false;
+    throw LogicError();
   }
   storage_node_id_ = storage_node.id();
   header_ = static_cast<ArrayHeader *>(storage_node.body());
   if (header_->value_size != value_size) {
     GRNXX_ERROR() << "parameter conflict: value_size = " << value_size
                   << ", stored_value_size = " << header_->value_size;
-    return false;
+    throw LogicError();
   }
   if (header_->page_size != page_size) {
     GRNXX_ERROR() << "parameter conflict: page_size = " << page_size
                   << ", stored_page_size = " << header_->page_size;
-    return false;
+    throw LogicError();
   }
   if (header_->table_size != table_size) {
     GRNXX_ERROR() << "parameter conflict: table_size = " << table_size
                   << ", stored_table_size = " << header_->table_size;
-    return false;
+    throw LogicError();
   }
   default_value_ = header_ + 1;
   fill_page_ = fill_page;
   StorageNode table_node = storage->open_node(header_->table_storage_node_id);
-  if (!table_node) {
-    return false;
-  }
   table_ = static_cast<uint32_t *>(table_node.body());
   table_cache_.reset(new (std::nothrow) void *[table_size]);
   if (!table_cache_) {
     GRNXX_ERROR() << "new void *[] failed: size = " << table_size;
-    return false;
+    throw MemoryError();
   }
   for (uint64_t i = 0; i < table_size; ++i) {
     table_cache_[i] = nullptr;
   }
-  return true;
 }
 
 bool Array2D::unlink(Storage *storage, uint32_t storage_node_id,
                      uint64_t value_size, uint64_t page_size,
                      uint64_t table_size) {
   Array2D array;
-  if (!array.open(storage, storage_node_id,
-                  value_size, page_size, table_size, nullptr)) {
-    return false;
-  }
+  array.open(storage, storage_node_id,
+             value_size, page_size, table_size, nullptr);
   return storage->unlink_node(storage_node_id);
 }
 
-bool Array2D::initialize_page(uint64_t page_id) {
+void Array2D::initialize_page(uint64_t page_id) {
   Lock inter_thread_lock(&mutex_);
   if (!table_cache_[page_id]) {
     StorageNode page_node;
@@ -278,24 +253,17 @@ bool Array2D::initialize_page(uint64_t page_id) {
         page_node =
             storage_->create_node(header_->table_storage_node_id,
                                   header_->value_size * header_->page_size);
-        if (!page_node) {
-          return false;
-        }
         if (default_value_) {
           fill_page_(page_node.body(), default_value_);
         }
         table_[page_id] = page_node.id();
         table_cache_[page_id] = page_node.body();
-        return true;
+        return;
       }
     }
     page_node = storage_->open_node(table_[page_id]);
-    if (!page_node) {
-      return false;
-    }
     table_cache_[page_id] = page_node.body();
   }
-  return true;
 }
 
 Array3D::Array3D()
@@ -312,13 +280,13 @@ Array3D::Array3D()
 
 Array3D::~Array3D() {}
 
-bool Array3D::create(Storage *storage, uint32_t storage_node_id,
+void Array3D::create(Storage *storage, uint32_t storage_node_id,
                      uint64_t value_size, uint64_t page_size,
                      uint64_t table_size, uint64_t secondary_table_size,
                      const void *default_value, FillPage fill_page) {
   if (!storage) {
     GRNXX_ERROR() << "invalid argument: storage = nullptr";
-    return nullptr;
+    throw LogicError();
   }
   storage_ = storage;
   uint64_t storage_node_size = sizeof(ArrayHeader);
@@ -327,74 +295,71 @@ bool Array3D::create(Storage *storage, uint32_t storage_node_id,
   }
   StorageNode storage_node =
       storage->create_node(storage_node_id, storage_node_size);
-  if (!storage_node) {
-    return false;
-  }
   storage_node_id_ = storage_node.id();
-  header_ = static_cast<ArrayHeader *>(storage_node.body());
-  *header_ = ArrayHeader();
-  header_->value_size = value_size;
-  header_->page_size = page_size;
-  header_->table_size = table_size;
-  header_->secondary_table_size = secondary_table_size;
-  if (default_value) {
-    header_->has_default_value = true;
-    default_value_ = header_ + 1;
-    std::memcpy(default_value_, default_value, value_size);
-    fill_page_ = fill_page;
-  }
-  table_caches_.reset(
-      new (std::nothrow) std::unique_ptr<void *[]>[secondary_table_size]);
-  if (!table_caches_) {
-    GRNXX_ERROR() << "new std::unique_ptr<void *[]>[] failed: size = "
-                  << secondary_table_size;
+  try {
+    header_ = static_cast<ArrayHeader *>(storage_node.body());
+    *header_ = ArrayHeader();
+    header_->value_size = value_size;
+    header_->page_size = page_size;
+    header_->table_size = table_size;
+    header_->secondary_table_size = secondary_table_size;
+    if (default_value) {
+      header_->has_default_value = true;
+      default_value_ = header_ + 1;
+      std::memcpy(default_value_, default_value, value_size);
+      fill_page_ = fill_page;
+    }
+    table_caches_.reset(
+        new (std::nothrow) std::unique_ptr<void *[]>[secondary_table_size]);
+    if (!table_caches_) {
+      GRNXX_ERROR() << "new std::unique_ptr<void *[]>[] failed: size = "
+                    << secondary_table_size;
+      throw MemoryError();
+    }
+  } catch (...) {
     storage->unlink_node(storage_node_id_);
-    return false;
+    throw;
   }
-  return true;
 }
 
-bool Array3D::open(Storage *storage, uint32_t storage_node_id,
+void Array3D::open(Storage *storage, uint32_t storage_node_id,
                    uint64_t value_size, uint64_t page_size,
                    uint64_t table_size, uint64_t secondary_table_size,
                    FillPage fill_page) {
   if (!storage) {
     GRNXX_ERROR() << "invalid argument: storage = nullptr";
-    return nullptr;
+    throw LogicError();
   }
   storage_ = storage;
   StorageNode storage_node = storage->open_node(storage_node_id);
-  if (!storage_node) {
-    return false;
-  }
   if (storage_node.size() < sizeof(ArrayHeader)) {
     GRNXX_ERROR() << "invalid format: node_size = " << storage_node.size()
                   << ", header_size = " << sizeof(ArrayHeader);
-    return false;
+    throw LogicError();
   }
   storage_node_id_ = storage_node.id();
   header_ = static_cast<ArrayHeader *>(storage_node.body());
   if (header_->value_size != value_size) {
     GRNXX_ERROR() << "parameter conflict: value_size = " << value_size
                   << ", stored_value_size = " << header_->value_size;
-    return false;
+    throw LogicError();
   }
   if (header_->page_size != page_size) {
     GRNXX_ERROR() << "parameter conflict: page_size = " << page_size
                   << ", stored_page_size = " << header_->page_size;
-    return false;
+    throw LogicError();
   }
   if (header_->table_size != table_size) {
     GRNXX_ERROR() << "parameter conflict: table_size = " << table_size
                   << ", stored_table_size = " << header_->table_size;
-    return false;
+    throw LogicError();
   }
   if (header_->table_size != table_size) {
     GRNXX_ERROR() << "parameter conflict: "
                   << "secondary_table_size = " << secondary_table_size
                   << ", stored_secondary_table_size = "
                   << header_->secondary_table_size;
-    return false;
+    throw LogicError();
   }
   default_value_ = header_ + 1;
   fill_page_ = fill_page;
@@ -403,34 +368,26 @@ bool Array3D::open(Storage *storage, uint32_t storage_node_id,
   if (!table_caches_) {
     GRNXX_ERROR() << "new std::unique_ptr<void *[]>[] failed: size = "
                   << secondary_table_size;
-    return false;
+    throw MemoryError();
   }
-  return true;
 }
 
 bool Array3D::unlink(Storage *storage, uint32_t storage_node_id,
                      uint64_t value_size, uint64_t page_size,
                      uint64_t table_size, uint64_t secondary_table_size) {
   Array3D array;
-  if (!array.open(storage, storage_node_id, value_size, page_size,
-                  table_size, secondary_table_size, nullptr)) {
-    return false;
-  }
+  array.open(storage, storage_node_id, value_size, page_size,
+             table_size, secondary_table_size, nullptr);
   return storage->unlink_node(storage_node_id);
 }
 
-bool Array3D::initialize_page(uint64_t table_id, uint64_t page_id) {
+void Array3D::initialize_page(uint64_t table_id, uint64_t page_id) {
   if (!table_caches_[table_id]) {
-    if (!initialize_table(table_id)) {
-      return false;
-    }
+    initialize_table(table_id);
   }
   Lock inter_thread_lock(&page_mutex_);
   if (!table_caches_[table_id][page_id]) {
     StorageNode table_node = storage_->open_node(secondary_table_[table_id]);
-    if (!table_node) {
-      return false;
-    }
     uint32_t * const table = static_cast<uint32_t *>(table_node.body());
     if (table[page_id] == STORAGE_INVALID_NODE_ID) {
       Lock inter_process_lock(&header_->page_mutex);
@@ -438,33 +395,24 @@ bool Array3D::initialize_page(uint64_t table_id, uint64_t page_id) {
         StorageNode page_node =
             storage_->create_node(secondary_table_[table_id],
                                   header_->value_size * header_->page_size);
-        if (!page_node) {
-          return false;
-        }
         if (default_value_) {
           fill_page_(page_node.body(), default_value_);
         }
         table[page_id] = page_node.id();
         table_caches_[table_id][page_id] = page_node.body();
-        return true;
+        return;
       }
     }
     StorageNode page_node = storage_->open_node(table[page_id]);
-    if (!page_node) {
-      return false;
-    }
     table_caches_[table_id][page_id] = page_node.body();
   }
-  return true;
 }
 
-bool Array3D::initialize_table(uint64_t table_id) {
+void Array3D::initialize_table(uint64_t table_id) {
   Lock inter_thread_lock(&table_mutex_);
   if (!table_caches_[table_id]) {
     if (!secondary_table_) {
-      if (!initialize_secondary_table()) {
-        return false;
-      }
+      initialize_secondary_table();
     }
     if (secondary_table_[table_id] == STORAGE_INVALID_NODE_ID) {
       Lock inter_process_lock(&header_->table_mutex);
@@ -472,9 +420,6 @@ bool Array3D::initialize_table(uint64_t table_id) {
         StorageNode table_node =
             storage_->create_node(header_->secondary_table_storage_node_id,
                                   sizeof(uint32_t) * header_->table_size);
-        if (!table_node) {
-          return false;
-        }
         uint32_t * const table = static_cast<uint32_t *>(table_node.body());
         for (uint64_t i = 0; i < header_->table_size; ++i) {
           table[i] = STORAGE_INVALID_NODE_ID;
@@ -485,17 +430,16 @@ bool Array3D::initialize_table(uint64_t table_id) {
     void ** const table_cache = new void *[header_->table_size];
     if (!table_cache) {
       GRNXX_ERROR() << "new void *[] failed: size = " << header_->table_size;
-      return false;
+      throw MemoryError();
     }
     for (uint64_t i = 0; i < header_->table_size; ++i) {
       table_cache[i] = nullptr;
     }
     table_caches_[table_id].reset(table_cache);
   }
-  return true;
 }
 
-bool Array3D::initialize_secondary_table() {
+void Array3D::initialize_secondary_table() {
   Lock inter_thread_lock(&secondary_table_mutex_);
   if (!secondary_table_) {
     if (header_->secondary_table_storage_node_id == STORAGE_INVALID_NODE_ID) {
@@ -505,9 +449,6 @@ bool Array3D::initialize_secondary_table() {
             sizeof(uint32_t) * header_->secondary_table_size;
         StorageNode secondary_table_node =
             storage_->create_node(storage_node_id_, secondary_table_size);
-        if (!secondary_table_node) {
-          return false;
-        }
         uint32_t * const secondary_table =
             static_cast<uint32_t *>(secondary_table_node.body());
         for (uint64_t i = 0; i < header_->secondary_table_size; ++i) {
@@ -515,17 +456,13 @@ bool Array3D::initialize_secondary_table() {
         }
         header_->secondary_table_storage_node_id = secondary_table_node.id();
         secondary_table_ = secondary_table;
-        return true;
+        return;
       }
     }
     StorageNode secondary_table_node =
         storage_->open_node(header_->secondary_table_storage_node_id);
-    if (!secondary_table_node) {
-      return false;
-    }
     secondary_table_ = static_cast<uint32_t *>(secondary_table_node.body());
   }
-  return true;
 }
 
 }  // namespace grnxx

  Modified: lib/grnxx/array_impl.hpp (+46 -66)
===================================================================
--- lib/grnxx/array_impl.hpp    2013-07-03 17:03:53 +0900 (b1fee0f)
+++ lib/grnxx/array_impl.hpp    2013-07-03 17:38:48 +0900 (dbaf6ec)
@@ -40,11 +40,11 @@ class Array1D {
   Array1D();
   ~Array1D();
 
-  bool create(Storage *storage, uint32_t storage_node_id,
+  void create(Storage *storage, uint32_t storage_node_id,
               uint64_t value_size, uint64_t page_size,
               const void *default_value = nullptr,
               FillPage fill_page = nullptr);
-  bool open(Storage *storage, uint32_t storage_node_id,
+  void open(Storage *storage, uint32_t storage_node_id,
             uint64_t value_size, uint64_t page_size);
 
   static bool unlink(Storage *storage, uint32_t storage_node_id,
@@ -55,7 +55,7 @@ class Array1D {
   }
 
   template <typename T>
-  T *get_page() {
+  T *get_page(uint64_t) {
     return static_cast<T *>(page_);
   }
 
@@ -72,12 +72,12 @@ class Array2D {
   Array2D();
   ~Array2D();
 
-  bool create(Storage *storage, uint32_t storage_node_id,
+  void create(Storage *storage, uint32_t storage_node_id,
               uint64_t value_size, uint64_t page_size,
               uint64_t table_size,
               const void *default_value = nullptr,
               FillPage fill_page = nullptr);
-  bool open(Storage *storage, uint32_t storage_node_id,
+  void open(Storage *storage, uint32_t storage_node_id,
             uint64_t value_size, uint64_t page_size,
             uint64_t table_size, FillPage fill_page);
 
@@ -92,9 +92,7 @@ class Array2D {
   template <typename T, uint64_t TABLE_SIZE>
   T *get_page(uint64_t page_id) {
     if (!table_cache_[page_id]) {
-      if (!initialize_page(page_id)) {
-        return nullptr;
-      }
+      initialize_page(page_id);
     }
     return static_cast<T *>(table_cache_[page_id]);
   }
@@ -109,7 +107,7 @@ class Array2D {
   std::unique_ptr<void *[]> table_cache_;
   Mutex mutex_;
 
-  bool initialize_page(uint64_t page_id);
+  void initialize_page(uint64_t page_id);
 };
 
 class Array3D {
@@ -119,13 +117,13 @@ class Array3D {
   Array3D();
   ~Array3D();
 
-  bool create(Storage *storage, uint32_t storage_node_id,
+  void create(Storage *storage, uint32_t storage_node_id,
                uint64_t value_size, uint64_t page_size,
                uint64_t table_size, uint64_t secondary_table_size,
                const void *default_value = nullptr,
                FillPage fill_page = nullptr);
 
-  bool open(Storage *storage, uint32_t storage_node_id,
+  void open(Storage *storage, uint32_t storage_node_id,
             uint64_t value_size, uint64_t page_size,
             uint64_t table_size, uint64_t secondary_table_size,
             FillPage fill_page);
@@ -143,9 +141,7 @@ class Array3D {
     const uint64_t table_id = page_id / TABLE_SIZE;
     page_id %= TABLE_SIZE;
     if (!table_caches_[table_id] || !table_caches_[table_id][page_id]) {
-      if (!initialize_page(table_id, page_id)) {
-        return nullptr;
-      }
+      initialize_page(table_id, page_id);
     }
     return static_cast<T *>(table_caches_[table_id][page_id]);
   }
@@ -162,9 +158,9 @@ class Array3D {
   Mutex table_mutex_;
   Mutex secondary_table_mutex_;
 
-  bool initialize_page(uint64_t table_id, uint64_t page_id);
-  bool initialize_table(uint64_t table_id);
-  bool initialize_secondary_table();
+  void initialize_page(uint64_t table_id, uint64_t page_id);
+  void initialize_table(uint64_t table_id);
+  void initialize_secondary_table();
 };
 
 template <typename T,
@@ -191,18 +187,18 @@ class ArrayImpl<T, PAGE_SIZE, 1, 1> {
   }
 
   // Create an array.
-  bool create(Storage *storage, uint32_t storage_node_id) {
-    return impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE);
+  void create(Storage *storage, uint32_t storage_node_id) {
+    impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE);
   }
   // Create an array with the default value.
-  bool create(Storage *storage, uint32_t storage_node_id,
+  void create(Storage *storage, uint32_t storage_node_id,
               ValueArg default_value) {
-    return impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
-                        &default_value, fill_page);
+    impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
+                 &default_value, fill_page);
   }
   // Open an array.
-  bool open(Storage *storage, uint32_t storage_node_id) {
-    return impl_.open(storage, storage_node_id, sizeof(Value), PAGE_SIZE);
+  void open(Storage *storage, uint32_t storage_node_id) {
+    impl_.open(storage, storage_node_id, sizeof(Value), PAGE_SIZE);
   }
 
   // Unlink an array.
@@ -237,7 +233,8 @@ class ArrayImpl<T, PAGE_SIZE, 1, 1> {
   bool get(uint64_t value_id, Value *value) {
     const Value * const page = get_page(value_id / PAGE_SIZE);
     if (value) {
-      *value = page[value_id % PAGE_SIZE];
+      *value = page[value_id];
+//      *value = page[value_id % PAGE_SIZE];
     }
     return true;
   }
@@ -245,19 +242,21 @@ class ArrayImpl<T, PAGE_SIZE, 1, 1> {
   // Set a value and return true.
   bool set(uint64_t value_id, ValueArg value) {
     Value * const page = get_page(value_id / PAGE_SIZE);
-    page[value_id % PAGE_SIZE] = value;
+    page[value_id] = value;
+//    page[value_id % PAGE_SIZE] = value;
     return true;
   }
 
   // Get a value and return its address.
   Value *get_pointer(uint64_t value_id) {
     Value * const page = get_page(value_id / PAGE_SIZE);
-    return &page[value_id % PAGE_SIZE];
+    return &page[value_id];
+//    return &page[value_id % PAGE_SIZE];
   }
 
   // Get a page and return its starting address.
-  Value *get_page(uint64_t) {
-    return impl_.get_page<Value>();
+  Value *get_page(uint64_t page_id) {
+    return impl_.get_page<Value>(page_id);
   }
 
  private:
@@ -292,20 +291,20 @@ class ArrayImpl<T, PAGE_SIZE, TABLE_SIZE, 1> {
   }
 
   // Create an array.
-  bool create(Storage *storage, uint32_t storage_node_id) {
-    return impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
-                        TABLE_SIZE);
+  void create(Storage *storage, uint32_t storage_node_id) {
+    impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
+                 TABLE_SIZE);
   }
   // Create an array with the default value.
-  bool create(Storage *storage, uint32_t storage_node_id,
+  void create(Storage *storage, uint32_t storage_node_id,
               ValueArg default_value) {
-    return impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
-                        TABLE_SIZE, &default_value, fill_page);
+    impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
+                 TABLE_SIZE, &default_value, fill_page);
   }
   // Open an array.
-  bool open(Storage *storage, uint32_t storage_node_id) {
-    return impl_.open(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
-                      TABLE_SIZE, fill_page);
+  void open(Storage *storage, uint32_t storage_node_id) {
+    impl_.open(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
+               TABLE_SIZE, fill_page);
   }
 
   // Unlink an array.
@@ -340,9 +339,6 @@ class ArrayImpl<T, PAGE_SIZE, TABLE_SIZE, 1> {
   // The value is assigned to "*value" iff "value" != nullptr.
   bool get(uint64_t value_id, Value *value) {
     const Value * const page = get_page(value_id / PAGE_SIZE);
-    if (!page) {
-      return false;
-    }
     if (value) {
       *value = page[value_id % PAGE_SIZE];
     }
@@ -352,9 +348,6 @@ class ArrayImpl<T, PAGE_SIZE, TABLE_SIZE, 1> {
   // Set a value and return true on success.
   bool set(uint64_t value_id, ValueArg value) {
     Value * const page = get_page(value_id / PAGE_SIZE);
-    if (!page) {
-      return false;
-    }
     page[value_id % PAGE_SIZE] = value;
     return true;
   }
@@ -362,9 +355,6 @@ class ArrayImpl<T, PAGE_SIZE, TABLE_SIZE, 1> {
   // Get a value and return its address on success.
   Value *get_pointer(uint64_t value_id) {
     Value * const page = get_page(value_id / PAGE_SIZE);
-    if (!page) {
-      return nullptr;
-    }
     return &page[value_id % PAGE_SIZE];
   }
 
@@ -410,21 +400,20 @@ class ArrayImpl {
   }
 
   // Create an array.
-  bool create(Storage *storage, uint32_t storage_node_id) {
-    return impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
-                        TABLE_SIZE, SECONDARY_TABLE_SIZE);
+  void create(Storage *storage, uint32_t storage_node_id) {
+    impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
+                 TABLE_SIZE, SECONDARY_TABLE_SIZE);
   }
   // Create an array with the default value.
-  bool create(Storage *storage, uint32_t storage_node_id,
+  void create(Storage *storage, uint32_t storage_node_id,
               ValueArg default_value) {
-    return impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
-                        TABLE_SIZE, SECONDARY_TABLE_SIZE, &default_value,
-                        fill_page);
+    impl_.create(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
+                 TABLE_SIZE, SECONDARY_TABLE_SIZE, &default_value, fill_page);
   }
   // Open an array.
-  bool open(Storage *storage, uint32_t storage_node_id) {
-    return impl_.open(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
-                      TABLE_SIZE, SECONDARY_TABLE_SIZE, fill_page);
+  void open(Storage *storage, uint32_t storage_node_id) {
+    impl_.open(storage, storage_node_id, sizeof(Value), PAGE_SIZE,
+               TABLE_SIZE, SECONDARY_TABLE_SIZE, fill_page);
   }
 
   // Unlink an array.
@@ -459,9 +448,6 @@ class ArrayImpl {
   // The value is assigned to "*value" iff "value" != nullptr.
   bool get(uint64_t value_id, Value *value) {
     const Value * const page = get_page(value_id / PAGE_SIZE);
-    if (!page) {
-      return false;
-    }
     if (value) {
       *value = page[value_id % PAGE_SIZE];
     }
@@ -471,9 +457,6 @@ class ArrayImpl {
   // Set a value and return true on success.
   bool set(uint64_t value_id, ValueArg value) {
     Value * const page = get_page(value_id / PAGE_SIZE);
-    if (!page) {
-      return false;
-    }
     page[value_id % PAGE_SIZE] = value;
     return true;
   }
@@ -481,9 +464,6 @@ class ArrayImpl {
   // Get a value and return its address on success.
   Value *get_pointer(uint64_t value_id) {
     Value * const page = get_page(value_id / PAGE_SIZE);
-    if (!page) {
-      return nullptr;
-    }
     return &page[value_id % PAGE_SIZE];
   }
 

  Modified: lib/grnxx/map/hash_table/key_id_array.hpp (+13 -15)
===================================================================
--- lib/grnxx/map/hash_table/key_id_array.hpp    2013-07-03 17:03:53 +0900 (a328d0d)
+++ lib/grnxx/map/hash_table/key_id_array.hpp    2013-07-03 17:38:48 +0900 (7aafbcd)
@@ -21,6 +21,7 @@
 #include "grnxx/features.hpp"
 
 #include "grnxx/array_impl.hpp"
+#include "grnxx/exception.hpp"
 #include "grnxx/logger.hpp"
 #include "grnxx/map.hpp"
 #include "grnxx/storage.hpp"
@@ -174,11 +175,8 @@ class KeyIDArray {
   static KeyIDArray *create_instance() {
     KeyIDArray * const array = new (std::nothrow) KeyIDArray;
     if (!array) {
-      GRNXX_ERROR() << "new grnxx::Array failed: "
-                    << "value_size = " << sizeof(Value)
-                    << ", page_size = " << page_size()
-                    << ", table_size = " << table_size()
-                    << ", secondary_table_size = " << secondary_table_size();
+      GRNXX_ERROR() << "new grnxx::map::hash_table::KeyIDArray failed";
+      throw MemoryError();
     }
     return array;
   }
@@ -192,15 +190,17 @@ class KeyIDArray {
       return false;
     }
     storage_node_id_ = storage_node.id();
-    header_ = static_cast<KeyIDArrayHeader *>(storage_node.body());
-    *header_ = KeyIDArrayHeader();
-    if (!impl_.create(storage, storage_node_id_, MAP_INVALID_KEY_ID)) {
+    try {
+      header_ = static_cast<KeyIDArrayHeader *>(storage_node.body());
+      *header_ = KeyIDArrayHeader();
+      impl_.create(storage, storage_node_id_, MAP_INVALID_KEY_ID);
+      mask_ = mask;
+      header_->impl_storage_node_id = impl_.storage_node_id();
+      header_->mask = mask_;
+    } catch (...) {
       storage_->unlink_node(storage_node_id_);
-      return false;
+      throw;
     }
-    mask_ = mask;
-    header_->impl_storage_node_id = impl_.storage_node_id();
-    header_->mask = mask_;
     return true;
   }
 
@@ -212,9 +212,7 @@ class KeyIDArray {
     }
     storage_node_id_ = storage_node.id();
     header_ = static_cast<KeyIDArrayHeader *>(storage_node.body());
-    if (!impl_.open(storage, header_->impl_storage_node_id)) {
-      return false;
-    }
+    impl_.open(storage, header_->impl_storage_node_id);
     mask_ = header_->mask;
     return true;
   }

  Modified: test/test_array.cpp (+0 -6)
===================================================================
--- test/test_array.cpp    2013-07-03 17:03:53 +0900 (f1cccf3)
+++ test/test_array.cpp    2013-07-03 17:38:48 +0900 (a14f285)
@@ -104,7 +104,6 @@ void test_array() {
 
   // Create an Array and test its member functions.
   array.reset(array->create(storage.get(), grnxx::STORAGE_ROOT_NODE_ID));
-  assert(array);
   assert(array->page_size() == PAGE_SIZE);
   assert(array->table_size() == TABLE_SIZE);
   assert(array->secondary_table_size() == SECONDARY_TABLE_SIZE);
@@ -139,7 +138,6 @@ void test_array() {
   // Create an Array with default value.
   array.reset(array->create(storage.get(), grnxx::STORAGE_ROOT_NODE_ID,
                             values[0]));
-  assert(array);
   for (std::uint64_t i = 0; i < array->size(); ++i) {
     T value;
     assert(array->get(i, &value));
@@ -164,7 +162,6 @@ void test_bit_array() {
 
   // Create an Array and test its member functions.
   array.reset(array->create(storage.get(), grnxx::STORAGE_ROOT_NODE_ID));
-  assert(array);
   assert(array->unit_size() == (sizeof(Unit) * 8));
   assert(array->page_size() == PAGE_SIZE);
   assert(array->table_size() == TABLE_SIZE);
@@ -198,7 +195,6 @@ void test_bit_array() {
 
   // Open the Array and get values.
   array.reset(array->open(storage.get(), storage_node_id));
-  assert(array);
   for (std::uint64_t i = 0; i < array->size(); ++i) {
     const bool expected_bit = (units[i / 64] >> (i % 64)) & 1;
     bool stored_bit;
@@ -208,14 +204,12 @@ void test_bit_array() {
 
   // Create Arrays with default value.
   array.reset(array->create(storage.get(), grnxx::STORAGE_ROOT_NODE_ID, false));
-  assert(array);
   for (std::uint64_t i = 0; i < array->size(); ++i) {
     bool bit;
     assert(array->get(i, &bit));
     assert(!bit);
   }
   array.reset(array->create(storage.get(), grnxx::STORAGE_ROOT_NODE_ID, true));
-  assert(array);
   for (std::uint64_t i = 0; i < array->size(); ++i) {
     bool bit;
     assert(array->get(i, &bit));
-------------- next part --------------
HTML����������������������������...
Download 



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