Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
hash.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2 -*- */
2 /* Copyright(C) 2009-2012 Brazil
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 version 2.1 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public
14  License along with this library; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 #ifndef GRN_HASH_H
18 #define GRN_HASH_H
19 
20 #ifndef GROONGA_IN_H
21 #include "groonga_in.h"
22 #endif /* GROONGA_IN_H */
23 
24 #ifndef GRN_CTX_H
25 #include "ctx.h"
26 #endif /* GRN_CTX_H */
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 /**** grn_tiny_array ****/
33 
34 /*
35  * grn_tiny_array_init() accepts a logical OR of the following flags.
36  * Note that other flags, such as (1 << 30), will be ignored.
37  *
38  * - GRN_TINY_ARRAY_CLEAR specifies to initialize a new block with zeros.
39  * It is valid only iff specified with GRN_TINY_ARRAY_USE_MALLOC.
40  * - GRN_TINY_ARRAY_THREADSAFE specifies to create a critical section when
41  * allocating memory.
42  * - GRN_TINY_ARRAY_USE_MALLOC specifies to use GRN_MALLOC/CALLOC/FREE instead
43  * of GRN_CTX_ALLOC/FREE.
44  */
45 #define GRN_TINY_ARRAY_CLEAR (1 << 0)
46 #define GRN_TINY_ARRAY_THREADSAFE (1 << 1)
47 #define GRN_TINY_ARRAY_USE_MALLOC (1 << 2)
48 
49 /*
50  * - GRN_TINY_ARRAY_FACTOR is the global parameter of grn_tiny_array.
51  * - GRN_TINY_ARRAY_GET_OFFSET() returns the offset of a specified block.
52  * - GRN_TINY_ARRAY_BASE_BLOCK_SIZE is the number of elements in the first
53  * block.
54  * - GRN_TINY_ARRAY_GET_BLOCK_SIZE() returns the number of elements in a
55  * specified block.
56  * - GRN_TINY_ARRAY_NUM_BLOCKS is the maximum number of blocks.
57  */
58 #define GRN_TINY_ARRAY_FACTOR 0
59 #define GRN_TINY_ARRAY_GET_OFFSET(block_id) \
60  (1 << ((block_id) << GRN_TINY_ARRAY_FACTOR))
61 #define GRN_TINY_ARRAY_BASE_BLOCK_SIZE \
62  (GRN_TINY_ARRAY_GET_OFFSET(1) - GRN_TINY_ARRAY_GET_OFFSET(0))
63 #define GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id) \
64  (GRN_TINY_ARRAY_BASE_BLOCK_SIZE * GRN_TINY_ARRAY_GET_OFFSET(block_id))
65 #define GRN_TINY_ARRAY_NUM_BLOCKS (32 >> GRN_TINY_ARRAY_FACTOR)
66 
67 /*
68  * grn_tiny_array uses several blocks to emulate an array.
69  * The k-th block, blocks[k - 1], consists of 2^(k-1) elements.
70  */
72 
76  uint16_t element_size;
77  uint16_t flags;
79  grn_critical_section lock;
80 };
81 
82 #define GRN_TINY_ARRAY_EACH(array, head, tail, key, value, block) do { \
83  int _block_id; \
84  const grn_id _head = (head); \
85  const grn_id _tail = (tail); \
86  for (_block_id = 0, (key) = (_head); \
87  _block_id < GRN_TINY_ARRAY_NUM_BLOCKS && (key) <= (_tail); \
88  _block_id++) { \
89  int _id = GRN_TINY_ARRAY_GET_BLOCK_SIZE(_block_id); \
90  (value) = (array)->blocks[_block_id]; \
91  if (value) { \
92  while (_id-- && (key) <= (_tail)) { \
93  { \
94  block \
95  } \
96  (key)++; \
97  (value) = (void *)((byte *)(value) + (array)->element_size); \
98  } \
99  } else { \
100  (key) += _id; \
101  } \
102  } \
103 } while (0)
104 
106  uint16_t element_size, uint16_t flags);
110  const void *element_address);
111 
112 /**** grn_tiny_bitmap ****/
113 
115 
119 };
120 
121 /**** grn_array ****/
122 
123 #define GRN_ARRAY_TINY (0x01<<6)
124 
125 /*
126  * grn_array uses grn_io or grn_tiny_array to represent an array.
127  *
128  * To create a grn_tiny_array-based grn_array, specify the GRN_ARRAY_TINY flag
129  * to grn_array_create(). Note that a grn_tiny_array-based grn_array is not
130  * backed by a file.
131  */
132 struct _grn_array {
135  uint32_t value_size;
136  int32_t n_keys;
138  uint32_t *n_garbages;
139  uint32_t *n_entries;
140 
141  /* For grn_io_array. */
144  uint32_t *lock;
145 
146  /* For grn_tiny_array. */
147  uint32_t n_garbages_buf;
148  uint32_t n_entries_buf;
152 };
153 
160  unsigned int rest;
161  int dir;
162 };
163 
164 #define GRN_ARRAY_SIZE(array) (*((array)->n_entries))
165 
168  grn_table_sort_key *keys, int n_keys);
169 
170 /* grn_table_queue */
171 
173 
175  grn_mutex mutex;
181 };
182 
191 
192 /**** grn_hash ****/
193 
194 #define GRN_HASH_TINY (0x01<<6)
195 #define GRN_HASH_MAX_KEY_SIZE GRN_TABLE_MAX_KEY_SIZE
196 
197 struct _grn_hash {
200  uint32_t key_size;
202  uint32_t value_size;
203  uint32_t entry_size;
204  uint32_t *n_garbages;
205  uint32_t *n_entries;
206  uint32_t *max_offset;
209 
210  /* For grn_io_hash. */
213  uint32_t *lock;
214  // uint32_t nref;
215  // unsigned int max_n_subrecs;
216  // unsigned int record_size;
217  // unsigned int subrec_size;
218  // grn_rec_unit record_unit;
219  // grn_rec_unit subrec_unit;
220  // uint8_t arrayp;
221  // grn_recordh *curr_rec;
222  // grn_set_cursor *cursor;
223  // int limit;
224  // void *userdata;
225  // grn_id subrec_id;
226 
227  /* For grn_tiny_hash. */
228  uint32_t max_offset_;
229  uint32_t n_garbages_;
230  uint32_t n_entries_;
235 };
236 
237 /* Header of grn_io_hash. */
239  uint32_t flags;
241  uint32_t key_size;
242  uint32_t value_size;
244  uint32_t curr_rec;
245  int32_t curr_key;
246  uint32_t idx_offset;
247  uint32_t entry_size;
248  uint32_t max_offset;
249  uint32_t n_entries;
250  uint32_t n_garbages;
251  uint32_t lock;
253  uint32_t reserved[15];
256 };
257 
264  unsigned int rest;
265  int dir;
266 };
267 
268 /* deprecated */
269 
270 #define GRN_TABLE_SORT_BY_KEY 0
271 #define GRN_TABLE_SORT_BY_ID (1L<<1)
272 #define GRN_TABLE_SORT_BY_VALUE (1L<<2)
273 #define GRN_TABLE_SORT_RES_ID 0
274 #define GRN_TABLE_SORT_RES_KEY (1L<<3)
275 #define GRN_TABLE_SORT_AS_BIN 0
276 #define GRN_TABLE_SORT_AS_NUMBER (1L<<4)
277 #define GRN_TABLE_SORT_AS_SIGNED 0
278 #define GRN_TABLE_SORT_AS_UNSIGNED (1L<<5)
279 #define GRN_TABLE_SORT_AS_INT32 0
280 #define GRN_TABLE_SORT_AS_INT64 (1L<<6)
281 #define GRN_TABLE_SORT_NO_PROC 0
282 #define GRN_TABLE_SORT_WITH_PROC (1L<<7)
283 
285 
288  int (*compar)(grn_ctx *ctx,
289  grn_obj *table1, void *target1, unsigned int target1_size,
290  grn_obj *table2, void *target2, unsigned int target2_size,
291  void *compare_arg);
292  void *compar_arg;
294  int offset;
295 };
296 
297 GRN_API int grn_hash_sort(grn_ctx *ctx, grn_hash *hash, int limit,
298  grn_array *result, grn_table_sort_optarg *optarg);
299 
300 grn_rc grn_hash_lock(grn_ctx *ctx, grn_hash *hash, int timeout);
303 
304 #define GRN_HASH_SIZE(hash) (*((hash)->n_entries))
305 
306 /* private */
307 typedef enum {
313 } grn_rec_unit;
314 
316 
317 int grn_rec_unit_size(grn_rec_unit unit, int rec_size);
318 
319 const char * _grn_hash_key(grn_ctx *ctx, grn_hash *hash, grn_id id, uint32_t *key_size);
320 
321 int grn_hash_get_key_value(grn_ctx *ctx, grn_hash *hash, grn_id id,
322  void *keybuf, int bufsize, void *valuebuf);
323 
324 int _grn_hash_get_key_value(grn_ctx *ctx, grn_hash *hash, grn_id id,
325  void **key, void **value);
326 
327 grn_id grn_hash_next(grn_ctx *ctx, grn_hash *hash, grn_id id);
328 
329 /* only valid for hash tables, GRN_OBJ_KEY_VAR_SIZE && GRN_HASH_TINY */
330 const char *_grn_hash_strkey_by_val(void *v, uint16_t *size);
331 
332 const char *grn_hash_get_value_(grn_ctx *ctx, grn_hash *hash, grn_id id, uint32_t *size);
333 
334 grn_rc grn_hash_remove(grn_ctx *ctx, const char *path);
335 grn_rc grn_array_remove(grn_ctx *ctx, const char *path);
336 
337 grn_id grn_hash_at(grn_ctx *ctx, grn_hash *hash, grn_id id);
338 grn_id grn_array_at(grn_ctx *ctx, grn_array *array, grn_id id);
339 
340 void grn_hash_check(grn_ctx *ctx, grn_hash *hash);
341 
342 #ifdef __cplusplus
343 }
344 #endif
345 
346 #endif /* GRN_HASH_H */