[Groonga-commit] ranguba/rroonga at 546600c [master] Add Groonga::RequestCanceler

Back to archive index

Kouhei Sutou null+****@clear*****
Mon May 16 11:48:34 JST 2016


Kouhei Sutou	2016-05-16 11:48:34 +0900 (Mon, 16 May 2016)

  New Revision: 546600cb4186624c355ff020c9e3fb4d91cdba48
  https://github.com/ranguba/rroonga/commit/546600cb4186624c355ff020c9e3fb4d91cdba48

  Message:
    Add Groonga::RequestCanceler

  Added files:
    ext/groonga/rb-grn-request-canceler.c
    test/test-request-canceler.rb
  Modified files:
    ext/groonga/rb-grn.h
    ext/groonga/rb-groonga.c

  Added: ext/groonga/rb-grn-request-canceler.c (+164 -0) 100644
===================================================================
--- /dev/null
+++ ext/groonga/rb-grn-request-canceler.c    2016-05-16 11:48:34 +0900 (3e6342c)
@@ -0,0 +1,164 @@
+/* -*- 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"
+
+/*
+ * Document-module: Groonga::RequestCanceler
+ *
+ * This module provides API for canceling requests.
+ */
+
+VALUE rb_mGrnRequestCanceler;
+
+/*
+ * Registers a request. The request can be canceled by
+ * {Groonga::RequestCanceler.cancel}.
+ *
+ * @example Register a request
+ *   request_id = "request-29"
+ *   Groonga::RequestCanceler.register(request_id)
+ *
+ * @overload register(request_id)
+ *   @param request_id [String] The ID of request to be registered.
+ *   @return [void]
+ *
+ * @since 6.0.2
+ */
+static VALUE
+rb_grn_request_canceler_s_register (int argc, VALUE *argv, VALUE module)
+{
+    VALUE rb_request_id;
+    VALUE rb_options;
+    VALUE rb_context;
+    const char *request_id;
+    unsigned int request_id_size;
+    grn_ctx *context;
+
+    rb_scan_args(argc, argv, "11", &rb_request_id, &rb_options);
+    rb_grn_scan_options(rb_options,
+                        "context", &rb_context,
+                        NULL);
+    context = rb_grn_context_ensure(&rb_context);
+
+    request_id = StringValuePtr(rb_request_id);
+    request_id_size = RSTRING_LEN(rb_request_id);
+    grn_request_canceler_register(context, request_id, request_id_size);
+
+    return Qnil;
+}
+
+/*
+ * Unregisters the specified request.
+ *
+ * @example Unregister a request by ID
+ *   request_id = "request-29"
+ *   Groonga::RequestCanceler.unregister(request_id)
+ *
+ * @overload unregister(request_id)
+ *   @param request_id [String] The ID of request to be unregistered.
+ *   @return [void]
+ *
+ * @since 6.0.2
+ */
+static VALUE
+rb_grn_request_canceler_s_unregister (int argc, VALUE *argv, VALUE module)
+{
+    VALUE rb_request_id;
+    VALUE rb_options;
+    VALUE rb_context;
+    const char *request_id;
+    unsigned int request_id_size;
+    grn_ctx *context;
+
+    rb_scan_args(argc, argv, "11", &rb_request_id, &rb_options);
+    rb_grn_scan_options(rb_options,
+                        "context", &rb_context,
+                        NULL);
+    context = rb_grn_context_ensure(&rb_context);
+
+    request_id = StringValuePtr(rb_request_id);
+    request_id_size = RSTRING_LEN(rb_request_id);
+    grn_request_canceler_unregister(context, request_id, request_id_size);
+
+    return Qnil;
+}
+
+/*
+ * Cancels the specified request.
+ *
+ * @example Cancels a request by ID
+ *   request_id = "request-29"
+ *   Groonga::RequestCanceler.cancel(request_id)
+ *
+ * @overload cancel(request_id)
+ *   @param request_id [String] The ID of request to be canceled.
+ *   @return [Boolean] `true` if the request is canceled, `false` otherwise.
+ *
+ * @since 6.0.2
+ */
+static VALUE
+rb_grn_request_canceler_s_cancel (VALUE module, VALUE rb_request_id)
+{
+    const char *request_id;
+    unsigned int request_id_size;
+    grn_bool canceled;
+
+    request_id = StringValuePtr(rb_request_id);
+    request_id_size = RSTRING_LEN(rb_request_id);
+    canceled = grn_request_canceler_cancel(request_id, request_id_size);
+
+    return CBOOL2RVAL(canceled);
+}
+
+/*
+ * Cancels all running requests.
+ *
+ * @example Cancels all requests
+ *   Groonga::RequestCanceler.cancel_all
+ *
+ * @overload cancel_all
+ *   @return [Boolean] `true` if one or more requests are canceled,
+ *     `false` otherwise.
+ *
+ * @since 6.0.2
+ */
+static VALUE
+rb_grn_request_canceler_s_cancel_all (VALUE module)
+{
+    grn_bool canceled;
+
+    canceled = grn_request_canceler_cancel_all();
+
+    return CBOOL2RVAL(canceled);
+}
+
+void
+rb_grn_init_request_canceler (VALUE mGrn)
+{
+    rb_mGrnRequestCanceler = rb_define_module_under(mGrn, "RequestCanceler");
+
+    rb_define_singleton_method(rb_mGrnRequestCanceler, "register",
+                               rb_grn_request_canceler_s_register, -1);
+    rb_define_singleton_method(rb_mGrnRequestCanceler, "unregister",
+                               rb_grn_request_canceler_s_unregister, -1);
+    rb_define_singleton_method(rb_mGrnRequestCanceler, "cancel",
+                               rb_grn_request_canceler_s_cancel, 1);
+    rb_define_singleton_method(rb_mGrnRequestCanceler, "cancel_all",
+                               rb_grn_request_canceler_s_cancel_all, 0);
+}

  Modified: ext/groonga/rb-grn.h (+2 -0)
===================================================================
--- ext/groonga/rb-grn.h    2016-05-16 10:58:22 +0900 (0f5c578)
+++ ext/groonga/rb-grn.h    2016-05-16 11:48:34 +0900 (7fd5e5b)
@@ -300,6 +300,7 @@ RB_GRN_VAR VALUE rb_cGrnColumnExpressionBuilder;
 RB_GRN_VAR VALUE rb_cGrnPlugin;
 RB_GRN_VAR VALUE rb_cGrnNormalizer;
 RB_GRN_VAR VALUE rb_cGrnIndex;
+RB_GRN_VAR VALUE rb_mGrnRequestCanceler;
 
 void           rb_grn_init_utils                    (VALUE mGrn);
 void           rb_grn_init_exception                (VALUE mGrn);
@@ -355,6 +356,7 @@ 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);
+void           rb_grn_init_request_canceler         (VALUE mGrn);
 
 VALUE          rb_grn_rc_to_exception               (grn_rc rc);
 const char    *rb_grn_rc_to_message                 (grn_rc rc);

  Modified: ext/groonga/rb-groonga.c (+1 -0)
===================================================================
--- ext/groonga/rb-groonga.c    2016-05-16 10:58:22 +0900 (1d99050)
+++ ext/groonga/rb-groonga.c    2016-05-16 11:48:34 +0900 (dffa45d)
@@ -249,4 +249,5 @@ Init_groonga (void)
     rb_grn_init_thread(mGrn);
     rb_grn_init_config(mGrn);
     rb_grn_init_index(mGrn);
+    rb_grn_init_request_canceler(mGrn);
 }

  Added: test/test-request-canceler.rb (+46 -0) 100644
===================================================================
--- /dev/null
+++ test/test-request-canceler.rb    2016-05-16 11:48:34 +0900 (db87650)
@@ -0,0 +1,46 @@
+# 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
+
+class RequestCancelerTest < Test::Unit::TestCase
+  include GroongaTestUtils
+
+  def setup
+    @request_id = "request-29"
+  end
+
+  def teardown
+    Groonga::RequestCanceler.unregister(@request_id)
+  end
+
+  def test_cancel
+    assert do
+      not Groonga::RequestCanceler.cancel(@request_id)
+    end
+    Groonga::RequestCanceler.register(@request_id)
+    assert do
+      Groonga::RequestCanceler.cancel(@request_id)
+    end
+  end
+
+  def test_cancel_all
+    assert do
+      not Groonga::RequestCanceler.cancel_all
+    end
+    Groonga::RequestCanceler.register(@request_id)
+    assert do
+      Groonga::RequestCanceler.cancel_all
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
下載 



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