Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grn-test-hash-factory.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2008-2009 Kouhei Sutou <kou@cozmixng.org>
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-test-hash-factory.h"
20 
21 #define GRN_TEST_HASH_FACTORY_GET_PRIVATE(obj) \
22  (G_TYPE_INSTANCE_GET_PRIVATE((obj), GRN_TYPE_TEST_HASH_FACTORY, \
23  GrnTestHashFactoryPrivate))
24 
27 {
33  gchar *path;
34  guint32 key_size;
35  guint32 value_size;
38  gchar *cursor_min;
39  guint32 cursor_min_size;
40  gchar *cursor_max;
41  guint32 cursor_max_size;
43 };
44 
45 
46 G_DEFINE_TYPE(GrnTestHashFactory, grn_test_hash_factory, G_TYPE_OBJECT)
47 
48 static void dispose (GObject *object);
49 
50 static void
51 grn_test_hash_factory_class_init(GrnTestHashFactoryClass *klass)
52 {
53  GObjectClass *gobject_class;
54 
55  gobject_class = G_OBJECT_CLASS(klass);
56 
57  gobject_class->dispose = dispose;
58 
59  g_type_class_add_private(gobject_class, sizeof(GrnTestHashFactoryPrivate));
60 }
61 
62 static void
63 grn_test_hash_factory_init(GrnTestHashFactory *factory)
64 {
66 
67  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory);
68 
69  priv->context = NULL;
70  priv->hash = NULL;
71  priv->cursor = NULL;
73  priv->logger = NULL;
74  priv->path = NULL;
75  priv->key_size = sizeof(guint32);
79  priv->cursor_min = NULL;
80  priv->cursor_min_size = 0;
81  priv->cursor_max = NULL;
82  priv->cursor_max_size = 0;
84 }
85 
86 static void
87 cursor_free(GrnTestHashFactoryPrivate *priv)
88 {
89  if (priv->cursor) {
90  grn_hash_cursor_close(priv->context, priv->cursor);
91  priv->cursor = NULL;
92  }
93 }
94 
95 static void
96 hash_free(GrnTestHashFactoryPrivate *priv)
97 {
98  if (priv->hash) {
99  cursor_free(priv);
100  grn_hash_close(priv->context, priv->hash);
101  priv->hash = NULL;
102  }
103 }
104 
105 static void
106 context_free(GrnTestHashFactoryPrivate *priv)
107 {
108  if (priv->context) {
109  hash_free(priv);
110  grn_ctx_fin(priv->context);
111  g_free(priv->context);
112  priv->context = NULL;
113  }
114 }
115 
116 static void
117 dispose(GObject *object)
118 {
120 
121  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(object);
122 
123  context_free(priv);
124 
125  if (priv->logger)
126  priv->logger = NULL;
127 
128  if (priv->path) {
129  g_free(priv->path);
130  priv->path = NULL;
131  }
132 
133  if (priv->cursor_min) {
134  g_free(priv->cursor_min);
135  priv->cursor_min = NULL;
136  }
137 
138  if (priv->cursor_max) {
139  g_free(priv->cursor_max);
140  priv->cursor_max = NULL;
141  }
142 
143  G_OBJECT_CLASS(grn_test_hash_factory_parent_class)->dispose(object);
144 }
145 
146 GQuark
148 {
149  return g_quark_from_static_string("grn-test-hash-factory-error-quark");
150 }
151 
154 {
155  return g_object_new(GRN_TYPE_TEST_HASH_FACTORY, NULL);
156 }
157 
158 static gboolean
159 grn_test_hash_factory_ensure_context(GrnTestHashFactory *factory, GError **error)
160 {
162  grn_rc result;
163 
164  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory);
165  context_free(priv);
166 
167  priv->context = g_new0(grn_ctx, 1);
168  result = grn_ctx_init(priv->context, priv->context_flags);
170  if (result != GRN_SUCCESS) {
171  g_set_error(error,
174  "failed to init grn_ctx"
175  /* FIXME: add error detail */);
176  return FALSE;
177  }
178 
179  return TRUE;
180 }
181 
182 grn_hash *
184 {
186 
187  if (!grn_test_hash_factory_ensure_context(factory, error))
188  return NULL;
189 
190  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory);
191  priv->hash = grn_hash_open(priv->context, priv->path);
192  if (!priv->hash) {
193  g_set_error(error,
196  "failed to open grn_hash"
197  /* FIXME: add error detail */);
198  }
199  return priv->hash;
200 }
201 
202 grn_hash *
204 {
206 
207  if (!grn_test_hash_factory_ensure_context(factory, error))
208  return NULL;
209 
210  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory);
212  priv->hash = grn_hash_create(priv->context, priv->path, priv->key_size,
213  priv->value_size, priv->flags);
214  if (!priv->hash) {
215  g_set_error(error,
218  "failed to create grn_hash"
219  /* FIXME: add error detail */);
220  }
221 
222  return priv->hash;
223 }
224 
227 {
229 
230  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory);
231  if (!priv->hash) {
232  g_set_error(error,
235  "grn_hash is neither opened nor created.");
236  return NULL;
237  }
238 
239  cursor_free(priv);
240  priv->cursor = grn_hash_cursor_open(priv->context, priv->hash,
241  priv->cursor_min, priv->cursor_min_size,
242  priv->cursor_max, priv->cursor_max_size,
243  0, -1, priv->cursor_flags);
244  if (!priv->cursor) {
245  g_set_error(error,
248  "failed to open grn_hash_cursor"
249  /* FIXME: add error detail */);
250  }
251 
252  return priv->cursor;
253 }
254 
255 grn_ctx *
257 {
258  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->context;
259 }
260 
263 {
264  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->context_flags;
265 }
266 
267 void
269  GrnTestContextFlags flags)
270 {
271  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->context_flags = flags;
272 }
273 
276 {
277  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->logger;
278 }
279 
280 void
282  grn_logger_info *logger)
283 {
284  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->logger = logger;
285 }
286 
287 const gchar *
289 {
290  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->path;
291 }
292 
293 void
295  const gchar *path)
296 {
298 
299  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory);
300  if (priv->path)
301  g_free(priv->path);
302  priv->path = g_strdup(path);
303 }
304 
305 guint32
307 {
308  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->key_size;
309 }
310 
311 void
313  guint32 key_size)
314 {
315  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->key_size = key_size;
316 }
317 
318 guint32
320 {
321  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->value_size;
322 }
323 
324 void
326  guint32 value_size)
327 {
328  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->value_size = value_size;
329 }
330 
333 {
334  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->flags;
335 }
336 
337 void
339  GrnTestHashFlags flags)
340 {
341  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->flags = flags;
342 }
343 
344 void
346  GrnTestHashFlags flags)
347 {
348  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->flags |= flags;
349 }
350 
353 {
354  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->encoding;
355 }
356 
357 void
359  GrnTestEncoding encoding)
360 {
361  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->encoding = encoding;
362 }
363 
364 const gchar *
366 {
367  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->cursor_min;
368 }
369 
370 guint32
372 {
373  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->cursor_min_size;
374 }
375 
376 void
378  const gchar *min,
379  guint32 size)
380 {
382 
383  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory);
384  if (priv->cursor_min)
385  g_free(priv->cursor_min);
386  priv->cursor_min = g_memdup(min, size);
387  priv->cursor_min_size = size;
388 }
389 
390 const gchar *
392 {
393  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->cursor_max;
394 }
395 
396 guint32
398 {
399  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->cursor_max_size;
400 }
401 
402 void
404  const gchar *max,
405  guint32 size)
406 {
408 
409  priv = GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory);
410  if (priv->cursor_max)
411  g_free(priv->cursor_max);
412  priv->cursor_max = g_memdup(max, size);
413  priv->cursor_max_size = size;
414 }
415 
418 {
419  return GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->cursor_flags;
420 }
421 
422 void
424  GrnTestCursorFlags flags)
425 {
426  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->cursor_flags = flags;
427 }
428 
429 void
431  GrnTestCursorFlags flags)
432 {
433  GRN_TEST_HASH_FACTORY_GET_PRIVATE(factory)->cursor_flags |= flags;
434 }
435