<commit_before>/* -*- c-basic-offset: 2 -*- */
/* Copyright(C) 2009 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "proc.h"
#include "ql.h"

/**** globals for procs ****/
const char *grn_admin_html_path = NULL;

/**** procs ****/

#define DEFAULT_LIMIT           10
#define DEFAULT_OUTPUT_COLUMNS  "_id _key _value *"

static void
print_return_code_with_body(grn_ctx *ctx, grn_obj *buf, grn_content_type ct,
                            grn_obj *body)
{
  switch (ct) {
  case GRN_CONTENT_JSON:
    GRN_TEXT_PUTS(ctx, buf, "[[");
    grn_text_itoa(ctx, buf, ctx->rc);
    if (ctx->rc != GRN_SUCCESS) {
      GRN_TEXT_PUTS(ctx, buf, ",");
      grn_text_esc(ctx, buf, ctx->errbuf, strlen(ctx->errbuf));
    }
    if (body) {
      GRN_TEXT_PUTS(ctx, buf, "],");
      GRN_TEXT_PUT(ctx, buf, GRN_TEXT_VALUE(body), GRN_TEXT_LEN(body));
      GRN_TEXT_PUTS(ctx, buf, "]");
    } else {
      GRN_TEXT_PUTS(ctx, buf, "]]");
    }
    break;
  case GRN_CONTENT_TSV:
  case GRN_CONTENT_XML:
    /* TODO: implement */
    break;
  case GRN_CONTENT_NONE:
    break;
  }
}

static void
print_return_code(grn_ctx *ctx, grn_obj *buf, grn_content_type ct)
{
  print_return_code_with_body(ctx, buf, ct, NULL);
}

static grn_obj *
proc_select(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
{
  uint32_t nvars;
  grn_expr_var *vars;
  grn_content_type ct;
  grn_obj *outbuf = args[0];

  grn_proc_get_info(ctx, user_data, &vars, &nvars, NULL);
  ct = GRN_INT32_VALUE(&vars[14].value);

  if (nvars == 15) {
    int offset = GRN_TEXT_LEN(&vars[7].value)
      ? grn_atoi(GRN_TEXT_VALUE(&vars[7].value), GRN_BULK_CURR(&vars[7].value), NULL)
      : 0;
    int limit = GRN_TEXT_LEN(&vars[8].value)
      ? grn_atoi(GRN_TEXT_VALUE(&vars[8].value), GRN_BULK_CURR(&vars[8].value), NULL)
      : DEFAULT_LIMIT;
    char *output_columns = GRN_TEXT_VALUE(&vars[6].value);
    uint32_t output_columns_len = GRN_TEXT_LEN(&vars[6].value);
    if (!output_columns_len) {
      output_columns = DEFAULT_OUTPUT_COLUMNS;
      output_columns_len = strlen(DEFAULT_OUTPUT_COLUMNS);
    }
    grn_select(ctx, outbuf, ct,
               GRN_TEXT_VALUE(&vars[0].value), GRN_TEXT_LEN(&vars[0].value),
               GRN_TEXT_VALUE(&vars[1].value), GRN_TEXT_LEN(&vars[1].value),
               GRN_TEXT_VALUE(&vars[2].value), GRN_TEXT_LEN(&vars[2].value),
               GRN_TEXT_VALUE(&vars[3].value), GRN_TEXT_LEN(&vars[3].value),
               GRN_TEXT_VALUE(&vars[4].value), GRN_TEXT_LEN(&vars[4].value),
               GRN_TEXT_VALUE(&vars[5].value), GRN_TEXT_LEN(&vars[5].value),
               output_columns, output_columns_len,
               offset, limit,
               GRN_TEXT_VALUE(&vars[9].value), GRN_TEXT_LEN(&vars[9].value),
               GRN_TEXT_VALUE(&vars[10].value), GRN_TEXT_LEN(&vars[10].value),
               GRN_TEXT_VALUE(&vars[11].value), GRN_TEXT_LEN(&vars[11].value),
               grn_atoi(GRN_TEXT_VALUE(&vars[12].value), GRN_BULK_CURR(&vars[12].value), NULL),
               grn_atoi(GRN_TEXT_VALUE(&vars[13].value), GRN_BULK_CURR(&vars[13].value), NULL));
  }
  return outbuf;
}

static grn_obj *
proc_define_selector(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
{
  uint32_t nvars;
  grn_expr_var *vars;
  grn_obj *outbuf = args[0];
  grn_proc_get_info(ctx, user_data, &vars, &nvars, NULL);
  if (grn_proc_create(ctx,
                      GRN_TEXT_VALUE(&vars[0].value), GRN_TEXT_LEN(&vars[0].value),
                      NULL, GRN_PROC_PROCEDURE, proc_select, NULL, NULL, nvars - 1, vars + 1)) {
    GRN_TEXT_PUT(ctx, outbuf, GRN_TEXT_VALUE(&vars[0].value), GRN_TEXT_LEN(&vars[0].value));
  }
  return outbuf;
}

static grn_obj *
proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
{
  uint32_t nvars;
  grn_content_type ct;
  grn_obj *outbuf = args[0];
  grn_expr_var *vars;
  grn_obj *proc = grn_proc_get_info(ctx, user_data, &vars, &nvars, NULL);

  if (nvars == 6) {
    ct = grn_get_ctype(&vars[4].value);
    grn_load(ctx, ct,
             GRN_TEXT_VALUE(&vars[1].value), GRN_TEXT_LEN(&vars[1].value),
             GRN_TEXT_VALUE(&vars[2].value), GRN_TEXT_LEN(&vars[2].value),
             GRN_TEXT_VALUE(&vars[0].value), GRN_TEXT_LEN(&vars[0].value),
             GRN_TEXT_VALUE(&vars[3].value), GRN_TEXT_LEN(&vars[3].value));<commit_msg>define_selector support JSON format result.<commit_after>/* -*- c-basic-offset: 2 -*- */
/* Copyright(C) 2009 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "proc.h"
#include "ql.h"

/**** globals for procs ****/
const char *grn_admin_html_path = NULL;

/**** procs ****/

#define DEFAULT_LIMIT           10
#define DEFAULT_OUTPUT_COLUMNS  "_id _key _value *"

static void
print_return_code_with_body(grn_ctx *ctx, grn_obj *buf, grn_content_type ct,
                            grn_obj *body)
{
  switch (ct) {
  case GRN_CONTENT_JSON:
    GRN_TEXT_PUTS(ctx, buf, "[[");
    grn_text_itoa(ctx, buf, ctx->rc);
    if (ctx->rc != GRN_SUCCESS) {
      GRN_TEXT_PUTS(ctx, buf, ",");
      grn_text_esc(ctx, buf, ctx->errbuf, strlen(ctx->errbuf));
    }
    if (body) {
      GRN_TEXT_PUTS(ctx, buf, "],");
      GRN_TEXT_PUT(ctx, buf, GRN_TEXT_VALUE(body), GRN_TEXT_LEN(body));
      GRN_TEXT_PUTS(ctx, buf, "]");
    } else {
      GRN_TEXT_PUTS(ctx, buf, "]]");
    }
    break;
  case GRN_CONTENT_TSV:
  case GRN_CONTENT_XML:
    /* TODO: implement */
    break;
  case GRN_CONTENT_NONE:
    break;
  }
}

static void
print_return_code(grn_ctx *ctx, grn_obj *buf, grn_content_type ct)
{
  print_return_code_with_body(ctx, buf, ct, NULL);
}

static grn_obj *
proc_select(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
{
  uint32_t nvars;
  grn_expr_var *vars;
  grn_content_type ct;
  grn_obj *outbuf = args[0];

  grn_proc_get_info(ctx, user_data, &vars, &nvars, NULL);
  ct = GRN_INT32_VALUE(&vars[14].value);

  if (nvars == 15) {
    int offset = GRN_TEXT_LEN(&vars[7].value)
      ? grn_atoi(GRN_TEXT_VALUE(&vars[7].value), GRN_BULK_CURR(&vars[7].value), NULL)
      : 0;
    int limit = GRN_TEXT_LEN(&vars[8].value)
      ? grn_atoi(GRN_TEXT_VALUE(&vars[8].value), GRN_BULK_CURR(&vars[8].value), NULL)
      : DEFAULT_LIMIT;
    char *output_columns = GRN_TEXT_VALUE(&vars[6].value);
    uint32_t output_columns_len = GRN_TEXT_LEN(&vars[6].value);
    if (!output_columns_len) {
      output_columns = DEFAULT_OUTPUT_COLUMNS;
      output_columns_len = strlen(DEFAULT_OUTPUT_COLUMNS);
    }
    grn_select(ctx, outbuf, ct,
               GRN_TEXT_VALUE(&vars[0].value), GRN_TEXT_LEN(&vars[0].value),
               GRN_TEXT_VALUE(&vars[1].value), GRN_TEXT_LEN(&vars[1].value),
               GRN_TEXT_VALUE(&vars[2].value), GRN_TEXT_LEN(&vars[2].value),
               GRN_TEXT_VALUE(&vars[3].value), GRN_TEXT_LEN(&vars[3].value),
               GRN_TEXT_VALUE(&vars[4].value), GRN_TEXT_LEN(&vars[4].value),
               GRN_TEXT_VALUE(&vars[5].value), GRN_TEXT_LEN(&vars[5].value),
               output_columns, output_columns_len,
               offset, limit,
               GRN_TEXT_VALUE(&vars[9].value), GRN_TEXT_LEN(&vars[9].value),
               GRN_TEXT_VALUE(&vars[10].value), GRN_TEXT_LEN(&vars[10].value),
               GRN_TEXT_VALUE(&vars[11].value), GRN_TEXT_LEN(&vars[11].value),
               grn_atoi(GRN_TEXT_VALUE(&vars[12].value), GRN_BULK_CURR(&vars[12].value), NULL),
               grn_atoi(GRN_TEXT_VALUE(&vars[13].value), GRN_BULK_CURR(&vars[13].value), NULL));
  }
  return outbuf;
}

static grn_obj *
proc_define_selector(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
{
  uint32_t nvars;
  grn_expr_var *vars;
  grn_obj *outbuf = args[0];
  grn_proc_get_info(ctx, user_data, &vars, &nvars, NULL);
  if (nvars == 16) {
    grn_proc_create(ctx,
                    GRN_TEXT_VALUE(&vars[0].value), GRN_TEXT_LEN(&vars[0].value),
                    NULL, GRN_PROC_PROCEDURE, proc_select, NULL, NULL, nvars - 1, vars + 1);
    print_return_code(ctx, outbuf, grn_get_ctype(&vars[15].value));
  }
  return outbuf;
}

static grn_obj *
proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
{
  uint32_t nvars;
  grn_content_type ct;
  grn_obj *outbuf = args[0];
  grn_expr_var *vars;
  grn_obj *proc = grn_proc_get_info(ctx, user_data, &vars, &nvars, NULL);

  if (nvars == 6) {
    ct = grn_get_ctype(&vars[4].value);
    grn_load(ctx, ct,
             GRN_TEXT_VALUE(&vars[1].value), GRN_TEXT_LEN(&vars[1].value),
             GRN_TEXT_VALUE(&vars[2].value), GRN_TEXT_LEN(&vars[2].value),
             GRN_TEXT_VALUE(&vars[0].value), GRN_TEXT_LEN(&vars[0].value),
             GRN_TEXT_VALUE(&vars[3].value), GRN_TEXT_LEN(&vars[3].value));
{'fraction_non_alphanumeric': 0.1372840976771888, 'fraction_numerical': 0.019058963668850508, 'mean_word_length': 3.569160997732426, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3452770', 'n_tokens_mistral': 3920, 'n_tokens_neox': 3809, 'n_words': 750}