[Groonga-commit] ranguba/rroonga at eae67e8 [master] Add Groonga::Column#find_indexes

Back to archive index

Kouhei Sutou null+****@clear*****
Sat Mar 5 16:49:59 JST 2016


Kouhei Sutou	2016-03-05 16:49:59 +0900 (Sat, 05 Mar 2016)

  New Revision: eae67e80785eddd931f36718759a4d3c20c1cec9
  https://github.com/ranguba/rroonga/commit/eae67e80785eddd931f36718759a4d3c20c1cec9

  Message:
    Add Groonga::Column#find_indexes
    
    It returns an array of Groonga::Index. Groonga::Index has index column
    and section.
    
    Groonga::Column#all_indexes is removed.

  Added files:
    ext/groonga/rb-grn-index.c
    lib/groonga/index.rb
  Modified files:
    ext/groonga/rb-grn-column.c
    ext/groonga/rb-grn.h
    ext/groonga/rb-groonga.c
    lib/groonga.rb
    test/test-column.rb

  Modified: ext/groonga/rb-grn-column.c (+50 -15)
===================================================================
--- ext/groonga/rb-grn-column.c    2016-03-05 16:02:31 +0900 (d84f9ad)
+++ ext/groonga/rb-grn-column.c    2016-03-05 16:49:59 +0900 (4a98ea6)
@@ -755,38 +755,72 @@ rb_grn_column_get_indexes (int argc, VALUE *argv, VALUE self)
 }
 
 /*
- * Return all indexes on `column`.
+ * Return indexes on `column`. If operator is specified, indexes that
+ * can executes the operator are only returned. Otherwise, all indexes
+ * are returned.
  *
- * @overload all_indexes
- * @return [Array<index_column>] All indexes on `column`.
+ * Index means that index column and section.
+ *
+ * @overload find_indexes(options={})
+ *   @param options [::Hash] The name and value pairs.
+ *      Omitted names are initialized as the default value.
+ *   @option options :operator (nil) The operator that should be
+ *      executable by indexes. `nil` means that all operators.
+ *   @return [Array<Groonga::Index>] Target indexes on `column`.
  *
  * @since 6.0.0
  */
 static VALUE
-rb_grn_column_get_all_indexes (VALUE self)
+rb_grn_column_find_indexes (int argc, VALUE *argv, VALUE self)
 {
+    VALUE rb_options;
+    VALUE rb_operator;
     grn_ctx *context;
     grn_obj *column;
     grn_index_datum *index_data = NULL;
     int i, n_indexes;
     VALUE rb_indexes;
 
+    rb_scan_args(argc, argv, "01", &rb_options);
+    rb_grn_scan_options(rb_options,
+                        "operator", &rb_operator,
+                        NULL);
+
     rb_grn_column_deconstruct(SELF(self), &column, &context,
                               NULL, NULL,
                               NULL, NULL, NULL);
 
-    rb_indexes = rb_ary_new();
-    n_indexes = grn_column_get_all_index_data(context, column, NULL, 0);
-    if (n_indexes == 0)
-        return rb_indexes;
+    if (NIL_P(rb_operator)) {
+        n_indexes = grn_column_get_all_index_data(context, column, NULL, 0);
+        if (n_indexes == 0)
+            return rb_ary_new();
 
-    index_data = xmalloc(sizeof(grn_index_datum) * n_indexes);
-    n_indexes = grn_column_get_all_index_data(context, column,
-                                              index_data, n_indexes);
+        index_data = xmalloc(sizeof(grn_index_datum) * n_indexes);
+        n_indexes = grn_column_get_all_index_data(context, column,
+                                                  index_data, n_indexes);
+    } else {
+        grn_operator operator;
+        operator = RVAL2GRNOPERATOR(rb_operator);
+        n_indexes = grn_column_find_index_data(context, column, operator,
+                                               NULL, 0);
+        if (n_indexes == 0)
+            return rb_ary_new();
+
+        index_data = xmalloc(sizeof(grn_index_datum) * n_indexes);
+        n_indexes = grn_column_find_index_data(context, column, operator,
+                                               index_data, n_indexes);
+    }
+
+    rb_indexes = rb_ary_new_capa(n_indexes);
     for (i = 0; i < n_indexes; i++) {
-        VALUE rb_index;
-        rb_index = GRNOBJECT2RVAL(Qnil, context, index_data[i].index, GRN_FALSE);
-        rb_ary_push(rb_indexes, rb_index);
+        VALUE rb_index_column;
+        VALUE rb_section;
+        rb_index_column = GRNOBJECT2RVAL(Qnil,
+                                         context,
+                                         index_data[i].index,
+                                         GRN_FALSE);
+        rb_section = UINT2NUM(index_data[i].section);
+        rb_ary_push(rb_indexes, rb_grn_index_new(rb_index_column, rb_section));
         grn_obj_unlink(context, index_data[i].index);
     }
     xfree(index_data);
@@ -853,7 +887,8 @@ rb_grn_init_column (VALUE mGrn)
                      rb_grn_column_with_weight_p, 0);
 
     rb_define_method(rb_cGrnColumn, "indexes", rb_grn_column_get_indexes, -1);
-    rb_define_method(rb_cGrnColumn, "all_indexes", rb_grn_column_get_all_indexes, 0);
+    rb_define_method(rb_cGrnColumn, "find_indexes",
+                     rb_grn_column_find_indexes, -1);
 
     rb_define_method(rb_cGrnColumn, "rename", rb_grn_column_rename, 1);
 

  Added: ext/groonga/rb-grn-index.c (+34 -0) 100644
===================================================================
--- /dev/null
+++ ext/groonga/rb-grn-index.c    2016-03-05 16:49:59 +0900 (41c6225)
@@ -0,0 +1,34 @@
+/* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+  Copyright (C) 2016  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
+  License version 2.1 as published by the Free Software Foundation.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "rb-grn.h"
+
+VALUE rb_cGrnIndex;
+
+VALUE
+rb_grn_index_new (VALUE rb_index_column, VALUE rb_section)
+{
+    return rb_funcall(rb_cGrnIndex, rb_intern("new"), 2,
+                      rb_index_column, rb_section);
+}
+
+void
+rb_grn_init_index (VALUE mGrn)
+{
+    rb_cGrnIndex = rb_const_get(mGrn, rb_intern("Index"));
+}

  Modified: ext/groonga/rb-grn.h (+5 -0)
===================================================================
--- ext/groonga/rb-grn.h    2016-03-05 16:02:31 +0900 (d4f1e99)
+++ ext/groonga/rb-grn.h    2016-03-05 16:49:59 +0900 (b8c6e44)
@@ -301,6 +301,7 @@ RB_GRN_VAR VALUE rb_cGrnRecordExpressionBuilder;
 RB_GRN_VAR VALUE rb_cGrnColumnExpressionBuilder;
 RB_GRN_VAR VALUE rb_cGrnPlugin;
 RB_GRN_VAR VALUE rb_cGrnNormalizer;
+RB_GRN_VAR VALUE rb_cGrnIndex;
 
 void           rb_grn_init_utils                    (VALUE mGrn);
 void           rb_grn_init_exception                (VALUE mGrn);
@@ -355,6 +356,7 @@ void           rb_grn_init_plugin                   (VALUE mGrn);
 void           rb_grn_init_normalizer               (VALUE mGrn);
 void           rb_grn_init_thread                   (VALUE mGrn);
 void           rb_grn_init_config                   (VALUE mGrn);
+void           rb_grn_init_index                    (VALUE mGrn);
 
 VALUE          rb_grn_rc_to_exception               (grn_rc rc);
 const char    *rb_grn_rc_to_message                 (grn_rc rc);
@@ -634,6 +636,9 @@ VALUE          rb_grn_column_expression_builder_new (VALUE column,
 VALUE          rb_grn_column_expression_builder_build
                                                     (VALUE self);
 
+VALUE          rb_grn_index_new                     (VALUE rb_index_column,
+                                                     VALUE rb_section);
+
 
 #define RB_GRN_INTERN(c_string)       (rb_to_symbol(rb_str_new_cstr(c_string)))
 

  Modified: ext/groonga/rb-groonga.c (+2 -1)
===================================================================
--- ext/groonga/rb-groonga.c    2016-03-05 16:02:31 +0900 (9afe8d0)
+++ ext/groonga/rb-groonga.c    2016-03-05 16:49:59 +0900 (2e9d26a)
@@ -1,6 +1,6 @@
 /* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /*
-  Copyright (C) 2009-2015  Kouhei Sutou <kou �� clear-code.com>
+  Copyright (C) 2009-2016  Kouhei Sutou <kou �� clear-code.com>
   Copyright (C) 2016  Masafumi Yokoyama <yokoyama �� clear-code.com>
 
   This library is free software; you can redistribute it and/or
@@ -219,4 +219,5 @@ Init_groonga (void)
     rb_grn_init_normalizer(mGrn);
     rb_grn_init_thread(mGrn);
     rb_grn_init_config(mGrn);
+    rb_grn_init_index(mGrn);
 }

  Modified: lib/groonga.rb (+1 -0)
===================================================================
--- lib/groonga.rb    2016-03-05 16:02:31 +0900 (37c39b0)
+++ lib/groonga.rb    2016-03-05 16:49:59 +0900 (8397535)
@@ -38,6 +38,7 @@ require "groonga/geo-point"
 require "groonga/record"
 require "groonga/expression-builder"
 require "groonga/posting"
+require "groonga/index"
 begin
   major, minor, _ = RUBY_VERSION.split(/\./)
   require "#{major}.#{minor}/groonga.so"

  Added: lib/groonga/index.rb (+32 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/index.rb    2016-03-05 16:49:59 +0900 (cdc20a7)
@@ -0,0 +1,32 @@
+# Copyright (C) 2016  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
+# License version 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+module Groonga
+  class Index
+    attr_reader :column
+    attr_reader :section
+
+    def initialize(column, section)
+      @column = column
+      @section = section
+    end
+
+    def ==(other)
+      other.is_a?(self.class) and
+        @column == other.column and
+        @section == other.section
+    end
+  end
+end

  Modified: test/test-column.rb (+62 -23)
===================================================================
--- test/test-column.rb    2016-03-05 16:02:31 +0900 (58b677e)
+++ test/test-column.rb    2016-03-05 16:49:59 +0900 (af21691)
@@ -1,4 +1,4 @@
-# Copyright (C) 2009-2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2009-2016  Kouhei Sutou <kou �� clear-code.com>
 # Copyright (C) 2016  Masafumi Yokoyama <yokoyama �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
@@ -586,35 +586,74 @@ class ColumnTest < Test::Unit::TestCase
     end
   end
 
-  class AllIndexedTest < self
-    def setup
-      super
-      Groonga::Schema.define do |schema|
-        schema.create_table("Comments") do |table|
-          table.short_text("title")
+  class FindIndexesTest < self
+    class NoOperatorTest < self
+      def setup
+        super
+        Groonga::Schema.define do |schema|
+          schema.create_table("Comments") do |table|
+            table.short_text("title")
+            table.short_text("body")
+          end
         end
+        @title = Groonga["Comments.title"]
       end
-      @title = Groonga["Comments.title"]
-    end
 
-    def test_nothing
-      assert_equal([], @title.all_indexes)
-    end
+      def test_nothing
+        assert_equal([], @title.find_indexes)
+      end
 
-    def test_one_index
-      Groonga::Schema.define do |schema|
-        schema.create_table("Terms",
-                            :type => :patricia_trie,
-                            :default_tokenizer => "TokenBigram") do |table|
-          table.index("Comments.title")
+      def test_one_index
+        Groonga::Schema.define do |schema|
+          schema.create_table("Terms",
+                              :type => :patricia_trie,
+                              :key_type => :short_text,
+                              :default_tokenizer => "TokenBigram") do |table|
+            table.index("Comments.title")
+          end
         end
+        assert_equal([
+                       Groonga::Index.new(Groonga["Terms.Comments_title"], 0),
+                     ],
+                     @title.find_indexes)
       end
-      assert_equal([Groonga["Terms.Comments_title"]],
-                   @title.all_indexes)
-    end
 
-    def test_multiple_indexes
-      # TODO
+      def test_multiple_indexes
+        Groonga::Schema.define do |schema|
+          schema.create_table("Terms",
+                              :type => :patricia_trie,
+                              :key_type => :short_text,
+                              :default_tokenizer => "TokenBigram") do |table|
+            table.index("Comments.title")
+          end
+
+          schema.create_table("Titles",
+                              :type => :hash,
+                              :key_type => :short_text) do |table|
+            table.index("Comments.title")
+          end
+        end
+        assert_equal([
+                       Groonga::Index.new(Groonga["Terms.Comments_title"], 0),
+                       Groonga::Index.new(Groonga["Titles.Comments_title"], 0),
+                     ],
+                     @title.find_indexes.sort_by {|index| index.column.name})
+      end
+
+      def test_multiple_column_index
+        Groonga::Schema.define do |schema|
+          schema.create_table("Terms",
+                              :type => :patricia_trie,
+                              :default_tokenizer => "TokenBigram") do |table|
+            table.index("Comments", "body", "title",
+                        :name => "comments")
+          end
+        end
+        assert_equal([
+                       Groonga::Index.new(Groonga["Terms.comments"], 2),
+                     ],
+                     @title.find_indexes)
+      end
     end
   end
 end
-------------- next part --------------
HTML����������������������������...
Télécharger 



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