Kouhei Sutou
null+****@clear*****
Mon May 16 16:35:19 JST 2016
Kouhei Sutou 2016-05-16 16:35:19 +0900 (Mon, 16 May 2016) New Revision: 12e2b750ae304b492058f708d1a0fd73822b23cc https://github.com/ranguba/rroonga/commit/12e2b750ae304b492058f708d1a0fd73822b23cc Message: Add Groonga::RequestTimer Added files: ext/groonga/rb-grn-request-timer-id.c ext/groonga/rb-grn-request-timer.c test/test-request-timer.rb Modified files: ext/groonga/rb-grn.h ext/groonga/rb-groonga.c Added: ext/groonga/rb-grn-request-timer-id.c (+64 -0) 100644 =================================================================== --- /dev/null +++ ext/groonga/rb-grn-request-timer-id.c 2016-05-16 16:35:19 +0900 (def7ebb) @@ -0,0 +1,64 @@ +/* -*- 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-class: Groonga::RequestTimerID + * + * This class represents request timer ID. It's used with request timer. + */ + +VALUE rb_cGrnRequestTimerID; + +static rb_data_type_t data_type = { + "Groonga::RequestTimeID", + { + NULL, + NULL, + NULL, + }, + NULL, + NULL, + 0 +}; + +void * +rb_grn_request_timer_id_from_ruby_object (VALUE rb_id) +{ + if (NIL_P(rb_id)) { + return NULL; + } else { + void *id; + TypedData_Get_Struct(rb_id, void *, &data_type, id); + return id; + } +} + +VALUE +rb_grn_request_timer_id_to_ruby_object (void *id) +{ + return TypedData_Wrap_Struct(rb_cGrnRequestTimerID, &data_type, id); +} + +void +rb_grn_init_request_timer_id (VALUE mGrn) +{ + rb_cGrnRequestTimerID = + rb_define_class_under(mGrn, "RequestTimerID", rb_cData); +} Added: ext/groonga/rb-grn-request-timer.c (+155 -0) 100644 =================================================================== --- /dev/null +++ ext/groonga/rb-grn-request-timer.c 2016-05-16 16:35:19 +0900 (3f1d394) @@ -0,0 +1,155 @@ +/* -*- 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::RequestTimer + * + * This module provides API for canceling requests after the specified + * time. + */ + +VALUE rb_mGrnRequestTimer; + +/* + * Registers a request with timeout. If the request isn't finished + * after the specified timeout, the request is canceled. + * + * @example Register a request that will be timed out after 2.9 seconds + * request_id = "request-29" + * timeout_in_second = 2.9 + * Groonga::RequestTimer.register(request_id, timeout_in_second) + * + * @overload register(request_id, timeout=nil) + * @param request_id [String] The ID of request to be registered. + * @param timeout (nil) [Float, nil] The timeout in second. If + * `timeout` is `nil`, {Groonga::RequestTimer.default_timeout} is + * used. + * @return [Groonga::RequestTimerID] The ID of the request timer. + * + * @since 6.0.2 + */ +static VALUE +rb_grn_request_timer_s_register (int argc, VALUE *argv, VALUE module) +{ + VALUE rb_request_id; + VALUE rb_timeout; + const char *request_id; + unsigned int request_id_size; + double timeout; + void *timer_id; + + rb_scan_args(argc, argv, "11", &rb_request_id, &rb_timeout); + + request_id = StringValuePtr(rb_request_id); + request_id_size = RSTRING_LEN(rb_request_id); + if (NIL_P(rb_timeout)) { + timeout = grn_get_default_request_timeout(); + } else { + timeout = NUM2DBL(rb_timeout); + } + timer_id = grn_request_timer_register(request_id, request_id_size, timeout); + + return GRN_REQUEST_TIMER_ID2RVAL(timer_id); +} + +/* + * Unregisters the specified request timer. + * + * @example Unregister a request timer by ID + * timer_id = Groonga::RequestTimer.register("request-29", 2.9) + * Groonga::RequestTimer.unregister(timer_id) + * + * @overload unregister(timer_id) + * @param timer_id [Groonga::RequestTimerID] The ID of request timer + * to be unregistered. + * @return [void] + * + * @since 6.0.2 + */ +static VALUE +rb_grn_request_timer_s_unregister (VALUE module, VALUE rb_timer_id) +{ + void *timer_id; + + timer_id = RVAL2GRN_REQUEST_TIMER_ID(rb_timer_id); + grn_request_timer_unregister(timer_id); + + return Qnil; +} + +/* + * Gets the default timeout used by request timer. + * + * @example Gets the default timeout + * Groonga::RequestTimer.default_timeout + * + * @overload default_timeout + * @return [Float] The default timeout used by request timer. + * + * @since 6.0.2 + */ +static VALUE +rb_grn_request_timer_s_get_default_timeout (VALUE module) +{ + double timeout; + + timeout = grn_get_default_request_timeout(); + + return rb_float_new(timeout); +} + +/* + * Sets the default timeout used by request timer. + * + * @example Sets the default timeout + * Groonga::RequestTimer.default_timeout = 2.9 + * + * @overload default_timeout=(timeout) + * @return [Float] The default timeout used by request timer. If + * `timeout` is `0.0`, the default timeout is disabled. + * @return [void] + * + * @since 6.0.2 + */ +static VALUE +rb_grn_request_timer_s_set_default_timeout (VALUE module, VALUE rb_timeout) +{ + double timeout; + + timeout = NUM2DBL(rb_timeout); + grn_set_default_request_timeout(timeout); + + return Qnil; +} + +void +rb_grn_init_request_timer (VALUE mGrn) +{ + rb_mGrnRequestTimer = rb_define_module_under(mGrn, "RequestTimer"); + + rb_define_singleton_method(rb_mGrnRequestTimer, "register", + rb_grn_request_timer_s_register, -1); + rb_define_singleton_method(rb_mGrnRequestTimer, "unregister", + rb_grn_request_timer_s_unregister, 1); + rb_define_singleton_method(rb_mGrnRequestTimer, "default_timeout", + rb_grn_request_timer_s_get_default_timeout, 0); + rb_define_singleton_method(rb_mGrnRequestTimer, "default_timeout=", + rb_grn_request_timer_s_set_default_timeout, 1); +} Modified: ext/groonga/rb-grn.h (+14 -0) =================================================================== --- ext/groonga/rb-grn.h 2016-05-16 14:48:14 +0900 (7fd5e5b) +++ ext/groonga/rb-grn.h 2016-05-16 16:35:19 +0900 (bb3c81a) @@ -301,6 +301,8 @@ RB_GRN_VAR VALUE rb_cGrnPlugin; RB_GRN_VAR VALUE rb_cGrnNormalizer; RB_GRN_VAR VALUE rb_cGrnIndex; RB_GRN_VAR VALUE rb_mGrnRequestCanceler; +RB_GRN_VAR VALUE rb_mGrnRequestTimer; +RB_GRN_VAR VALUE rb_cGrnRequestTimerID; void rb_grn_init_utils (VALUE mGrn); void rb_grn_init_exception (VALUE mGrn); @@ -357,6 +359,8 @@ 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); +void rb_grn_init_request_timer (VALUE mGrn); +void rb_grn_init_request_timer_id (VALUE mGrn); VALUE rb_grn_rc_to_exception (grn_rc rc); const char *rb_grn_rc_to_message (grn_rc rc); @@ -740,6 +744,11 @@ VALUE rb_grn_index_new (VALUE rb_index_column, #define RVAL2GRNVARIABLE(object, context) \ (rb_grn_variable_from_ruby_object(object, context)) +#define GRN_REQUEST_TIMER_ID2RVAL(id) \ + (rb_grn_request_timer_id_to_ruby_object(id)) +#define RVAL2GRN_REQUEST_TIMER_ID(rb_id) \ + (rb_grn_request_timer_id_from_ruby_object(rb_id)) + grn_encoding rb_grn_encoding_from_ruby_object (VALUE object, grn_ctx *context); @@ -884,6 +893,11 @@ VALUE rb_grn_obj_to_ruby_object (VALUE klass, grn_obj *obj, VALUE related_object); +void *rb_grn_request_timer_id_from_ruby_object + (VALUE rb_id); +VALUE rb_grn_request_timer_id_to_ruby_object + (void *id); + void rb_grn_snippet_bind (RbGrnSnippet *rb_grn_snippet, grn_ctx *context, grn_obj *snippet); Modified: ext/groonga/rb-groonga.c (+2 -0) =================================================================== --- ext/groonga/rb-groonga.c 2016-05-16 14:48:14 +0900 (dffa45d) +++ ext/groonga/rb-groonga.c 2016-05-16 16:35:19 +0900 (d80495e) @@ -250,4 +250,6 @@ Init_groonga (void) rb_grn_init_config(mGrn); rb_grn_init_index(mGrn); rb_grn_init_request_canceler(mGrn); + rb_grn_init_request_timer(mGrn); + rb_grn_init_request_timer_id(mGrn); } Added: test/test-request-timer.rb (+40 -0) 100644 =================================================================== --- /dev/null +++ test/test-request-timer.rb 2016-05-16 16:35:19 +0900 (2d7db91) @@ -0,0 +1,40 @@ +# 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 RequestTimerTest < Test::Unit::TestCase + include GroongaTestUtils + + def setup + @request_id = "request-29" + @timeout = 2.9 + end + + def teardown + end + + def test_register + timer_id = Groonga::RequestTimer.register(@request_id, @timeout) + assert do + timer_id.is_a?(Groonga::RequestTimerID) + end + Groonga::RequestTimer.unregister(timer_id) + end + + def test_deafult_timeout + assert_in_delta(0.0, Groonga::RequestTimer.default_timeout, 0.01) + Groonga::RequestTimer.default_timeout = @timeout + assert_in_delta(@timeout, Groonga::RequestTimer.default_timeout, 0.01) + end +end -------------- next part -------------- HTML����������������������������...下载