1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* |
3 | Copyright(C) 2015 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_ctx_impl.h" |
20 | |
21 | #ifdef GRN_WITH_MRUBY |
22 | #include <mruby.h> |
23 | #include <mruby/class.h> |
24 | #include <mruby/data.h> |
25 | |
26 | #include "../grn_db.h" |
27 | #include "../grn_cache.h" |
28 | #include "mrb_bulk.h" |
29 | #include "mrb_cache.h" |
30 | |
31 | static struct mrb_data_type mrb_grn_cache_type = { |
32 | "Groonga::Cache" , |
33 | NULL |
34 | }; |
35 | |
36 | static mrb_value |
37 | mrb_grn_cache_class_current(mrb_state *mrb, mrb_value klass) |
38 | { |
39 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
40 | grn_cache *cache; |
41 | mrb_value mrb_cache; |
42 | |
43 | cache = grn_cache_current_get(ctx); |
44 | mrb_cache = mrb_funcall(mrb, klass, "new" , 1, mrb_cptr_value(mrb, cache)); |
45 | |
46 | return mrb_cache; |
47 | } |
48 | |
49 | static mrb_value |
50 | mrb_grn_cache_initialize(mrb_state *mrb, mrb_value self) |
51 | { |
52 | mrb_value mrb_cache_ptr; |
53 | |
54 | mrb_get_args(mrb, "o" , &mrb_cache_ptr); |
55 | DATA_TYPE(self) = &mrb_grn_cache_type; |
56 | DATA_PTR(self) = mrb_cptr(mrb_cache_ptr); |
57 | return self; |
58 | } |
59 | |
60 | static mrb_value |
61 | mrb_grn_cache_fetch(mrb_state *mrb, mrb_value self) |
62 | { |
63 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
64 | grn_cache *cache; |
65 | char *key; |
66 | mrb_int key_size; |
67 | grn_rc rc; |
68 | grn_obj cache_value; |
69 | mrb_value mrb_cache_value; |
70 | |
71 | cache = DATA_PTR(self); |
72 | mrb_get_args(mrb, "s" , &key, &key_size); |
73 | |
74 | GRN_TEXT_INIT(&cache_value, 0); |
75 | rc = grn_cache_fetch(ctx, cache, key, key_size, &cache_value); |
76 | if (rc == GRN_SUCCESS) { |
77 | mrb_cache_value = grn_mrb_value_from_bulk(mrb, &cache_value); |
78 | } else { |
79 | mrb_cache_value = mrb_nil_value(); |
80 | } |
81 | GRN_OBJ_FIN(ctx, &cache_value); |
82 | |
83 | return mrb_cache_value; |
84 | } |
85 | |
86 | static mrb_value |
87 | mrb_grn_cache_update(mrb_state *mrb, mrb_value self) |
88 | { |
89 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
90 | grn_cache *cache; |
91 | char *key; |
92 | mrb_int key_size; |
93 | char *value; |
94 | mrb_int value_size; |
95 | grn_obj value_buffer; |
96 | |
97 | cache = DATA_PTR(self); |
98 | mrb_get_args(mrb, "ss" , &key, &key_size, &value, &value_size); |
99 | |
100 | GRN_TEXT_INIT(&value_buffer, GRN_OBJ_DO_SHALLOW_COPY); |
101 | GRN_TEXT_SET(ctx, &value_buffer, value, value_size); |
102 | grn_cache_update(ctx, cache, key, key_size, &value_buffer); |
103 | GRN_OBJ_FIN(ctx, &value_buffer); |
104 | |
105 | return mrb_nil_value(); |
106 | } |
107 | |
108 | void |
109 | grn_mrb_cache_init(grn_ctx *ctx) |
110 | { |
111 | grn_mrb_data *data = &(ctx->impl->mrb); |
112 | mrb_state *mrb = data->state; |
113 | struct RClass *module = data->module; |
114 | struct RClass *klass; |
115 | |
116 | klass = mrb_define_class_under(mrb, module, "Cache" , mrb->object_class); |
117 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
118 | |
119 | mrb_define_class_method(mrb, klass, "current" , |
120 | mrb_grn_cache_class_current, MRB_ARGS_NONE()); |
121 | |
122 | mrb_define_method(mrb, klass, "initialize" , |
123 | mrb_grn_cache_initialize, MRB_ARGS_REQ(1)); |
124 | |
125 | mrb_define_method(mrb, klass, "fetch" , |
126 | mrb_grn_cache_fetch, MRB_ARGS_REQ(1)); |
127 | mrb_define_method(mrb, klass, "update" , |
128 | mrb_grn_cache_update, MRB_ARGS_REQ(2)); |
129 | } |
130 | #endif |
131 | |