[Groonga-commit] ranguba/rroonga at c1d7eff [master] Extract code to create grn_table_sort_key *

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Jun 13 18:30:38 JST 2016


Kouhei Sutou	2016-06-13 18:30:38 +0900 (Mon, 13 Jun 2016)

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

  Message:
    Extract code to create grn_table_sort_key *

  Added files:
    ext/groonga/rb-grn-table-sort-keys.c
  Modified files:
    ext/groonga/rb-grn-table.c
    ext/groonga/rb-grn.h

  Added: ext/groonga/rb-grn-table-sort-keys.c (+85 -0) 100644
===================================================================
--- /dev/null
+++ ext/groonga/rb-grn-table-sort-keys.c    2016-06-13 18:30:38 +0900 (9c1d45f)
@@ -0,0 +1,85 @@
+/* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+  Copyright (C) 2009-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"
+
+void
+rb_grn_table_sort_keys_fill (grn_ctx *context,
+                             grn_table_sort_key *sort_keys,
+                             size_t n_sort_keys,
+                             VALUE rb_sort_keys,
+                             VALUE rb_table)
+{
+    int i;
+
+    for (i = 0; i < n_sort_keys; i++) {
+        VALUE rb_sort_key;
+        VALUE rb_sort_options;
+        VALUE rb_key;
+        VALUE rb_resolved_key;
+        VALUE rb_order;
+
+        rb_sort_key = RARRAY_PTR(rb_sort_keys)[i];
+        if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_key, rb_cHash))) {
+            rb_sort_options = rb_sort_key;
+        } else if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_key, rb_cArray))) {
+            rb_sort_options = rb_hash_new();
+            rb_hash_aset(rb_sort_options,
+                         RB_GRN_INTERN("key"),
+                         rb_ary_entry(rb_sort_key, 0));
+            rb_hash_aset(rb_sort_options,
+                         RB_GRN_INTERN("order"),
+                         rb_ary_entry(rb_sort_key, 1));
+        } else {
+            rb_sort_options = rb_hash_new();
+            rb_hash_aset(rb_sort_options,
+                         RB_GRN_INTERN("key"),
+                         rb_sort_key);
+        }
+        rb_grn_scan_options(rb_sort_options,
+                            "key", &rb_key,
+                            "order", &rb_order,
+                            NULL);
+        if (RVAL2CBOOL(rb_obj_is_kind_of(rb_key, rb_cString))) {
+            rb_resolved_key = rb_grn_table_get_column(rb_table, rb_key);
+        } else {
+            rb_resolved_key = rb_key;
+        }
+        sort_keys[i].key = RVAL2GRNOBJECT(rb_resolved_key, &context);
+        if (!sort_keys[i].key) {
+            rb_raise(rb_eGrnNoSuchColumn,
+                     "no such column: <%s>: <%s>",
+                     rb_grn_inspect(rb_key),
+                     rb_grn_inspect(rb_table));
+        }
+        if (NIL_P(rb_order) ||
+            rb_grn_equal_option(rb_order, "asc") ||
+            rb_grn_equal_option(rb_order, "ascending")) {
+            sort_keys[i].flags = GRN_TABLE_SORT_ASC;
+        } else if (rb_grn_equal_option(rb_order, "desc") ||
+                   rb_grn_equal_option(rb_order, "descending")) {
+            sort_keys[i].flags = GRN_TABLE_SORT_DESC;
+        } else {
+            rb_raise(rb_eArgError,
+                     "order should be one of "
+                     "[nil, :desc, :descending, :asc, :ascending]: %s",
+                     rb_grn_inspect(rb_order));
+        }
+    }
+
+}

  Modified: ext/groonga/rb-grn-table.c (+2 -51)
===================================================================
--- ext/groonga/rb-grn-table.c    2016-06-13 18:03:38 +0900 (7b37fed)
+++ ext/groonga/rb-grn-table.c    2016-06-13 18:30:38 +0900 (a64d97b)
@@ -1218,11 +1218,10 @@ rb_grn_table_sort (int argc, VALUE *argv, VALUE self)
     grn_obj *table;
     grn_obj *result;
     grn_table_sort_key *keys;
-    int i, n_keys;
+    int n_keys;
     int offset = 0, limit = -1;
     VALUE rb_keys, options;
     VALUE rb_offset, rb_limit;
-    VALUE *rb_sort_keys;
     VALUE exception;
 
     rb_grn_table_deconstruct(SELF(self), &table, &context,
@@ -1237,56 +1236,8 @@ rb_grn_table_sort (int argc, VALUE *argv, VALUE self)
                  rb_grn_inspect(rb_keys));
 
     n_keys = RARRAY_LEN(rb_keys);
-    rb_sort_keys = RARRAY_PTR(rb_keys);
     keys = ALLOCA_N(grn_table_sort_key, n_keys);
-    for (i = 0; i < n_keys; i++) {
-        VALUE rb_sort_options, rb_key, rb_resolved_key, rb_order;
-
-        if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_keys[i], rb_cHash))) {
-            rb_sort_options = rb_sort_keys[i];
-        } else if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_keys[i], rb_cArray))) {
-            rb_sort_options = rb_hash_new();
-            rb_hash_aset(rb_sort_options,
-                         RB_GRN_INTERN("key"),
-                         rb_ary_entry(rb_sort_keys[i], 0));
-            rb_hash_aset(rb_sort_options,
-                         RB_GRN_INTERN("order"),
-                         rb_ary_entry(rb_sort_keys[i], 1));
-        } else {
-            rb_sort_options = rb_hash_new();
-            rb_hash_aset(rb_sort_options,
-                         RB_GRN_INTERN("key"),
-                         rb_sort_keys[i]);
-        }
-        rb_grn_scan_options(rb_sort_options,
-                            "key", &rb_key,
-                            "order", &rb_order,
-                            NULL);
-        if (RVAL2CBOOL(rb_obj_is_kind_of(rb_key, rb_cString))) {
-            rb_resolved_key = rb_grn_table_get_column(self, rb_key);
-        } else {
-            rb_resolved_key = rb_key;
-        }
-        keys[i].key = RVAL2GRNOBJECT(rb_resolved_key, &context);
-        if (!keys[i].key) {
-            rb_raise(rb_eGrnNoSuchColumn,
-                     "no such column: <%s>: <%s>",
-                     rb_grn_inspect(rb_key), rb_grn_inspect(self));
-        }
-        if (NIL_P(rb_order) ||
-            rb_grn_equal_option(rb_order, "asc") ||
-            rb_grn_equal_option(rb_order, "ascending")) {
-            keys[i].flags = GRN_TABLE_SORT_ASC;
-        } else if (rb_grn_equal_option(rb_order, "desc") ||
-                   rb_grn_equal_option(rb_order, "descending")) {
-            keys[i].flags = GRN_TABLE_SORT_DESC;
-        } else {
-            rb_raise(rb_eArgError,
-                     "order should be one of "
-                     "[nil, :desc, :descending, :asc, :ascending]: %s",
-                     rb_grn_inspect(rb_order));
-        }
-    }
+    rb_grn_table_sort_keys_fill(context, keys, n_keys, rb_keys, self);
 
     rb_grn_scan_options(options,
                         "offset", &rb_offset,

  Modified: ext/groonga/rb-grn.h (+6 -0)
===================================================================
--- ext/groonga/rb-grn.h    2016-06-13 18:03:38 +0900 (099a802)
+++ ext/groonga/rb-grn.h    2016-06-13 18:30:38 +0900 (0199c97)
@@ -510,6 +510,12 @@ VALUE          rb_grn_table_set_column_value        (VALUE self,
 VALUE          rb_grn_table_inspect_content         (VALUE object,
                                                      VALUE inspected);
 
+void           rb_grn_table_sort_keys_fill          (grn_ctx *context,
+                                                     grn_table_sort_key *sort_keys,
+                                                     size_t n_sort_keys,
+                                                     VALUE rb_sort_keys,
+                                                     VALUE rb_table);
+
 grn_ctx       *rb_grn_table_cursor_ensure_context   (VALUE cursor,
                                                      VALUE *rb_context);
 int            rb_grn_table_cursor_order_to_flag    (VALUE rb_order);
-------------- next part --------------
HTML����������������������������...
Télécharger 



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