Debian Codegraph @

lib/mrb/mrb_cache.c

raw symbols SCIP
1 /*
2 Copyright(C) 2015 Brazil
3 Copyright(C) 2022 Sutou Kouhei <[email protected]>
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 as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "../grn_ctx_impl.h"
21
22 #ifdef GRN_WITH_MRUBY
23 #include <mruby.h>
24 #include <mruby/class.h>
25 #include <mruby/data.h>
26
27 #include "../grn_db.h"
28 #include "../grn_cache.h"
29 #include "mrb_bulk.h"
30 #include "mrb_cache.h"
31
32 static struct mrb_data_type mrb_grn_cache_type = {
33 "Groonga::Cache",
34 NULL
35 };
36
37 static mrb_value
38 mrb_grn_cache_class_current(mrb_state *mrb, mrb_value klass)
39 {
40 grn_ctx *ctx = (grn_ctx *)mrb->ud;
41 grn_cache *cache;
42 mrb_value mrb_cache;
43
44 cache = grn_cache_current_get(ctx);
45 mrb_cache = mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, cache));
46
47 return mrb_cache;
48 }
49
50 static mrb_value
51 mrb_grn_cache_initialize(mrb_state *mrb, mrb_value self)
52 {
53 mrb_value mrb_cache_ptr;
54
55 mrb_get_args(mrb, "o", &mrb_cache_ptr);
56 DATA_TYPE(self) = &mrb_grn_cache_type;
57 DATA_PTR(self) = mrb_cptr(mrb_cache_ptr);
58 return self;
59 }
60
61 static mrb_value
62 mrb_grn_cache_fetch(mrb_state *mrb, mrb_value self)
63 {
64 grn_ctx *ctx = (grn_ctx *)mrb->ud;
65 grn_cache *cache;
66 char *key;
67 mrb_int key_size;
68 grn_rc rc;
69 grn_obj cache_value;
70 mrb_value mrb_cache_value;
71
72 cache = DATA_PTR(self);
73 mrb_get_args(mrb, "s", &key, &key_size);
74
75 GRN_TEXT_INIT(&cache_value, 0);
76 rc = grn_cache_fetch(ctx, cache, key, (uint32_t)key_size, &cache_value);
77 if (rc == GRN_SUCCESS) {
78 mrb_cache_value = grn_mrb_value_from_bulk(mrb, &cache_value);
79 } else {
80 mrb_cache_value = mrb_nil_value();
81 }
82 GRN_OBJ_FIN(ctx, &cache_value);
83
84 return mrb_cache_value;
85 }
86
87 static mrb_value
88 mrb_grn_cache_update(mrb_state *mrb, mrb_value self)
89 {
90 grn_ctx *ctx = (grn_ctx *)mrb->ud;
91 grn_cache *cache;
92 char *key;
93 mrb_int key_size;
94 char *value;
95 mrb_int value_size;
96 grn_obj value_buffer;
97
98 cache = DATA_PTR(self);
99 mrb_get_args(mrb, "ss", &key, &key_size, &value, &value_size);
100
101 GRN_TEXT_INIT(&value_buffer, GRN_OBJ_DO_SHALLOW_COPY);
102 GRN_TEXT_SET(ctx, &value_buffer, value, value_size);
103 grn_cache_update(ctx, cache, key, (uint32_t)key_size, &value_buffer);
104 GRN_OBJ_FIN(ctx, &value_buffer);
105
106 return mrb_nil_value();
107 }
108
109 void
110 grn_mrb_cache_init(grn_ctx *ctx)
111 {
112 grn_mrb_data *data = &(ctx->impl->mrb);
113 mrb_state *mrb = data->state;
114 struct RClass *module = data->module;
115 struct RClass *klass;
116
117 klass = mrb_define_class_under(mrb, module, "Cache", mrb->object_class);
118 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
119
120 mrb_define_class_method(mrb, klass, "current",
121 mrb_grn_cache_class_current, MRB_ARGS_NONE());
122
123 mrb_define_method(mrb, klass, "initialize",
124 mrb_grn_cache_initialize, MRB_ARGS_REQ(1));
125
126 mrb_define_method(mrb, klass, "fetch",
127 mrb_grn_cache_fetch, MRB_ARGS_REQ(1));
128 mrb_define_method(mrb, klass, "update",
129 mrb_grn_cache_update, MRB_ARGS_REQ(2));
130 }
131 #endif