null+****@clear*****
null+****@clear*****
2011年 11月 4日 (金) 10:08:49 JST
Susumu Yata 2011-11-04 01:08:49 +0000 (Fri, 04 Nov 2011)
New Revision: 3e698069aa51d42f4f3fd84a828de0dae6020c22
Log:
implement grn_dat_update(), grn_dat_update_by_id(), and grn_dat_cursor_delete().
Modified files:
lib/dat.cpp
lib/dat.h
lib/dat/trie.cpp
lib/dat/trie.hpp
Modified: lib/dat.cpp (+57 -18)
===================================================================
--- lib/dat.cpp 2011-11-04 00:47:17 +0000 (d093f68)
+++ lib/dat.cpp 2011-11-04 01:08:49 +0000 (9a707f5)
@@ -399,18 +399,48 @@ grn_dat_delete(grn_ctx *ctx, grn_dat *dat, const void *key, unsigned int key_siz
return GRN_SUCCESS;
}
-//grn_rc
-//grn_dat_update_by_id(grn_ctx *ctx, grn_dat *dat, grn_id id,
-// const void *key, unsigned int key_size)
-//{
-//}
-
-//grn_rc
-//grn_dat_update(grn_ctx *ctx, grn_dat *dat,
-// const void *src_key, unsigned int src_key_size,
-// const void *dest_key, unsigned int dest_key_size)
-//{
-//}
+grn_rc
+grn_dat_update_by_id(grn_ctx *ctx, grn_dat *dat, grn_id id,
+ const void *key, unsigned int key_size)
+{
+ if (!grn_dat_open_trie_if_needed(ctx, dat)) {
+ return ctx->rc;
+ }
+#ifndef WIN32
+ try {
+ grn::dat::Trie * const trie = static_cast<grn::dat::Trie *>(dat->trie);
+ if (!trie->update(id, key, key_size)) {
+ return GRN_INVALID_ARGUMENT;
+ }
+ } catch (...) {
+ // ERR
+ return GRN_INVALID_ARGUMENT;
+ }
+#endif
+ return GRN_SUCCESS;
+}
+
+grn_rc
+grn_dat_update(grn_ctx *ctx, grn_dat *dat,
+ const void *src_key, unsigned int src_key_size,
+ const void *dest_key, unsigned int dest_key_size)
+{
+ if (!grn_dat_open_trie_if_needed(ctx, dat)) {
+ return ctx->rc;
+ }
+#ifndef WIN32
+ try {
+ grn::dat::Trie * const trie = static_cast<grn::dat::Trie *>(dat->trie);
+ if (!trie->update(src_key, src_key_size, dest_key, dest_key_size)) {
+ return GRN_INVALID_ARGUMENT;
+ }
+ } catch (...) {
+ // ERR
+ return GRN_INVALID_ARGUMENT;
+ }
+#endif
+ return GRN_SUCCESS;
+}
unsigned int
grn_dat_size(grn_ctx *ctx, grn_dat *dat)
@@ -550,12 +580,21 @@ grn_dat_cursor_get_key(grn_ctx *ctx, grn_dat_cursor *c, void **key)
return 0;
}
-//grn_rc
-//grn_dat_cursor_delete(grn_ctx *ctx, grn_dat_cursor *c,
-// grn_table_delete_optarg *optarg)
-//{
-// return GRN_SUCCESS;
-//}
+grn_rc
+grn_dat_cursor_delete(grn_ctx *ctx, grn_dat_cursor *c,
+ grn_table_delete_optarg *optarg)
+{
+ if (!c || !c->cursor) {
+ 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;
+ }
+#endif
+ return GRN_INVALID_ARGUMENT;
+}
grn_id
grn_dat_curr_id(grn_ctx *ctx, grn_dat *dat)
Modified: lib/dat.h (+7 -7)
===================================================================
--- lib/dat.h 2011-11-04 00:47:17 +0000 (e5ff21b)
+++ lib/dat.h 2011-11-04 01:08:49 +0000 (0337a36)
@@ -78,11 +78,11 @@ GRN_API grn_rc grn_dat_delete_by_id(grn_ctx *ctx, grn_dat *dat, grn_id id,
GRN_API grn_rc grn_dat_delete(grn_ctx *ctx, grn_dat *dat, const void *key, unsigned int key_size,
grn_table_delete_optarg *optarg);
-//GRN_API grn_rc grn_dat_update_by_id(grn_ctx *ctx, grn_dat *dat, grn_id id,
-// const void *key, unsigned int key_size);
-//GRN_API grn_rc grn_dat_update(grn_ctx *ctx, grn_dat *dat,
-// const void *src_key, unsigned int src_key_size,
-// const void *dest_key, unsigned int dest_key_size);
+GRN_API grn_rc grn_dat_update_by_id(grn_ctx *ctx, grn_dat *dat, grn_id id,
+ const void *key, unsigned int key_size);
+GRN_API grn_rc grn_dat_update(grn_ctx *ctx, grn_dat *dat,
+ const void *src_key, unsigned int src_key_size,
+ const void *dest_key, unsigned int dest_key_size);
GRN_API unsigned int grn_dat_size(grn_ctx *ctx, grn_dat *dat);
@@ -94,8 +94,8 @@ 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 grn_rc grn_dat_cursor_delete(grn_ctx *ctx, grn_dat_cursor *c,
-// grn_table_delete_optarg *optarg);
+GRN_API grn_rc grn_dat_cursor_delete(grn_ctx *ctx, grn_dat_cursor *c,
+ grn_table_delete_optarg *optarg);
grn_id grn_dat_curr_id(grn_ctx *ctx, grn_dat *dat);
Modified: lib/dat/trie.cpp (+4 -7)
===================================================================
--- lib/dat/trie.cpp 2011-11-04 00:47:17 +0000 (b592346)
+++ lib/dat/trie.cpp 2011-11-04 01:08:49 +0000 (a7ee1f5)
@@ -466,11 +466,11 @@ bool Trie::insert_linker(const UInt8 *ptr, UInt32 length,
}
}
-bool Trie::update_key(UInt32 key_id, const UInt8 *ptr, UInt32 length,
+bool Trie::update_key(const Key &key, const UInt8 *ptr, UInt32 length,
UInt32 *key_pos) {
GRN_DAT_DEBUG_THROW_IF((ptr == NULL) && (length != 0));
- if (!ith_entry(key_id).is_valid()) {
+ if (!key.is_valid()) {
return false;
}
@@ -485,12 +485,9 @@ bool Trie::update_key(UInt32 key_id, const UInt8 *ptr, UInt32 length,
return false;
}
- const Key &key = ith_key(key_id);
- const UInt32 new_key_pos = append_key(ptr, length, key_id);
-
+ const UInt32 new_key_pos = append_key(ptr, length, key.id());
header_->set_total_key_length(total_key_length() + length - key.length());
-
- ith_entry(key_id).set_key_pos(new_key_pos);
+ ith_entry(key.id()).set_key_pos(new_key_pos);
ith_node(node_id).set_key_pos(new_key_pos);
if (key_pos != NULL) {
*key_pos = new_key_pos;
Modified: lib/dat/trie.hpp (+17 -5)
===================================================================
--- lib/dat/trie.hpp 2011-11-04 00:47:17 +0000 (c1b1962)
+++ lib/dat/trie.hpp 2011-11-04 01:08:49 +0000 (89a0a55)
@@ -73,9 +73,6 @@ class Trie {
return search_key(static_cast<const UInt8 *>(ptr), length, key_pos);
}
- bool remove(const void *ptr, UInt32 length) {
- return remove_key(static_cast<const UInt8 *>(ptr), length);
- }
bool remove(UInt32 key_id) {
const Key &key = ith_key(key_id);
if (key.id() == INVALID_KEY_ID) {
@@ -83,6 +80,9 @@ class Trie {
}
return remove(key.ptr(), key.length());
}
+ bool remove(const void *ptr, UInt32 length) {
+ return remove_key(static_cast<const UInt8 *>(ptr), length);
+ }
bool insert(const void *ptr, UInt32 length, UInt32 *key_pos = NULL) {
return insert_key(static_cast<const UInt8 *>(ptr), length, key_pos);
@@ -90,7 +90,19 @@ class Trie {
bool update(UInt32 key_id, const void *ptr, UInt32 length,
UInt32 *key_pos = NULL) {
- return update_key(key_id, static_cast<const UInt8 *>(ptr), length, key_pos);
+ return update_key(ith_key(key_id), static_cast<const UInt8 *>(ptr),
+ length, key_pos);
+ }
+ bool update(const void *src_ptr, UInt32 src_length,
+ const void *dest_ptr, UInt32 dest_length,
+ UInt32 *key_pos = NULL) {
+ UInt32 src_key_pos;
+ if (!search(src_ptr, src_length, &src_key_pos)) {
+ return false;
+ }
+ const Key &src_key = get_key(src_key_pos);
+ return update_key(src_key, static_cast<const UInt8 *>(dest_ptr),
+ dest_length, key_pos);
}
const Node &ith_node(UInt32 i) const {
@@ -200,7 +212,7 @@ class Trie {
bool insert_linker(const UInt8 *ptr, UInt32 length,
UInt32 &node_id, UInt32 query_pos);
- bool update_key(UInt32 key_id, const UInt8 *ptr, UInt32 length,
+ bool update_key(const Key &key, const UInt8 *ptr, UInt32 length,
UInt32 *key_pos);
UInt32 insert_node(UInt32 node_id, UInt16 label);