[groonga-commit:635] groonga [groonga (trunk) r672] * implement Groonga::Object#append and Groonga::Object#prepend.

null at cozmixng.org null at cozmixng.org
Wed Sep 23 19:59:06 EDT 2009


retro	2009-09-24 08:59:05 +0900 (Thu, 24 Sep 2009)

  New Revision: 672

  Log:
    * implement Groonga::Object#append and Groonga::Object#prepend.
  Modified files:
    groonga/trunk/ext/rb-grn-object.c
    groonga/trunk/ext/rb-grn-utils.c
    groonga/trunk/lib/groonga/record.rb
    groonga/trunk/test/test-variable-size-column.rb


  Modified: groonga/trunk/ext/rb-grn-object.c (+16 -1)
===================================================================
--- groonga/trunk/ext/rb-grn-object.c	2009-09-22 19:31:32 +09:00 (rev 671)
+++ groonga/trunk/ext/rb-grn-object.c	2009-09-24 08:59:05 -15:00 (rev 672)
@@ -1052,7 +1052,7 @@
  * call-seq:
  *   object.append(id, value)
  *
- * _object_の_id_に対応する値に_value_を追加する。
+ * _object_の_id_に対応する値の最後に_value_を追加する。
  */
 static VALUE
 rb_grn_object_append_value (VALUE self, VALUE rb_id, VALUE rb_value)
@@ -1060,7 +1060,21 @@
     return rb_grn_object_set(self, rb_id, rb_value, GRN_OBJ_APPEND);
 }
 
+/*
+ * Document-method: prepend
+ *
+ * call-seq:
+ *   object.prepend(id, value)
+ *
+ * _object_の_id_に対応する値の最初に_value_を追加する。
+ */
 static VALUE
+rb_grn_object_prepend_value (VALUE self, VALUE rb_id, VALUE rb_value)
+{
+    return rb_grn_object_set(self, rb_id, rb_value, GRN_OBJ_PREPEND);
+}
+
+static VALUE
 rb_grn_object_remove (VALUE self)
 {
     RbGrnObject *rb_grn_object;
@@ -1101,6 +1115,7 @@
     rb_define_method(rb_cGrnObject, "[]", rb_grn_object_array_reference, 1);
     rb_define_method(rb_cGrnObject, "[]=", rb_grn_object_array_set, 2);
     rb_define_method(rb_cGrnObject, "append", rb_grn_object_append_value, 2);
+    rb_define_method(rb_cGrnObject, "prepend", rb_grn_object_prepend_value, 2);
 
     rb_define_method(rb_cGrnObject, "remove", rb_grn_object_remove, 0);
 }

  Modified: groonga/trunk/test/test-variable-size-column.rb (+28 -0)
===================================================================
--- groonga/trunk/test/test-variable-size-column.rb	2009-09-22 19:31:32 +09:00 (rev 671)
+++ groonga/trunk/test/test-variable-size-column.rb	2009-09-24 08:59:05 -15:00 (rev 672)
@@ -20,6 +20,7 @@
     setup_database
 
     setup_users_table
+    setup_users
   end
 
   def setup_users_table
@@ -30,8 +31,19 @@
     @users_name_column_path = @columns_dir + "name"
     @name = @users.define_column("name", "<shorttext>",
                                  :path => @users_name_column_path.to_s)
+
+    @users_friends_column_path = @columns_dir + "friends"
+    @friends = @users.define_column("friends", @users,
+                                    :type => :vector,
+                                    :path => @users_friends_column_path.to_s)
   end
 
+  def setup_users
+    @morita = @users.add(:name => "mori daijiro")
+    @gunyara_kun = @users.add(:name => "Tasuku SUENAGA")
+    @yu = @users.add(:name => "Yutaro Shimamura")
+  end
+
   def test_inspect
     assert_equal("#<Groonga::VariableSizeColumn " +
                  "id: <#{@name.id}>, " +
@@ -51,4 +63,20 @@
   def test_table
     assert_equal(@users, @name.table)
   end
+
+  def test_vector_append
+    assert_equal([], @morita["friends"])
+    @morita.append("friends", @yu)
+    assert_equal([@yu], @morita["friends"])
+    @morita.append("friends", @gunyara_kun)
+    assert_equal([@yu, @gunyara_kun], @morita["friends"])
+  end
+
+  def test_vector_prepend
+    assert_equal([], @morita["friends"])
+    @morita.prepend("friends", @yu)
+    assert_equal([@yu], @morita["friends"])
+    @morita.prepend("friends", @gunyara_kun)
+    assert_equal([@gunyara_kun, @yu], @morita["friends"])
+  end
 end

  Modified: groonga/trunk/lib/groonga/record.rb (+5 -1)
===================================================================
--- groonga/trunk/lib/groonga/record.rb	2009-09-22 19:31:32 +09:00 (rev 671)
+++ groonga/trunk/lib/groonga/record.rb	2009-09-24 08:59:05 -15:00 (rev 672)
@@ -45,6 +45,10 @@
       column(column_name).append(@id, value)
     end
 
+    def prepend(column_name, value)
+      column(column_name).prepend(@id, value)
+    end
+
     def have_column?(name)
       column(name).is_a?(Groonga::Column)
     rescue Groonga::InvalidArgument
@@ -92,7 +96,7 @@
       table_name = @table.name
       columns.each do |column|
         next if column.is_a?(Groonga::IndexColumn)
-        attributes[column.name[(table_name.size + 1)..-1]] = column[@id]
+        attributes[column.local_name] = column[@id]
       end
       attributes
     end

  Modified: groonga/trunk/ext/rb-grn-utils.c (+19 -0)
===================================================================
--- groonga/trunk/ext/rb-grn-utils.c	2009-09-22 19:31:32 +09:00 (rev 671)
+++ groonga/trunk/ext/rb-grn-utils.c	2009-09-24 08:59:05 -15:00 (rev 672)
@@ -521,6 +521,25 @@
 	    value->header.domain = grn_obj_id(context, range);
 	return GRNBULK2RVAL(context, value, related_object);
 	break;
+      case GRN_UVECTOR:
+	{
+	    VALUE rb_value, rb_range = Qnil;
+	    grn_id *uvector, *uvector_end;
+
+	    rb_value = rb_ary_new();
+	    if (range)
+		rb_range = GRNTABLE2RVAL(context, range, RB_GRN_FALSE);
+	    uvector = (grn_id *)GRN_BULK_HEAD(value);
+	    uvector_end = (grn_id *)GRN_BULK_CURR(value);
+	    for (; uvector < uvector_end; uvector++) {
+		VALUE record = Qnil;
+		if (*uvector != GRN_ID_NIL)
+		    record = rb_grn_record_new(rb_range, *uvector, Qnil);
+		rb_ary_push(rb_value, record);
+	    }
+	    return rb_value;
+	}
+	break;
       default:
 	rb_raise(rb_eGrnError,
 		 "unsupported value type: 0x%0x: %s",



More information about the groonga-commit mailing list