null+****@clear*****
null+****@clear*****
2011年 6月 28日 (火) 23:24:30 JST
Susumu Yata 2011-06-28 14:24:30 +0000 (Tue, 28 Jun 2011)
New Revision: 3d49f9a3a94f0d119db9b08090f3ebbe7a7614c1
Log:
updated error handling of grn::dat. renamed CommonPrefixSearchCursor and PredictiveSearchCursor to CommonPrefixCursor and PredictiveCursor, respectively.
Modified files:
lib/dat.cpp
lib/dat/Makefile.am
lib/dat/check.hpp
lib/dat/cursor-factory.cpp
lib/dat/cursor-factory.hpp
lib/dat/dat.hpp
lib/dat/id-cursor.cpp
lib/dat/key-cursor.cpp
lib/dat/node.hpp
lib/dat/trie.cpp
Renamed files:
lib/dat/common-prefix-cursor.cpp
(from lib/dat/common-prefix-search-cursor.cpp)
lib/dat/common-prefix-cursor.hpp
(from lib/dat/common-prefix-search-cursor.hpp)
lib/dat/predictive-cursor.cpp
(from lib/dat/predictive-search-cursor.cpp)
lib/dat/predictive-cursor.hpp
(from lib/dat/predictive-search-cursor.hpp)
Modified: lib/dat.cpp (+3 -3)
===================================================================
--- lib/dat.cpp 2011-06-28 06:34:10 +0000 (07ee2a5)
+++ lib/dat.cpp 2011-06-28 14:24:30 +0000 (5129fe3)
@@ -21,7 +21,7 @@
#include "dat.h"
#include "util.h"
#include "dat/trie.hpp"
-#include "dat/common-prefix-search-cursor.hpp"
+#include "dat/common-prefix-cursor.hpp"
extern "C" {
@@ -304,8 +304,8 @@ grn_dat_cursor_open(grn_ctx *ctx, grn_dat *dat,
if (max && max_size) {
if ((dat->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE)) {
grn::dat::Trie *trie = static_cast<grn::dat::Trie *>(dat->handle);
- grn::dat::CommonPrefixSearchCursor *cursor;
- cursor = new grn::dat::CommonPrefixSearchCursor;
+ grn::dat::CommonPrefixCursor *cursor;
+ cursor = new grn::dat::CommonPrefixCursor;
cursor->open(*trie, max, min_size, max_size, offset, limit);
dc->cursor = cursor;
} else {
Modified: lib/dat/Makefile.am (+4 -4)
===================================================================
--- lib/dat/Makefile.am 2011-06-28 06:34:10 +0000 (34b6c0c)
+++ lib/dat/Makefile.am 2011-06-28 14:24:30 +0000 (8aeee17)
@@ -4,18 +4,18 @@ if !OS_WIN32
noinst_LTLIBRARIES = libgrndat.la
libgrndat_la_SOURCES = \
- common-prefix-search-cursor.cpp \
+ common-prefix-cursor.cpp \
cursor-factory.cpp \
id-cursor.cpp \
key-cursor.cpp \
- predictive-search-cursor.cpp \
+ predictive-cursor.cpp \
trie.cpp
noinst_HEADERS = \
base.hpp \
block.hpp \
check.hpp \
- common-prefix-search-cursor.hpp \
+ common-prefix-cursor.hpp \
cursor-factory.hpp \
cursor.hpp \
dat.hpp \
@@ -25,7 +25,7 @@ noinst_HEADERS = \
key-info.hpp \
key.hpp \
node.hpp \
- predictive-search-cursor.hpp \
+ predictive-cursor.hpp \
timer.hpp \
trie.hpp \
usage.hpp
Modified: lib/dat/check.hpp (+3 -9)
===================================================================
--- lib/dat/check.hpp 2011-06-28 06:34:10 +0000 (418e4d8)
+++ lib/dat/check.hpp 2011-06-28 14:24:30 +0000 (c2af731)
@@ -6,12 +6,6 @@
namespace grn {
namespace dat {
-// The most significant bit (MSB) represents whether or not the node ID is used
-// as an offset. This bit is independent of the other bits.
-// The second most significant bit represents whether or not the node is a
-// phantom node. If it is true, next() and prev() are available. These
-// functions return the next/previous phantom node respectively.
-// A non-phantom node
class Check {
public:
Check()
@@ -50,7 +44,7 @@ class Check {
// A label is attached to each non-phantom node. A label is represented by
// a byte except for a terminal label '\256'. Note that a phantom node always
- // returns an invalid label with its phantom flag bit so as to reject a
+ // returns an invalid label with its phantom bit flag so as to reject a
// transition to the phantom node.
UInt32 label() const {
return check_ & (IS_PHANTOM_FLAG | LABEL_MASK);
@@ -83,8 +77,8 @@ class Check {
check_ = (check_ & IS_OFFSET_FLAG) | x;
}
- // To reject a transition to an incomplete node, its label is overwritten
- // with an invalid label when it becomes a non-phantom node.
+ // To reject a transition to an incomplete node, set_is_phantom() overwrites
+ // its label with an invalid label when a node becomes a non-phantom node.
void set_is_phantom(bool x) {
if (x) {
GRN_DAT_DEBUG_THROW_IF(is_phantom());
Renamed: lib/dat/common-prefix-cursor.cpp (+19 -19) 78%
===================================================================
--- lib/dat/common-prefix-search-cursor.cpp 2011-06-28 06:34:10 +0000 (5712dad)
+++ lib/dat/common-prefix-cursor.cpp 2011-06-28 14:24:30 +0000 (5a1835e)
@@ -1,4 +1,4 @@
-#include "common-prefix-search-cursor.hpp"
+#include "common-prefix-cursor.hpp"
#include <algorithm>
#include <cstring>
@@ -6,7 +6,7 @@
namespace grn {
namespace dat {
-CommonPrefixSearchCursor::CommonPrefixSearchCursor()
+CommonPrefixCursor::CommonPrefixCursor()
: trie_(NULL),
offset_(0),
limit_(UINT32_MAX),
@@ -15,27 +15,27 @@ CommonPrefixSearchCursor::CommonPrefixSearchCursor()
cur_(0),
end_(0) {}
-CommonPrefixSearchCursor::~CommonPrefixSearchCursor() {
+CommonPrefixCursor::~CommonPrefixCursor() {
close();
}
-void CommonPrefixSearchCursor::open(const Trie &trie,
+void CommonPrefixCursor::open(const Trie &trie,
const void *ptr,
UInt32 min_length,
UInt32 max_length,
UInt32 offset,
UInt32 limit,
UInt32 flags) {
- GRN_DAT_PARAM_ERROR_IF((ptr == NULL) && (max_length != 0));
- GRN_DAT_PARAM_ERROR_IF(min_length > max_length);
+ GRN_DAT_THROW_IF(PARAM_ERROR, (ptr == NULL) && (max_length != 0));
+ GRN_DAT_THROW_IF(PARAM_ERROR, min_length > max_length);
flags = fix_flags(flags);
- CommonPrefixSearchCursor new_cursor(trie, offset, limit, flags);
+ CommonPrefixCursor new_cursor(trie, offset, limit, flags);
new_cursor.init(static_cast<const UInt8 *>(ptr), min_length, max_length);
new_cursor.swap(this);
}
-void CommonPrefixSearchCursor::close() {
+void CommonPrefixCursor::close() {
trie_ = NULL;
offset_ = 0;
limit_ = UINT32_MAX;
@@ -45,7 +45,7 @@ void CommonPrefixSearchCursor::close() {
end_ = 0;
}
-bool CommonPrefixSearchCursor::next(Key *key) {
+bool CommonPrefixCursor::next(Key *key) {
if (cur_ == end_) {
return false;
}
@@ -57,27 +57,27 @@ bool CommonPrefixSearchCursor::next(Key *key) {
return true;
}
-UInt32 CommonPrefixSearchCursor::fix_flags(UInt32 flags) const {
+UInt32 CommonPrefixCursor::fix_flags(UInt32 flags) const {
const UInt32 cursor_type = flags & CURSOR_TYPE_MASK;
- GRN_DAT_PARAM_ERROR_IF((cursor_type != 0) &&
- (cursor_type != COMMON_PREFIX_CURSOR));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_type != 0) &&
+ (cursor_type != COMMON_PREFIX_CURSOR));
flags |= COMMON_PREFIX_CURSOR;
const UInt32 cursor_order = flags & CURSOR_ORDER_MASK;
- GRN_DAT_PARAM_ERROR_IF((cursor_order != 0) &&
- (cursor_order != ASCENDING_CURSOR) &&
- (cursor_order != DESCENDING_CURSOR));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_order != 0) &&
+ (cursor_order != ASCENDING_CURSOR) &&
+ (cursor_order != DESCENDING_CURSOR));
if (cursor_order == 0) {
flags |= ASCENDING_CURSOR;
}
const UInt32 cursor_options = flags & CURSOR_OPTIONS_MASK;
- GRN_DAT_PARAM_ERROR_IF(cursor_options & ~EXCEPT_EXACT_MATCH);
+ GRN_DAT_THROW_IF(PARAM_ERROR, cursor_options & ~EXCEPT_EXACT_MATCH);
return flags;
}
-CommonPrefixSearchCursor::CommonPrefixSearchCursor(const Trie &trie,
+CommonPrefixCursor::CommonPrefixCursor(const Trie &trie,
UInt32 offset,
UInt32 limit,
UInt32 flags)
@@ -89,7 +89,7 @@ CommonPrefixSearchCursor::CommonPrefixSearchCursor(const Trie &trie,
cur_(0),
end_(0) {}
-void CommonPrefixSearchCursor::init(const UInt8 *ptr,
+void CommonPrefixCursor::init(const UInt8 *ptr,
UInt32 min_length,
UInt32 max_length) {
if ((limit_ == 0) || (offset_ > (max_length - min_length))) {
@@ -156,7 +156,7 @@ void CommonPrefixSearchCursor::init(const UInt8 *ptr,
}
}
-void CommonPrefixSearchCursor::swap(CommonPrefixSearchCursor *cursor) {
+void CommonPrefixCursor::swap(CommonPrefixCursor *cursor) {
std::swap(trie_, cursor->trie_);
std::swap(offset_, cursor->offset_);
std::swap(limit_, cursor->limit_);
Renamed: lib/dat/common-prefix-cursor.hpp (+11 -11) 60%
===================================================================
--- lib/dat/common-prefix-search-cursor.hpp 2011-06-28 06:34:10 +0000 (022d7aa)
+++ lib/dat/common-prefix-cursor.hpp 2011-06-28 14:24:30 +0000 (3504c72)
@@ -1,5 +1,5 @@
-#ifndef GRN_DAT_COMMON_PREFIX_SEARCH_CURSOR_H
-#define GRN_DAT_COMMON_PREFIX_SEARCH_CURSOR_H
+#ifndef GRN_DAT_COMMON_PREFIX_CURSOR_H
+#define GRN_DAT_COMMON_PREFIX_CURSOR_H
#include "cursor.hpp"
#include "trie.hpp"
@@ -9,10 +9,10 @@
namespace grn {
namespace dat {
-class CommonPrefixSearchCursor : public Cursor {
+class CommonPrefixCursor : public Cursor {
public:
- CommonPrefixSearchCursor();
- ~CommonPrefixSearchCursor();
+ CommonPrefixCursor();
+ ~CommonPrefixCursor();
void open(const Trie &trie,
const void *ptr,
@@ -47,17 +47,17 @@ class CommonPrefixSearchCursor : public Cursor {
UInt32 end_;
UInt32 fix_flags(UInt32 flags) const;
- CommonPrefixSearchCursor(const Trie &trie,
- UInt32 offset, UInt32 limit, UInt32 flags);
+ CommonPrefixCursor(const Trie &trie,
+ UInt32 offset, UInt32 limit, UInt32 flags);
void init(const UInt8 *ptr, UInt32 min_length, UInt32 max_length);
- void swap(CommonPrefixSearchCursor *cursor);
+ void swap(CommonPrefixCursor *cursor);
// Disallows copy and assignment.
- CommonPrefixSearchCursor(const CommonPrefixSearchCursor &);
- CommonPrefixSearchCursor &operator=(const CommonPrefixSearchCursor &);
+ CommonPrefixCursor(const CommonPrefixCursor &);
+ CommonPrefixCursor &operator=(const CommonPrefixCursor &);
};
} // namespace grn
} // namespace dat
-#endif // GRN_DAT_COMMON_PREFIX_SEARCH_CURSOR_H
+#endif // GRN_DAT_COMMON_PREFIX_CURSOR_H
Modified: lib/dat/cursor-factory.cpp (+5 -8)
===================================================================
--- lib/dat/cursor-factory.cpp 2011-06-28 06:34:10 +0000 (d22515b)
+++ lib/dat/cursor-factory.cpp 2011-06-28 14:24:30 +0000 (ee4ca5b)
@@ -1,8 +1,8 @@
#include "cursor-factory.hpp"
#include "id-cursor.hpp"
#include "key-cursor.hpp"
-#include "common-prefix-search-cursor.hpp"
-#include "predictive-search-cursor.hpp"
+#include "common-prefix-cursor.hpp"
+#include "predictive-cursor.hpp"
namespace grn {
namespace dat {
@@ -38,7 +38,7 @@ Cursor *CursorFactory::open(const Trie &trie,
return cursor;
}
case COMMON_PREFIX_CURSOR: {
- CommonPrefixSearchCursor *cursor = new CommonPrefixSearchCursor;
+ CommonPrefixCursor *cursor = new CommonPrefixCursor;
try {
cursor->open(trie, max_ptr, min_length, max_length,
offset, limit, flags);
@@ -49,7 +49,7 @@ Cursor *CursorFactory::open(const Trie &trie,
return cursor;
}
case PREDICTIVE_CURSOR: {
- PredictiveSearchCursor *cursor = new PredictiveSearchCursor;
+ PredictiveCursor *cursor = new PredictiveCursor;
try {
cursor->open(trie, min_ptr, min_length,
offset, limit, flags);
@@ -60,10 +60,7 @@ Cursor *CursorFactory::open(const Trie &trie,
return cursor;
}
default: {
- GRN_DAT_PARAM_ERROR_IF((cursor_type != ID_RANGE_CURSOR) &&
- (cursor_type != KEY_RANGE_CURSOR) &&
- (cursor_type != COMMON_PREFIX_CURSOR) &&
- (cursor_type != PREDICTIVE_CURSOR));
+ GRN_DAT_THROW(PARAM_ERROR, "unknown cursor type");
}
}
return NULL;
Modified: lib/dat/cursor-factory.hpp (+2 -1)
===================================================================
--- lib/dat/cursor-factory.hpp 2011-06-28 06:34:10 +0000 (6e632fa)
+++ lib/dat/cursor-factory.hpp 2011-06-28 14:24:30 +0000 (6db5012)
@@ -2,11 +2,12 @@
#define GRN_DAT_CURSOR_FACTORY_H
#include "cursor.hpp"
-#include "trie.hpp"
namespace grn {
namespace dat {
+class Trie;
+
class CursorFactory {
public:
static Cursor *open(const Trie &trie,
Modified: lib/dat/dat.hpp (+64 -48)
===================================================================
--- lib/dat/dat.hpp 2011-06-28 06:34:10 +0000 (d93680d)
+++ lib/dat/dat.hpp 2011-06-28 14:24:30 +0000 (810de75)
@@ -107,59 +107,83 @@ const UInt32 EXCEPT_EXACT_MATCH = 0x04000;
const UInt32 CURSOR_OPTIONS_MASK = 0xFF000;
// To be determined...
-//enum ErrorCode {
-// PARAM_ERROR = -1,
-// IO_ERROR = -2,
-// MEMORY_ERROR = -3,
-// SIZE_ERROR = -4
-//};
+enum ErrorCode {
+ NO_ERROR = 0,
+ PARAM_ERROR = -1,
+ IO_ERROR = -2,
+ MEMORY_ERROR = -3,
+ SIZE_ERROR = -4,
+ UNEXPECTED_ERROR = -5
+};
class Exception : public std::exception {
public:
Exception() throw()
- : what_("") {}
- Exception(const char *what) throw()
- : what_((what != NULL) ? what : "") {}
+ : file_(""),
+ line_(-1),
+ what_("") {}
+ Exception(const char *file, int line, const char *what) throw()
+ : file_(file),
+ line_(line),
+ what_((what != NULL) ? what : "") {}
Exception(const Exception &ex) throw()
- : what_(ex.what_) {}
+ : file_(ex.file_),
+ line_(ex.line_),
+ what_(ex.what_) {}
virtual ~Exception() throw() {}
- Exception &operator=(const Exception &ex) throw() {
+ virtual Exception &operator=(const Exception &ex) throw() {
+ file_ = ex.file_;
+ line_ = ex.line_;
what_ = ex.what_;
return *this;
}
+ virtual ErrorCode code() const throw() {
+ return NO_ERROR;
+ }
+ virtual const char *file() const throw() {
+ return file_;
+ }
+ virtual int line() const throw() {
+ return line_;
+ }
virtual const char *what() const throw() {
return what_;
}
private:
+ const char *file_;
+ int line_;
const char *what_;
};
-#define GRN_DAT_DEFINE_ERROR(error_type) \
- class error_type : public Exception { \
- public: \
- error_type() throw() \
- : Exception() {} \
- error_type(const char *what) throw() \
- : Exception(what) {} \
- error_type(const error_type &ex) throw() \
- : Exception(ex) {} \
- virtual ~error_type() throw() {} \
- \
- error_type &operator=(const error_type &ex) throw() { \
- *static_cast<Exception *>(this) = ex; \
- return *this; \
- } \
- }
-
-GRN_DAT_DEFINE_ERROR(ParamError);
-GRN_DAT_DEFINE_ERROR(IOError);
-GRN_DAT_DEFINE_ERROR(MemoryError);
-GRN_DAT_DEFINE_ERROR(SizeError);
-
-#undef GRN_DAT_DEFINE_ERROR
+template <ErrorCode T>
+class Error : public Exception {
+ public:
+ Error() throw()
+ : Exception() {}
+ Error(const char *file, int line, const char *what) throw()
+ : Exception(file, line, what) {}
+ Error(const Error &ex) throw()
+ : Exception(ex) {}
+ virtual ~Error() throw() {}
+
+ virtual Error &operator=(const Error &ex) throw() {
+ *static_cast<Exception *>(this) = ex;
+ return *this;
+ }
+
+ virtual ErrorCode code() const throw() {
+ return T;
+ }
+};
+
+typedef Error<PARAM_ERROR> ParamError;
+typedef Error<IO_ERROR> IOError;
+typedef Error<MEMORY_ERROR> MemoryError;
+typedef Error<SIZE_ERROR> SizeError;
+typedef Error<UNEXPECTED_ERROR> UnexpectedError;
#define GRN_DAT_INT_TO_STR(value) \
#value
@@ -168,23 +192,15 @@ GRN_DAT_DEFINE_ERROR(SizeError);
#define GRN_DAT_LINE_STR \
GRN_DAT_LINE_TO_STR(__LINE__)
-#define GRN_DAT_THROW(exception, what) \
- (throw exception(__FILE__ ":" GRN_DAT_LINE_STR ": " what))
-#define GRN_DAT_THROW_IF(cond) \
- (void)((!(cond)) || (GRN_DAT_THROW(grn::dat::Exception, #cond), 0))
-
-#define GRN_DAT_PARAM_ERROR_IF(cond) \
- (void)((!(cond)) || (GRN_DAT_THROW(grn::dat::ParamError, #cond), 0))
-#define GRN_DAT_IO_ERROR_IF(cond) \
- (void)((!(cond)) || (GRN_DAT_THROW(grn::dat::IOError, #cond), 0))
-#define GRN_DAT_MEMORY_ERROR_IF(cond) \
- (void)((!(cond)) || (GRN_DAT_THROW(grn::dat::MemoryError, #cond), 0))
-#define GRN_DAT_SIZE_ERROR_IF(cond) \
- (void)((!(cond)) || (GRN_DAT_THROW(grn::dat::SizeError, #cond), 0))
+#define GRN_DAT_THROW(code, msg) \
+ (throw grn::dat::Error<code>(__FILE__, __LINE__, \
+ __FILE__ ":" GRN_DAT_LINE_STR ": " #code ": " msg))
+#define GRN_DAT_THROW_IF(code, cond) \
+ (void)((!(cond)) || (GRN_DAT_THROW(code, #cond), 0))
#ifdef _DEBUG
#define GRN_DAT_DEBUG_THROW_IF(cond) \
- GRN_DAT_THROW_IF(cond)
+ GRN_DAT_THROW_IF(grn::dat::UNEXPECTED_ERROR, cond)
#define GRN_DAT_DEBUG_LOG(var) \
(std::clog << __FILE__ ":" GRN_DAT_LINE_STR ": " #var ": " \
<< (var) << std::endl)
Modified: lib/dat/id-cursor.cpp (+10 -10)
===================================================================
--- lib/dat/id-cursor.cpp 2011-06-28 06:34:10 +0000 (e0b7591)
+++ lib/dat/id-cursor.cpp 2011-06-28 14:24:30 +0000 (12a2667)
@@ -23,21 +23,21 @@ void IdCursor::open(const Trie &trie,
UInt32 offset,
UInt32 limit,
UInt32 flags) {
- GRN_DAT_PARAM_ERROR_IF((min_ptr == NULL) && (min_length != 0));
- GRN_DAT_PARAM_ERROR_IF((max_ptr == NULL) && (max_length != 0));
+ 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_PARAM_ERROR_IF(!trie.search(min_ptr, min_length, &min_key));
+ GRN_DAT_THROW_IF(PARAM_ERROR, !trie.search(min_ptr, min_length, &min_key));
}
Key max_key;
if (max_ptr == NULL) {
max_key.set_id(INVALID_KEY_ID);
} else {
- GRN_DAT_PARAM_ERROR_IF(!trie.search(max_ptr, max_length, &max_key));
+ GRN_DAT_THROW_IF(PARAM_ERROR, !trie.search(max_ptr, max_length, &max_key));
}
open(trie, min_key.id(), max_key.id(), offset, limit, flags);
@@ -79,20 +79,20 @@ bool IdCursor::next(Key *key) {
UInt32 IdCursor::fix_flags(UInt32 flags) const {
const UInt32 cursor_type = flags & CURSOR_TYPE_MASK;
- GRN_DAT_PARAM_ERROR_IF((cursor_type != 0) &&
- (cursor_type != ID_RANGE_CURSOR));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_type != 0) &&
+ (cursor_type != ID_RANGE_CURSOR));
flags |= ID_RANGE_CURSOR;
const UInt32 cursor_order = flags & CURSOR_ORDER_MASK;
- GRN_DAT_PARAM_ERROR_IF((cursor_order != 0) &&
- (cursor_order != ASCENDING_CURSOR) &&
- (cursor_order != DESCENDING_CURSOR));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_order != 0) &&
+ (cursor_order != ASCENDING_CURSOR) &&
+ (cursor_order != DESCENDING_CURSOR));
if (cursor_order == 0) {
flags |= ASCENDING_CURSOR;
}
const UInt32 cursor_options = flags & CURSOR_OPTIONS_MASK;
- GRN_DAT_PARAM_ERROR_IF(
+ GRN_DAT_THROW_IF(PARAM_ERROR,
cursor_options & ~(EXCEPT_LOWER_BOUND | EXCEPT_UPPER_BOUND));
return flags;
Modified: lib/dat/key-cursor.cpp (+8 -8)
===================================================================
--- lib/dat/key-cursor.cpp 2011-06-28 06:34:10 +0000 (b66bc64)
+++ lib/dat/key-cursor.cpp 2011-06-28 14:24:30 +0000 (08ca13b)
@@ -28,8 +28,8 @@ void KeyCursor::open(const Trie &trie,
UInt32 offset,
UInt32 limit,
UInt32 flags) {
- GRN_DAT_PARAM_ERROR_IF((min_ptr == NULL) && (min_length != 0));
- GRN_DAT_PARAM_ERROR_IF((max_ptr == NULL) && (max_length != 0));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (min_ptr == NULL) && (min_length != 0));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (max_ptr == NULL) && (max_length != 0));
flags = fix_flags(flags);
KeyCursor new_cursor(trie, offset, limit, flags);
@@ -68,20 +68,20 @@ bool KeyCursor::next(Key *key) {
UInt32 KeyCursor::fix_flags(UInt32 flags) const {
const UInt32 cursor_type = flags & CURSOR_TYPE_MASK;
- GRN_DAT_PARAM_ERROR_IF((cursor_type != 0) &&
- (cursor_type != KEY_RANGE_CURSOR));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_type != 0) &&
+ (cursor_type != KEY_RANGE_CURSOR));
flags |= KEY_RANGE_CURSOR;
const UInt32 cursor_order = flags & CURSOR_ORDER_MASK;
- GRN_DAT_PARAM_ERROR_IF((cursor_order != 0) &&
- (cursor_order != ASCENDING_CURSOR) &&
- (cursor_order != DESCENDING_CURSOR));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_order != 0) &&
+ (cursor_order != ASCENDING_CURSOR) &&
+ (cursor_order != DESCENDING_CURSOR));
if (cursor_order == 0) {
flags |= ASCENDING_CURSOR;
}
const UInt32 cursor_options = flags & CURSOR_OPTIONS_MASK;
- GRN_DAT_PARAM_ERROR_IF(
+ GRN_DAT_THROW_IF(PARAM_ERROR,
cursor_options & ~(EXCEPT_LOWER_BOUND | EXCEPT_UPPER_BOUND));
return flags;
Modified: lib/dat/node.hpp (+1 -0)
===================================================================
--- lib/dat/node.hpp 2011-06-28 06:34:10 +0000 (02bbfe9)
+++ lib/dat/node.hpp 2011-06-28 14:24:30 +0000 (ef416a6)
@@ -1,6 +1,7 @@
#ifndef GRN_DAT_NODE_HPP_
#define GRN_DAT_NODE_HPP_
+// See base.hpp and check.hpp for details.
#include "base.hpp"
#include "check.hpp"
Renamed: lib/dat/predictive-cursor.cpp (+20 -20) 78%
===================================================================
--- lib/dat/predictive-search-cursor.cpp 2011-06-28 06:34:10 +0000 (6558128)
+++ lib/dat/predictive-cursor.cpp 2011-06-28 14:24:30 +0000 (449d75e)
@@ -1,4 +1,4 @@
-#include "predictive-search-cursor.hpp"
+#include "predictive-cursor.hpp"
#include <algorithm>
#include <cstring>
@@ -6,7 +6,7 @@
namespace grn {
namespace dat {
-PredictiveSearchCursor::PredictiveSearchCursor()
+PredictiveCursor::PredictiveCursor()
: trie_(NULL),
offset_(0),
limit_(UINT32_MAX),
@@ -16,25 +16,25 @@ PredictiveSearchCursor::PredictiveSearchCursor()
end_(0),
min_length_(0) {}
-PredictiveSearchCursor::~PredictiveSearchCursor() {
+PredictiveCursor::~PredictiveCursor() {
close();
}
-void PredictiveSearchCursor::open(const Trie &trie,
+void PredictiveCursor::open(const Trie &trie,
const void *ptr,
UInt32 length,
UInt32 offset,
UInt32 limit,
UInt32 flags) {
- GRN_DAT_PARAM_ERROR_IF((ptr == NULL) && (length != 0));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (ptr == NULL) && (length != 0));
flags = fix_flags(flags);
- PredictiveSearchCursor new_cursor(trie, offset, limit, flags);
+ PredictiveCursor new_cursor(trie, offset, limit, flags);
new_cursor.init(static_cast<const UInt8 *>(ptr), length);
new_cursor.swap(this);
}
-void PredictiveSearchCursor::close() {
+void PredictiveCursor::close() {
trie_ = NULL;
offset_ = 0;
limit_ = UINT32_MAX;
@@ -45,7 +45,7 @@ void PredictiveSearchCursor::close() {
min_length_ = 0;
}
-bool PredictiveSearchCursor::next(Key *key) {
+bool PredictiveCursor::next(Key *key) {
if (cur_ == end_) {
return false;
}
@@ -57,27 +57,27 @@ bool PredictiveSearchCursor::next(Key *key) {
}
}
-UInt32 PredictiveSearchCursor::fix_flags(UInt32 flags) const {
+UInt32 PredictiveCursor::fix_flags(UInt32 flags) const {
const UInt32 cursor_type = flags & CURSOR_TYPE_MASK;
- GRN_DAT_PARAM_ERROR_IF((cursor_type != 0) &&
- (cursor_type != PREDICTIVE_CURSOR));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_type != 0) &&
+ (cursor_type != PREDICTIVE_CURSOR));
flags |= PREDICTIVE_CURSOR;
const UInt32 cursor_order = flags & CURSOR_ORDER_MASK;
- GRN_DAT_PARAM_ERROR_IF((cursor_order != 0) &&
- (cursor_order != ASCENDING_CURSOR) &&
- (cursor_order != DESCENDING_CURSOR));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (cursor_order != 0) &&
+ (cursor_order != ASCENDING_CURSOR) &&
+ (cursor_order != DESCENDING_CURSOR));
if (cursor_order == 0) {
flags |= ASCENDING_CURSOR;
}
const UInt32 cursor_options = flags & CURSOR_OPTIONS_MASK;
- GRN_DAT_PARAM_ERROR_IF(cursor_options & ~(EXCEPT_EXACT_MATCH));
+ GRN_DAT_THROW_IF(PARAM_ERROR, cursor_options & ~(EXCEPT_EXACT_MATCH));
return flags;
}
-PredictiveSearchCursor::PredictiveSearchCursor(const Trie &trie,
+PredictiveCursor::PredictiveCursor(const Trie &trie,
UInt32 offset,
UInt32 limit,
UInt32 flags)
@@ -90,7 +90,7 @@ PredictiveSearchCursor::PredictiveSearchCursor(const Trie &trie,
end_(0),
min_length_(0) {}
-void PredictiveSearchCursor::init(const UInt8 *ptr, UInt32 length) {
+void PredictiveCursor::init(const UInt8 *ptr, UInt32 length) {
if (limit_ == 0) {
return;
}
@@ -124,7 +124,7 @@ void PredictiveSearchCursor::init(const UInt8 *ptr, UInt32 length) {
buf_.push_back(node_id);
}
-void PredictiveSearchCursor::swap(PredictiveSearchCursor *cursor) {
+void PredictiveCursor::swap(PredictiveCursor *cursor) {
std::swap(trie_, cursor->trie_);
std::swap(offset_, cursor->offset_);
std::swap(limit_, cursor->limit_);
@@ -135,7 +135,7 @@ void PredictiveSearchCursor::swap(PredictiveSearchCursor *cursor) {
std::swap(min_length_, cursor->min_length_);
}
-bool PredictiveSearchCursor::ascending_next(Key *key) {
+bool PredictiveCursor::ascending_next(Key *key) {
while (!buf_.empty()) {
const UInt32 node_id = buf_.back();
buf_.pop_back();
@@ -161,7 +161,7 @@ bool PredictiveSearchCursor::ascending_next(Key *key) {
return false;
}
-bool PredictiveSearchCursor::descending_next(Key *key) {
+bool PredictiveCursor::descending_next(Key *key) {
while (!buf_.empty()) {
const bool post_order = (buf_.back() & POST_ORDER_FLAG) == POST_ORDER_FLAG;
const UInt32 node_id = buf_.back() & ~POST_ORDER_FLAG;
Renamed: lib/dat/predictive-cursor.hpp (+11 -11) 63%
===================================================================
--- lib/dat/predictive-search-cursor.hpp 2011-06-28 06:34:10 +0000 (0f30357)
+++ lib/dat/predictive-cursor.hpp 2011-06-28 14:24:30 +0000 (f24afe0)
@@ -1,5 +1,5 @@
-#ifndef GRN_DAT_PREDICTIVE_SEARCH_CURSOR_HPP_
-#define GRN_DAT_PREDICTIVE_SEARCH_CURSOR_HPP_
+#ifndef GRN_DAT_PREDICTIVE_CURSOR_HPP_
+#define GRN_DAT_PREDICTIVE_CURSOR_HPP_
#include "cursor.hpp"
#include "trie.hpp"
@@ -9,10 +9,10 @@
namespace grn {
namespace dat {
-class PredictiveSearchCursor : public Cursor {
+class PredictiveCursor : public Cursor {
public:
- PredictiveSearchCursor();
- ~PredictiveSearchCursor();
+ PredictiveCursor();
+ ~PredictiveCursor();
void open(const Trie &trie,
const void *ptr,
@@ -47,10 +47,10 @@ class PredictiveSearchCursor : public Cursor {
UInt32 min_length_;
UInt32 fix_flags(UInt32 flags) const;
- PredictiveSearchCursor(const Trie &trie,
- UInt32 offset, UInt32 limit, UInt32 flags);
+ PredictiveCursor(const Trie &trie,
+ UInt32 offset, UInt32 limit, UInt32 flags);
void init(const UInt8 *ptr, UInt32 length);
- void swap(PredictiveSearchCursor *cursor);
+ void swap(PredictiveCursor *cursor);
bool ascending_next(Key *key);
bool descending_next(Key *key);
@@ -58,11 +58,11 @@ class PredictiveSearchCursor : public Cursor {
static const UInt32 POST_ORDER_FLAG = 0x80000000U;
// Disallows copy and assignment.
- PredictiveSearchCursor(const PredictiveSearchCursor &);
- PredictiveSearchCursor &operator=(const PredictiveSearchCursor &);
+ PredictiveCursor(const PredictiveCursor &);
+ PredictiveCursor &operator=(const PredictiveCursor &);
};
} // namespace grn
} // namespace dat
-#endif // GRN_DAT_PREDICTIVE_SEARCH_CURSOR_HPP_
+#endif // GRN_DAT_PREDICTIVE_CURSOR_HPP_
Modified: lib/dat/trie.cpp (+40 -40)
===================================================================
--- lib/dat/trie.cpp 2011-06-28 06:34:10 +0000 (d205f7e)
+++ lib/dat/trie.cpp 2011-06-28 14:24:30 +0000 (3ebcd15)
@@ -28,25 +28,25 @@ void Trie::create(const char *file_name,
UInt32 max_num_keys,
double num_nodes_per_key,
double average_key_length) {
- GRN_DAT_PARAM_ERROR_IF((file_size != 0) && (max_num_keys != 0));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (file_size != 0) && (max_num_keys != 0));
if (num_nodes_per_key == 0.0) {
num_nodes_per_key = DEFAULT_NUM_NODES_PER_KEY;
}
- GRN_DAT_PARAM_ERROR_IF(num_nodes_per_key < 1.0);
+ GRN_DAT_THROW_IF(PARAM_ERROR, num_nodes_per_key < 1.0);
if (average_key_length == 0.0) {
average_key_length = DEFAULT_AVERAGE_KEY_LENGTH;
}
- GRN_DAT_PARAM_ERROR_IF(average_key_length < 1.0);
- GRN_DAT_PARAM_ERROR_IF(average_key_length > MAX_KEY_LENGTH);
+ GRN_DAT_THROW_IF(PARAM_ERROR, average_key_length < 1.0);
+ GRN_DAT_THROW_IF(PARAM_ERROR, average_key_length > MAX_KEY_LENGTH);
if (max_num_keys == 0) {
if (file_size == 0) {
file_size = DEFAULT_FILE_SIZE;
}
} else {
- GRN_DAT_PARAM_ERROR_IF(max_num_keys > MAX_NUM_KEYS);
+ GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys > MAX_NUM_KEYS);
}
Trie new_trie;
@@ -61,7 +61,7 @@ void Trie::create(const Trie &trie,
UInt32 max_num_keys,
double num_nodes_per_key,
double average_key_length) {
- GRN_DAT_PARAM_ERROR_IF((file_size != 0) && (max_num_keys != 0));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (file_size != 0) && (max_num_keys != 0));
if (num_nodes_per_key == 0.0) {
if (trie.num_keys() == 0) {
@@ -70,7 +70,7 @@ void Trie::create(const Trie &trie,
num_nodes_per_key = 1.0 * trie.num_nodes() / trie.num_keys();
}
}
- GRN_DAT_PARAM_ERROR_IF(num_nodes_per_key < 1.0);
+ GRN_DAT_THROW_IF(PARAM_ERROR, num_nodes_per_key < 1.0);
if (average_key_length == 0.0) {
if (trie.num_keys() == 0) {
@@ -79,17 +79,17 @@ void Trie::create(const Trie &trie,
average_key_length = 1.0 * trie.total_key_length() / trie.num_keys();
}
}
- GRN_DAT_PARAM_ERROR_IF(average_key_length < 1.0);
- GRN_DAT_PARAM_ERROR_IF(average_key_length > MAX_KEY_LENGTH);
+ GRN_DAT_THROW_IF(PARAM_ERROR, average_key_length < 1.0);
+ GRN_DAT_THROW_IF(PARAM_ERROR, average_key_length > MAX_KEY_LENGTH);
if (max_num_keys == 0) {
if (file_size == 0) {
file_size = trie.file_size();
}
- GRN_DAT_PARAM_ERROR_IF(file_size < trie.virtual_size());
+ GRN_DAT_THROW_IF(PARAM_ERROR, file_size < trie.virtual_size());
} else {
- GRN_DAT_PARAM_ERROR_IF(max_num_keys < trie.num_keys());
- GRN_DAT_PARAM_ERROR_IF(max_num_keys > MAX_KEY_ID);
+ GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys < trie.num_keys());
+ GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys > MAX_KEY_ID);
}
Trie new_trie;
@@ -102,7 +102,7 @@ void Trie::create(const Trie &trie,
}
void Trie::open(const char *file_name) {
- GRN_DAT_PARAM_ERROR_IF(file_name == NULL);
+ GRN_DAT_THROW_IF(PARAM_ERROR, file_name == NULL);
Trie new_trie;
new_trie.open_file(file_name);
@@ -133,8 +133,8 @@ void Trie::create_file(const char *file_name,
UInt32 max_num_keys,
double num_nodes_per_key,
double average_key_length) {
- GRN_DAT_PARAM_ERROR_IF((file_size == 0) && (max_num_keys == 0));
- GRN_DAT_PARAM_ERROR_IF((file_size != 0) && (max_num_keys != 0));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (file_size == 0) && (max_num_keys == 0));
+ GRN_DAT_THROW_IF(PARAM_ERROR, (file_size != 0) && (max_num_keys != 0));
UInt32 max_num_blocks;
UInt32 key_buf_size;
@@ -143,22 +143,22 @@ void Trie::create_file(const char *file_name,
const double num_bytes_per_key = (sizeof(Node) * num_nodes_per_key)
+ (1.0 * sizeof(Block) / BLOCK_SIZE * num_nodes_per_key)
+ sizeof(KeyInfo) + average_key_length;
- GRN_DAT_PARAM_ERROR_IF((avail / num_bytes_per_key) > MAX_NUM_KEYS);
+ GRN_DAT_THROW_IF(PARAM_ERROR, (avail / num_bytes_per_key) > MAX_NUM_KEYS);
max_num_keys = (UInt32)(avail / num_bytes_per_key);
- GRN_DAT_PARAM_ERROR_IF(max_num_keys == 0);
+ GRN_DAT_THROW_IF(PARAM_ERROR, max_num_keys == 0);
}
{
const double max_num_nodes = num_nodes_per_key * max_num_keys;
- GRN_DAT_PARAM_ERROR_IF(max_num_nodes > MAX_NUM_NODES);
+ GRN_DAT_THROW_IF(PARAM_ERROR, max_num_nodes > MAX_NUM_NODES);
max_num_blocks = ((UInt32)max_num_nodes + BLOCK_SIZE - 1) / BLOCK_SIZE;
- GRN_DAT_PARAM_ERROR_IF(max_num_blocks == 0);
- GRN_DAT_PARAM_ERROR_IF(max_num_blocks > MAX_NUM_BLOCKS);
+ GRN_DAT_THROW_IF(PARAM_ERROR, max_num_blocks == 0);
+ GRN_DAT_THROW_IF(PARAM_ERROR, max_num_blocks > MAX_NUM_BLOCKS);
}
if (file_size == 0) {
const double total_key_length = average_key_length * max_num_keys;
- GRN_DAT_PARAM_ERROR_IF(total_key_length > MAX_KEY_BUF_SIZE);
+ GRN_DAT_THROW_IF(PARAM_ERROR, total_key_length > MAX_KEY_BUF_SIZE);
key_buf_size = (UInt32)total_key_length;
file_size = sizeof(Header)
@@ -171,8 +171,8 @@ void Trie::create_file(const char *file_name,
- (sizeof(Block) * max_num_blocks)
- (sizeof(Node) * BLOCK_SIZE * max_num_blocks)
- (sizeof(KeyInfo) * (max_num_keys + 1));
- GRN_DAT_PARAM_ERROR_IF(avail == 0);
- GRN_DAT_PARAM_ERROR_IF(avail > MAX_KEY_BUF_SIZE);
+ GRN_DAT_THROW_IF(PARAM_ERROR, avail == 0);
+ GRN_DAT_THROW_IF(PARAM_ERROR, avail > MAX_KEY_BUF_SIZE);
key_buf_size = (UInt32)avail;
}
@@ -185,7 +185,7 @@ void Trie::create_file(const char *file_name,
UInt32 max_num_keys,
UInt32 max_num_blocks,
UInt32 key_buf_size) {
- GRN_DAT_PARAM_ERROR_IF(file_size != (sizeof(Header)
+ GRN_DAT_THROW_IF(PARAM_ERROR, file_size != (sizeof(Header)
+ (sizeof(Block) * max_num_blocks)
+ (sizeof(Node) * BLOCK_SIZE * max_num_blocks)
+ (sizeof(KeyInfo) * (max_num_keys + 1))
@@ -194,10 +194,10 @@ void Trie::create_file(const char *file_name,
int fd = -1;
if ((file_name != NULL) && (file_name[0] != '\0')) {
fd = ::open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
- GRN_DAT_IO_ERROR_IF(fd == -1);
+ GRN_DAT_THROW_IF(IO_ERROR, fd == -1);
if (::ftruncate(fd, file_size) == -1) {
::close(fd);
- GRN_DAT_THROW(IOError, "::ftruncate(fd, file_size) == -1");
+ GRN_DAT_THROW(IO_ERROR, "::ftruncate(fd, file_size) == -1");
}
}
@@ -208,10 +208,10 @@ void Trie::create_file(const char *file_name,
if (fd != -1) {
::close(fd);
}
- GRN_DAT_THROW(IOError, "address == MAP_FAILED");
+ GRN_DAT_THROW(IO_ERROR, "address == MAP_FAILED");
} else if ((fd != -1) && (::close(fd) == -1)) {
::munmap(address, file_size);
- GRN_DAT_THROW(IOError, "(fd != -1) && (::close(fd) == -1)");
+ GRN_DAT_THROW(IO_ERROR, "(fd != -1) && (::close(fd) == -1)");
}
Header * const header = static_cast<Header *>(address);
@@ -229,37 +229,37 @@ void Trie::create_file(const char *file_name,
}
void Trie::open_file(const char *file_name) {
- GRN_DAT_PARAM_ERROR_IF(file_name == NULL);
+ GRN_DAT_THROW_IF(PARAM_ERROR, file_name == NULL);
const int fd = ::open(file_name, O_RDWR);
- GRN_DAT_IO_ERROR_IF(fd == -1);
+ GRN_DAT_THROW_IF(IO_ERROR, fd == -1);
void *address = ::mmap(NULL, sizeof(Header), PROT_READ, MAP_PRIVATE, fd, 0);
if (address == MAP_FAILED) {
::close(fd);
- GRN_DAT_THROW(IOError, "address == MAP_FAILED");
+ GRN_DAT_THROW(IO_ERROR, "address == MAP_FAILED");
}
const UInt64 file_size = static_cast<const Header *>(address)->file_size();
if (::munmap(address, sizeof(Header)) == -1) {
::close(fd);
- GRN_DAT_THROW(IOError, "::munmap(address, sizeof(Header)) == -1");
+ GRN_DAT_THROW(IO_ERROR, "::munmap(address, sizeof(Header)) == -1");
}
address = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (address == MAP_FAILED) {
::close(fd);
- GRN_DAT_THROW(IOError, "address == MAP_FAILED");
+ GRN_DAT_THROW(IO_ERROR, "address == MAP_FAILED");
} else if (::close(fd) == -1) {
::munmap(address, file_size);
- GRN_DAT_THROW(IOError, "::close(fd) == -1");
+ GRN_DAT_THROW(IO_ERROR, "::close(fd) == -1");
}
map_address(address);
}
void Trie::map_address(void *address) {
- GRN_DAT_PARAM_ERROR_IF(address == NULL);
+ GRN_DAT_THROW_IF(PARAM_ERROR, address == NULL);
header_ = static_cast<Header *>(address);
nodes_ = static_cast<Node *>(static_cast<void *>(header_ + 1));
@@ -463,11 +463,11 @@ bool Trie::insert_from_terminal(const UInt8 *ptr,
return false;
}
- GRN_DAT_SIZE_ERROR_IF(num_keys() >= max_num_keys());
+ GRN_DAT_THROW_IF(SIZE_ERROR, num_keys() >= max_num_keys());
key_id = max_key_id() + 1;
const UInt32 key_offset = ith_key_info(key_id).offset();
- GRN_DAT_SIZE_ERROR_IF(length > (key_buf_size() - key_offset));
+ GRN_DAT_THROW_IF(SIZE_ERROR, length > (key_buf_size() - key_offset));
for (UInt32 k = i; k < j; ++k) {
node_id = insert_node(node_id, ptr[k]);
@@ -497,11 +497,11 @@ bool Trie::insert_from_nonterminal(const UInt8 *ptr,
GRN_DAT_DEBUG_THROW_IF(ith_node(node_id).is_terminal());
GRN_DAT_DEBUG_THROW_IF(i > length);
- GRN_DAT_SIZE_ERROR_IF(num_keys() >= max_num_keys());
+ GRN_DAT_THROW_IF(SIZE_ERROR, num_keys() >= max_num_keys());
const UInt32 key_id = max_key_id() + 1;
const UInt32 key_offset = ith_key_info(key_id).offset();
- GRN_DAT_SIZE_ERROR_IF(length > (key_buf_size() - key_offset));
+ GRN_DAT_THROW_IF(SIZE_ERROR, length > (key_buf_size() - key_offset));
const UInt16 label = (i < length) ? (UInt16)ptr[i] : (UInt16)TERMINAL_LABEL;
const Base base = ith_node(node_id).base();
@@ -807,7 +807,7 @@ void Trie::reserve_node(UInt32 node_id) {
void Trie::reserve_block(UInt32 block_id) {
GRN_DAT_DEBUG_THROW_IF(block_id != num_blocks());
- GRN_DAT_SIZE_ERROR_IF(block_id >= max_num_blocks());
+ GRN_DAT_THROW_IF(SIZE_ERROR, block_id >= max_num_blocks());
header_->set_num_blocks(block_id + 1);
ith_block(block_id).set_fail_count(0);