1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* |
3 | Copyright(C) 2009-2016 Brazil |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License version 2.1 as published by the Free Software Foundation. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with this library; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | |
19 | #include "../grn_proc.h" |
20 | |
21 | #include "../grn_ctx.h" |
22 | |
23 | #include <groonga/plugin.h> |
24 | |
25 | static grn_obj * |
26 | command_lock_clear(grn_ctx *ctx, |
27 | int nargs, |
28 | grn_obj **args, |
29 | grn_user_data *user_data) |
30 | { |
31 | int target_name_len; |
32 | grn_obj *target_name; |
33 | grn_obj *obj; |
34 | |
35 | target_name = grn_plugin_proc_get_var(ctx, user_data, "target_name" , -1); |
36 | target_name_len = GRN_TEXT_LEN(target_name); |
37 | |
38 | if (target_name_len) { |
39 | obj = grn_ctx_get(ctx, GRN_TEXT_VALUE(target_name), target_name_len); |
40 | } else { |
41 | obj = grn_ctx_db(ctx); |
42 | } |
43 | |
44 | if (obj) { |
45 | grn_obj_clear_lock(ctx, obj); |
46 | } else { |
47 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
48 | "[lock][clear] target object not found: <%.*s>" , |
49 | target_name_len, GRN_TEXT_VALUE(target_name)); |
50 | } |
51 | |
52 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
53 | |
54 | return NULL; |
55 | } |
56 | |
57 | void |
58 | grn_proc_init_clearlock(grn_ctx *ctx) |
59 | { |
60 | grn_expr_var vars[1]; |
61 | |
62 | /* Deprecated. Use "lock_clear" instead. */ |
63 | grn_plugin_expr_var_init(ctx, &(vars[0]), "target_name" , -1); |
64 | grn_plugin_command_create(ctx, |
65 | "clearlock" , -1, |
66 | command_lock_clear, |
67 | 1, |
68 | vars); |
69 | } |
70 | |
71 | void |
72 | grn_proc_init_lock_clear(grn_ctx *ctx) |
73 | { |
74 | grn_expr_var vars[1]; |
75 | |
76 | grn_plugin_expr_var_init(ctx, &(vars[0]), "target_name" , -1); |
77 | grn_plugin_command_create(ctx, |
78 | "lock_clear" , -1, |
79 | command_lock_clear, |
80 | 1, |
81 | vars); |
82 | } |
83 | |
84 | static grn_obj * |
85 | command_lock_acquire(grn_ctx *ctx, |
86 | int nargs, |
87 | grn_obj **args, |
88 | grn_user_data *user_data) |
89 | { |
90 | int target_name_len; |
91 | grn_obj *target_name; |
92 | grn_obj *obj; |
93 | |
94 | target_name = grn_plugin_proc_get_var(ctx, user_data, "target_name" , -1); |
95 | target_name_len = GRN_TEXT_LEN(target_name); |
96 | |
97 | if (target_name_len) { |
98 | obj = grn_ctx_get(ctx, GRN_TEXT_VALUE(target_name), target_name_len); |
99 | } else { |
100 | obj = grn_ctx_db(ctx); |
101 | } |
102 | |
103 | if (obj) { |
104 | grn_obj_lock(ctx, obj, GRN_ID_NIL, grn_lock_timeout); |
105 | } else { |
106 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
107 | "[lock][acquire] target object not found: <%.*s>" , |
108 | target_name_len, GRN_TEXT_VALUE(target_name)); |
109 | } |
110 | |
111 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
112 | |
113 | return NULL; |
114 | } |
115 | |
116 | void |
117 | grn_proc_init_lock_acquire(grn_ctx *ctx) |
118 | { |
119 | grn_expr_var vars[1]; |
120 | |
121 | grn_plugin_expr_var_init(ctx, &(vars[0]), "target_name" , -1); |
122 | grn_plugin_command_create(ctx, |
123 | "lock_acquire" , -1, |
124 | command_lock_acquire, |
125 | 1, |
126 | vars); |
127 | } |
128 | |
129 | static grn_obj * |
130 | command_lock_release(grn_ctx *ctx, |
131 | int nargs, |
132 | grn_obj **args, |
133 | grn_user_data *user_data) |
134 | { |
135 | int target_name_len; |
136 | grn_obj *target_name; |
137 | grn_obj *obj; |
138 | |
139 | target_name = grn_plugin_proc_get_var(ctx, user_data, "target_name" , -1); |
140 | target_name_len = GRN_TEXT_LEN(target_name); |
141 | |
142 | if (target_name_len) { |
143 | obj = grn_ctx_get(ctx, GRN_TEXT_VALUE(target_name), target_name_len); |
144 | } else { |
145 | obj = grn_ctx_db(ctx); |
146 | } |
147 | |
148 | if (obj) { |
149 | grn_obj_unlock(ctx, obj, GRN_ID_NIL); |
150 | } else { |
151 | GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, |
152 | "[lock][release] target object not found: <%.*s>" , |
153 | target_name_len, GRN_TEXT_VALUE(target_name)); |
154 | } |
155 | |
156 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
157 | |
158 | return NULL; |
159 | } |
160 | |
161 | void |
162 | grn_proc_init_lock_release(grn_ctx *ctx) |
163 | { |
164 | grn_expr_var vars[1]; |
165 | |
166 | grn_plugin_expr_var_init(ctx, &(vars[0]), "target_name" , -1); |
167 | grn_plugin_command_create(ctx, |
168 | "lock_release" , -1, |
169 | command_lock_release, |
170 | 1, |
171 | vars); |
172 | } |
173 | |