[Groonga-commit] groonga/groonga at 88bfc13 [master] Support function/math plugin

Back to archive index

Kentaro Hayashi null+****@clear*****
Mon Jul 3 12:00:05 JST 2017


Kentaro Hayashi	2017-07-03 12:00:05 +0900 (Mon, 03 Jul 2017)

  New Revision: 88bfc1318f586ecd00beaddb4536cd9c92b8873f
  https://github.com/groonga/groonga/commit/88bfc1318f586ecd00beaddb4536cd9c92b8873f

  Merged 469ab17: Merge pull request #721 from kenhys/support-abs

  Message:
    Support function/math plugin
    
    This plugin supports math_abs() function.
    
    TODO: add missing UInt64 test case when GitHub#722 is resolved.

  Added files:
    plugins/functions/math.c
    plugins/functions/math_sources.am
  Modified files:
    plugins/functions/CMakeLists.txt
    plugins/functions/Makefile.am

  Modified: plugins/functions/CMakeLists.txt (+20 -0)
===================================================================
--- plugins/functions/CMakeLists.txt    2017-07-03 10:53:04 +0900 (42314bd)
+++ plugins/functions/CMakeLists.txt    2017-07-03 12:00:05 +0900 (611e30b)
@@ -119,3 +119,23 @@ else()
     DESTINATION "${GRN_FUNCTIONS_PLUGIN_DIR}")
 endif()
 target_link_libraries(index_column_functions libgroonga)
+
+read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/math_sources.am
+  MATH_SOURCES)
+set_source_files_properties(${MATH_SOURCES}
+  PROPERTIES
+  COMPILE_FLAGS "${GRN_C_COMPILE_FLAGS}")
+if(GRN_EMBED)
+  add_library(math_functions STATIC ${MATH_SOURCES})
+  set_target_properties(
+    math_functions
+    PROPERTIES
+    POSITION_INDEPENDENT_CODE ON)
+else()
+  add_library(math_functions MODULE ${MATH_SOURCES})
+  set_target_properties(math_functions PROPERTIES
+    PREFIX ""
+    OUTPUT_NAME "math")
+  install(TARGETS math_functions DESTINATION "${GRN_FUNCTIONS_PLUGIN_DIR}")
+endif()
+target_link_libraries(math_functions libgroonga)

  Modified: plugins/functions/Makefile.am (+3 -0)
===================================================================
--- plugins/functions/Makefile.am    2017-07-03 10:53:04 +0900 (b24947b)
+++ plugins/functions/Makefile.am    2017-07-03 12:00:05 +0900 (f57ee03)
@@ -20,11 +20,14 @@ function_plugins_LTLIBRARIES += string.la
 function_plugins_LTLIBRARIES += number.la
 function_plugins_LTLIBRARIES += time.la
 function_plugins_LTLIBRARIES += index_column.la
+function_plugins_LTLIBRARIES += math.la
 
 include vector_sources.am
 include string_sources.am
 include number_sources.am
 include time_sources.am
 include index_column_sources.am
+include math_sources.am
 
 number_la_LIBADD = -lm
+math_la_LIBADD = -lm

  Added: plugins/functions/math.c (+130 -0) 100644
===================================================================
--- /dev/null
+++ plugins/functions/math.c    2017-07-03 12:00:05 +0900 (6aea87c)
@@ -0,0 +1,130 @@
+/* -*- c-basic-offset: 2 -*- */
+/*
+  Copyright(C) 2017 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_math
+#endif
+
+#include <groonga/plugin.h>
+
+#include <math.h>
+#include <stdlib.h>
+
+static grn_obj *
+func_math_abs(grn_ctx *ctx, int n_args, grn_obj **args,
+              grn_user_data *user_data)
+{
+  grn_obj *number;
+  grn_obj *grn_abs_number;
+
+  if (n_args != 1) {
+    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
+                     "math_abs(): wrong number of arguments (%d for 1)",
+                     n_args);
+    return NULL;
+  }
+
+  number = args[0];
+  if (!(number->header.type == GRN_BULK &&
+        grn_type_id_is_number_family(ctx, number->header.domain))) {
+    grn_obj inspected;
+
+    GRN_TEXT_INIT(&inspected, 0);
+    grn_inspect(ctx, &inspected, number);
+    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
+                     "math_abs(): the first argument must be a number: "
+                     "<%.*s>",
+                     (int)GRN_TEXT_LEN(&inspected),
+                     GRN_TEXT_VALUE(&inspected));
+    GRN_OBJ_FIN(ctx, &inspected);
+    return NULL;
+  }
+
+#define ABS_CONVERT_TYPE(func, return_type, to_type, getter, setter) {   \
+    grn_abs_number = grn_plugin_proc_alloc(ctx,			\
+                                           user_data,		\
+                                           (return_type),	\
+                                           0);			\
+    if (!grn_abs_number) {					\
+      return NULL;						\
+    }								\
+    to_type abs_number_raw = (to_type)(func)(getter(number));	\
+    setter(ctx, grn_abs_number, abs_number_raw);		\
+  }
+
+  switch (number->header.domain) {
+  case GRN_DB_INT8:
+    ABS_CONVERT_TYPE(abs, GRN_DB_UINT8, uint8_t, GRN_INT8_VALUE, GRN_UINT8_SET);
+    break;
+  case GRN_DB_UINT8:
+    ABS_CONVERT_TYPE(abs, GRN_DB_UINT8, uint8_t, GRN_UINT8_VALUE, GRN_UINT8_SET);
+    break;
+  case GRN_DB_INT16:
+    ABS_CONVERT_TYPE(abs, GRN_DB_UINT16, uint16_t, GRN_INT16_VALUE, GRN_UINT16_SET);
+    break;
+  case GRN_DB_UINT16:
+    ABS_CONVERT_TYPE(abs, GRN_DB_UINT16, uint16_t, GRN_UINT16_VALUE, GRN_UINT16_SET);
+    break;
+  case GRN_DB_INT32:
+    ABS_CONVERT_TYPE(labs, GRN_DB_UINT32, uint32_t, GRN_INT32_VALUE, GRN_UINT32_SET);
+    break;
+  case GRN_DB_UINT32:
+    ABS_CONVERT_TYPE(labs, GRN_DB_UINT32, uint32_t, GRN_UINT32_VALUE, GRN_UINT32_SET);
+    break;
+  case GRN_DB_INT64:
+    ABS_CONVERT_TYPE(llabs, GRN_DB_UINT64, uint64_t, GRN_INT64_VALUE, GRN_UINT64_SET);
+    break;
+  case GRN_DB_UINT64:
+    ABS_CONVERT_TYPE(llabs, GRN_DB_UINT64, uint64_t, GRN_UINT64_VALUE, GRN_UINT64_SET);
+    break;
+  case GRN_DB_FLOAT:
+    ABS_CONVERT_TYPE(fabs, GRN_DB_FLOAT, double, GRN_FLOAT_VALUE, GRN_FLOAT_SET);
+    break;
+  default :
+    break;
+  }
+#undef ABS_CONVERT_TYPE
+  
+  return grn_abs_number;
+}
+
+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,
+                  "math_abs", -1,
+                  GRN_PROC_FUNCTION,
+                  func_math_abs,
+                  NULL, NULL, 0, NULL);
+
+  return rc;
+}
+
+grn_rc
+GRN_PLUGIN_FIN(grn_ctx *ctx)
+{
+  return GRN_SUCCESS;
+}

  Added: plugins/functions/math_sources.am (+2 -0) 100644
===================================================================
--- /dev/null
+++ plugins/functions/math_sources.am    2017-07-03 12:00:05 +0900 (8c14ca7)
@@ -0,0 +1,2 @@
+math_la_SOURCES =				\
+	math.c
-------------- next part --------------
HTML����������������������������...
Download 



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