Kouhei Sutou
null+****@clear*****
Thu May 5 23:18:41 JST 2016
Kouhei Sutou 2016-05-05 23:18:41 +0900 (Thu, 05 May 2016) New Revision: 677cc20c0b1d1c15a99c787108e8364c939d4c6d https://github.com/groonga/groonga/commit/677cc20c0b1d1c15a99c787108e8364c939d4c6d Message: Add functions/time plugin time_classify_second() is included. Added files: plugins/functions/time.c plugins/functions/time_sources.am test/command/suite/select/function/time/time_classify_second/default.expected test/command/suite/select/function/time/time_classify_second/default.test test/command/suite/select/function/time/time_classify_second/interval.expected test/command/suite/select/function/time/time_classify_second/interval.test Modified files: plugins/functions/CMakeLists.txt plugins/functions/Makefile.am Modified: plugins/functions/CMakeLists.txt (+20 -0) =================================================================== --- plugins/functions/CMakeLists.txt 2016-05-05 23:13:37 +0900 (11e38b3) +++ plugins/functions/CMakeLists.txt 2016-05-05 23:18:41 +0900 (d221576) @@ -78,3 +78,23 @@ else() install(TARGETS number_functions DESTINATION "${GRN_FUNCTIONS_PLUGIN_DIR}") endif() target_link_libraries(number_functions libgroonga "${M_LIBS}") + +read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/time_sources.am + TIME_SOURCES) +set_source_files_properties(${TIME_SOURCES} + PROPERTIES + COMPILE_FLAGS "${GRN_C_COMPILE_FLAGS}") +if(GRN_EMBED) + add_library(time_functions STATIC ${TIME_SOURCES}) + set_target_properties( + time_functions + PROPERTIES + POSITION_INDEPENDENT_CODE ON) +else() + add_library(time_functions MODULE ${TIME_SOURCES}) + set_target_properties(time_functions PROPERTIES + PREFIX "" + OUTPUT_NAME "time") + install(TARGETS time_functions DESTINATION "${GRN_FUNCTIONS_PLUGIN_DIR}") +endif() +target_link_libraries(time_functions libgroonga) Modified: plugins/functions/Makefile.am (+2 -0) =================================================================== --- plugins/functions/Makefile.am 2016-05-05 23:13:37 +0900 (888e36d) +++ plugins/functions/Makefile.am 2016-05-05 23:18:41 +0900 (179f574) @@ -18,9 +18,11 @@ function_plugins_LTLIBRARIES = function_plugins_LTLIBRARIES += vector.la function_plugins_LTLIBRARIES += string.la function_plugins_LTLIBRARIES += number.la +function_plugins_LTLIBRARIES += time.la include vector_sources.am include string_sources.am include number_sources.am +include time_sources.am number_la_LIBADD = -lm Added: plugins/functions/time.c (+142 -0) 100644 =================================================================== --- /dev/null +++ plugins/functions/time.c 2016-05-05 23:18:41 +0900 (b334175) @@ -0,0 +1,142 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2016 Brazil + + 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 +*/ + +#ifdef GRN_EMBEDDED +# define GRN_PLUGIN_FUNCTION_TAG functions_time +#endif + +#include <groonga/plugin.h> + +#include <math.h> + +static grn_obj * +func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args, + grn_user_data *user_data) +{ + grn_obj *time; + uint32_t interval_raw = 1; + grn_obj *classed_time; + + if (!(n_args == 1 || n_args == 2)) { + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, + "time_classify_second(): " + "wrong time of arguments (%d for 1..2)", + n_args); + return NULL; + } + + time = args[0]; + if (!(time->header.type == GRN_BULK && + time->header.domain == GRN_DB_TIME)) { + grn_obj inspected; + + GRN_TEXT_INIT(&inspected, 0); + grn_inspect(ctx, &inspected, time); + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, + "time_classify_second(): " + "the first argument must be a time: " + "<%.*s>", + (int)GRN_TEXT_LEN(&inspected), + GRN_TEXT_VALUE(&inspected)); + GRN_OBJ_FIN(ctx, &inspected); + return NULL; + } + + if (n_args == 2) { + grn_obj *interval; + grn_obj casted_interval; + + interval = args[1]; + if (!(interval->header.type == GRN_BULK && + grn_type_id_is_number_family(ctx, interval->header.domain))) { + grn_obj inspected; + + GRN_TEXT_INIT(&inspected, 0); + grn_inspect(ctx, &inspected, interval); + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, + "time_classify_second(): " + "the second argument must be a number: " + "<%.*s>", + (int)GRN_TEXT_LEN(&inspected), + GRN_TEXT_VALUE(&inspected)); + GRN_OBJ_FIN(ctx, &inspected); + return NULL; + } + + GRN_VALUE_FIX_SIZE_INIT(&casted_interval, 0, GRN_DB_UINT32); + grn_obj_cast(ctx, interval, &casted_interval, GRN_FALSE); + interval_raw = GRN_UINT32_VALUE(&casted_interval); + GRN_OBJ_FIN(ctx, &casted_interval); + } + + { + int64_t time_raw; + struct tm tm; + int classed_second; + int64_t classed_time_raw; + + time_raw = GRN_TIME_VALUE(time); + if (!grn_time_to_tm(ctx, time_raw, &tm)) { + return NULL; + } + + classed_second = (tm.tm_sec / interval_raw) * interval_raw; + tm.tm_sec = classed_second; + + if (!grn_time_from_tm(ctx, &classed_time_raw, &tm)) { + return NULL; + } + + classed_time = grn_plugin_proc_alloc(ctx, + user_data, + time->header.domain, + 0); + if (!classed_time) { + return NULL; + } + GRN_TIME_SET(ctx, classed_time, classed_time_raw); + + return classed_time; + } +} + +grn_rc +GRN_PLUGIN_INIT(grn_ctx *ctx) +{ + return ctx->rc; +} + +grn_rc +GRN_PLUGIN_REGISTER(grn_ctx *ctx) +{ + grn_rc rc = GRN_SUCCESS; + + grn_proc_create(ctx, + "time_classify_second", -1, + GRN_PROC_FUNCTION, + func_time_classify_second, + NULL, NULL, 0, NULL); + + return rc; +} + +grn_rc +GRN_PLUGIN_FIN(grn_ctx *ctx) +{ + return GRN_SUCCESS; +} Added: plugins/functions/time_sources.am (+2 -0) 100644 =================================================================== --- /dev/null +++ plugins/functions/time_sources.am 2016-05-05 23:18:41 +0900 (2c55a57) @@ -0,0 +1,2 @@ +time_la_SOURCES = \ + time.c Added: test/command/suite/select/function/time/time_classify_second/default.expected (+63 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/time/time_classify_second/default.expected 2016-05-05 23:18:41 +0900 (8410799) @@ -0,0 +1,63 @@ +plugin_register functions/time +[[0,0.0,0.0],true] +table_create Timestamps TABLE_PAT_KEY Time +[[0,0.0,0.0],true] +load --table Timestamps +[ +{"_key": "2016-05-05 22:33:09.999999"}, +{"_key": "2016-05-05 22:33:10.000000"}, +{"_key": "2016-05-05 22:33:10.000001"}, +{"_key": "2016-05-05 22:33:10.999999"}, +{"_key": "2016-05-05 22:33:11.000000"}, +{"_key": "2016-05-05 22:33:11.000001"} +] +[[0,0.0,0.0],6] +select Timestamps --sortby _id --limit -1 --output_columns '_key, time_classify_second(_key)' +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 6 + ], + [ + [ + "_key", + "Time" + ], + [ + "time_classify_second", + "null" + ] + ], + [ + 1462455189.999999, + 1462455189.0 + ], + [ + 1462455190.0, + 1462455190.0 + ], + [ + 1462455190.000001, + 1462455190.0 + ], + [ + 1462455190.999999, + 1462455190.0 + ], + [ + 1462455191.0, + 1462455191.0 + ], + [ + 1462455191.000001, + 1462455191.0 + ] + ] + ] +] Added: test/command/suite/select/function/time/time_classify_second/default.test (+18 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/time/time_classify_second/default.test 2016-05-05 23:18:41 +0900 (a3c5c9c) @@ -0,0 +1,18 @@ +plugin_register functions/time + +table_create Timestamps TABLE_PAT_KEY Time + +load --table Timestamps +[ +{"_key": "2016-05-05 22:33:09.999999"}, +{"_key": "2016-05-05 22:33:10.000000"}, +{"_key": "2016-05-05 22:33:10.000001"}, +{"_key": "2016-05-05 22:33:10.999999"}, +{"_key": "2016-05-05 22:33:11.000000"}, +{"_key": "2016-05-05 22:33:11.000001"} +] + +select Timestamps \ + --sortby _id \ + --limit -1 \ + --output_columns '_key, time_classify_second(_key)' Added: test/command/suite/select/function/time/time_classify_second/interval.expected (+63 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/time/time_classify_second/interval.expected 2016-05-05 23:18:41 +0900 (e6033ae) @@ -0,0 +1,63 @@ +plugin_register functions/time +[[0,0.0,0.0],true] +table_create Timestamps TABLE_PAT_KEY Time +[[0,0.0,0.0],true] +load --table Timestamps +[ +{"_key": "2016-05-05 22:33:09.999999"}, +{"_key": "2016-05-05 22:33:10.000000"}, +{"_key": "2016-05-05 22:33:10.000001"}, +{"_key": "2016-05-05 22:33:19.999999"}, +{"_key": "2016-05-05 22:33:20.000000"}, +{"_key": "2016-05-05 22:33:20.000001"} +] +[[0,0.0,0.0],6] +select Timestamps --sortby _id --limit -1 --output_columns '_key, time_classify_second(_key, 10)' +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 6 + ], + [ + [ + "_key", + "Time" + ], + [ + "time_classify_second", + "null" + ] + ], + [ + 1462455189.999999, + 1462455180.0 + ], + [ + 1462455190.0, + 1462455190.0 + ], + [ + 1462455190.000001, + 1462455190.0 + ], + [ + 1462455199.999999, + 1462455190.0 + ], + [ + 1462455200.0, + 1462455200.0 + ], + [ + 1462455200.000001, + 1462455200.0 + ] + ] + ] +] Added: test/command/suite/select/function/time/time_classify_second/interval.test (+18 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/time/time_classify_second/interval.test 2016-05-05 23:18:41 +0900 (4069574) @@ -0,0 +1,18 @@ +plugin_register functions/time + +table_create Timestamps TABLE_PAT_KEY Time + +load --table Timestamps +[ +{"_key": "2016-05-05 22:33:09.999999"}, +{"_key": "2016-05-05 22:33:10.000000"}, +{"_key": "2016-05-05 22:33:10.000001"}, +{"_key": "2016-05-05 22:33:19.999999"}, +{"_key": "2016-05-05 22:33:20.000000"}, +{"_key": "2016-05-05 22:33:20.000001"} +] + +select Timestamps \ + --sortby _id \ + --limit -1 \ + --output_columns '_key, time_classify_second(_key, 10)' -------------- next part -------------- HTML����������������������������...Download