[groonga-commit:297] groonga [groonga (trunk) r306] * Hash#[] returns value and Hash#lookup returns record.

null at cozmixng.org null at cozmixng.org
Sun May 10 05:56:48 EDT 2009


retro	2009-05-10 18:56:47 +0900 (Sun, 10 May 2009)

  New Revision: 306

  Modified files:
    groonga/trunk/ext/rb-grn-table-key-support.c
    groonga/trunk/test/test-hash.rb

  Log:
    * Hash#[] returns value and Hash#lookup returns record.


  Modified: groonga/trunk/ext/rb-grn-table-key-support.c (+34 -2)
===================================================================
--- groonga/trunk/ext/rb-grn-table-key-support.c	2009-05-09 11:23:56 +09:00 (rev 305)
+++ groonga/trunk/ext/rb-grn-table-key-support.c	2009-05-10 18:56:47 +09:00 (rev 306)
@@ -248,8 +248,8 @@
     }
 }
 
-static VALUE
-rb_grn_table_key_support_array_reference_by_key (VALUE self, VALUE rb_key)
+static grn_id
+rb_grn_table_key_support_lookup_raw (VALUE self, VALUE rb_key)
 {
     grn_ctx *context;
     grn_obj *table, *key, *domain;
@@ -267,6 +267,16 @@
 			  &flags);
     rb_grn_context_check(context, self);
 
+    return id;
+}
+
+static VALUE
+rb_grn_table_key_support_lookup (VALUE self, VALUE rb_key)
+{
+    grn_id id;
+
+    id = rb_grn_table_key_support_lookup_raw(self, rb_key);
+
     if (id == GRN_ID_NIL)
 	return Qnil;
     else
@@ -274,6 +284,26 @@
 }
 
 static VALUE
+rb_grn_table_key_support_array_reference_by_key (VALUE self, VALUE rb_key)
+{
+    grn_ctx *context;
+    grn_obj *table, *value, *range;
+    grn_id id;
+
+    id = rb_grn_table_key_support_lookup_raw(self, rb_key);
+
+    if (id == GRN_ID_NIL)
+	return Qnil;
+
+    rb_grn_table_key_support_deconstruct(self, &table, &context, NULL, NULL,
+					 &value, &range);
+    grn_obj_get_value(context, table, id, value);
+    rb_grn_context_check(context, self);
+
+    return GRNVALUE2RVAL(context, value, range, self);
+}
+
+static VALUE
 rb_grn_table_key_support_array_reference (VALUE self, VALUE rb_id_or_key)
 {
     if (FIXNUM_P(rb_id_or_key)) {
@@ -414,6 +444,8 @@
     rb_define_method(rb_mGrnTableKeySupport, "delete",
 		     rb_grn_table_key_support_delete, 1);
 
+    rb_define_method(rb_mGrnTableKeySupport, "lookup",
+		     rb_grn_table_key_support_lookup, 1);
     rb_define_method(rb_mGrnTableKeySupport, "[]",
 		     rb_grn_table_key_support_array_reference, 1);
     rb_define_method(rb_mGrnTableKeySupport, "[]=",

  Modified: groonga/trunk/test/test-hash.rb (+10 -1)
===================================================================
--- groonga/trunk/test/test-hash.rb	2009-05-09 11:23:56 +09:00 (rev 305)
+++ groonga/trunk/test/test-hash.rb	2009-05-10 18:56:47 +09:00 (rev 306)
@@ -44,7 +44,16 @@
     bookmarks = Groonga::Hash.create(:name => "bookmarks",
                                      :path => bookmarks_path.to_s,
                                      :key_type => "<shorttext>")
+    bookmarks["http://google.com/"] = "groonga"
+    assert_equal("groonga", bookmarks["http://google.com/"])
+  end
+
+  def test_lookup
+    bookmarks_path = @tables_dir + "bookmarks"
+    bookmarks = Groonga::Hash.create(:name => "bookmarks",
+                                     :path => bookmarks_path.to_s,
+                                     :key_type => "<shorttext>")
     bookmark = bookmarks.add("http://google.com/")
-    assert_equal(bookmark, bookmarks["http://google.com/"])
+    assert_equal(bookmark, bookmarks.lookup("http://google.com/"))
   end
 end



More information about the groonga-commit mailing list