[Groonga-commit] ranguba/rroonga at 16e0243 [master] Add Groonga::DoubleArrayTrie#update

Zurück zum Archiv-Index

Masafumi Yokoyama null+****@clear*****
Sun Sep 28 15:56:39 JST 2014


Masafumi Yokoyama	2014-09-28 15:56:39 +0900 (Sun, 28 Sep 2014)

  New Revision: 16e02435391be1072ebcc1cd16b09ac58d619b5b
  https://github.com/ranguba/rroonga/commit/16e02435391be1072ebcc1cd16b09ac58d619b5b

  Merged 883cd11: Merge pull request #24 from myokoym/bind-grn-table-update

  Message:
    Add Groonga::DoubleArrayTrie#update
    
    This is a low level API for `grn_table_update_by_id()` and
    `grn_table_update()`.
    
    [GitHub #22] Bind `grn_table_update_by_id()` and `grn_table_update()`
    
    TODO: add an easy-to-use API (e.g.: Groonga::Record#rename).

  Modified files:
    ext/groonga/rb-grn-double-array-trie.c
    test/test-double-array-trie.rb

  Modified: ext/groonga/rb-grn-double-array-trie.c (+60 -1)
===================================================================
--- ext/groonga/rb-grn-double-array-trie.c    2014-09-27 17:57:23 +0900 (c670f9d)
+++ ext/groonga/rb-grn-double-array-trie.c    2014-09-28 15:56:39 +0900 (2d6ffc0)
@@ -1,7 +1,7 @@
 /* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* vim: set sts=4 sw=4 ts=8 noet: */
 /*
-  Copyright (C) 2011  Kouhei Sutou <kou �� clear-code.com>
+  Copyright (C) 2011-2014  Kouhei Sutou <kou �� clear-code.com>
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -495,6 +495,62 @@ rb_grn_double_array_trie_open_prefix_cursor (int argc, VALUE *argv, VALUE self)
         return rb_cursor;
 }
 
+VALUE
+rb_grn_double_array_trie_update_by_id (VALUE self, VALUE rb_id, VALUE rb_new_key)
+{
+    grn_ctx *context;
+    grn_obj *table;
+    grn_id id, domain_id;
+    grn_obj *new_key, *domain;
+    grn_rc rc;
+
+    rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
+                                         &new_key, &domain_id, &domain,
+                                         NULL, NULL, NULL,
+                                         NULL);
+
+    id = NUM2UINT(rb_id);
+    RVAL2GRNKEY(rb_new_key, context, new_key, domain_id, domain, self);
+    rc = grn_table_update_by_id(context, table, id,
+                                GRN_BULK_HEAD(new_key), GRN_BULK_VSIZE(new_key));
+    rb_grn_rc_check(rc, self);
+
+    return Qnil;
+}
+
+/*
+ * Renames key of a record.
+ *
+ * @overload update(current_key, new_key)
+ * @overload update(id, new_key, :id=>true)
+ *
+ * @since 4.0.5
+ */
+static VALUE
+rb_grn_double_array_trie_update (int argc, VALUE *argv, VALUE self)
+{
+    grn_id id;
+    VALUE rb_current_key_or_id, rb_new_key, rb_options;
+    VALUE rb_id;
+
+    rb_scan_args(argc, argv, "21",
+                 &rb_current_key_or_id, &rb_new_key, &rb_options);
+    if (!NIL_P(rb_options)) {
+        VALUE rb_option_id;
+        rb_grn_scan_options(rb_options,
+                            "id", &rb_option_id,
+                            NULL);
+        if (RVAL2CBOOL(rb_option_id)) {
+            rb_id = rb_current_key_or_id;
+            return rb_grn_double_array_trie_update_by_id(self, rb_id, rb_new_key);
+        }
+    }
+
+    id = rb_grn_table_key_support_get(self, rb_current_key_or_id);
+    rb_id = UINT2NUM(id);
+    return rb_grn_double_array_trie_update_by_id(self, rb_id, rb_new_key);
+}
+
 void
 rb_grn_init_double_array_trie (VALUE mGrn)
 {
@@ -510,4 +566,7 @@ rb_grn_init_double_array_trie (VALUE mGrn)
 
     rb_define_method(rb_cGrnDoubleArrayTrie, "open_prefix_cursor",
                      rb_grn_double_array_trie_open_prefix_cursor, -1);
+
+    rb_define_method(rb_cGrnDoubleArrayTrie, "update",
+                     rb_grn_double_array_trie_update, -1);
 }

  Modified: test/test-double-array-trie.rb (+18 -0)
===================================================================
--- test/test-double-array-trie.rb    2014-09-27 17:57:23 +0900 (977b051)
+++ test/test-double-array-trie.rb    2014-09-28 15:56:39 +0900 (90e147f)
@@ -134,6 +134,24 @@ class DoubleArrayTrieTest < Test::Unit::TestCase
     assert_equal(expected, actual)
   end
 
+  def test_update_by_key
+    users = Groonga::DoubleArrayTrie.create(:name => "Users",
+                                            :key_type => "ShortText")
+    users.add("Bob")
+
+    users.update("Bob", "Alice")
+    assert_equal(["Alice"], users.collect {|user| user.key})
+  end
+
+  def test_update_by_id
+    users = Groonga::DoubleArrayTrie.create(:name => "Users",
+                                            :key_type => "ShortText")
+    users.add("Bob")
+
+    users.update(users["Bob"].id, "Alice", :id => true)
+    assert_equal(["Alice"], users.collect {|user| user.key})
+  end
+
   def test_add_uint_key
     numbers = Groonga::DoubleArrayTrie.create(:name => "Numbers",
                                               :key_type => "UInt32")
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index