[Groonga-commit] ranguba/rroonga at 81f9fbb [master] Add a code for supporting object literal in function argument

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Mar 6 18:25:56 JST 2016


Kouhei Sutou	2016-03-06 18:25:56 +0900 (Sun, 06 Mar 2016)

  New Revision: 81f9fbbf7c2d5e962ee8f9f251fe185964962161
  https://github.com/ranguba/rroonga/commit/81f9fbbf7c2d5e962ee8f9f251fe185964962161

  Message:
    Add a code for supporting object literal in function argument
    
    But it's disabled for now. Because it requires Groonga 6.0.1.
    
    TODO: We should enable the code when Groonga 6.0.1 is released.

  Modified files:
    ext/groonga/rb-grn-expression.c
    lib/groonga/expression-builder.rb
    test/test-expression-builder.rb

  Modified: ext/groonga/rb-grn-expression.c (+63 -3)
===================================================================
--- ext/groonga/rb-grn-expression.c    2016-03-06 12:43:26 +0900 (92d8027)
+++ ext/groonga/rb-grn-expression.c    2016-03-06 18:25:56 +0900 (f118d5d)
@@ -203,6 +203,43 @@ rb_grn_expression_define_variable (int argc, VALUE *argv, VALUE self)
     return rb_variable;
 }
 
+/* TODO: Enable when Groonga 6.0.1 is released. */
+/*
+typedef struct
+{
+    grn_ctx *context;
+    grn_hash *hash;
+    VALUE rb_hash;
+} RbGrnHashFromRubyHashData;
+
+static int
+rb_grn_hash_from_ruby_hash_body (VALUE rb_key,
+                                 VALUE rb_value,
+                                 VALUE user_data)
+{
+    RbGrnHashFromRubyHashData *data = (RbGrnHashFromRubyHashData *)user_data;
+    grn_obj *value;
+    int added;
+
+    rb_key = rb_grn_convert_to_string(rb_key);
+
+    grn_hash_add(data->context,
+                 data->hash,
+                 RSTRING_PTR(rb_key),
+                 RSTRING_LEN(rb_key),
+                 (void **)&value,
+                 &added);
+    rb_grn_context_check(data->context, data->rb_hash);
+
+    if (added) {
+        GRN_VOID_INIT(value);
+    }
+    RVAL2GRNBULK(rb_value, data->context, value);
+
+    return ST_CONTINUE;
+}
+*/
+
 /*
  * _object_ を追加し、 _n_arguments_ 個の引数を取る _operation_ を追加する。
  *
@@ -232,9 +269,32 @@ rb_grn_expression_append_object (int argc, VALUE *argv, VALUE self)
                                   NULL, NULL,
                                   NULL, NULL, NULL);
 
-    object = RVAL2GRNOBJECT(rb_object, &context);
-    grn_expr_append_obj(context, expression, object,
-                        operation, n_arguments);
+    /* TODO: Enable when Groonga 6.0.1 is released. */
+    /*
+    if (RB_TYPE_P(rb_object, RUBY_T_HASH)) {
+        RbGrnHashFromRubyHashData data;
+        data.context = context;
+        data.hash = grn_hash_create(context, NULL,
+                                    GRN_TABLE_MAX_KEY_SIZE,
+                                    sizeof(grn_obj),
+                                    GRN_OBJ_KEY_VAR_SIZE |
+                                    GRN_OBJ_TEMPORARY |
+                                    GRN_HASH_TINY);
+        grn_expr_take_obj(context, expression, (grn_obj *)(data.hash));
+        data.rb_hash = rb_object;
+        rb_hash_foreach(rb_object,
+                        rb_grn_hash_from_ruby_hash_body,
+                        (VALUE)&data);
+        grn_expr_append_obj(context, expression, (grn_obj *)(data.hash),
+                            operation, n_arguments);
+    } else {
+    */
+        object = RVAL2GRNOBJECT(rb_object, &context);
+        grn_expr_append_obj(context, expression, object,
+                            operation, n_arguments);
+    /*
+    }
+    */
     rb_grn_context_check(context, self);
     rb_ary_push(rb_iv_get(self, "@objects"), rb_object);
 

  Modified: lib/groonga/expression-builder.rb (+2 -0)
===================================================================
--- lib/groonga/expression-builder.rb    2016-03-06 12:43:26 +0900 (18928bf)
+++ lib/groonga/expression-builder.rb    2016-03-06 18:25:56 +0900 (c8aac5c)
@@ -509,6 +509,8 @@ module Groonga
           case argument
           when String, Integer, Time
             expression.append_constant(argument)
+          when ::Hash
+            expression.append_object(argument)
           else
             argument.build(expression, variable)
           end

  Modified: test/test-expression-builder.rb (+32 -0)
===================================================================
--- test/test-expression-builder.rb    2016-03-06 12:43:26 +0900 (b3b135b)
+++ test/test-expression-builder.rb    2016-03-06 18:25:56 +0900 (54a8a50)
@@ -655,6 +655,38 @@ EOC
                      result.collect(&:timestamp))
       end
     end
+
+    class HashTest < self
+      def setup_tables
+        Groonga::Schema.define do |schema|
+          schema.create_table("Tags",
+                              :type => :patricia_trie,
+                              :key_type => :short_text) do |table|
+          end
+        end
+
+        @tags = Groonga["Tags"]
+      end
+
+      def setup_data
+        @tags.add("Tom")
+        @tags.add("Tomy")
+        @tags.add("Ken")
+      end
+
+      def test_search
+        omit("TODO: Enable me when Groonga 6.0.1 is released.")
+        result =****@tags***** do |record|
+          record.key.fuzzy_search("Toym", :with_transposition => true)
+        end
+        sorted_result = result.sort(["_score", "_key"])
+        assert_equal([
+                       ["Tom", 1.0],
+                       ["Tomy", 1.0],
+                     ],
+                     sorted_result.collect {|r| [r._key, r.score]})
+      end
+    end
   end
 
   class RecordTest < self
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index