summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/lib/proc/proc_config.c
blob: 98533e3bccc9634251b99553f9eb6a26354d2287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* -*- 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
*/

#include "../grn_proc.h"

#include <groonga/plugin.h>

static grn_obj *
command_config_get(grn_ctx *ctx,
                   int nargs,
                   grn_obj **args,
                   grn_user_data *user_data)
{
  grn_obj *key;
  const char *value;
  uint32_t value_size;

  key = grn_plugin_proc_get_var(ctx, user_data, "key", -1);
  if (GRN_TEXT_LEN(key) == 0) {
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "[config][get] key is missing");
    return NULL;
  }

  grn_config_get(ctx,
                 GRN_TEXT_VALUE(key), GRN_TEXT_LEN(key),
                 &value, &value_size);
  if (ctx->rc) {
    return NULL;
  }

  grn_ctx_output_str(ctx, value, value_size);

  return NULL;
}

static grn_obj *
command_config_set(grn_ctx *ctx,
                   int nargs,
                   grn_obj **args,
                   grn_user_data *user_data)
{
  grn_obj *key;
  grn_obj *value;

  key = grn_plugin_proc_get_var(ctx, user_data, "key", -1);
  if (GRN_TEXT_LEN(key) == 0) {
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "[config][set] key is missing");
    return NULL;
  }

  value = grn_plugin_proc_get_var(ctx, user_data, "value", -1);
  grn_config_set(ctx,
                 GRN_TEXT_VALUE(key), GRN_TEXT_LEN(key),
                 GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value));

  grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS);

  return NULL;
}

static grn_obj *
command_config_delete(grn_ctx *ctx,
                      int nargs,
                      grn_obj **args,
                      grn_user_data *user_data)
{
  grn_obj *key;

  key = grn_plugin_proc_get_var(ctx, user_data, "key", -1);
  if (GRN_TEXT_LEN(key) == 0) {
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "[config][delete] key is missing");
    return NULL;
  }

  grn_config_delete(ctx,
                    GRN_TEXT_VALUE(key), GRN_TEXT_LEN(key));

  grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS);

  return NULL;
}

void
grn_proc_init_config_get(grn_ctx *ctx)
{
  grn_expr_var vars[1];

  grn_plugin_expr_var_init(ctx, &(vars[0]), "key", -1);
  grn_plugin_command_create(ctx,
                            "config_get", -1,
                            command_config_get,
                            1,
                            vars);
}

void
grn_proc_init_config_set(grn_ctx *ctx)
{
  grn_expr_var vars[2];

  grn_plugin_expr_var_init(ctx, &(vars[0]), "key", -1);
  grn_plugin_expr_var_init(ctx, &(vars[1]), "value", -1);
  grn_plugin_command_create(ctx,
                            "config_set", -1,
                            command_config_set,
                            2,
                            vars);
}

void
grn_proc_init_config_delete(grn_ctx *ctx)
{
  grn_expr_var vars[1];

  grn_plugin_expr_var_init(ctx, &(vars[0]), "key", -1);
  grn_plugin_command_create(ctx,
                            "config_delete", -1,
                            command_config_delete,
                            1,
                            vars);
}