[Groonga-commit] groonga/groonga [master] added code to catch exceptions thrown by std::vector.

Back to archive index

null+****@clear***** null+****@clear*****
2011年 7月 1日 (金) 17:33:11 JST


Susumu Yata	2011-07-01 08:33:11 +0000 (Fri, 01 Jul 2011)

  New Revision: 5e8f52f4aca31d75a8bd0c30d5f6f4f48873fe9e

  Log:
    added code to catch exceptions thrown by std::vector.

  Added files:
    lib/dat/vector.hpp
  Modified files:
    lib/dat/Makefile.am
    lib/dat/common-prefix-cursor.cpp
    lib/dat/common-prefix-cursor.hpp
    lib/dat/id-cursor.cpp
    lib/dat/id-cursor.hpp
    lib/dat/key-cursor.cpp
    lib/dat/key-cursor.hpp
    lib/dat/memory-mapped-file-impl.cpp
    lib/dat/memory-mapped-file-impl.hpp
    lib/dat/memory-mapped-file.cpp
    lib/dat/memory-mapped-file.hpp
    lib/dat/predictive-cursor.cpp
    lib/dat/predictive-cursor.hpp
    lib/dat/trie.cpp

  Modified: lib/dat/Makefile.am (+2 -1)
===================================================================
--- lib/dat/Makefile.am    2011-07-01 04:02:49 +0000 (1072448)
+++ lib/dat/Makefile.am    2011-07-01 08:33:11 +0000 (93a1b85)
@@ -32,7 +32,8 @@ noinst_HEADERS =				\
 	predictive-cursor.hpp			\
 	timer.hpp				\
 	trie.hpp				\
-	usage.hpp
+	usage.hpp				\
+	vector.hpp
 endif
 
 CLEANFILES = *.gcno *.gcda

  Modified: lib/dat/common-prefix-cursor.cpp (+22 -26)
===================================================================
--- lib/dat/common-prefix-cursor.cpp    2011-07-01 04:02:49 +0000 (5a1835e)
+++ lib/dat/common-prefix-cursor.cpp    2011-07-01 08:33:11 +0000 (2fb913b)
@@ -3,6 +3,8 @@
 #include <algorithm>
 #include <cstring>
 
+#include "trie.hpp"
+
 namespace grn {
 namespace dat {
 
@@ -15,17 +17,15 @@ CommonPrefixCursor::CommonPrefixCursor()
       cur_(0),
       end_(0) {}
 
-CommonPrefixCursor::~CommonPrefixCursor() {
-  close();
-}
+CommonPrefixCursor::~CommonPrefixCursor() {}
 
 void CommonPrefixCursor::open(const Trie &trie,
-                                    const void *ptr,
-                                    UInt32 min_length,
-                                    UInt32 max_length,
-                                    UInt32 offset,
-                                    UInt32 limit,
-                                    UInt32 flags) {
+                              const void *ptr,
+                              UInt32 min_length,
+                              UInt32 max_length,
+                              UInt32 offset,
+                              UInt32 limit,
+                              UInt32 flags) {
   GRN_DAT_THROW_IF(PARAM_ERROR, (ptr == NULL) && (max_length != 0));
   GRN_DAT_THROW_IF(PARAM_ERROR, min_length > max_length);
 
@@ -36,13 +36,8 @@ void CommonPrefixCursor::open(const Trie &trie,
 }
 
 void CommonPrefixCursor::close() {
-  trie_ = NULL;
-  offset_ = 0;
-  limit_ = UINT32_MAX;
-  flags_ = COMMON_PREFIX_CURSOR;
-  buf_.clear();
-  cur_ = 0;
-  end_ = 0;
+  CommonPrefixCursor new_cursor;
+  new_cursor.swap(this);
 }
 
 bool CommonPrefixCursor::next(Key *key) {
@@ -78,9 +73,9 @@ UInt32 CommonPrefixCursor::fix_flags(UInt32 flags) const {
 }
 
 CommonPrefixCursor::CommonPrefixCursor(const Trie &trie,
-                                                   UInt32 offset,
-                                                   UInt32 limit,
-                                                   UInt32 flags)
+                                        UInt32 offset,
+                                        UInt32 limit,
+                                        UInt32 flags)
     : trie_(&trie),
       offset_(offset),
       limit_(limit),
@@ -90,8 +85,8 @@ CommonPrefixCursor::CommonPrefixCursor(const Trie &trie,
       end_(0) {}
 
 void CommonPrefixCursor::init(const UInt8 *ptr,
-                                    UInt32 min_length,
-                                    UInt32 max_length) {
+                              UInt32 min_length,
+                              UInt32 max_length) {
   if ((limit_ == 0) || (offset_ > (max_length - min_length))) {
     return;
   }
@@ -107,7 +102,7 @@ void CommonPrefixCursor::init(const UInt8 *ptr,
           (std::memcmp(ptr + i, key.ptr() + i, key.length() - i) == 0) &&
           ((key.length() < max_length) ||
            ((flags_ & EXCEPT_EXACT_MATCH) != EXCEPT_EXACT_MATCH))) {
-        buf_.push_back(key.id());
+        GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(key.id()));
       }
       break;
     }
@@ -115,7 +110,8 @@ void CommonPrefixCursor::init(const UInt8 *ptr,
     if ((i >= min_length) &&
         (trie_->ith_node(node_id).child() == TERMINAL_LABEL)) {
       const UInt32 terminal = base.offset() ^ TERMINAL_LABEL;
-      buf_.push_back(trie_->ith_node(terminal).key_id());
+      GRN_DAT_THROW_IF(MEMORY_ERROR,
+                       !buf_.push_back(trie_->ith_node(terminal).key_id()));
     }
 
     node_id = base.offset() ^ ptr[i];
@@ -131,14 +127,14 @@ void CommonPrefixCursor::init(const UInt8 *ptr,
       Key key;
       trie_->ith_key(base.key_id(), &key);
       if ((key.length() >= min_length) && (key.length() <= max_length)) {
-        buf_.push_back(key.id());
+        GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(key.id()));
       }
     } else if (trie_->ith_node(node_id).child() == TERMINAL_LABEL) {
       const UInt32 terminal = base.offset() ^ TERMINAL_LABEL;
       Key key;
       trie_->ith_key(trie_->ith_node(terminal).key_id(), &key);
       if ((key.length() >= min_length) && (key.length() <= max_length)) {
-        buf_.push_back(key.id());
+        GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(key.id()));
       }
     }
   }
@@ -161,7 +157,7 @@ void CommonPrefixCursor::swap(CommonPrefixCursor *cursor) {
   std::swap(offset_, cursor->offset_);
   std::swap(limit_, cursor->limit_);
   std::swap(flags_, cursor->flags_);
-  buf_.swap(cursor->buf_);
+  buf_.swap(&cursor->buf_);
   std::swap(cur_, cursor->cur_);
   std::swap(end_, cursor->end_);
 }

  Modified: lib/dat/common-prefix-cursor.hpp (+4 -4)
===================================================================
--- lib/dat/common-prefix-cursor.hpp    2011-07-01 04:02:49 +0000 (3504c72)
+++ lib/dat/common-prefix-cursor.hpp    2011-07-01 08:33:11 +0000 (27cd3da)
@@ -2,13 +2,13 @@
 #define GRN_DAT_COMMON_PREFIX_CURSOR_H
 
 #include "cursor.hpp"
-#include "trie.hpp"
-
-#include <vector>
+#include "vector.hpp"
 
 namespace grn {
 namespace dat {
 
+class Trie;
+
 class CommonPrefixCursor : public Cursor {
  public:
   CommonPrefixCursor();
@@ -42,7 +42,7 @@ class CommonPrefixCursor : public Cursor {
   UInt32 limit_;
   UInt32 flags_;
 
-  std::vector<UInt32> buf_;
+  Vector<UInt32> buf_;
   UInt32 cur_;
   UInt32 end_;
 

  Modified: lib/dat/id-cursor.cpp (+17 -20)
===================================================================
--- lib/dat/id-cursor.cpp    2011-07-01 04:02:49 +0000 (12a2667)
+++ lib/dat/id-cursor.cpp    2011-07-01 08:33:11 +0000 (312602b)
@@ -2,6 +2,8 @@
 
 #include <algorithm>
 
+#include "trie.hpp"
+
 namespace grn {
 namespace dat {
 
@@ -13,9 +15,7 @@ IdCursor::IdCursor()
       cur_(INVALID_KEY_ID),
       end_(INVALID_KEY_ID) {}
 
-IdCursor::~IdCursor() {
-  close();
-}
+IdCursor::~IdCursor() {}
 
 void IdCursor::open(const Trie &trie,
                     const void *min_ptr, UInt32 min_length,
@@ -26,21 +26,21 @@ void IdCursor::open(const Trie &trie,
   GRN_DAT_THROW_IF(PARAM_ERROR, (min_ptr == NULL) && (min_length != 0));
   GRN_DAT_THROW_IF(PARAM_ERROR, (max_ptr == NULL) && (max_length != 0));
 
-  Key min_key;
-  if (min_ptr == NULL) {
-    min_key.set_id(INVALID_KEY_ID);
-  } else {
-    GRN_DAT_THROW_IF(PARAM_ERROR, !trie.search(min_ptr, min_length, &min_key));
+  UInt32 min_id = INVALID_KEY_ID;
+  if (min_ptr != NULL) {
+    Key key;
+    GRN_DAT_THROW_IF(PARAM_ERROR, !trie.search(min_ptr, min_length, &key));
+    min_id = key.id();
   }
 
-  Key max_key;
-  if (max_ptr == NULL) {
-    max_key.set_id(INVALID_KEY_ID);
-  } else {
-    GRN_DAT_THROW_IF(PARAM_ERROR, !trie.search(max_ptr, max_length, &max_key));
+  UInt32 max_id = INVALID_KEY_ID;
+  if (max_ptr != NULL) {
+    Key key;
+    GRN_DAT_THROW_IF(PARAM_ERROR, !trie.search(max_ptr, max_length, &key));
+    max_id = key.id();
   }
 
-  open(trie, min_key.id(), max_key.id(), offset, limit, flags);
+  open(trie, min_id, max_id, offset, limit, flags);
 }
 
 void IdCursor::open(const Trie &trie,
@@ -50,18 +50,15 @@ void IdCursor::open(const Trie &trie,
                     UInt32 limit,
                     UInt32 flags) {
   flags = fix_flags(flags);
+
   IdCursor new_cursor(trie, offset, limit, flags);
   new_cursor.init(min_id, max_id);
   new_cursor.swap(this);
 }
 
 void IdCursor::close() {
-  trie_ = NULL;
-  offset_ = 0;
-  limit_ = UINT32_MAX;
-  flags_ = ID_RANGE_CURSOR;
-  cur_ = INVALID_KEY_ID;
-  end_ = INVALID_KEY_ID;
+  IdCursor new_cursor;
+  new_cursor.swap(this);
 }
 
 bool IdCursor::next(Key *key) {

  Modified: lib/dat/id-cursor.hpp (+2 -1)
===================================================================
--- lib/dat/id-cursor.hpp    2011-07-01 04:02:49 +0000 (dcb8de8)
+++ lib/dat/id-cursor.hpp    2011-07-01 08:33:11 +0000 (83a4b1d)
@@ -2,11 +2,12 @@
 #define GRN_DAT_ID_CURSOR_H
 
 #include "cursor.hpp"
-#include "trie.hpp"
 
 namespace grn {
 namespace dat {
 
+class Trie;
+
 class IdCursor : public Cursor {
  public:
   IdCursor();

  Modified: lib/dat/key-cursor.cpp (+29 -28)
===================================================================
--- lib/dat/key-cursor.cpp    2011-07-01 04:02:49 +0000 (08ca13b)
+++ lib/dat/key-cursor.cpp    2011-07-01 08:33:11 +0000 (75e52b2)
@@ -3,6 +3,8 @@
 #include <algorithm>
 #include <cstring>
 
+#include "trie.hpp"
+
 namespace grn {
 namespace dat {
 
@@ -19,7 +21,9 @@ KeyCursor::KeyCursor()
       end_length_(0) {}
 
 KeyCursor::~KeyCursor() {
-  close();
+  if ((end_ptr_ != NULL) && (end_length_ != 0)) {
+    delete [] end_ptr_;
+  }
 }
 
 void KeyCursor::open(const Trie &trie,
@@ -39,19 +43,8 @@ void KeyCursor::open(const Trie &trie,
 }
 
 void KeyCursor::close() {
-  trie_ = NULL;
-  offset_ = 0;
-  limit_ = UINT32_MAX;
-  flags_ = KEY_RANGE_CURSOR;
-  buf_.clear();
-  count_ = 0;
-  max_count_ = 0;
-  end_ = false;
-  if ((end_ptr_ != NULL) && (end_length_ != 0)) {
-    delete [] end_ptr_;
-  }
-  end_ptr_ = NULL;
-  end_length_ = 0;
+  KeyCursor new_cursor;
+  new_cursor.swap(this);
 }
 
 bool KeyCursor::next(Key *key) {
@@ -140,7 +133,7 @@ void KeyCursor::ascending_init(const UInt8 *min_ptr, UInt32 min_length,
       const int result = compare(key, min_ptr, min_length, i);
       if ((result > 0) || ((result == 0) &&
           ((flags_ & EXCEPT_LOWER_BOUND) != EXCEPT_LOWER_BOUND))) {
-        buf_.push_back(node_id);
+        GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(node_id));
       }
       return;
     }
@@ -153,7 +146,8 @@ void KeyCursor::ascending_init(const UInt8 *min_ptr, UInt32 min_length,
       }
       while (label != INVALID_LABEL) {
         if (label > min_ptr[i]) {
-          buf_.push_back(node.offset() ^ label);
+          GRN_DAT_THROW_IF(MEMORY_ERROR,
+                           !buf_.push_back(node.offset() ^ label));
           break;
         }
         label = trie_->ith_node(node.offset() ^ label).sibling();
@@ -163,7 +157,8 @@ void KeyCursor::ascending_init(const UInt8 *min_ptr, UInt32 min_length,
 
     node = trie_->ith_node(node_id);
     if (node.sibling() != INVALID_LABEL) {
-      buf_.push_back(node_id ^ min_ptr[i] ^ node.sibling());
+      GRN_DAT_THROW_IF(MEMORY_ERROR,
+                       !buf_.push_back(node_id ^ min_ptr[i] ^ node.sibling()));
     }
   }
 
@@ -173,7 +168,7 @@ void KeyCursor::ascending_init(const UInt8 *min_ptr, UInt32 min_length,
     trie_->ith_key(base.key_id(), &key);
     if ((key.length() != min_length) ||
         ((flags_ & EXCEPT_LOWER_BOUND) != EXCEPT_LOWER_BOUND)) {
-      buf_.push_back(node_id);
+      GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(node_id));
     }
     return;
   }
@@ -184,7 +179,7 @@ void KeyCursor::ascending_init(const UInt8 *min_ptr, UInt32 min_length,
     label = trie_->ith_node(base.offset() ^ label).sibling();
   }
   if (label != INVALID_LABEL) {
-    buf_.push_back(base.offset() ^ label);
+    GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(base.offset() ^ label));
   }
 }
 
@@ -209,7 +204,8 @@ void KeyCursor::descending_init(const UInt8 *min_ptr, UInt32 min_length,
       const int result = compare(key, max_ptr, max_length, i);
       if ((result < 0) || ((result == 0) &&
           ((flags_ & EXCEPT_UPPER_BOUND) != EXCEPT_UPPER_BOUND))) {
-        buf_.push_back(node_id | POST_ORDER_FLAG);
+        GRN_DAT_THROW_IF(MEMORY_ERROR,
+                         !buf_.push_back(node_id | POST_ORDER_FLAG));
       }
       return;
     }
@@ -217,13 +213,14 @@ void KeyCursor::descending_init(const UInt8 *min_ptr, UInt32 min_length,
     UInt32 label = trie_->ith_node(node_id).child();
     if (label == TERMINAL_LABEL) {
       node_id = base.offset() ^ label;
-      buf_.push_back(node_id | POST_ORDER_FLAG);
+      GRN_DAT_THROW_IF(MEMORY_ERROR,
+                       !buf_.push_back(node_id | POST_ORDER_FLAG));
       label = trie_->ith_node(node_id).sibling();
     }
     while (label != INVALID_LABEL) {
       node_id = base.offset() ^ label;
       if (label < max_ptr[i]) {
-        buf_.push_back(node_id);
+        GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(node_id));
       } else if (label > max_ptr[i]) {
         return;
       } else {
@@ -242,7 +239,8 @@ void KeyCursor::descending_init(const UInt8 *min_ptr, UInt32 min_length,
     trie_->ith_key(base.key_id(), &key);
     if ((key.length() == max_length) &&
         ((flags_ & EXCEPT_UPPER_BOUND) != EXCEPT_UPPER_BOUND)) {
-      buf_.push_back(node_id | POST_ORDER_FLAG);
+      GRN_DAT_THROW_IF(MEMORY_ERROR,
+                       !buf_.push_back(node_id | POST_ORDER_FLAG));
     }
     return;
   }
@@ -250,7 +248,8 @@ void KeyCursor::descending_init(const UInt8 *min_ptr, UInt32 min_length,
   UInt16 label = trie_->ith_node(node_id).child();
   if ((label == TERMINAL_LABEL) &&
       ((flags_ & EXCEPT_UPPER_BOUND) != EXCEPT_UPPER_BOUND)) {
-    buf_.push_back((base.offset() ^ label) | POST_ORDER_FLAG);
+    GRN_DAT_THROW_IF(MEMORY_ERROR,
+        !buf_.push_back((base.offset() ^ label) | POST_ORDER_FLAG));
   }
 }
 
@@ -259,7 +258,7 @@ void KeyCursor::swap(KeyCursor *cursor) {
   std::swap(offset_, cursor->offset_);
   std::swap(limit_, cursor->limit_);
   std::swap(flags_, cursor->flags_);
-  buf_.swap(cursor->buf_);
+  buf_.swap(&cursor->buf_);
   std::swap(count_, cursor->count_);
   std::swap(max_count_, cursor->max_count_);
   std::swap(end_, cursor->end_);
@@ -274,11 +273,13 @@ bool KeyCursor::ascending_next(Key *key) {
 
     const Node node = trie_->ith_node(node_id);
     if (node.sibling() != INVALID_LABEL) {
-      buf_.push_back(node_id ^ node.label() ^ node.sibling());
+      GRN_DAT_THROW_IF(MEMORY_ERROR,
+          !buf_.push_back(node_id ^ node.label() ^ node.sibling()));
     }
 
     if (node.child() != INVALID_LABEL) {
-      buf_.push_back(node.offset() ^ node.child());
+      GRN_DAT_THROW_IF(MEMORY_ERROR,
+                       !buf_.push_back(node.offset() ^ node.child()));
     }
 
     if (node.is_terminal()) {
@@ -329,7 +330,7 @@ bool KeyCursor::descending_next(Key *key) {
       buf_.back() |= POST_ORDER_FLAG;
       UInt16 label = trie_->ith_node(node_id).child();
       while (label != INVALID_LABEL) {
-        buf_.push_back(base.offset() ^ label);
+        GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(base.offset() ^ label));
         label = trie_->ith_node(base.offset() ^ label).sibling();
       }
     }

  Modified: lib/dat/key-cursor.hpp (+4 -4)
===================================================================
--- lib/dat/key-cursor.hpp    2011-07-01 04:02:49 +0000 (6315d05)
+++ lib/dat/key-cursor.hpp    2011-07-01 08:33:11 +0000 (4825a63)
@@ -2,13 +2,13 @@
 #define GRN_DAT_KEY_CURSOR_HPP_
 
 #include "cursor.hpp"
-#include "trie.hpp"
-
-#include <vector>
+#include "vector.hpp"
 
 namespace grn {
 namespace dat {
 
+class Trie;
+
 class KeyCursor : public Cursor {
  public:
   KeyCursor();
@@ -41,7 +41,7 @@ class KeyCursor : public Cursor {
   UInt32 limit_;
   UInt32 flags_;
 
-  std::vector<UInt32> buf_;
+  Vector<UInt32> buf_;
   UInt32 count_;
   UInt32 max_count_;
   bool end_;

  Modified: lib/dat/memory-mapped-file-impl.cpp (+11 -17)
===================================================================
--- lib/dat/memory-mapped-file-impl.cpp    2011-07-01 04:02:49 +0000 (14128cd)
+++ lib/dat/memory-mapped-file-impl.cpp    2011-07-01 08:33:11 +0000 (a80c596)
@@ -16,6 +16,7 @@
 #include <unistd.h>
 #endif  // WIN32
 
+#include <algorithm>
 #include <limits>
 
 namespace grn {
@@ -92,11 +93,11 @@ void MemoryMappedFileImpl::close() {
 #ifdef WIN32
 
 void MemoryMappedFileImpl::swap(MemoryMappedFileImpl *rhs) {
-  swap(ptr_, rhs->ptr_);
-  swap(size_, rhs->size_);
-  swap(file_, rhs->file_);
-  swap(map_, rhs->map_);
-  swap(addr_, rhs->addr_);
+  std::swap(ptr_, rhs->ptr_);
+  std::swap(size_, rhs->size_);
+  std::swap(file_, rhs->file_);
+  std::swap(map_, rhs->map_);
+  std::swap(addr_, rhs->addr_);
 }
 
 void MemoryMappedFileImpl::create_(const char *path, UInt64 size) {
@@ -157,11 +158,11 @@ void MemoryMappedFileImpl::open_(const char *path) {
 #else  // WIN32
 
 void MemoryMappedFileImpl::swap(MemoryMappedFileImpl *rhs) {
-  swap(ptr_, rhs->ptr_);
-  swap(size_, rhs->size_);
-  swap(fd_, rhs->fd_);
-  swap(addr_, rhs->addr_);
-  swap(length_, rhs->length_);
+  std::swap(ptr_, rhs->ptr_);
+  std::swap(size_, rhs->size_);
+  std::swap(fd_, rhs->fd_);
+  std::swap(addr_, rhs->addr_);
+  std::swap(length_, rhs->length_);
 }
 
 void MemoryMappedFileImpl::create_(const char *path, UInt64 size) {
@@ -211,12 +212,5 @@ void MemoryMappedFileImpl::open_(const char *path) {
 
 #endif  // WIN32
 
-template <typename T>
-void MemoryMappedFileImpl::swap(T &lhs, T &rhs) const {
-  T temp = lhs;
-  lhs = rhs;
-  rhs = temp;
-}
-
 }  // namespace grn
 }  // namespace dat

  Modified: lib/dat/memory-mapped-file-impl.hpp (+5 -8)
===================================================================
--- lib/dat/memory-mapped-file-impl.hpp    2011-07-01 04:02:49 +0000 (059c191)
+++ lib/dat/memory-mapped-file-impl.hpp    2011-07-01 08:33:11 +0000 (aa6cf0c)
@@ -1,12 +1,12 @@
-#ifndef GRN_DAT_MEMORY_MAPPED_FILE_IMPL_H_
-#define GRN_DAT_MEMORY_MAPPED_FILE_IMPL_H_
-
-#include "dat.hpp"
+#ifndef GRN_DAT_MEMORY_MAPPED_FILE_IMPL_HPP_
+#define GRN_DAT_MEMORY_MAPPED_FILE_IMPL_HPP_
 
 #ifdef WIN32
 #include <Windows.h>
 #endif  // WIN32
 
+#include "dat.hpp"
+
 namespace grn {
 namespace dat {
 
@@ -45,9 +45,6 @@ class MemoryMappedFileImpl {
   void create_(const char *path, UInt64 size);
   void open_(const char *path);
 
-  template <typename T>
-  void swap(T &lhs, T &rhs) const;
-
   // Disallows copy and assignment.
   MemoryMappedFileImpl(const MemoryMappedFileImpl &);
   MemoryMappedFileImpl &operator=(const MemoryMappedFileImpl &);
@@ -56,4 +53,4 @@ class MemoryMappedFileImpl {
 }  // namespace grn
 }  // namespace dat
 
-#endif  // GRN_DAT_MEMORY_MAPPED_FILE_IMPL_H_
+#endif  // GRN_DAT_MEMORY_MAPPED_FILE_IMPL_HPP_

  Modified: lib/dat/memory-mapped-file.cpp (+2 -1)
===================================================================
--- lib/dat/memory-mapped-file.cpp    2011-07-01 04:02:49 +0000 (bfd23f7)
+++ lib/dat/memory-mapped-file.cpp    2011-07-01 08:33:11 +0000 (9c76ef5)
@@ -1,8 +1,9 @@
 #include "memory-mapped-file.hpp"
-#include "memory-mapped-file-impl.hpp"
 
 #include <new>
 
+#include "memory-mapped-file-impl.hpp"
+
 namespace grn {
 namespace dat {
 

  Modified: lib/dat/memory-mapped-file.hpp (+3 -3)
===================================================================
--- lib/dat/memory-mapped-file.hpp    2011-07-01 04:02:49 +0000 (8f9142a)
+++ lib/dat/memory-mapped-file.hpp    2011-07-01 08:33:11 +0000 (7d85f6b)
@@ -1,5 +1,5 @@
-#ifndef GRN_DAT_MEMORY_MAPPED_FILE_H_
-#define GRN_DAT_MEMORY_MAPPED_FILE_H_
+#ifndef GRN_DAT_MEMORY_MAPPED_FILE_HPP_
+#define GRN_DAT_MEMORY_MAPPED_FILE_HPP_
 
 #include "dat.hpp"
 
@@ -33,4 +33,4 @@ class MemoryMappedFile {
 }  // namespace grn
 }  // namespace dat
 
-#endif  // GRN_DAT_MEMORY_MAPPED_FILE_H_
+#endif  // GRN_DAT_MEMORY_MAPPED_FILE_HPP_

  Modified: lib/dat/predictive-cursor.cpp (+21 -25)
===================================================================
--- lib/dat/predictive-cursor.cpp    2011-07-01 04:02:49 +0000 (449d75e)
+++ lib/dat/predictive-cursor.cpp    2011-07-01 08:33:11 +0000 (58d31c3)
@@ -3,6 +3,8 @@
 #include <algorithm>
 #include <cstring>
 
+#include "trie.hpp"
+
 namespace grn {
 namespace dat {
 
@@ -16,16 +18,14 @@ PredictiveCursor::PredictiveCursor()
       end_(0),
       min_length_(0) {}
 
-PredictiveCursor::~PredictiveCursor() {
-  close();
-}
+PredictiveCursor::~PredictiveCursor() {}
 
 void PredictiveCursor::open(const Trie &trie,
-                                  const void *ptr,
-                                  UInt32 length,
-                                  UInt32 offset,
-                                  UInt32 limit,
-                                  UInt32 flags) {
+                            const void *ptr,
+                            UInt32 length,
+                            UInt32 offset,
+                            UInt32 limit,
+                            UInt32 flags) {
   GRN_DAT_THROW_IF(PARAM_ERROR, (ptr == NULL) && (length != 0));
 
   flags = fix_flags(flags);
@@ -35,14 +35,8 @@ void PredictiveCursor::open(const Trie &trie,
 }
 
 void PredictiveCursor::close() {
-  trie_ = NULL;
-  offset_ = 0;
-  limit_ = UINT32_MAX;
-  flags_ = PREDICTIVE_CURSOR;
-  buf_.clear();
-  cur_ = 0;
-  end_ = 0;
-  min_length_ = 0;
+  PredictiveCursor new_cursor;
+  new_cursor.swap(this);
 }
 
 bool PredictiveCursor::next(Key *key) {
@@ -78,9 +72,9 @@ UInt32 PredictiveCursor::fix_flags(UInt32 flags) const {
 }
 
 PredictiveCursor::PredictiveCursor(const Trie &trie,
-                                               UInt32 offset,
-                                               UInt32 limit,
-                                               UInt32 flags)
+                                   UInt32 offset,
+                                   UInt32 limit,
+                                   UInt32 flags)
     : trie_(&trie),
       offset_(offset),
       limit_(limit),
@@ -110,7 +104,7 @@ void PredictiveCursor::init(const UInt8 *ptr, UInt32 length) {
         trie_->ith_key(base.key_id(), &key);
         if ((key.length() >= length) &&
             (std::memcmp(ptr + i, key.ptr() + i, length - i) == 0)) {
-          buf_.push_back(node_id);
+          GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(node_id));
         }
       }
       return;
@@ -121,7 +115,7 @@ void PredictiveCursor::init(const UInt8 *ptr, UInt32 length) {
       return;
     }
   }
-  buf_.push_back(node_id);
+  GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(node_id));
 }
 
 void PredictiveCursor::swap(PredictiveCursor *cursor) {
@@ -129,7 +123,7 @@ void PredictiveCursor::swap(PredictiveCursor *cursor) {
   std::swap(offset_, cursor->offset_);
   std::swap(limit_, cursor->limit_);
   std::swap(flags_, cursor->flags_);
-  buf_.swap(cursor->buf_);
+  buf_.swap(&cursor->buf_);
   std::swap(cur_, cursor->cur_);
   std::swap(end_, cursor->end_);
   std::swap(min_length_, cursor->min_length_);
@@ -142,7 +136,8 @@ bool PredictiveCursor::ascending_next(Key *key) {
 
     const Node node = trie_->ith_node(node_id);
     if (node.sibling() != INVALID_LABEL) {
-      buf_.push_back(node_id ^ node.label() ^ node.sibling());
+      GRN_DAT_THROW_IF(MEMORY_ERROR,
+          !buf_.push_back(node_id ^ node.label() ^ node.sibling()));
     }
 
     if (node.is_terminal()) {
@@ -155,7 +150,8 @@ bool PredictiveCursor::ascending_next(Key *key) {
         }
       }
     } else if (node.child() != INVALID_LABEL) {
-      buf_.push_back(node.offset() ^ node.child());
+      GRN_DAT_THROW_IF(MEMORY_ERROR,
+                       !buf_.push_back(node.offset() ^ node.child()));
     }
   }
   return false;
@@ -183,7 +179,7 @@ bool PredictiveCursor::descending_next(Key *key) {
       buf_.back() |= POST_ORDER_FLAG;
       UInt16 label = trie_->ith_node(node_id).child();
       while (label != INVALID_LABEL) {
-        buf_.push_back(base.offset() ^ label);
+        GRN_DAT_THROW_IF(MEMORY_ERROR, !buf_.push_back(base.offset() ^ label));
         label = trie_->ith_node(base.offset() ^ label).sibling();
       }
     }

  Modified: lib/dat/predictive-cursor.hpp (+4 -4)
===================================================================
--- lib/dat/predictive-cursor.hpp    2011-07-01 04:02:49 +0000 (f24afe0)
+++ lib/dat/predictive-cursor.hpp    2011-07-01 08:33:11 +0000 (2caa671)
@@ -2,13 +2,13 @@
 #define GRN_DAT_PREDICTIVE_CURSOR_HPP_
 
 #include "cursor.hpp"
-#include "trie.hpp"
-
-#include <vector>
+#include "vector.hpp"
 
 namespace grn {
 namespace dat {
 
+class Trie;
+
 class PredictiveCursor : public Cursor {
  public:
   PredictiveCursor();
@@ -41,7 +41,7 @@ class PredictiveCursor : public Cursor {
   UInt32 limit_;
   UInt32 flags_;
 
-  std::vector<UInt32> buf_;
+  Vector<UInt32> buf_;
   UInt32 cur_;
   UInt32 end_;
   UInt32 min_length_;

  Modified: lib/dat/trie.cpp (+5 -19)
===================================================================
--- lib/dat/trie.cpp    2011-07-01 04:02:49 +0000 (21d808d)
+++ lib/dat/trie.cpp    2011-07-01 08:33:11 +0000 (7e1a74a)
@@ -14,9 +14,7 @@ Trie::Trie()
       key_infos_(NULL),
       key_buf_(NULL) {}
 
-Trie::~Trie() {
-  close();
-}
+Trie::~Trie() {}
 
 void Trie::create(const char *file_name,
                   UInt64 file_size,
@@ -105,12 +103,8 @@ void Trie::open(const char *file_name) {
 }
 
 void Trie::close() {
-  memory_mapped_file_.close();
-  header_ = NULL;
-  nodes_ = NULL;
-  blocks_ = NULL;
-  key_infos_ = NULL;
-  key_buf_ = NULL;
+  Trie new_trie;
+  new_trie.swap(this);
 }
 
 void Trie::swap(Trie *trie) {
@@ -303,11 +297,7 @@ bool Trie::search_from_root(const UInt8 *ptr,
   GRN_DAT_DEBUG_THROW_IF(!ith_node(node_id).is_terminal());
 
   if (key != NULL) {
-    const UInt32 key_id = ith_node(node_id).key_id();
-    key->set_ptr(key_buf_ + ith_key_info(key_id).offset());
-    key->set_length(ith_key_info(key_id + 1).offset()
-        - ith_key_info(key_id).offset());
-    key->set_id(key_id);
+    ith_key(ith_node(node_id).key_id(), key);
   }
   return true;
 }
@@ -373,11 +363,7 @@ bool Trie::insert_from_root(const UInt8 *ptr,
   GRN_DAT_DEBUG_THROW_IF(!ith_node(next).is_terminal());
 
   if (key != NULL) {
-    const UInt32 key_id = ith_node(next).key_id();
-    key->set_ptr(key_buf_ + ith_key_info(key_id).offset());
-    key->set_length(ith_key_info(key_id + 1).offset()
-        - ith_key_info(key_id).offset());
-    key->set_id(key_id);
+    ith_key(ith_node(next).key_id(), key);
   }
   return false;
 }

  Added: lib/dat/vector.hpp (+66 -0) 100644
===================================================================
--- /dev/null
+++ lib/dat/vector.hpp    2011-07-01 08:33:11 +0000 (e77a932)
@@ -0,0 +1,66 @@
+#ifndef GRN_DAT_VECTOR_HPP_
+#define GRN_DAT_VECTOR_HPP_
+
+#include "dat.hpp"
+
+#include <vector>
+
+namespace grn {
+namespace dat {
+
+template <typename T>
+class Vector {
+ public:
+  Vector()
+      : buf_() {}
+  ~Vector() {}
+
+  const T &operator[](UInt32 i) const {
+    return buf_[i];
+  }
+  T &operator[](UInt32 i) {
+    return buf_[i];
+  }
+
+  const T &back() const {
+    return buf_.back();
+  }
+  T &back() {
+    return buf_.back();
+  }
+
+  bool push_back(const T &x) try {
+    buf_.push_back(x);
+    return true;
+  } catch (...) {
+    return false;
+  }
+
+  void pop_back() {
+    buf_.pop_back();
+  }
+
+  void swap(Vector *rhs) {
+    buf_.swap(rhs->buf_);
+  }
+
+  bool empty() const {
+    return buf_.empty();
+  }
+
+  UInt32 size() const {
+    return static_cast<UInt32>(buf_.size());
+  }
+
+ private:
+  std::vector<T> buf_;
+
+  // Disallows copy and assignment.
+  Vector(const Vector &);
+  Vector &operator=(const Vector &);
+};
+
+}  // namespace grn
+}  // namespace dat
+
+#endif  // GRN_DAT_VECTOR_HPP_




Groonga-commit メーリングリストの案内
Back to archive index