Debian Codegraph @

lib/proc/proc_reference.c

raw symbols SCIP
1 /*
2 Copyright (C) 2020-2022 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
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 #include "../grn_db.h"
23
24 #include <groonga/plugin.h>
25
26 static grn_obj *
27 command_reference_acquire(grn_ctx *ctx,
28 int nargs,
29 grn_obj **args,
30 grn_user_data *user_data)
31 {
32 grn_raw_string target_name;
33 target_name.value =
34 grn_plugin_proc_get_var_string(ctx,
35 user_data,
36 "target_name", -1,
37 &(target_name.length));
38 grn_raw_string recursive;
39 recursive.value =
40 grn_plugin_proc_get_var_string(ctx,
41 user_data,
42 "recursive", -1,
43 &(recursive.length));
44
45 grn_obj *obj;
46 bool need_unref = false;
47 if (target_name.length > 0) {
48 obj = grn_ctx_get(ctx, target_name.value, (int)(target_name.length));
49 need_unref = true;
50 } else {
51 obj = grn_ctx_db(ctx);
52 }
53
54 if (obj) {
55 int32_t auto_release_count_arg = -1;
56 if (need_unref) {
57 auto_release_count_arg =
58 grn_plugin_proc_get_var_int32(ctx,
59 user_data,
60 "auto_release_count",
61 -1,
62 auto_release_count_arg);
63 }
64 uint32_t auto_release_count = 0;
65 if (auto_release_count_arg > 0) {
66 /* Don't count this command */
67 auto_release_count = (uint32_t)auto_release_count_arg + 1;
68 }
69 if (GRN_RAW_STRING_EQUAL_CSTRING(recursive, "dependent")) {
70 grn_obj_refer_recursive_dependent_auto_release(ctx,
71 obj,
72 auto_release_count);
73 } else if (GRN_RAW_STRING_EQUAL_CSTRING(recursive, "no")) {
74 grn_obj_refer_auto_release(ctx, obj, auto_release_count);
75 } else {
76 grn_obj_refer_recursive_auto_release(ctx, obj, auto_release_count);
77 }
78 if (need_unref) {
79 grn_obj_unref(ctx, obj);
80 }
81 } else {
82 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
83 "[reference][acquire] target object not found: <%.*s>",
84 (int)(target_name.length),
85 target_name.value);
86 }
87
88 grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS);
89
90 return NULL;
91 }
92
93 void
94 grn_proc_init_reference_acquire(grn_ctx *ctx)
95 {
96 grn_expr_var vars[3];
97
98 grn_plugin_expr_var_init(ctx, &(vars[0]), "target_name", -1);
99 grn_plugin_expr_var_init(ctx, &(vars[1]), "recursive", -1);
100 grn_plugin_expr_var_init(ctx, &(vars[2]), "auto_release_count", -1);
101 grn_plugin_command_create(ctx,
102 "reference_acquire", -1,
103 command_reference_acquire,
104 3,
105 vars);
106 }
107
108 static grn_obj *
109 command_reference_release(grn_ctx *ctx,
110 int nargs,
111 grn_obj **args,
112 grn_user_data *user_data)
113 {
114 grn_raw_string target_name;
115 target_name.value =
116 grn_plugin_proc_get_var_string(ctx,
117 user_data,
118 "target_name", -1,
119 &(target_name.length));
120 grn_raw_string recursive;
121 recursive.value =
122 grn_plugin_proc_get_var_string(ctx,
123 user_data,
124 "recursive", -1,
125 &(recursive.length));
126
127 grn_obj *obj;
128 bool need_unref = false;
129 if (target_name.length > 0) {
130 obj = grn_ctx_get(ctx, target_name.value, (int)(target_name.length));
131 need_unref = true;
132 } else {
133 obj = grn_ctx_db(ctx);
134 }
135
136 if (obj) {
137 if (GRN_RAW_STRING_EQUAL_CSTRING(recursive, "dependent")) {
138 grn_obj_unref_recursive_dependent(ctx, obj);
139 } else if (GRN_RAW_STRING_EQUAL_CSTRING(recursive, "no")) {
140 grn_obj_unref(ctx, obj);
141 } else {
142 grn_obj_unref_recursive(ctx, obj);
143 }
144 if (need_unref) {
145 grn_obj_unref(ctx, obj);
146 }
147 } else {
148 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
149 "[reference][release] target object not found: <%.*s>",
150 (int)(target_name.length),
151 target_name.value);
152 }
153
154 grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS);
155
156 return NULL;
157 }
158
159 void
160 grn_proc_init_reference_release(grn_ctx *ctx)
161 {
162 grn_expr_var vars[2];
163
164 grn_plugin_expr_var_init(ctx, &(vars[0]), "target_name", -1);
165 grn_plugin_expr_var_init(ctx, &(vars[1]), "recursive", -1);
166 grn_plugin_command_create(ctx,
167 "reference_release", -1,
168 command_reference_release,
169 2,
170 vars);
171 }