null+****@clear*****
null+****@clear*****
2011年 11月 7日 (月) 11:13:41 JST
Susumu Yata 2011-11-07 02:13:41 +0000 (Mon, 07 Nov 2011)
New Revision: 91339bb547fd22a6f2d395afd99b9bd66a5ac544
Log:
add grn::dat::Key * to grn_dat_cursor for grn_dat_cursor_get_key(). add const to the 3rd argument of grn_dat_cursor_get_key().
Modified files:
lib/dat.cpp
lib/dat.h
lib/db.c
Modified: lib/dat.cpp (+14 -10)
===================================================================
--- lib/dat.cpp 2011-11-07 01:49:54 +0000 (51ae757)
+++ lib/dat.cpp 2011-11-07 02:13:41 +0000 (bd35658)
@@ -194,6 +194,7 @@ void grn_dat_cursor_init(grn_ctx *, grn_dat_cursor *cursor) {
GRN_DB_OBJ_SET_TYPE(cursor, GRN_CURSOR_TABLE_DAT_KEY);
cursor->dat = NULL;
cursor->cursor = NULL;
+ cursor->key = &grn::dat::Key::invalid_key();
cursor->curr_rec = GRN_ID_NIL;
}
@@ -203,6 +204,7 @@ void grn_dat_cursor_fin(grn_ctx *, grn_dat_cursor *cursor) {
#endif
cursor->dat = NULL;
cursor->cursor = NULL;
+ cursor->key = &grn::dat::Key::invalid_key();
cursor->curr_rec = GRN_ID_NIL;
}
@@ -686,6 +688,7 @@ grn_dat_cursor_next(grn_ctx *ctx, grn_dat_cursor *c)
try {
grn::dat::Cursor * const cursor = static_cast<grn::dat::Cursor *>(c->cursor);
const grn::dat::Key &key = cursor->next();
+ c->key = &key;
c->curr_rec = key.is_valid() ? key.id() : GRN_ID_NIL;
} catch (const grn::dat::Exception &ex) {
ERR(grn_dat_translate_error_code(ex.code()),
@@ -706,18 +709,13 @@ grn_dat_cursor_close(grn_ctx *ctx, grn_dat_cursor *c)
}
int
-grn_dat_cursor_get_key(grn_ctx *ctx, grn_dat_cursor *c, void **key)
+grn_dat_cursor_get_key(grn_ctx *ctx, grn_dat_cursor *c, const void **key)
{
- // Hmm... grn_dat_cursor should maintain the latest key?
- // If not, this function returns 0 when it is deleted after the last next().
- // Also, the key must not be modified.
if (!c || !c->cursor) {
return 0;
}
#ifdef WIN32
- const grn::dat::Trie * const trie =
- static_cast<const grn::dat::Trie *>(c->cursor->dat->trie);
- const grn::dat::Key &key = trie->ith_key(c->curr_rec);
+ const grn::dat::Key &key = static_cast<const grn::dat::Key *>(c->key);
if (key.is_valid()) {
*key = key.ptr();
return (int)key.length();
@@ -734,9 +732,15 @@ grn_dat_cursor_delete(grn_ctx *ctx, grn_dat_cursor *c,
return GRN_INVALID_ARGUMENT;
}
#ifdef WIN32
- grn::dat::Trie * const trie = static_cast<const grn::dat::Trie *>(c->cursor->dat->trie);
- if (trie->remove(c->curr_rec)) {
- return GRN_SUCCESS;
+ try {
+ grn::dat::Trie * const trie = static_cast<const grn::dat::Trie *>(c->cursor->dat->trie);
+ if (trie->remove(c->curr_rec)) {
+ return GRN_SUCCESS;
+ }
+ } catch (const grn::dat::Exception &ex) {
+ ERR(grn_dat_translate_error_code(ex.code()),
+ const_cast<char *>("grn::dat::Trie::remove failed"));
+ return GRN_INVALID_ARGUMENT;
}
#endif
return GRN_INVALID_ARGUMENT;
Modified: lib/dat.h (+2 -1)
===================================================================
--- lib/dat.h 2011-11-07 01:49:54 +0000 (0337a36)
+++ lib/dat.h 2011-11-07 02:13:41 +0000 (0652180)
@@ -53,6 +53,7 @@ struct _grn_dat_cursor {
grn_db_obj obj;
grn_dat *dat;
void *cursor;
+ const void *key;
grn_id curr_rec;
};
@@ -93,7 +94,7 @@ GRN_API grn_dat_cursor *grn_dat_cursor_open(grn_ctx *ctx, grn_dat *dat,
GRN_API grn_id grn_dat_cursor_next(grn_ctx *ctx, grn_dat_cursor *c);
GRN_API void grn_dat_cursor_close(grn_ctx *ctx, grn_dat_cursor *c);
-GRN_API int grn_dat_cursor_get_key(grn_ctx *ctx, grn_dat_cursor *c, void **key);
+GRN_API int grn_dat_cursor_get_key(grn_ctx *ctx, grn_dat_cursor *c, const void **key);
GRN_API grn_rc grn_dat_cursor_delete(grn_ctx *ctx, grn_dat_cursor *c,
grn_table_delete_optarg *optarg);
Modified: lib/db.c (+1 -1)
===================================================================
--- lib/db.c 2011-11-07 01:49:54 +0000 (0ce5d1c)
+++ lib/db.c 2011-11-07 02:13:41 +0000 (1c7ca2b)
@@ -2180,7 +2180,7 @@ grn_table_cursor_get_key(grn_ctx *ctx, grn_table_cursor *tc, void **key)
len = grn_pat_cursor_get_key(ctx, (grn_pat_cursor *)tc, key);
break;
case GRN_CURSOR_TABLE_DAT_KEY :
- len = grn_dat_cursor_get_key(ctx, (grn_dat_cursor *)tc, key);
+ len = grn_dat_cursor_get_key(ctx, (grn_dat_cursor *)tc, (const void **)key);
break;
case GRN_CURSOR_TABLE_HASH_KEY :
len = grn_hash_cursor_get_key(ctx, (grn_hash_cursor *)tc, key);