Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pat.c
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 #include "groonga_in.h"
18 #include <string.h>
19 #include <limits.h>
20 #include "pat.h"
21 #include "output.h"
22 #include "util.h"
23 #include "normalizer_in.h"
24 
25 #define GRN_PAT_DELETED (GRN_ID_MAX + 1)
26 
27 #define GRN_PAT_SEGMENT_SIZE 0x400000
28 #define W_OF_KEY_IN_A_SEGMENT 22
29 #define W_OF_PAT_IN_A_SEGMENT 18
30 #define W_OF_SIS_IN_A_SEGMENT 19
31 #define KEY_MASK_IN_A_SEGMENT 0x3fffff
32 #define PAT_MASK_IN_A_SEGMENT 0x3ffff
33 #define SIS_MASK_IN_A_SEGMENT 0x7ffff
34 #define SEG_NOT_ASSIGNED 0xffff
35 #define GRN_PAT_MAX_SEGMENT 0x1000
36 #define GRN_PAT_MDELINFOS (GRN_PAT_NDELINFOS - 1)
37 
38 #define GRN_PAT_BIN_KEY 0x70000
39 
40 typedef struct {
41  grn_id lr[2];
42  /*
43  lr[0]: the left node.
44  lr[1]: the right node.
45 
46  The left node has 0 at the nth bit at the nth byte.
47  The right node has 1 at the nth bit at the nth byte.
48  'check' value indicate 'at the nth bit at the nth byte'.
49 
50  The both available nodes has larger check value rather
51  than the current node.
52 
53  The first node (PAT_AT(pat, GRN_ID_NIL, node)) has only
54  the right node and the node is the start point.
55  */
56  uint32_t key;
57  /*
58  PAT_IMD(node) == 0: key bytes offset in memory map.
59  PAT_IMD(node) == 1: the key bytes.
60  */
61  uint16_t check;
62  /*
63  nth byte: 12, nth bit: 3, terminated: 1
64 
65  nth byte is different in key bytes: (check >> 4): max == 4095
66  the left most byte is the 0th byte and the right most byte is the 11th byte.
67 
68  nth bit is different in nth byte: ((check >> 1) & 0b111)
69  the left most bit is the 0th bit and the right most bit is the 7th bit.
70 
71  terminated: (check & 0b1)
72  terminated == 1: key is terminated.
73  */
74  uint16_t bits;
75  /* length : 13, deleting : 1, immediate : 1 */
76 } pat_node;
77 
78 #define PAT_DELETING (1<<1)
79 #define PAT_IMMEDIATE (1<<2)
80 
81 #define PAT_DEL(x) ((x)->bits & PAT_DELETING)
82 #define PAT_IMD(x) ((x)->bits & PAT_IMMEDIATE)
83 #define PAT_LEN(x) (((x)->bits >> 3) + 1)
84 #define PAT_CHK(x) ((x)->check)
85 #define PAT_DEL_ON(x) ((x)->bits |= PAT_DELETING)
86 #define PAT_IMD_ON(x) ((x)->bits |= PAT_IMMEDIATE)
87 #define PAT_DEL_OFF(x) ((x)->bits &= ~PAT_DELETING)
88 #define PAT_IMD_OFF(x) ((x)->bits &= ~PAT_IMMEDIATE)
89 #define PAT_LEN_SET(x,v) ((x)->bits = ((x)->bits & ((1<<3) - 1))|(((v) - 1) << 3))
90 #define PAT_CHK_SET(x,v) ((x)->check = (v))
91 
92 typedef struct {
95 } sis_node;
96 
97 enum {
101 };
102 
103 /* bit operation */
104 
105 #define nth_bit(key,n,l) ((((key)[(n)>>4]) >> (7 - (((n)>>1) & 7))) & 1)
106 
107 /* segment operation */
108 
109 /* patricia array operation */
110 
111 #define PAT_AT(pat,id,n) do {\
112  int flags = 0;\
113  GRN_IO_ARRAY_AT(pat->io, segment_pat, id, &flags, n);\
114 } while (0)
115 
116 inline static pat_node *
117 pat_get(grn_ctx *ctx, grn_pat *pat, grn_id id)
118 {
119  pat_node *res;
120  int flags = GRN_TABLE_ADD;
121  if (id > GRN_ID_MAX) { return NULL; }
122  GRN_IO_ARRAY_AT(pat->io, segment_pat, id, &flags, res);
123  return res;
124 }
125 
126 inline static pat_node *
127 pat_node_new(grn_ctx *ctx, grn_pat *pat, grn_id *id)
128 {
129  uint32_t n = pat->header->curr_rec + 1;
130  pat_node *res;
131  if (n > GRN_ID_MAX) { return NULL; }
132  if ((res = pat_get(ctx, pat, n))) {
133  pat->header->curr_rec = n;
134  pat->header->n_entries++;
135  }
136  if (id) { *id = n; }
137  return res;
138 }
139 
140 /* sis operation */
141 
142 inline static sis_node *
143 sis_at(grn_ctx *ctx, grn_pat *pat, grn_id id)
144 {
145  sis_node *res;
146  int flags = 0;
147  if (id > GRN_ID_MAX) { return NULL; }
148  GRN_IO_ARRAY_AT(pat->io, segment_sis, id, &flags, res);
149  return res;
150 }
151 
152 inline static sis_node *
153 sis_get(grn_ctx *ctx, grn_pat *pat, grn_id id)
154 {
155  sis_node *res;
156  int flags = GRN_TABLE_ADD;
157  if (id > GRN_ID_MAX) { return NULL; }
158  GRN_IO_ARRAY_AT(pat->io, segment_sis, id, &flags, res);
159  return res;
160 }
161 
162 #define MAX_LEVEL 16
163 
164 static void
165 sis_collect(grn_ctx *ctx, grn_pat *pat, grn_hash *h, grn_id id, uint32_t level)
166 {
167  uint32_t *offset;
168  sis_node *sl = sis_at(ctx, pat, id);
169  if (sl) {
170  grn_id sid = sl->children;
171  while (sid && sid != id) {
172  if (grn_hash_add(ctx, h, &sid, sizeof(grn_id), (void **) &offset, NULL)) {
173  *offset = level;
174  if (level < MAX_LEVEL) { sis_collect(ctx, pat, h, sid, level + 1); }
175  if (!(sl = sis_at(ctx, pat, sid))) { break; }
176  sid = sl->sibling;
177  } else {
178  /* todo : must be handled */
179  }
180  }
181  }
182 }
183 
184 /* key operation */
185 
186 #define KEY_AT(pat,pos,ptr,addp) do {\
187  int flags = addp;\
188  GRN_IO_ARRAY_AT(pat->io, segment_key, pos, &flags, ptr);\
189 } while (0)
190 
191 inline static uint32_t
192 key_put(grn_ctx *ctx, grn_pat *pat, const uint8_t *key, int len)
193 {
194  uint32_t res, ts;
195 // if (len >= GRN_PAT_SEGMENT_SIZE) { return 0; /* error */ }
196  res = pat->header->curr_key;
197  ts = (res + len) >> W_OF_KEY_IN_A_SEGMENT;
198  if (res >> W_OF_KEY_IN_A_SEGMENT != ts) {
199  res = pat->header->curr_key = ts << W_OF_KEY_IN_A_SEGMENT;
200  }
201  {
202  uint8_t *dest;
203  KEY_AT(pat, res, dest, GRN_TABLE_ADD);
204  if (!dest) { return 0; }
205  memcpy(dest, key, len);
206  }
207  pat->header->curr_key += len;
208  return res;
209 }
210 
211 inline static uint8_t *
212 pat_node_get_key(grn_ctx *ctx, grn_pat *pat, pat_node *n)
213 {
214  if (PAT_IMD(n)) {
215  return (uint8_t *) &n->key;
216  } else {
217  uint8_t *res;
218  KEY_AT(pat, n->key, res, 0);
219  return res;
220  }
221 }
222 
223 inline static grn_rc
224 pat_node_set_key(grn_ctx *ctx, grn_pat *pat, pat_node *n, const uint8_t *key, unsigned int len)
225 {
226  if (!key || !len) { return GRN_INVALID_ARGUMENT; }
227  PAT_LEN_SET(n, len);
228  if (len <= sizeof(uint32_t)) {
229  PAT_IMD_ON(n);
230  memcpy(&n->key, key, len);
231  } else {
232  PAT_IMD_OFF(n);
233  n->key = key_put(ctx, pat, key, len);
234  }
235  return GRN_SUCCESS;
236 }
237 
238 /* delinfo operation */
239 
240 enum {
241  DL_EMPTY = 0,
244 };
245 
246 inline static grn_pat_delinfo *
247 delinfo_search(grn_pat *pat, grn_id id)
248 {
249  int i;
250  grn_pat_delinfo *di;
251  for (i = (pat->header->curr_del2) & GRN_PAT_MDELINFOS;
252  i != pat->header->curr_del;
253  i = (i + 1) & GRN_PAT_MDELINFOS) {
254  di = &pat->header->delinfos[i];
255  if (di->stat != DL_PHASE1) { continue; }
256  if (di->ld == id) { return di; }
257  if (di->d == id) { return di; }
258  }
259  return NULL;
260 }
261 
262 inline static grn_rc
263 delinfo_turn_2(grn_ctx *ctx, grn_pat *pat, grn_pat_delinfo *di)
264 {
265  grn_id d, *p = NULL;
266  pat_node *ln, *dn;
267  // grn_log("delinfo_turn_2> di->d=%d di->ld=%d stat=%d", di->d, di->ld, di->stat);
268  if (di->stat != DL_PHASE1) { return GRN_SUCCESS; }
269  PAT_AT(pat, di->ld, ln);
270  if (!ln) { return GRN_INVALID_ARGUMENT; }
271  if (!(d = di->d)) { return GRN_INVALID_ARGUMENT; }
272  PAT_AT(pat, d, dn);
273  if (!dn) { return GRN_INVALID_ARGUMENT; }
274  PAT_DEL_OFF(ln);
275  PAT_DEL_OFF(dn);
276  {
277  grn_id r, *p0;
278  pat_node *rn;
279  int c0 = -1, c;
280  uint32_t len = PAT_LEN(dn) * 16;
281  const uint8_t *key = pat_node_get_key(ctx, pat, dn);
282  if (!key) { return GRN_INVALID_ARGUMENT; }
283  PAT_AT(pat, 0, rn);
284  p0 = &rn->lr[1];
285  while ((r = *p0)) {
286  if (r == d) {
287  p = p0;
288  break;
289  }
290  PAT_AT(pat, r, rn);
291  if (!rn) { return GRN_FILE_CORRUPT; }
292  c = PAT_CHK(rn);
293  if (c <= c0 || len <= c) { break; }
294  if (c & 1) {
295  p0 = (c + 1 < len) ? &rn->lr[1] : &rn->lr[0];
296  } else {
297  p0 = &rn->lr[nth_bit((uint8_t *)key, c, len)];
298  }
299  c0 = c;
300  }
301  }
302  if (p) {
303  PAT_CHK_SET(ln, PAT_CHK(dn));
304  ln->lr[1] = dn->lr[1];
305  ln->lr[0] = dn->lr[0];
306  *p = di->ld;
307  } else {
308  /* debug */
309  int j;
310  grn_id dd;
311  grn_pat_delinfo *ddi;
312  GRN_LOG(ctx, GRN_LOG_DEBUG, "failed to find d=%d", d);
313  for (j = (pat->header->curr_del2 + 1) & GRN_PAT_MDELINFOS;
314  j != pat->header->curr_del;
315  j = (j + 1) & GRN_PAT_MDELINFOS) {
316  ddi = &pat->header->delinfos[j];
317  if (ddi->stat != DL_PHASE1) { continue; }
318  PAT_AT(pat, ddi->ld, ln);
319  if (!ln) { continue; }
320  if (!(dd = ddi->d)) { continue; }
321  if (d == ddi->ld) {
322  GRN_LOG(ctx, GRN_LOG_DEBUG, "found!!!, d(%d) become ld of (%d)", d, dd);
323  }
324  }
325  /* debug */
326  }
327  di->stat = DL_PHASE2;
328  di->d = d;
329  // grn_log("delinfo_turn_2< di->d=%d di->ld=%d", di->d, di->ld);
330  return GRN_SUCCESS;
331 }
332 
333 inline static grn_rc
334 delinfo_turn_3(grn_ctx *ctx, grn_pat *pat, grn_pat_delinfo *di)
335 {
336  pat_node *dn;
337  uint32_t size;
338  if (di->stat != DL_PHASE2) { return GRN_SUCCESS; }
339  PAT_AT(pat, di->d, dn);
340  if (!dn) { return GRN_INVALID_ARGUMENT; }
341  if (di->shared) {
342  PAT_IMD_ON(dn);
343  size = 0;
344  } else {
345  if (PAT_IMD(dn)) {
346  size = 0;
347  } else {
348  size = PAT_LEN(dn);
349  }
350  }
351  di->stat = DL_EMPTY;
352  // dn->lr[1] = GRN_PAT_DELETED;
353  dn->lr[0] = pat->header->garbages[size];
354  pat->header->garbages[size] = di->d;
355  return GRN_SUCCESS;
356 }
357 
358 inline static grn_pat_delinfo *
359 delinfo_new(grn_ctx *ctx, grn_pat *pat)
360 {
361  grn_pat_delinfo *res = &pat->header->delinfos[pat->header->curr_del];
362  uint32_t n = (pat->header->curr_del + 1) & GRN_PAT_MDELINFOS;
363  int gap = ((n + GRN_PAT_NDELINFOS - pat->header->curr_del2) & GRN_PAT_MDELINFOS)
364  - (GRN_PAT_NDELINFOS / 2);
365  while (gap-- > 0) {
366  if (delinfo_turn_2(ctx, pat, &pat->header->delinfos[pat->header->curr_del2])) {
367  GRN_LOG(ctx, GRN_LOG_CRIT, "d2 failed: %d", pat->header->delinfos[pat->header->curr_del2].ld);
368  }
369  pat->header->curr_del2 = (pat->header->curr_del2 + 1) & GRN_PAT_MDELINFOS;
370  }
371  if (n == pat->header->curr_del3) {
372  if (delinfo_turn_3(ctx, pat, &pat->header->delinfos[pat->header->curr_del3])) {
373  GRN_LOG(ctx, GRN_LOG_CRIT, "d3 failed: %d", pat->header->delinfos[pat->header->curr_del3].ld);
374  }
375  pat->header->curr_del3 = (pat->header->curr_del3 + 1) & GRN_PAT_MDELINFOS;
376  }
377  pat->header->curr_del = n;
378  return res;
379 }
380 
381 /* pat operation */
382 
383 inline static grn_pat *
384 _grn_pat_create(grn_ctx *ctx, grn_pat *pat,
385  const char *path, uint32_t key_size,
386  uint32_t value_size, uint32_t flags) {
387  grn_io *io;
388  pat_node *node0;
389  struct grn_pat_header *header;
390  uint32_t entry_size, w_of_element;
392  if (flags & GRN_OBJ_KEY_WITH_SIS) {
393  entry_size = sizeof(sis_node) + value_size;
394  } else {
395  entry_size = value_size;
396  }
397  for (w_of_element = 0; (1 << w_of_element) < entry_size; w_of_element++) {
398  /* nop */
399  }
400  {
401  grn_io_array_spec array_spec[3];
402  array_spec[segment_key].w_of_element = 0;
403  array_spec[segment_key].max_n_segments = 0x400;
404  array_spec[segment_pat].w_of_element = 4;
405  array_spec[segment_pat].max_n_segments = 1 << (30 - (22 - 4));
406  array_spec[segment_sis].w_of_element = w_of_element;
407  array_spec[segment_sis].max_n_segments = 1 << (30 - (22 - w_of_element));
408  io = grn_io_create_with_array(ctx, path, sizeof(struct grn_pat_header),
409  GRN_PAT_SEGMENT_SIZE, grn_io_auto, 3, array_spec);
410  }
411  if (!io) { return NULL; }
412  if (encoding == GRN_ENC_DEFAULT) { encoding = grn_gctx.encoding; }
413  header = grn_io_header(io);
415  header->flags = flags;
416  header->encoding = encoding;
417  header->key_size = key_size;
418  header->value_size = value_size;
419  header->n_entries = 0;
420  header->curr_rec = 0;
421  header->curr_key = -1;
422  header->curr_del = 0;
423  header->curr_del2 = 0;
424  header->curr_del3 = 0;
425  header->n_garbages = 0;
426  header->tokenizer = GRN_ID_NIL;
427  if (header->flags & GRN_OBJ_KEY_NORMALIZE) {
428  header->flags &= ~GRN_OBJ_KEY_NORMALIZE;
430  header->normalizer = grn_obj_id(ctx, pat->normalizer);
431  } else {
432  pat->normalizer = NULL;
433  header->normalizer = GRN_ID_NIL;
434  }
435  pat->io = io;
436  pat->header = header;
437  pat->key_size = key_size;
438  pat->value_size = value_size;
439  pat->tokenizer = NULL;
440  pat->encoding = encoding;
441  pat->obj.header.flags = header->flags;
442  if (!(node0 = pat_get(ctx, pat, 0))) {
443  grn_io_close(ctx, io);
444  return NULL;
445  }
446  node0->lr[1] = 0;
447  node0->lr[0] = 0;
448  node0->key = 0;
449  return pat;
450 }
451 
452 grn_pat *
453 grn_pat_create(grn_ctx *ctx, const char *path, uint32_t key_size,
454  uint32_t value_size, uint32_t flags)
455 {
456  grn_pat *pat;
457  if (!(pat = GRN_MALLOC(sizeof(grn_pat)))) {
458  return NULL;
459  }
461  if (!_grn_pat_create(ctx, pat, path, key_size, value_size, flags)) {
462  GRN_FREE(pat);
463  return NULL;
464  }
465  pat->cache = NULL;
466  pat->cache_size = 0;
467  return pat;
468 }
469 
470 /*
471  grn_pat_cache_enable() and grn_pat_cache_disable() are not thread-safe.
472  So far, they can be used only from single threaded programs.
473  */
474 
475 grn_rc
476 grn_pat_cache_enable(grn_ctx *ctx, grn_pat *pat, uint32_t cache_size)
477 {
478  if (pat->cache || pat->cache_size) {
479  ERR(GRN_INVALID_ARGUMENT, "cache is already enabled");
480  return ctx->rc;
481  }
482  if (cache_size & (cache_size - 1)) {
483  ERR(GRN_INVALID_ARGUMENT, "cache_size(%u) must be a power of two", cache_size);
484  return ctx->rc;
485  }
486  if (!(pat->cache = GRN_CALLOC(cache_size * sizeof(grn_id)))) {
487  return ctx->rc;
488  }
489  pat->cache_size = cache_size;
490  return GRN_SUCCESS;
491 }
492 
493 void
495 {
496  if (pat->cache) {
497  GRN_FREE(pat->cache);
498  pat->cache_size = 0;
499  pat->cache = NULL;
500  }
501 }
502 
503 grn_pat *
504 grn_pat_open(grn_ctx *ctx, const char *path)
505 {
506  grn_io *io;
507  grn_pat *pat;
508  pat_node *node0;
509  struct grn_pat_header *header;
510  io = grn_io_open(ctx, path, grn_io_auto);
511  if (!io) { return NULL; }
512  header = grn_io_header(io);
513  if (grn_io_get_type(io) != GRN_TABLE_PAT_KEY) {
514  ERR(GRN_INVALID_FORMAT, "file type unmatch");
515  grn_io_close(ctx, io);
516  return NULL;
517  }
518  if (!(pat = GRN_MALLOC(sizeof(grn_pat)))) {
519  grn_io_close(ctx, io);
520  return NULL;
521  }
523  pat->io = io;
524  pat->header = header;
525  pat->key_size = header->key_size;
526  pat->value_size = header->value_size;
527  pat->encoding = header->encoding;
528  pat->tokenizer = grn_ctx_at(ctx, header->tokenizer);
529  if (header->flags & GRN_OBJ_KEY_NORMALIZE) {
530  header->flags &= ~GRN_OBJ_KEY_NORMALIZE;
532  header->normalizer = grn_obj_id(ctx, pat->normalizer);
533  } else {
534  pat->normalizer = grn_ctx_at(ctx, header->normalizer);
535  }
536  pat->obj.header.flags = header->flags;
537  PAT_AT(pat, 0, node0);
538  if (!node0) {
539  grn_io_close(ctx, io);
540  GRN_GFREE(pat);
541  return NULL;
542  }
543  pat->cache = NULL;
544  pat->cache_size = 0;
545  return pat;
546 }
547 
548 grn_rc
550 {
551  grn_rc rc;
552  if ((rc = grn_io_close(ctx, pat->io))) {
553  ERR(rc, "grn_io_close failed");
554  } else {
555  if (pat->cache) { grn_pat_cache_disable(ctx, pat); }
556  GRN_FREE(pat);
557  }
558  return rc;
559 }
560 
561 grn_rc
562 grn_pat_remove(grn_ctx *ctx, const char *path)
563 {
564  if (!path) {
565  ERR(GRN_INVALID_ARGUMENT, "path is null");
566  return GRN_INVALID_ARGUMENT;
567  }
568  return grn_io_remove(ctx, path);
569 }
570 
571 grn_rc
573 {
574  grn_rc rc;
575  const char *io_path;
576  char *path;
577  uint32_t key_size, value_size, flags;
578 
579  if ((io_path = grn_io_path(pat->io)) && *io_path != '\0') {
580  if (!(path = GRN_STRDUP(io_path))) {
581  ERR(GRN_NO_MEMORY_AVAILABLE, "cannot duplicate path: <%s>", io_path);
583  }
584  } else {
585  path = NULL;
586  }
587  key_size = pat->key_size;
588  value_size = pat->value_size;
589  flags = pat->obj.header.flags;
590  if ((rc = grn_io_close(ctx, pat->io))) { goto exit; }
591  pat->io = NULL;
592  if (path && (rc = grn_io_remove(ctx, path))) { goto exit; }
593  if (!_grn_pat_create(ctx, pat, path, key_size, value_size, flags)) {
594  rc = GRN_UNKNOWN_ERROR;
595  }
596  if (pat->cache && pat->cache_size) {
597  memset(pat->cache, 0, pat->cache_size * sizeof(grn_id));
598  }
599 exit:
600  if (path) { GRN_FREE(path); }
601  return rc;
602 }
603 
604 inline static grn_id
605 _grn_pat_add(grn_ctx *ctx, grn_pat *pat, const uint8_t *key, uint32_t size, uint32_t *new, uint32_t *lkey)
606 {
607  grn_id r, r0, *p0, *p1 = NULL;
608  pat_node *rn, *rn0;
609  int c, c0 = -1, c1 = -1, len;
610 
611  uint32_t cache_id = 0;
612  if (pat->cache) {
613  const uint8_t *p = key;
614  uint32_t length = size;
615  for (cache_id = 0; length--; p++) { cache_id = (cache_id * 37) + *p; }
616  cache_id &= (pat->cache_size - 1);
617  if (pat->cache[cache_id]) {
618  PAT_AT(pat, pat->cache[cache_id], rn);
619  if (rn) {
620  const uint8_t *k = pat_node_get_key(ctx, pat, rn);
621  if (k && size == PAT_LEN(rn) && !memcmp(k, key, size)) {
622  return pat->cache[cache_id];
623  }
624  }
625  }
626  }
627 
628  *new = 0;
629  len = (int)size * 16;
630  PAT_AT(pat, 0, rn0);
631  p0 = &rn0->lr[1];
632  if (*p0) {
633  uint32_t size2;
634  int xor, mask;
635  const uint8_t *s, *d;
636  for (;;) {
637  if (!(r0 = *p0)) {
638  if (!(s = pat_node_get_key(ctx, pat, rn0))) { return 0; }
639  size2 = PAT_LEN(rn0);
640  break;
641  }
642  PAT_AT(pat, r0, rn0);
643  if (!rn0) { return GRN_ID_NIL; }
644  if (c0 < rn0->check && rn0->check < len) {
645  c1 = c0; c0 = rn0->check;
646  p1 = p0;
647  if (c0 & 1) {
648  p0 = (c0 + 1 < len) ? &rn0->lr[1] : &rn0->lr[0];
649  } else {
650  p0 = &rn0->lr[nth_bit(key, c0, len)];
651  }
652  } else {
653  if (!(s = pat_node_get_key(ctx, pat, rn0))) { return 0; }
654  size2 = PAT_LEN(rn0);
655  if (size == size2 && !memcmp(s, key, size)) {
656  if (pat->cache) { pat->cache[cache_id] = r0; }
657  return r0;
658  }
659  break;
660  }
661  }
662  {
663  uint32_t min = size > size2 ? size2 : size;
664  for (c = 0, d = key; min && *s == *d; c += 16, s++, d++, min--);
665  if (min) {
666  for (xor = *s ^ *d, mask = 0x80; !(xor & mask); mask >>= 1, c += 2);
667  } else {
668  c--;
669  }
670  }
671  if (c == c0 && !*p0) {
672  if (c < len - 2) { c += 2; }
673  } else {
674  if (c < c0) {
675  if (c > c1) {
676  p0 = p1;
677  } else {
678  PAT_AT(pat, 0, rn0);
679  p0 = &rn0->lr[1];
680  while ((r0 = *p0)) {
681  PAT_AT(pat, r0, rn0);
682  if (!rn0) { return 0; }
683  c0 = PAT_CHK(rn0);
684  if (c < c0) { break; }
685  if (c0 & 1) {
686  p0 = (c0 + 1 < len) ? &rn0->lr[1] : &rn0->lr[0];
687  } else {
688  p0 = &rn0->lr[nth_bit(key, c0, len)];
689  }
690  }
691  }
692  }
693  }
694  if (c >= len) { return 0; }
695  } else {
696  c = len - 2;
697  }
698  {
699  uint32_t size2 = size > sizeof(uint32_t) ? size : 0;
700  if (*lkey && size2) {
701  if (pat->header->garbages[0]) {
702  r = pat->header->garbages[0];
703  pat->header->n_entries++;
704  pat->header->n_garbages--;
705  PAT_AT(pat, r, rn);
706  if (!rn) { return 0; }
707  pat->header->garbages[0] = rn->lr[0];
708  } else {
709  if (!(rn = pat_node_new(ctx, pat, &r))) { return 0; }
710  }
711  PAT_IMD_OFF(rn);
712  PAT_LEN_SET(rn, size);
713  rn->key = *lkey;
714  } else {
715  if (pat->header->garbages[size2]) {
716  uint8_t *keybuf;
717  r = pat->header->garbages[size2];
718  pat->header->n_entries++;
719  pat->header->n_garbages--;
720  PAT_AT(pat, r, rn);
721  if (!rn) { return 0; }
722  pat->header->garbages[size2] = rn->lr[0];
723  if (!(keybuf = pat_node_get_key(ctx, pat, rn))) { return 0; }
724  PAT_LEN_SET(rn, size);
725  memcpy(keybuf, key, size);
726  } else {
727  if (!(rn = pat_node_new(ctx, pat, &r))) { return 0; }
728  pat_node_set_key(ctx, pat, rn, key, size);
729  }
730  *lkey = rn->key;
731  }
732  }
733  PAT_CHK_SET(rn, c);
734  PAT_DEL_OFF(rn);
735  if ((c & 1) ? (c + 1 < len) : nth_bit(key, c, len)) {
736  rn->lr[1] = r;
737  rn->lr[0] = *p0;
738  } else {
739  rn->lr[1] = *p0;
740  rn->lr[0] = r;
741  }
742  // smp_wmb();
743  *p0 = r;
744  *new = 1;
745  if (pat->cache) { pat->cache[cache_id] = r; }
746  return r;
747 }
748 
749 inline static int
750 chop(grn_ctx *ctx, grn_pat *pat, const char **key, const char *end, uint32_t *lkey)
751 {
752  size_t len = grn_charlen(ctx, *key, end);
753  if (len) {
754  *lkey += len;
755  *key += len;
756  return **key;
757  } else {
758  return 0;
759  }
760 }
761 
762 #define MAX_FIXED_KEY_SIZE (sizeof(int64_t))
763 
764 #define KEY_NEEDS_CONVERT(pat,size) \
765  (!((pat)->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) && (size) <= MAX_FIXED_KEY_SIZE)
766 
767 #define KEY_ENC(pat,keybuf,key,size) do {\
768  switch ((pat)->obj.header.flags & GRN_OBJ_KEY_MASK) {\
769  case GRN_OBJ_KEY_UINT :\
770  if (((pat)->obj.header.domain != GRN_DB_TOKYO_GEO_POINT) &&\
771  ((pat)->obj.header.domain != GRN_DB_WGS84_GEO_POINT)) {\
772  grn_hton((keybuf), (key), (size));\
773  break;\
774  }\
775  case GRN_OBJ_KEY_GEO_POINT :\
776  grn_gton((keybuf), (key), (size));\
777  break;\
778  case GRN_OBJ_KEY_INT :\
779  grn_hton((keybuf), (key), (size));\
780  *((uint8_t *)(keybuf)) ^= 0x80;\
781  break;\
782  case GRN_OBJ_KEY_FLOAT :\
783  if ((size) == sizeof(int64_t)) {\
784  int64_t v = *(int64_t *)(key);\
785  v ^= ((v >> 63)|(1LL << 63));\
786  grn_hton((keybuf), &v, (size));\
787  }\
788  break;\
789  }\
790 } while (0)
791 
792 #define KEY_DEC(pat,keybuf,key,size) do {\
793  switch ((pat)->obj.header.flags & GRN_OBJ_KEY_MASK) {\
794  case GRN_OBJ_KEY_UINT :\
795  if (((pat)->obj.header.domain != GRN_DB_TOKYO_GEO_POINT) &&\
796  ((pat)->obj.header.domain != GRN_DB_WGS84_GEO_POINT)) {\
797  grn_ntoh((keybuf), (key), (size));\
798  break;\
799  }\
800  case GRN_OBJ_KEY_GEO_POINT :\
801  grn_ntog((keybuf), (key), (size));\
802  break;\
803  case GRN_OBJ_KEY_INT :\
804  grn_ntohi((keybuf), (key), (size));\
805  break;\
806  case GRN_OBJ_KEY_FLOAT :\
807  if ((size) == sizeof(int64_t)) {\
808  int64_t v;\
809  grn_hton(&v, (key), (size));\
810  *((int64_t *)(keybuf)) = v ^ (((v^(1LL<<63))>> 63)|(1LL<<63)); \
811  }\
812  break;\
813  }\
814 } while (0)
815 
816 #define KEY_ENCODE(pat,keybuf,key,size) do {\
817  if (KEY_NEEDS_CONVERT(pat,size)) {\
818  KEY_ENC((pat), (keybuf), (key), (size));\
819  (key) = (keybuf);\
820  }\
821 } while (0)
822 
823 grn_id
824 grn_pat_add(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size,
825  void **value, int *added)
826 {
827  uint32_t new, lkey = 0;
828  grn_id r0;
829  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
830  if (!key || !key_size) { return GRN_ID_NIL; }
831  if (key_size > GRN_TABLE_MAX_KEY_SIZE) {
832  ERR(GRN_INVALID_ARGUMENT, "too long key: (%u)", key_size);
833  return GRN_ID_NIL;
834  }
835  KEY_ENCODE(pat, keybuf, key, key_size);
836  r0 = _grn_pat_add(ctx, pat, (uint8_t *)key, key_size, &new, &lkey);
837  if (added) { *added = new; }
838  if (r0 && (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) &&
839  (*((uint8_t *)key) & 0x80)) { // todo: refine!!
840  sis_node *sl, *sr;
841  grn_id l = r0, r;
842  if (new && (sl = sis_get(ctx, pat, l))) {
843  const char *sis = key, *end = sis + key_size;
844  sl->children = l;
845  sl->sibling = 0;
846  while (chop(ctx, pat, &sis, end, &lkey)) {
847  if (!(*sis & 0x80)) { break; }
848  if (!(r = _grn_pat_add(ctx, pat, (uint8_t *)sis, end - sis, &new, &lkey))) {
849  break;
850  }
851  if (!(sr = sis_get(ctx, pat, r))) { break; }
852  if (new) {
853  sl->sibling = r;
854  sr->children = l;
855  sr->sibling = 0;
856  } else {
857  sl->sibling = sr->children;
858  sr->children = l;
859  break;
860  }
861  l = r;
862  sl = sr;
863  }
864  }
865  }
866  if (r0 && value) {
867  byte *v = (byte *)sis_get(ctx, pat, r0);
868  if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) {
869  *value = v + sizeof(sis_node);
870  } else {
871  *value = v;
872  }
873  }
874  return r0;
875 }
876 
877 inline static grn_id
878 _grn_pat_get(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size, void **value)
879 {
880  grn_id r;
881  pat_node *rn;
882  int c0 = -1, c;
883  uint32_t len = key_size * 16;
884  PAT_AT(pat, 0, rn);
885  for (r = rn->lr[1]; r;) {
886  PAT_AT(pat, r, rn);
887  if (!rn) { break; /* corrupt? */ }
888  c = PAT_CHK(rn);
889  if (len <= c) { break; }
890  if (c <= c0) {
891  const uint8_t *k = pat_node_get_key(ctx, pat, rn);
892  if (k && key_size == PAT_LEN(rn) && !memcmp(k, key, key_size)) {
893  if (value) {
894  byte *v = (byte *)sis_get(ctx, pat, r);
895  if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) {
896  *value = v + sizeof(sis_node);
897  } else {
898  *value = v;
899  }
900  }
901  return r;
902  }
903  break;
904  }
905  if (c & 1) {
906  r = (c + 1 < len) ? rn->lr[1] : rn->lr[0];
907  } else {
908  r = rn->lr[nth_bit((uint8_t *)key, c, len)];
909  }
910  c0 = c;
911  }
912  return GRN_ID_NIL;
913 }
914 
915 grn_id
916 grn_pat_get(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size, void **value)
917 {
918  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
919  KEY_ENCODE(pat, keybuf, key, key_size);
920  return _grn_pat_get(ctx, pat, key, key_size, value);
921 }
922 
923 grn_id
924 grn_pat_nextid(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size)
925 {
926  grn_id r = GRN_ID_NIL;
927  if (pat && key) {
928  if (!(r = pat->header->garbages[key_size > sizeof(uint32_t) ? key_size : 0])) {
929  r = pat->header->curr_rec + 1;
930  }
931  }
932  return r;
933 }
934 
935 static void
936 get_tc(grn_ctx *ctx, grn_pat *pat, grn_hash *h, pat_node *rn)
937 {
938  grn_id id;
939  pat_node *node;
940  id = rn->lr[1];
941  if (id) {
942  PAT_AT(pat, id, node);
943  if (node) {
944  if (PAT_CHK(node) > PAT_CHK(rn)) {
945  get_tc(ctx, pat, h, node);
946  } else {
947  grn_hash_add(ctx, h, &id, sizeof(grn_id), NULL, NULL);
948  }
949  }
950  }
951  id = rn->lr[0];
952  if (id) {
953  PAT_AT(pat, id, node);
954  if (node) {
955  if (PAT_CHK(node) > PAT_CHK(rn)) {
956  get_tc(ctx, pat, h, node);
957  } else {
958  grn_hash_add(ctx, h, &id, sizeof(grn_id), NULL, NULL);
959  }
960  }
961  }
962 }
963 
964 grn_rc
966  const void *key, uint32_t key_size, grn_hash *h)
967 {
968  int c0 = -1, c;
969  const uint8_t *k;
970  uint32_t len = key_size * 16;
971  grn_id r;
972  pat_node *rn;
973  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
974  KEY_ENCODE(pat, keybuf, key, key_size);
975  PAT_AT(pat, 0, rn);
976  r = rn->lr[1];
977  while (r) {
978  PAT_AT(pat, r, rn);
979  if (!rn) { return GRN_FILE_CORRUPT; }
980  c = PAT_CHK(rn);
981  if (c0 < c && c < len - 1) {
982  if (c & 1) {
983  r = (c + 1 < len) ? rn->lr[1] : rn->lr[0];
984  } else {
985  r = rn->lr[nth_bit((uint8_t *)key, c, len)];
986  }
987  c0 = c;
988  continue;
989  }
990  if (!(k = pat_node_get_key(ctx, pat, rn))) { break; }
991  if (PAT_LEN(rn) < key_size) { break; }
992  if (!memcmp(k, key, key_size)) {
993  if (c >= len - 1) {
994  get_tc(ctx, pat, h, rn);
995  } else {
996  grn_hash_add(ctx, h, &r, sizeof(grn_id), NULL, NULL);
997  }
998  return GRN_SUCCESS;
999  }
1000  break;
1001  }
1002  return GRN_END_OF_DATA;
1003 }
1004 
1005 grn_hash *
1006 grn_pat_prefix_search2(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size)
1007 {
1008  grn_hash *h;
1009  if (!pat || !key) { return NULL; }
1010  if ((h = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, 0))) {
1011  if (grn_pat_prefix_search(ctx, pat, key, key_size, h)) {
1012  grn_hash_close(ctx, h);
1013  h = NULL;
1014  }
1015  }
1016  return h;
1017 }
1018 
1019 grn_rc
1021  const void *key, uint32_t key_size, grn_hash *h)
1022 {
1023  grn_id r;
1024  if ((r = grn_pat_get(ctx, pat, key, key_size, NULL))) {
1025  uint32_t *offset;
1026  if (grn_hash_add(ctx, h, &r, sizeof(grn_id), (void **) &offset, NULL)) {
1027  *offset = 0;
1028  if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { sis_collect(ctx, pat, h, r, 1); }
1029  return GRN_SUCCESS;
1030  }
1031  }
1032  return GRN_END_OF_DATA;
1033 }
1034 
1035 grn_hash *
1036 grn_pat_suffix_search2(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size)
1037 {
1038  grn_hash *h;
1039  if (!pat || !key) { return NULL; }
1040  if ((h = grn_hash_create(ctx, NULL, sizeof(grn_id), sizeof(uint32_t), 0))) {
1041  if (grn_pat_suffix_search(ctx, pat, key, key_size, h)) {
1042  grn_hash_close(ctx, h);
1043  h = NULL;
1044  }
1045  }
1046  return h;
1047 }
1048 
1049 grn_id
1050 grn_pat_lcp_search(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size)
1051 {
1052  pat_node *rn;
1053  grn_id r, r2 = GRN_ID_NIL;
1054  uint32_t len = key_size * 16;
1055  int c0 = -1, c;
1056  if (!pat || !key || !(pat->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE)) { return GRN_ID_NIL; }
1057  PAT_AT(pat, 0, rn);
1058  for (r = rn->lr[1]; r;) {
1059  PAT_AT(pat, r, rn);
1060  if (!rn) { break; /* corrupt? */ }
1061  c = PAT_CHK(rn);
1062  if (c <= c0) {
1063  if (PAT_LEN(rn) <= key_size) {
1064  uint8_t *p = pat_node_get_key(ctx, pat, rn);
1065  if (!p) { break; }
1066  if (!memcmp(p, key, PAT_LEN(rn))) { return r; }
1067  }
1068  break;
1069  }
1070  if (len <= c) { break; }
1071  if (c & 1) {
1072  uint8_t *p;
1073  pat_node *rn0;
1074  grn_id r0 = rn->lr[0];
1075  PAT_AT(pat, r0, rn0);
1076  if (!rn0) { break; /* corrupt? */ }
1077  p = pat_node_get_key(ctx, pat, rn0);
1078  if (!p) { break; }
1079  if (PAT_LEN(rn0) <= key_size && !memcmp(p, key, PAT_LEN(rn0))) { r2 = r0; }
1080  r = (c + 1 < len) ? rn->lr[1] : rn->lr[0];
1081  } else {
1082  r = rn->lr[nth_bit((uint8_t *)key, c, len)];
1083  }
1084  c0 = c;
1085  }
1086  return r2;
1087 }
1088 
1089 inline static grn_rc
1090 _grn_pat_del(grn_ctx *ctx, grn_pat *pat, const char *key, uint32_t key_size, int shared,
1091  grn_table_delete_optarg *optarg)
1092 {
1093  grn_pat_delinfo *di;
1094  uint8_t direction;
1095  pat_node *rn, *rn0 = NULL, *rno;
1096  int c, c0 = -1, ch;
1097  uint32_t len = key_size * 16;
1098  grn_id r, otherside, *proot, *p, *p0 = NULL;
1099 
1100  di = delinfo_new(ctx, pat); /* must be called before find rn */
1101  di->shared = shared;
1102  PAT_AT(pat, 0, rn);
1103  c = -1;
1104  proot = p = &rn->lr[1];
1105  for (;;) {
1106  if (!(r = *p)) { return GRN_INVALID_ARGUMENT; }
1107  PAT_AT(pat, r, rn);
1108  if (!rn) { return GRN_FILE_CORRUPT; }
1109  ch = PAT_CHK(rn);
1110  if (len <= ch) { return GRN_INVALID_ARGUMENT; }
1111  if (c >= ch) {
1112  const uint8_t *k = pat_node_get_key(ctx, pat, rn);
1113  if (!k) { return GRN_INVALID_ARGUMENT; }
1114  if (key_size == PAT_LEN(rn) && !memcmp(k, key, key_size)) {
1115  break; /* found */
1116  } else { return GRN_INVALID_ARGUMENT; }
1117  }
1118  c0 = c;
1119  p0 = p;
1120  if ((c = ch) & 1) {
1121  p = (c + 1 < len) ? &rn->lr[1] : &rn->lr[0];
1122  } else {
1123  p = &rn->lr[nth_bit((uint8_t *)key, c, len)];
1124  }
1125  rn0 = rn;
1126  }
1127  if (optarg && optarg->func &&
1128  !optarg->func(ctx, (grn_obj *)pat, r, optarg->func_arg)) {
1129  return GRN_SUCCESS;
1130  }
1131  direction = (rn0->lr[1] == r);
1132  otherside = direction ? rn0->lr[0] : rn0->lr[1];
1133  if (rn == rn0) {
1134  di->stat = DL_PHASE2;
1135  di->d = r;
1136  if (otherside) {
1137  PAT_AT(pat, otherside, rno);
1138  if (rno && c0 < PAT_CHK(rno) && PAT_CHK(rno) <= c) {
1139  if (!delinfo_search(pat, otherside)) {
1140  GRN_LOG(ctx, GRN_LOG_ERROR, "no delinfo found %d", otherside);
1141  }
1142  PAT_CHK_SET(rno, 0);
1143  }
1144  if (proot == p0 && !rno->check) { rno->lr[0] = rno->lr[1] = otherside; }
1145  }
1146  *p0 = otherside;
1147  } else {
1148  grn_pat_delinfo *ldi = NULL, *ddi = NULL;
1149  if (PAT_DEL(rn)) { ldi = delinfo_search(pat, r); }
1150  if (PAT_DEL(rn0)) { ddi = delinfo_search(pat, *p0); }
1151  if (ldi) {
1152  PAT_DEL_OFF(rn);
1153  di->stat = DL_PHASE2;
1154  if (ddi) {
1155  PAT_DEL_OFF(rn0);
1156  ddi->stat = DL_PHASE2;
1157  if (ddi == ldi) {
1158  if (r != ddi->ld) {
1159  GRN_LOG(ctx, GRN_LOG_ERROR, "r(%d) != ddi->ld(%d)", r, ddi->ld);
1160  }
1161  di->d = r;
1162  } else {
1163  ldi->ld = ddi->ld;
1164  di->d = r;
1165  }
1166  } else {
1167  PAT_DEL_ON(rn0);
1168  ldi->ld = *p0;
1169  di->d = r;
1170  }
1171  } else {
1172  PAT_DEL_ON(rn);
1173  if (ddi) {
1174  if (ddi->d != *p0) {
1175  GRN_LOG(ctx, GRN_LOG_ERROR, "ddi->d(%d) != *p0(%d)", ddi->d, *p0);
1176  }
1177  PAT_DEL_OFF(rn0);
1178  ddi->stat = DL_PHASE2;
1179  di->stat = DL_PHASE1;
1180  di->ld = ddi->ld;
1181  di->d = r;
1182  /*
1183  PAT_DEL_OFF(rn0);
1184  ddi->d = r;
1185  di->stat = DL_PHASE2;
1186  di->d = *p0;
1187  */
1188  } else {
1189  PAT_DEL_ON(rn0);
1190  di->stat = DL_PHASE1;
1191  di->ld = *p0;
1192  di->d = r;
1193  // grn_log("pat_del d=%d ld=%d stat=%d", r, *p0, DL_PHASE1);
1194  }
1195  }
1196  if (*p0 == otherside) {
1197  PAT_CHK_SET(rn0, 0);
1198  if (proot == p0 && !rn0->check) { rn0->lr[0] = rn0->lr[1] = otherside; }
1199  } else {
1200  if (otherside) {
1201  PAT_AT(pat, otherside, rno);
1202  if (rno && c0 < PAT_CHK(rno) && PAT_CHK(rno) <= c) {
1203  if (!delinfo_search(pat, otherside)) {
1204  GRN_LOG(ctx, GRN_LOG_ERROR, "no delinfo found %d", otherside);
1205  }
1206  PAT_CHK_SET(rno, 0);
1207  }
1208  if (proot == p0 && !rno->check) { rno->lr[0] = rno->lr[1] = otherside; }
1209  }
1210  *p0 = otherside;
1211  }
1212  }
1213  pat->header->n_entries--;
1214  pat->header->n_garbages++;
1215  return GRN_SUCCESS;
1216 }
1217 
1218 static grn_rc
1219 _grn_pat_delete(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size,
1220  grn_table_delete_optarg *optarg)
1221 {
1222  if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) {
1223  grn_id id = grn_pat_get(ctx, pat, key, key_size, NULL);
1224  if (id && grn_pat_delete_with_sis(ctx, pat, id, optarg)) {
1225  return GRN_SUCCESS;
1226  }
1227  return GRN_INVALID_ARGUMENT;
1228  }
1229  return _grn_pat_del(ctx, pat, key, key_size, 0, optarg);
1230 }
1231 
1232 grn_rc
1233 grn_pat_delete(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size,
1234  grn_table_delete_optarg *optarg)
1235 {
1236  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
1237  if (!pat || !key || !key_size) { return GRN_INVALID_ARGUMENT; }
1238  KEY_ENCODE(pat, keybuf, key, key_size);
1239  return _grn_pat_delete(ctx, pat, key, key_size, optarg);
1240 }
1241 
1242 uint32_t
1244 {
1245  if (!pat) { return GRN_INVALID_ARGUMENT; }
1246  return pat->header->n_entries;
1247 }
1248 
1249 const char *
1250 _grn_pat_key(grn_ctx *ctx, grn_pat *pat, grn_id id, uint32_t *key_size)
1251 {
1252  pat_node *node;
1253  uint8_t *key;
1254  PAT_AT(pat, id, node);
1255  if (!node) {
1256  *key_size = 0;
1257  return NULL;
1258  }
1259  key = pat_node_get_key(ctx, pat, node);
1260  if (key) {
1261  *key_size = PAT_LEN(node);
1262  } else {
1263  *key_size = 0;
1264  }
1265  return (const char *)key;
1266 }
1267 
1268 grn_rc
1270  grn_table_delete_optarg *optarg)
1271 {
1272  if (!pat || !id) { return GRN_INVALID_ARGUMENT; }
1273  {
1274  uint32_t key_size;
1275  const char *key = _grn_pat_key(ctx, pat, id, &key_size);
1276  return _grn_pat_delete(ctx, pat, key, key_size, optarg);
1277  }
1278 }
1279 
1280 int
1281 grn_pat_get_key(grn_ctx *ctx, grn_pat *pat, grn_id id, void *keybuf, int bufsize)
1282 {
1283  int len;
1284  uint8_t *key;
1285  pat_node *node;
1286  if (!pat) { return GRN_INVALID_ARGUMENT; }
1287  PAT_AT(pat, id, node);
1288  if (!node) { return 0; }
1289  if (!(key = pat_node_get_key(ctx, pat, node))) { return 0; }
1290  len = PAT_LEN(node);
1291  if (keybuf && bufsize >= len) {
1292  if (KEY_NEEDS_CONVERT(pat, len)) {
1293  KEY_DEC(pat, keybuf, key, len);
1294  } else {
1295  memcpy(keybuf, key, len);
1296  }
1297  }
1298  return len;
1299 }
1300 
1301 int
1303 {
1304  uint32_t len;
1305  uint8_t *key;
1306  pat_node *node;
1307  if (!pat) { return GRN_INVALID_ARGUMENT; }
1308  PAT_AT(pat, id, node);
1309  if (!node) { return 0; }
1310  if (!(key = pat_node_get_key(ctx, pat, node))) { return 0; }
1311  len = PAT_LEN(node);
1312  if (KEY_NEEDS_CONVERT(pat, len)) {
1313  if (bulk->header.impl_flags & GRN_OBJ_REFER) {
1314  GRN_TEXT_INIT(bulk, 0);
1315  }
1316  if (!grn_bulk_reserve(ctx, bulk, len)) {
1317  char *curr = GRN_BULK_CURR(bulk);
1318  KEY_DEC(pat, curr, key, len);
1319  grn_bulk_truncate(ctx, bulk, GRN_BULK_VSIZE(bulk) + len);
1320  }
1321  } else {
1322  if (bulk->header.impl_flags & GRN_OBJ_REFER) {
1323  bulk->u.b.head = (char *)key;
1324  bulk->u.b.curr = (char *)key + len;
1325  } else {
1326  grn_bulk_write(ctx, bulk, (char *)key, len);
1327  }
1328  }
1329  return len;
1330 }
1331 
1332 int
1333 grn_pat_get_value(grn_ctx *ctx, grn_pat *pat, grn_id id, void *valuebuf)
1334 {
1335  int value_size = (int)pat->value_size;
1336  if (value_size) {
1337  byte *v = (byte *)sis_at(ctx, pat, id);
1338  if (v) {
1339  if (valuebuf) {
1340  if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) {
1341  memcpy(valuebuf, v + sizeof(sis_node), value_size);
1342  } else {
1343  memcpy(valuebuf, v, value_size);
1344  }
1345  }
1346  return value_size;
1347  }
1348  }
1349  return 0;
1350 }
1351 
1352 const char *
1353 grn_pat_get_value_(grn_ctx *ctx, grn_pat *pat, grn_id id, uint32_t *size)
1354 {
1355  const char *value = NULL;
1356  if ((*size = pat->value_size)) {
1357  if ((value = (const char *)sis_at(ctx, pat, id))
1358  && (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS)) {
1359  value += sizeof(sis_node);
1360  }
1361  }
1362  return value;
1363 }
1364 
1365 grn_rc
1367  const void *value, int flags)
1368 {
1369  if (value) {
1370  uint32_t value_size = pat->value_size;
1371  if (value_size) {
1372  byte *v = (byte *)sis_get(ctx, pat, id);
1373  if (v) {
1374  if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { v += sizeof(sis_node); }
1375  switch ((flags & GRN_OBJ_SET_MASK)) {
1376  case GRN_OBJ_SET :
1377  memcpy(v, value, value_size);
1378  return GRN_SUCCESS;
1379  case GRN_OBJ_INCR :
1380  switch (value_size) {
1381  case sizeof(int32_t) :
1382  *((int32_t *)v) += *((int32_t *)value);
1383  return GRN_SUCCESS;
1384  case sizeof(int64_t) :
1385  *((int64_t *)v) += *((int64_t *)value);
1386  return GRN_SUCCESS;
1387  default :
1388  return GRN_INVALID_ARGUMENT;
1389  }
1390  break;
1391  case GRN_OBJ_DECR :
1392  switch (value_size) {
1393  case sizeof(int32_t) :
1394  *((int32_t *)v) -= *((int32_t *)value);
1395  return GRN_SUCCESS;
1396  case sizeof(int64_t) :
1397  *((int64_t *)v) -= *((int64_t *)value);
1398  return GRN_SUCCESS;
1399  default :
1400  return GRN_INVALID_ARGUMENT;
1401  }
1402  break;
1403  default :
1404  // todo : support other types.
1405  return GRN_INVALID_ARGUMENT;
1406  }
1407  } else {
1408  return GRN_NO_MEMORY_AVAILABLE;
1409  }
1410  }
1411  }
1412  return GRN_INVALID_ARGUMENT;
1413 }
1414 
1415 grn_rc
1416 grn_pat_info(grn_ctx *ctx, grn_pat *pat, int *key_size, unsigned int *flags,
1417  grn_encoding *encoding, unsigned int *n_entries, unsigned int *file_size)
1418 {
1419  ERRCLR(NULL);
1420  if (!pat) { return GRN_INVALID_ARGUMENT; }
1421  if (key_size) { *key_size = pat->key_size; }
1422  if (flags) { *flags = pat->obj.header.flags; }
1423  if (encoding) { *encoding = pat->encoding; }
1424  if (n_entries) { *n_entries = pat->header->n_entries; }
1425  if (file_size) {
1426  grn_rc rc;
1427  uint64_t tmp = 0;
1428  if ((rc = grn_io_size(ctx, pat->io, &tmp))) {
1429  return rc;
1430  }
1431  *file_size = (unsigned int) tmp; /* FIXME: inappropriate cast */
1432  }
1433  return GRN_SUCCESS;
1434 }
1435 
1436 int
1438  grn_table_delete_optarg *optarg)
1439 {
1440  int level = 0, shared;
1441  const char *key = NULL, *_key;
1442  sis_node *sp, *ss = NULL, *si = sis_at(ctx, pat, id);
1443  while (id) {
1444  pat_node *rn;
1445  uint32_t key_size;
1446  if ((si && si->children && si->children != id) ||
1447  (optarg && optarg->func &&
1448  !optarg->func(ctx, (grn_obj *)pat, id, optarg->func_arg))) {
1449  break;
1450  }
1451  PAT_AT(pat, id, rn);
1452  if (!(_key = (char *)pat_node_get_key(ctx, pat, rn))) { return 0; }
1453  if (_key == key) {
1454  shared = 1;
1455  } else {
1456  key = _key;
1457  shared = 0;
1458  }
1459  key_size = PAT_LEN(rn);
1460  if (key && key_size) { _grn_pat_del(ctx, pat, key, key_size, shared, NULL); }
1461  if (si) {
1462  grn_id *p, sid;
1463  uint32_t lkey = 0;
1464  if ((*key & 0x80) && chop(ctx, pat, &key, key + key_size, &lkey)) {
1465  if ((sid = grn_pat_get(ctx, pat, key, key_size - lkey, NULL)) &&
1466  (ss = sis_at(ctx, pat, sid))) {
1467  for (p = &ss->children; *p && *p != sid; p = &sp->sibling) {
1468  if (*p == id) {
1469  *p = si->sibling;
1470  break;
1471  }
1472  if (!(sp = sis_at(ctx, pat, *p))) { break; }
1473  }
1474  }
1475  } else {
1476  sid = GRN_ID_NIL;
1477  }
1478  si->sibling = 0;
1479  si->children = 0;
1480  id = sid;
1481  si = ss;
1482  } else {
1483  id = GRN_ID_NIL;
1484  }
1485  level++;
1486  }
1487  if (level) {
1488  uint32_t lkey = 0;
1489  while (id && key) {
1490  uint32_t key_size;
1491  if (_grn_pat_key(ctx, pat, id, &key_size) != key) { break; }
1492  {
1493  pat_node *rn;
1494  PAT_AT(pat, id, rn);
1495  if (!rn) { break; }
1496  if (lkey) {
1497  rn->key = lkey;
1498  } else {
1499  pat_node_set_key(ctx, pat, rn, (uint8_t *)key, key_size);
1500  lkey = rn->key;
1501  }
1502  }
1503  {
1504  const char *end = key + key_size;
1505  if (!((*key & 0x80) && chop(ctx, pat, &key, end, &lkey))) { break; }
1506  id = grn_pat_get(ctx, pat, key, end - key, NULL);
1507  }
1508  }
1509  }
1510  return level;
1511 }
1512 
1513 grn_id
1515 {
1516  while (++id <= pat->header->curr_rec) {
1517  uint32_t key_size;
1518  const char *key = _grn_pat_key(ctx, pat, id, &key_size);
1519  if (id == grn_pat_get(ctx, pat, key, key_size, NULL)) {
1520  return id;
1521  }
1522  }
1523  return GRN_ID_NIL;
1524 }
1525 
1526 grn_id
1528 {
1529  uint32_t key_size;
1530  const char *key = _grn_pat_key(ctx, pat, id, &key_size);
1531  if (key && (id == _grn_pat_get(ctx, pat, key, key_size, NULL))) { return id; }
1532  return GRN_ID_NIL;
1533 }
1534 
1535 grn_id
1537 {
1538  return pat->header->curr_rec;
1539 }
1540 
1541 int
1542 grn_pat_scan(grn_ctx *ctx, grn_pat *pat, const char *str, unsigned int str_len,
1543  grn_pat_scan_hit *sh, unsigned int sh_size, const char **rest)
1544 {
1545  int n = 0;
1546  grn_id tid;
1547  if (pat->normalizer) {
1548  grn_obj *nstr = grn_string_open(ctx, str, str_len,
1550  if (nstr) {
1551  const short *cp = grn_string_get_checks(ctx, nstr);
1552  unsigned int offset = 0, offset0 = 0;
1553  unsigned int normalized_length_in_bytes;
1554  const char *sp, *se;
1555  grn_string_get_normalized(ctx, nstr, &sp, &normalized_length_in_bytes,
1556  NULL);
1557  se = sp + normalized_length_in_bytes;
1558  while (n < sh_size) {
1559  if ((tid = grn_pat_lcp_search(ctx, pat, sp, se - sp))) {
1560  uint32_t len;
1561  _grn_pat_key(ctx, pat, tid, &len);
1562  sh[n].id = tid;
1563  sh[n].offset = (*cp > 0) ? offset : offset0;
1564  while (len--) {
1565  if (*cp > 0) { offset0 = offset; offset += *cp; }
1566  sp++; cp++;
1567  }
1568  sh[n].length = offset - sh[n].offset;
1569  n++;
1570  } else {
1571  if (*cp > 0) { offset0 = offset; offset += *cp; }
1572  do {
1573  sp++; cp++;
1574  } while (sp < se && !*cp);
1575  }
1576  if (se <= sp) { offset = str_len; break; }
1577  }
1578  if (rest) {
1579  grn_string_get_original(ctx, nstr, rest, NULL);
1580  *rest += offset;
1581  }
1582  grn_obj_close(ctx, nstr);
1583  } else {
1584  n = -1;
1585  if (rest) { *rest = str; }
1586  }
1587  } else {
1588  uint32_t len;
1589  const char *sp, *se = str + str_len;
1590  for (sp = str; sp < se && n < sh_size; sp += len) {
1591  if ((tid = grn_pat_lcp_search(ctx, pat, sp, se - sp))) {
1592  _grn_pat_key(ctx, pat, tid, &len);
1593  sh[n].id = tid;
1594  sh[n].offset = sp - str;
1595  sh[n].length = len;
1596  n++;
1597  } else {
1598  len = grn_charlen(ctx, sp, se);
1599  }
1600  if (!len) { break; }
1601  }
1602  if (rest) { *rest = sp; }
1603  }
1604  return n;
1605 }
1606 
1607 #define INITIAL_SIZE 512
1608 
1609 inline static void
1610 push(grn_pat_cursor *c, grn_id id, uint16_t check)
1611 {
1612  grn_ctx *ctx = c->ctx;
1614  if (c->size <= c->sp) {
1615  if (c->ss) {
1616  uint32_t size = c->size * 4;
1617  grn_pat_cursor_entry *ss = GRN_REALLOC(c->ss, size);
1618  if (!ss) { return; /* give up */ }
1619  c->ss = ss;
1620  c->size = size;
1621  } else {
1622  if (!(c->ss = GRN_MALLOC(sizeof(grn_pat_cursor_entry) * INITIAL_SIZE))) {
1623  return; /* give up */
1624  }
1625  c->size = INITIAL_SIZE;
1626  }
1627  }
1628  se = &c->ss[c->sp++];
1629  se->id = id;
1630  se->check = check;
1631 }
1632 
1633 inline static grn_pat_cursor_entry *
1634 pop(grn_pat_cursor *c)
1635 {
1636  return c->sp ? &c->ss[--c->sp] : NULL;
1637 }
1638 
1639 static grn_id
1640 grn_pat_cursor_next_by_id(grn_ctx *ctx, grn_pat_cursor *c)
1641 {
1642  grn_pat *pat = c->pat;
1643  int dir = (c->obj.header.flags & GRN_CURSOR_DESCENDING) ? -1 : 1;
1644  while (c->curr_rec != c->tail) {
1645  c->curr_rec += dir;
1646  if (pat->header->n_garbages) {
1647  uint32_t key_size;
1648  const void *key = _grn_pat_key(ctx, pat, c->curr_rec, &key_size);
1649  if (_grn_pat_get(ctx, pat, key, key_size, NULL) != c->curr_rec) {
1650  continue;
1651  }
1652  }
1653  c->rest--;
1654  return c->curr_rec;
1655  }
1656  return GRN_ID_NIL;
1657 }
1658 
1659 grn_id
1661 {
1662  pat_node *node;
1664  if (!c->rest) { return GRN_ID_NIL; }
1665  if ((c->obj.header.flags & GRN_CURSOR_BY_ID)) {
1666  return grn_pat_cursor_next_by_id(ctx, c);
1667  }
1668  while ((se = pop(c))) {
1669  grn_id id = se->id;
1670  int check = se->check, ch;
1671  for (;;) {
1672  if (id) {
1673  PAT_AT(c->pat, id, node);
1674  if (node) {
1675  ch = PAT_CHK(node);
1676  if (ch > check) {
1678  push(c, node->lr[0], ch);
1679  id = node->lr[1];
1680  } else {
1681  push(c, node->lr[1], ch);
1682  id = node->lr[0];
1683  }
1684  check = ch;
1685  continue;
1686  } else {
1687  if (id == c->tail) {
1688  c->sp = 0;
1689  } else {
1690  if (!c->curr_rec && c->tail) {
1691  uint32_t lmin, lmax;
1692  pat_node *nmin, *nmax;
1693  const uint8_t *kmin, *kmax;
1695  PAT_AT(c->pat, c->tail, nmin);
1696  PAT_AT(c->pat, id, nmax);
1697  } else {
1698  PAT_AT(c->pat, id, nmin);
1699  PAT_AT(c->pat, c->tail, nmax);
1700  }
1701  lmin = PAT_LEN(nmin);
1702  lmax = PAT_LEN(nmax);
1703  kmin = pat_node_get_key(ctx, c->pat, nmin);
1704  kmax = pat_node_get_key(ctx, c->pat, nmax);
1705  if ((lmin < lmax) ?
1706  (memcmp(kmin, kmax, lmin) > 0) :
1707  (memcmp(kmin, kmax, lmax) >= 0)) {
1708  c->sp = 0;
1709  break;
1710  }
1711  }
1712  }
1713  c->curr_rec = id;
1714  c->rest--;
1715  return id;
1716  }
1717  }
1718  } else {
1719  break;
1720  }
1721  }
1722  }
1723  return GRN_ID_NIL;
1724 }
1725 
1726 void
1728 {
1729  GRN_ASSERT(c->ctx == ctx);
1730  if (c->ss) { GRN_FREE(c->ss); }
1731  GRN_FREE(c);
1732 }
1733 
1734 inline static int
1735 bitcmp(const void *s1, const void *s2, int offset, int length)
1736 {
1737  int r, rest = length + (offset & 7) - 8, bl = offset >> 3, mask = 0xff >> (offset & 7);
1738  unsigned char *a = (unsigned char *)s1 + bl, *b = (unsigned char *)s2 + bl;
1739  if (rest <= 0) {
1740  mask &= 0xff << -rest;
1741  return (*a & mask) - (*b & mask);
1742  }
1743  if ((r = (*a & mask) - (*b & mask))) { return r; }
1744  a++; b++;
1745  if ((bl = rest >> 3)) {
1746  if ((r = memcmp(a, b, bl))) { return r; }
1747  a += bl; b += bl;
1748  }
1749  mask = 0xff << (8 - (rest & 7));
1750  return (*a & mask) - (*b & mask);
1751 }
1752 
1753 inline static grn_rc
1754 set_cursor_prefix(grn_ctx *ctx, grn_pat *pat, grn_pat_cursor *c,
1755  const void *key, uint32_t key_size, int flags)
1756 {
1757  int c0 = -1, ch;
1758  const uint8_t *k;
1759  uint32_t len, byte_len;
1760  grn_id id;
1761  pat_node *node;
1762  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
1763  if (flags & GRN_CURSOR_SIZE_BY_BIT) {
1764  len = key_size * 2;
1765  byte_len = key_size >> 3;
1766  } else {
1767  len = key_size * 16;
1768  byte_len = key_size;
1769  }
1770  KEY_ENCODE(pat, keybuf, key, byte_len);
1771  PAT_AT(pat, 0, node);
1772  id = node->lr[1];
1773  while (id) {
1774  PAT_AT(pat, id, node);
1775  if (!node) { return GRN_FILE_CORRUPT; }
1776  ch = PAT_CHK(node);
1777  if (c0 < ch && ch < len - 1) {
1778  if (ch & 1) {
1779  id = (ch + 1 < len) ? node->lr[1] : node->lr[0];
1780  } else {
1781  id = node->lr[nth_bit((uint8_t *)key, ch, len)];
1782  }
1783  c0 = ch;
1784  continue;
1785  }
1786  if (!(k = pat_node_get_key(ctx, pat, node))) { break; }
1787  if (PAT_LEN(node) < byte_len) { break; }
1788  if ((flags & GRN_CURSOR_SIZE_BY_BIT)
1789  ? !bitcmp(k, key, 0, key_size)
1790  : !memcmp(k, key, key_size)) {
1791  if (c0 < ch) {
1792  if (flags & GRN_CURSOR_DESCENDING) {
1793  if ((ch > len - 1) || !(flags & GRN_CURSOR_GT)) {
1794  push(c, node->lr[0], ch);
1795  }
1796  push(c, node->lr[1], ch);
1797  } else {
1798  push(c, node->lr[1], ch);
1799  if ((ch > len - 1) || !(flags & GRN_CURSOR_GT)) {
1800  push(c, node->lr[0], ch);
1801  }
1802  }
1803  } else {
1804  if (PAT_LEN(node) * 16 > len || !(flags & GRN_CURSOR_GT)) {
1805  push(c, id, ch);
1806  }
1807  }
1808  }
1809  break;
1810  }
1811  return GRN_SUCCESS;
1812 }
1813 
1814 inline static grn_rc
1815 set_cursor_near(grn_ctx *ctx, grn_pat *pat, grn_pat_cursor *c,
1816  uint32_t min_size, const void *key, int flags)
1817 {
1818  grn_id id;
1819  pat_node *node;
1820  const uint8_t *k;
1821  int r, check = -1, ch;
1822  uint32_t min = min_size * 16;
1823  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
1824  KEY_ENCODE(pat, keybuf, key, pat->key_size);
1825  PAT_AT(pat, 0, node);
1826  for (id = node->lr[1]; id;) {
1827  PAT_AT(pat, id, node);
1828  if (!node) { return GRN_FILE_CORRUPT; }
1829  ch = PAT_CHK(node);
1830  if (ch <= check) {
1831  if (check >= min) { push(c, id, check); }
1832  break;
1833  }
1834  if ((check += 2) < ch) {
1835  if (!(k = pat_node_get_key(ctx, pat, node))) { return GRN_FILE_CORRUPT; }
1836  if ((r = bitcmp(key, k, check >> 1, (ch - check) >> 1))) {
1837  if (ch >= min) {
1838  push(c, node->lr[1], ch);
1839  push(c, node->lr[0], ch);
1840  }
1841  break;
1842  }
1843  }
1844  check = ch;
1845  if (nth_bit((uint8_t *)key, check, pat->key_size)) {
1846  if (check >= min) { push(c, node->lr[0], check); }
1847  id = node->lr[1];
1848  } else {
1849  if (check >= min) { push(c, node->lr[1], check); }
1850  id = node->lr[0];
1851  }
1852  }
1853  return GRN_SUCCESS;
1854 }
1855 
1856 inline static grn_rc
1857 set_cursor_common_prefix(grn_ctx *ctx, grn_pat *pat, grn_pat_cursor *c,
1858  uint32_t min_size, const void *key, uint32_t key_size, int flags)
1859 {
1860  grn_id id;
1861  pat_node *node;
1862  const uint8_t *k;
1863  int check = -1, ch;
1864  uint32_t len = key_size * 16;
1865  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
1866  KEY_ENCODE(pat, keybuf, key, key_size);
1867  PAT_AT(pat, 0, node);
1868  for (id = node->lr[1]; id;) {
1869  PAT_AT(pat, id, node);
1870  if (!node) { return GRN_FILE_CORRUPT; }
1871  ch = PAT_CHK(node);
1872  if (ch <= check) {
1873  if (!(k = pat_node_get_key(ctx, pat, node))) { return GRN_FILE_CORRUPT; }
1874  {
1875  uint32_t l = PAT_LEN(node);
1876  if (min_size <= l && l <= key_size) {
1877  if (!memcmp(key, k, l)) { push(c, id, check); }
1878  }
1879  }
1880  break;
1881  }
1882  check = ch;
1883  if (len <= check) { break; }
1884  if (check & 1) {
1885  grn_id id0 = node->lr[0];
1886  pat_node *node0;
1887  PAT_AT(pat, id0, node0);
1888  if (!node0) { return GRN_FILE_CORRUPT; }
1889  if (!(k = pat_node_get_key(ctx, pat, node0))) { return GRN_FILE_CORRUPT; }
1890  {
1891  uint32_t l = PAT_LEN(node0);
1892  if (memcmp(key, k, l)) { break; }
1893  if (min_size <= l) {
1894  push(c, id0, check);
1895  }
1896  }
1897  id = node->lr[1];
1898  } else {
1899  id = node->lr[nth_bit((uint8_t *)key, check, len)];
1900  }
1901  }
1902  return GRN_SUCCESS;
1903 }
1904 
1905 inline static grn_rc
1906 set_cursor_ascend(grn_ctx *ctx, grn_pat *pat, grn_pat_cursor *c,
1907  const void *key, uint32_t key_size, int flags)
1908 {
1909  grn_id id;
1910  pat_node *node;
1911  const uint8_t *k;
1912  int r, check = -1, ch, c2;
1913  uint32_t len = key_size * 16;
1914  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
1915  KEY_ENCODE(pat, keybuf, key, key_size);
1916  PAT_AT(pat, 0, node);
1917  for (id = node->lr[1]; id;) {
1918  PAT_AT(pat, id, node);
1919  if (!node) { return GRN_FILE_CORRUPT; }
1920  ch = PAT_CHK(node);
1921  if (ch <= check) {
1922  if (!(k = pat_node_get_key(ctx, pat, node))) { return GRN_FILE_CORRUPT; }
1923  {
1924  uint32_t l = PAT_LEN(node);
1925  if (l == key_size) {
1926  if (flags & GRN_CURSOR_GT) {
1927  if (memcmp(key, k, l) < 0) { push(c, id, check); }
1928  } else {
1929  if (memcmp(key, k, l) <= 0) { push(c, id, check); }
1930  }
1931  } else if (l < key_size) {
1932  if (memcmp(key, k, l) < 0) { push(c, id, check); }
1933  } else {
1934  if (memcmp(key, k, key_size) <= 0) { push(c, id, check); }
1935  }
1936  }
1937  break;
1938  }
1939  c2 = len < ch ? len : ch;
1940  if ((check += 2) < c2) {
1941  if (!(k = pat_node_get_key(ctx, pat, node))) { return GRN_FILE_CORRUPT; }
1942  if ((r = bitcmp(key, k, check >> 1, ((c2 + 1) >> 1) - (check >> 1)))) {
1943  if (r < 0) {
1944  push(c, node->lr[1], ch);
1945  push(c, node->lr[0], ch);
1946  }
1947  break;
1948  }
1949  }
1950  check = ch;
1951  if (len <= check) {
1952  push(c, node->lr[1], ch);
1953  push(c, node->lr[0], ch);
1954  break;
1955  }
1956  if (check & 1) {
1957  if (check + 1 < len) {
1958  id = node->lr[1];
1959  } else {
1960  push(c, node->lr[1], check);
1961  id = node->lr[0];
1962  }
1963  } else {
1964  if (nth_bit((uint8_t *)key, check, len)) {
1965  id = node->lr[1];
1966  } else {
1967  push(c, node->lr[1], check);
1968  id = node->lr[0];
1969  }
1970  }
1971  }
1972  return GRN_SUCCESS;
1973 }
1974 
1975 inline static grn_rc
1976 set_cursor_descend(grn_ctx *ctx, grn_pat *pat, grn_pat_cursor *c,
1977  const void *key, uint32_t key_size, int flags)
1978 {
1979  grn_id id;
1980  pat_node *node;
1981  const uint8_t *k;
1982  int r, check = -1, ch, c2;
1983  uint32_t len = key_size * 16;
1984  uint8_t keybuf[MAX_FIXED_KEY_SIZE];
1985  KEY_ENCODE(pat, keybuf, key, key_size);
1986  PAT_AT(pat, 0, node);
1987  for (id = node->lr[1]; id;) {
1988  PAT_AT(pat, id, node);
1989  if (!node) { return GRN_FILE_CORRUPT; }
1990  ch = PAT_CHK(node);
1991  if (ch <= check) {
1992  if (!(k = pat_node_get_key(ctx, pat, node))) { return GRN_FILE_CORRUPT; }
1993  {
1994  uint32_t l = PAT_LEN(node);
1995  if (l <= key_size) {
1996  if ((flags & GRN_CURSOR_LT) && l == key_size) {
1997  if (memcmp(key, k, l) > 0) { push(c, id, check); }
1998  } else {
1999  if (memcmp(key, k, l) >= 0) { push(c, id, check); }
2000  }
2001  } else {
2002  if (memcmp(key, k, key_size) > 0) { push(c, id, check); }
2003  }
2004  }
2005  break;
2006  }
2007  c2 = len < ch ? len : ch;
2008  if ((check += 2) < c2) {
2009  if (!(k = pat_node_get_key(ctx, pat, node))) { return GRN_FILE_CORRUPT; }
2010  if ((r = bitcmp(key, k, check >> 1, ((c2 + 1) >> 1) - (check >> 1)))) {
2011  if (r >= 0) {
2012  push(c, node->lr[0], ch);
2013  push(c, node->lr[1], ch);
2014  }
2015  break;
2016  }
2017  }
2018  check = ch;
2019  if (len <= check) { break; }
2020  if (check & 1) {
2021  if (check + 1 < len) {
2022  push(c, node->lr[0], check);
2023  id = node->lr[1];
2024  } else {
2025  id = node->lr[0];
2026  }
2027  } else {
2028  if (nth_bit((uint8_t *)key, check, len)) {
2029  push(c, node->lr[0], check);
2030  id = node->lr[1];
2031  } else {
2032  id = node->lr[0];
2033  }
2034  }
2035  }
2036  return GRN_SUCCESS;
2037 }
2038 
2039 static grn_pat_cursor *
2040 grn_pat_cursor_open_by_id(grn_ctx *ctx, grn_pat *pat,
2041  const void *min, uint32_t min_size,
2042  const void *max, uint32_t max_size,
2043  int offset, int limit, int flags)
2044 {
2045  int dir;
2046  grn_pat_cursor *c;
2047  if (!pat || !ctx) { return NULL; }
2048  if (!(c = GRN_MALLOCN(grn_pat_cursor, 1))) { return NULL; }
2050  c->pat = pat;
2051  c->ctx = ctx;
2052  c->obj.header.flags = flags;
2053  c->obj.header.domain = GRN_ID_NIL;
2054  c->size = 0;
2055  c->sp = 0;
2056  c->ss = NULL;
2057  c->tail = 0;
2058  if (flags & GRN_CURSOR_DESCENDING) {
2059  dir = -1;
2060  if (max) {
2061  if (!(c->curr_rec = grn_pat_get(ctx, pat, max, max_size, NULL))) {
2062  c->tail = GRN_ID_NIL;
2063  goto exit;
2064  }
2065  if (!(flags & GRN_CURSOR_LT)) { c->curr_rec++; }
2066  } else {
2067  c->curr_rec = pat->header->curr_rec + 1;
2068  }
2069  if (min) {
2070  if (!(c->tail = grn_pat_get(ctx, pat, min, min_size, NULL))) {
2071  c->curr_rec = GRN_ID_NIL;
2072  goto exit;
2073  }
2074  if ((flags & GRN_CURSOR_GT)) { c->tail++; }
2075  } else {
2076  c->tail = GRN_ID_NIL + 1;
2077  }
2078  if (c->curr_rec < c->tail) { c->tail = c->curr_rec; }
2079  } else {
2080  dir = 1;
2081  if (min) {
2082  if (!(c->curr_rec = grn_pat_get(ctx, pat, min, min_size, NULL))) {
2083  c->tail = GRN_ID_NIL;
2084  goto exit;
2085  }
2086  if (!(flags & GRN_CURSOR_GT)) { c->curr_rec--; }
2087  } else {
2088  c->curr_rec = GRN_ID_NIL;
2089  }
2090  if (max) {
2091  if (!(c->tail = grn_pat_get(ctx, pat, max, max_size, NULL))) {
2092  c->curr_rec = GRN_ID_NIL;
2093  goto exit;
2094  }
2095  if ((flags & GRN_CURSOR_LT)) { c->tail--; }
2096  } else {
2097  c->tail = pat->header->curr_rec;
2098  }
2099  if (c->tail < c->curr_rec) { c->tail = c->curr_rec; }
2100  }
2101  if (pat->header->n_garbages) {
2102  while (offset && c->curr_rec != c->tail) {
2103  uint32_t key_size;
2104  const void *key;
2105  c->curr_rec += dir;
2106  key = _grn_pat_key(ctx, pat, c->curr_rec, &key_size);
2107  if (_grn_pat_get(ctx, pat, key, key_size, NULL) == c->curr_rec) {
2108  offset--;
2109  }
2110  }
2111  } else {
2112  if ((dir * (c->tail - c->curr_rec)) < offset) {
2113  c->curr_rec = c->tail;
2114  } else {
2115  c->curr_rec += dir * offset;
2116  }
2117  }
2118  c->rest = (limit < 0) ? GRN_ID_MAX : limit;
2119 exit :
2120  return c;
2121 }
2122 
2123 static grn_rc set_cursor_rk(grn_ctx *ctx, grn_pat *pat, grn_pat_cursor *c,
2124  const void *key, uint32_t key_size, int flags);
2125 
2128  const void *min, uint32_t min_size,
2129  const void *max, uint32_t max_size,
2130  int offset, int limit, int flags)
2131 {
2132  grn_id id;
2133  pat_node *node;
2134  grn_pat_cursor *c;
2135  if (!pat || !ctx) { return NULL; }
2136  if ((flags & GRN_CURSOR_BY_ID)) {
2137  return grn_pat_cursor_open_by_id(ctx, pat, min, min_size, max, max_size,
2138  offset, limit, flags);
2139  }
2140  if (!(c = GRN_MALLOCN(grn_pat_cursor, 1))) { return NULL; }
2142  c->pat = pat;
2143  c->ctx = ctx;
2144  c->size = 0;
2145  c->sp = 0;
2146  c->ss = NULL;
2147  c->tail = 0;
2148  c->rest = GRN_ID_MAX;
2149  c->curr_rec = GRN_ID_NIL;
2150  c->obj.header.domain = GRN_ID_NIL;
2151  if (flags & GRN_CURSOR_PREFIX) {
2152  if (max && max_size) {
2153  if ((pat->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE)) {
2154  set_cursor_common_prefix(ctx, pat, c, min_size, max, max_size, flags);
2155  } else {
2156  set_cursor_near(ctx, pat, c, min_size, max, flags);
2157  }
2158  goto exit;
2159  } else {
2160  if (min && min_size) {
2161  if (flags & GRN_CURSOR_RK) {
2162  set_cursor_rk(ctx, pat, c, min, min_size, flags);
2163  } else {
2164  set_cursor_prefix(ctx, pat, c, min, min_size, flags);
2165  }
2166  goto exit;
2167  }
2168  }
2169  }
2170  if (flags & GRN_CURSOR_DESCENDING) {
2171  if (min && min_size) {
2172  set_cursor_ascend(ctx, pat, c, min, min_size, flags);
2174  c->tail = grn_pat_cursor_next(ctx, c);
2175  c->sp = 0;
2176  if (!c->tail) { goto exit; }
2177  }
2178  if (max && max_size) {
2179  set_cursor_descend(ctx, pat, c, max, max_size, flags);
2180  } else {
2181  PAT_AT(pat, 0, node);
2182  if (!node) {
2183  grn_pat_cursor_close(ctx, c);
2184  return NULL;
2185  }
2186  if ((id = node->lr[1])) {
2187  PAT_AT(pat, id, node);
2188  if (node) {
2189  int ch = PAT_CHK(node);
2190  push(c, node->lr[0], ch);
2191  push(c, node->lr[1], ch);
2192  }
2193  }
2194  }
2195  } else {
2196  if (max && max_size) {
2197  set_cursor_descend(ctx, pat, c, max, max_size, flags);
2199  c->tail = grn_pat_cursor_next(ctx, c);
2200  c->sp = 0;
2201  if (!c->tail) { goto exit; }
2202  }
2203  if (min && min_size) {
2204  set_cursor_ascend(ctx, pat, c, min, min_size, flags);
2205  } else {
2206  PAT_AT(pat, 0, node);
2207  if (!node) {
2208  grn_pat_cursor_close(ctx, c);
2209  return NULL;
2210  }
2211  if ((id = node->lr[1])) {
2212  PAT_AT(pat, id, node);
2213  if (node) {
2214  int ch = PAT_CHK(node);
2215  push(c, node->lr[1], ch);
2216  push(c, node->lr[0], ch);
2217  }
2218  }
2219  }
2220  }
2221 exit :
2222  c->obj.header.flags = flags;
2223  c->curr_rec = GRN_ID_NIL;
2224  while (offset--) { grn_pat_cursor_next(ctx, c); }
2225  c->rest = (limit < 0) ? GRN_ID_MAX : limit;
2226  return c;
2227 }
2228 
2229 int
2231 {
2232  *key = c->curr_key;
2233  return grn_pat_get_key(ctx, c->pat, c->curr_rec, *key, GRN_TABLE_MAX_KEY_SIZE);
2234 }
2235 
2236 int
2238 {
2239  int value_size = (int)c->pat->value_size;
2240  if (value_size) {
2241  byte *v = (byte *)sis_at(ctx, c->pat, c->curr_rec);
2242  if (v) {
2243  if (c->pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) {
2244  *value = v + sizeof(sis_node);
2245  } else {
2246  *value = v;
2247  }
2248  } else {
2249  *value = NULL;
2250  }
2251  }
2252  return value_size;
2253 }
2254 
2255 int
2257  void **key, uint32_t *key_size, void **value)
2258 {
2259  int value_size = (int)c->pat->value_size;
2260  if (key_size) {
2261  *key_size = (uint32_t) grn_pat_get_key(ctx, c->pat, c->curr_rec, c->curr_key,
2263  if (key) { *key = c->curr_key; }
2264  }
2265  if (value && value_size) {
2266  byte *v = (byte *)sis_at(ctx, c->pat, c->curr_rec);
2267  if (v) {
2268  if (c->pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) {
2269  *value = v + sizeof(sis_node);
2270  } else {
2271  *value = v;
2272  }
2273  } else {
2274  *value = NULL;
2275  }
2276  }
2277  return value_size;
2278 }
2279 
2280 grn_rc
2282  const void *value, int flags)
2283 {
2284  return grn_pat_set_value(ctx, c->pat, c->curr_rec, value, flags);
2285 }
2286 
2287 grn_rc
2289  grn_table_delete_optarg *optarg)
2290 {
2291  return grn_pat_delete_by_id(ctx, c->pat, c->curr_rec, optarg);
2292 }
2293 
2294 void
2296 {
2297  char buf[8];
2298  struct grn_pat_header *h = pat->header;
2299  GRN_OUTPUT_ARRAY_OPEN("RESULT", 1);
2300  GRN_OUTPUT_MAP_OPEN("SUMMARY", 23);
2301  GRN_OUTPUT_CSTR("flags");
2302  grn_itoh(h->flags, buf, 8);
2303  GRN_OUTPUT_STR(buf, 8);
2304  GRN_OUTPUT_CSTR("key size");
2306  GRN_OUTPUT_CSTR("value_size");
2308  GRN_OUTPUT_CSTR("tokenizer");
2310  GRN_OUTPUT_CSTR("normalizer");
2312  GRN_OUTPUT_CSTR("n_entries");
2314  GRN_OUTPUT_CSTR("curr_rec");
2316  GRN_OUTPUT_CSTR("curr_key");
2318  GRN_OUTPUT_CSTR("curr_del");
2320  GRN_OUTPUT_CSTR("curr_del2");
2322  GRN_OUTPUT_CSTR("curr_del3");
2324  GRN_OUTPUT_CSTR("n_garbages");
2328 }
2329 
2330 static void
2331 grn_pat_inspect_check(grn_ctx *ctx, grn_obj *buf, int check)
2332 {
2333  GRN_TEXT_PUTS(ctx, buf, "{");
2334  grn_text_lltoa(ctx, buf, check >> 4);
2335  GRN_TEXT_PUTS(ctx, buf, ",");
2336  grn_text_lltoa(ctx, buf, (check >> 1) & 7);
2337  GRN_TEXT_PUTS(ctx, buf, ",");
2338  grn_text_lltoa(ctx, buf, check & 1);
2339  GRN_TEXT_PUTS(ctx, buf, "}");
2340 }
2341 
2342 static void
2343 grn_pat_inspect_node(grn_ctx *ctx, grn_pat *pat, grn_id id, int check,
2344  grn_obj *key_buf, int indent, const char *prefix,
2345  grn_obj *buf)
2346 {
2347  pat_node *node = NULL;
2348  int i, c;
2349 
2350  PAT_AT(pat, id, node);
2351  c = PAT_CHK(node);
2352 
2353  for (i = 0; i < indent; i++) {
2354  GRN_TEXT_PUTC(ctx, buf, ' ');
2355  }
2356  GRN_TEXT_PUTS(ctx, buf, prefix);
2357  grn_text_lltoa(ctx, buf, id);
2358 
2359  if (c > check) {
2360  int key_size;
2361  uint8_t *key;
2362 
2363  key_size = PAT_LEN(node);
2364  GRN_BULK_REWIND(key_buf);
2365  grn_bulk_space(ctx, key_buf, key_size);
2366  grn_pat_get_key(ctx, pat, id, GRN_BULK_HEAD(key_buf), key_size);
2367  GRN_TEXT_PUTS(ctx, buf, "(");
2368  grn_inspect(ctx, buf, key_buf);
2369  GRN_TEXT_PUTS(ctx, buf, ")");
2370 
2371  grn_pat_inspect_check(ctx, buf, c);
2372 
2373  GRN_TEXT_PUTS(ctx, buf, "[");
2374  key = pat_node_get_key(ctx, pat, node);
2375  for (i = 0; i < key_size; i++) {
2376  int j;
2377  uint8_t byte = key[i];
2378  if (i != 0) {
2379  GRN_TEXT_PUTS(ctx, buf, " ");
2380  }
2381  for (j = 0; j < 8; j++) {
2382  grn_text_lltoa(ctx, buf, (byte >> (7 - j)) & 1);
2383  }
2384  }
2385  GRN_TEXT_PUTS(ctx, buf, "]");
2386  }
2387 
2388  if (c > check) {
2389  GRN_TEXT_PUTS(ctx, buf, "\n");
2390  grn_pat_inspect_node(ctx, pat, node->lr[0], c, key_buf,
2391  indent + 2, "L:", buf);
2392  GRN_TEXT_PUTS(ctx, buf, "\n");
2393  grn_pat_inspect_node(ctx, pat, node->lr[1], c, key_buf,
2394  indent + 2, "R:", buf);
2395  }
2396 }
2397 
2398 void
2400 {
2401  pat_node *node;
2402  grn_obj key_buf;
2403 
2404  GRN_TEXT_PUTS(ctx, buf, "{");
2405  PAT_AT(pat, GRN_ID_NIL, node);
2406  if (node->lr[1]) {
2407  GRN_TEXT_PUTS(ctx, buf, "\n");
2408  GRN_OBJ_INIT(&key_buf, GRN_BULK, 0, pat->obj.header.domain);
2409  grn_pat_inspect_node(ctx, pat, node->lr[1], -1, &key_buf, 0, "", buf);
2410  GRN_OBJ_FIN(ctx, &key_buf);
2411  GRN_TEXT_PUTS(ctx, buf, "\n");
2412  }
2413  GRN_TEXT_PUTS(ctx, buf, "}");
2414 }
2415 
2416 static void
2417 grn_pat_cursor_inspect_entries(grn_ctx *ctx, grn_pat_cursor *c, grn_obj *buf)
2418 {
2419  int i;
2420  GRN_TEXT_PUTS(ctx, buf, "[");
2421  for (i = 0; i < c->sp; i++) {
2422  grn_pat_cursor_entry *e = c->ss + i;
2423  if (i != 0) {
2424  GRN_TEXT_PUTS(ctx, buf, ", ");
2425  }
2426  GRN_TEXT_PUTS(ctx, buf, "[");
2427  grn_text_lltoa(ctx, buf, e->id);
2428  GRN_TEXT_PUTS(ctx, buf, ",");
2429  grn_pat_inspect_check(ctx, buf, e->check);
2430  GRN_TEXT_PUTS(ctx, buf, "]");
2431  }
2432  GRN_TEXT_PUTS(ctx, buf, "]");
2433 }
2434 
2435 void
2437 {
2438  GRN_TEXT_PUTS(ctx, buf, "#<cursor:pat:");
2439  grn_inspect_name(ctx, buf, (grn_obj *)(c->pat));
2440 
2441  GRN_TEXT_PUTS(ctx, buf, " ");
2442  GRN_TEXT_PUTS(ctx, buf, "current:");
2443  grn_text_lltoa(ctx, buf, c->curr_rec);
2444 
2445  GRN_TEXT_PUTS(ctx, buf, " ");
2446  GRN_TEXT_PUTS(ctx, buf, "tail:");
2447  grn_text_lltoa(ctx, buf, c->tail);
2448 
2449  GRN_TEXT_PUTS(ctx, buf, " ");
2450  GRN_TEXT_PUTS(ctx, buf, "flags:");
2451  if (c->obj.header.flags & GRN_CURSOR_PREFIX) {
2452  GRN_TEXT_PUTS(ctx, buf, "prefix");
2453  } else {
2454  if (c->obj.header.flags & GRN_CURSOR_DESCENDING) {
2455  GRN_TEXT_PUTS(ctx, buf, "descending");
2456  } else {
2457  GRN_TEXT_PUTS(ctx, buf, "ascending");
2458  }
2459  GRN_TEXT_PUTS(ctx, buf, "|");
2460  if (c->obj.header.flags & GRN_CURSOR_GT) {
2461  GRN_TEXT_PUTS(ctx, buf, "greater-than");
2462  } else {
2463  GRN_TEXT_PUTS(ctx, buf, "greater");
2464  }
2465  GRN_TEXT_PUTS(ctx, buf, "|");
2466  if (c->obj.header.flags & GRN_CURSOR_LT) {
2467  GRN_TEXT_PUTS(ctx, buf, "less-than");
2468  } else {
2469  GRN_TEXT_PUTS(ctx, buf, "less");
2470  }
2471  if (c->obj.header.flags & GRN_CURSOR_BY_ID) {
2472  GRN_TEXT_PUTS(ctx, buf, "|by-id");
2473  }
2474  if (c->obj.header.flags & GRN_CURSOR_BY_KEY) {
2475  GRN_TEXT_PUTS(ctx, buf, "|by-key");
2476  }
2477  }
2478 
2479  GRN_TEXT_PUTS(ctx, buf, " ");
2480  GRN_TEXT_PUTS(ctx, buf, "rest:");
2481  grn_text_lltoa(ctx, buf, c->rest);
2482 
2483  GRN_TEXT_PUTS(ctx, buf, " ");
2484  GRN_TEXT_PUTS(ctx, buf, "entries:");
2485  grn_pat_cursor_inspect_entries(ctx, c, buf);
2486 
2487  GRN_TEXT_PUTS(ctx, buf, ">");
2488 }
2489 
2490 typedef struct {
2491  uint8_t code;
2492  uint8_t next;
2493  uint8_t emit;
2494  uint8_t attr;
2495 } rk_tree_node;
2496 
2497 static uint16_t rk_str_idx[] = {
2498  0x0003, 0x0006, 0x0009, 0x000c, 0x0012, 0x0015, 0x0018, 0x001e, 0x0024, 0x002a,
2499  0x0030, 0x0036, 0x003c, 0x0042, 0x0048, 0x004e, 0x0054, 0x005a, 0x0060, 0x0066,
2500  0x006c, 0x0072, 0x0078, 0x007e, 0x0084, 0x008a, 0x0090, 0x0096, 0x009c, 0x00a2,
2501  0x00a8, 0x00ae, 0x00b4, 0x00ba, 0x00c0, 0x00c3, 0x00c6, 0x00c9, 0x00cc, 0x00cf,
2502  0x00d2, 0x00d5, 0x00db, 0x00e1, 0x00e7, 0x00ea, 0x00f0, 0x00f6, 0x00fc, 0x00ff,
2503  0x0105, 0x0108, 0x010e, 0x0111, 0x0114, 0x0117, 0x011a, 0x011d, 0x0120, 0x0123,
2504  0x0129, 0x012f, 0x0135, 0x013b, 0x013e, 0x0144, 0x014a, 0x0150, 0x0156, 0x0159,
2505  0x015c, 0x015f, 0x0162, 0x0165, 0x0168, 0x016b, 0x016e, 0x0171, 0x0177, 0x017d,
2506  0x0183, 0x0189, 0x018c, 0x0192, 0x0198, 0x019e, 0x01a1, 0x01a4, 0x01aa, 0x01b0,
2507  0x01b6, 0x01bc, 0x01bf, 0x01c2, 0x01c8, 0x01ce, 0x01d1, 0x01d7, 0x01dd, 0x01e0,
2508  0x01e6, 0x01e9, 0x01ef, 0x01f2, 0x01f5, 0x01fb, 0x0201, 0x0207, 0x020d, 0x0213,
2509  0x0216, 0x0219, 0x021c, 0x021f, 0x0222, 0x0225, 0x0228, 0x022e, 0x0234, 0x023a,
2510  0x023d, 0x0243, 0x0249, 0x024f, 0x0252, 0x0258, 0x025e, 0x0264, 0x0267, 0x026d,
2511  0x0273, 0x0279, 0x027f, 0x0285, 0x0288, 0x028b, 0x028e, 0x0291, 0x0294, 0x0297,
2512  0x029a, 0x029d, 0x02a0, 0x02a3, 0x02a9, 0x02af, 0x02b5, 0x02b8, 0x02bb, 0x02be,
2513  0x02c1, 0x02c4, 0x02c7, 0x02ca, 0x02cd, 0x02d0, 0x02d3, 0x02d6, 0x02dc, 0x02e2,
2514  0x02e8, 0x02eb, 0x02ee, 0x02f1, 0x02f4, 0x02f7, 0x02fa, 0x02fd, 0x0300, 0x0303,
2515  0x0309, 0x030c, 0x0312, 0x0318, 0x031e, 0x0324, 0x0327, 0x032a, 0x032d
2516 };
2517 static char rk_str[] = {
2518  0xe3, 0x82, 0xa1, 0xe3, 0x82, 0xa2, 0xe3, 0x82, 0xa3, 0xe3, 0x82, 0xa4, 0xe3,
2519  0x82, 0xa4, 0xe3, 0x82, 0xa7, 0xe3, 0x82, 0xa5, 0xe3, 0x82, 0xa6, 0xe3, 0x82,
2520  0xa6, 0xe3, 0x82, 0xa2, 0xe3, 0x82, 0xa6, 0xe3, 0x82, 0xa3, 0xe3, 0x82, 0xa6,
2521  0xe3, 0x82, 0xa4, 0xe3, 0x82, 0xa6, 0xe3, 0x82, 0xa6, 0xe3, 0x82, 0xa6, 0xe3,
2522  0x82, 0xa7, 0xe3, 0x82, 0xa6, 0xe3, 0x82, 0xa8, 0xe3, 0x82, 0xa6, 0xe3, 0x82,
2523  0xaa, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa0, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa1,
2524  0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa2, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa3, 0xe3,
2525  0x82, 0xa6, 0xe3, 0x83, 0xa4, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa5, 0xe3, 0x82,
2526  0xa6, 0xe3, 0x83, 0xa6, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xa6,
2527  0xe3, 0x83, 0xa8, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa9, 0xe3, 0x82, 0xa6, 0xe3,
2528  0x83, 0xaa, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xab, 0xe3, 0x82, 0xa6, 0xe3, 0x83,
2529  0xac, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xad, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xae,
2530  0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xaf, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xb0, 0xe3,
2531  0x82, 0xa6, 0xe3, 0x83, 0xb1, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xb2, 0xe3, 0x82,
2532  0xa6, 0xe3, 0x83, 0xb3, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xbc, 0xe3, 0x82, 0xa7,
2533  0xe3, 0x82, 0xa8, 0xe3, 0x82, 0xa9, 0xe3, 0x82, 0xaa, 0xe3, 0x82, 0xab, 0xe3,
2534  0x82, 0xac, 0xe3, 0x82, 0xad, 0xe3, 0x82, 0xad, 0xe3, 0x83, 0xa3, 0xe3, 0x82,
2535  0xad, 0xe3, 0x83, 0xa5, 0xe3, 0x82, 0xad, 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xae,
2536  0xe3, 0x82, 0xae, 0xe3, 0x83, 0xa3, 0xe3, 0x82, 0xae, 0xe3, 0x83, 0xa5, 0xe3,
2537  0x82, 0xae, 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xaf, 0xe3, 0x82, 0xaf, 0xe3, 0x82,
2538  0xa1, 0xe3, 0x82, 0xb0, 0xe3, 0x82, 0xb0, 0xe3, 0x82, 0xa1, 0xe3, 0x82, 0xb1,
2539  0xe3, 0x82, 0xb2, 0xe3, 0x82, 0xb3, 0xe3, 0x82, 0xb4, 0xe3, 0x82, 0xb5, 0xe3,
2540  0x82, 0xb6, 0xe3, 0x82, 0xb7, 0xe3, 0x82, 0xb7, 0xe3, 0x82, 0xa7, 0xe3, 0x82,
2541  0xb7, 0xe3, 0x83, 0xa3, 0xe3, 0x82, 0xb7, 0xe3, 0x83, 0xa5, 0xe3, 0x82, 0xb7,
2542  0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xb8, 0xe3, 0x82, 0xb8, 0xe3, 0x82, 0xa7, 0xe3,
2543  0x82, 0xb8, 0xe3, 0x83, 0xa3, 0xe3, 0x82, 0xb8, 0xe3, 0x83, 0xa5, 0xe3, 0x82,
2544  0xb8, 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xb9, 0xe3, 0x82, 0xba, 0xe3, 0x82, 0xbb,
2545  0xe3, 0x82, 0xbc, 0xe3, 0x82, 0xbd, 0xe3, 0x82, 0xbe, 0xe3, 0x82, 0xbf, 0xe3,
2546  0x83, 0x80, 0xe3, 0x83, 0x81, 0xe3, 0x83, 0x81, 0xe3, 0x82, 0xa7, 0xe3, 0x83,
2547  0x81, 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0x81, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x81,
2548  0xe3, 0x83, 0xa7, 0xe3, 0x83, 0x82, 0xe3, 0x83, 0x82, 0xe3, 0x83, 0xa3, 0xe3,
2549  0x83, 0x82, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x82, 0xe3, 0x83, 0xa7, 0xe3, 0x83,
2550  0x83, 0xe3, 0x83, 0x84, 0xe3, 0x83, 0x84, 0xe3, 0x82, 0xa1, 0xe3, 0x83, 0x84,
2551  0xe3, 0x82, 0xa3, 0xe3, 0x83, 0x84, 0xe3, 0x82, 0xa7, 0xe3, 0x83, 0x84, 0xe3,
2552  0x82, 0xa9, 0xe3, 0x83, 0x85, 0xe3, 0x83, 0x86, 0xe3, 0x83, 0x86, 0xe3, 0x82,
2553  0xa3, 0xe3, 0x83, 0x86, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x87, 0xe3, 0x83, 0x87,
2554  0xe3, 0x82, 0xa3, 0xe3, 0x83, 0x87, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x88, 0xe3,
2555  0x83, 0x88, 0xe3, 0x82, 0xa5, 0xe3, 0x83, 0x89, 0xe3, 0x83, 0x89, 0xe3, 0x82,
2556  0xa5, 0xe3, 0x83, 0x8a, 0xe3, 0x83, 0x8b, 0xe3, 0x83, 0x8b, 0xe3, 0x82, 0xa3,
2557  0xe3, 0x83, 0x8b, 0xe3, 0x82, 0xa7, 0xe3, 0x83, 0x8b, 0xe3, 0x83, 0xa3, 0xe3,
2558  0x83, 0x8b, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x8b, 0xe3, 0x83, 0xa7, 0xe3, 0x83,
2559  0x8c, 0xe3, 0x83, 0x8d, 0xe3, 0x83, 0x8e, 0xe3, 0x83, 0x8f, 0xe3, 0x83, 0x90,
2560  0xe3, 0x83, 0x91, 0xe3, 0x83, 0x92, 0xe3, 0x83, 0x92, 0xe3, 0x83, 0xa3, 0xe3,
2561  0x83, 0x92, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x92, 0xe3, 0x83, 0xa7, 0xe3, 0x83,
2562  0x93, 0xe3, 0x83, 0x93, 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0x93, 0xe3, 0x83, 0xa5,
2563  0xe3, 0x83, 0x93, 0xe3, 0x83, 0xa7, 0xe3, 0x83, 0x94, 0xe3, 0x83, 0x94, 0xe3,
2564  0x83, 0xa3, 0xe3, 0x83, 0x94, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x94, 0xe3, 0x83,
2565  0xa7, 0xe3, 0x83, 0x95, 0xe3, 0x83, 0x95, 0xe3, 0x82, 0xa1, 0xe3, 0x83, 0x95,
2566  0xe3, 0x82, 0xa3, 0xe3, 0x83, 0x95, 0xe3, 0x82, 0xa7, 0xe3, 0x83, 0x95, 0xe3,
2567  0x82, 0xa9, 0xe3, 0x83, 0x95, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x96, 0xe3, 0x83,
2568  0x97, 0xe3, 0x83, 0x98, 0xe3, 0x83, 0x99, 0xe3, 0x83, 0x9a, 0xe3, 0x83, 0x9b,
2569  0xe3, 0x83, 0x9c, 0xe3, 0x83, 0x9d, 0xe3, 0x83, 0x9e, 0xe3, 0x83, 0x9f, 0xe3,
2570  0x83, 0x9f, 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0x9f, 0xe3, 0x83, 0xa5, 0xe3, 0x83,
2571  0x9f, 0xe3, 0x83, 0xa7, 0xe3, 0x83, 0xa0, 0xe3, 0x83, 0xa1, 0xe3, 0x83, 0xa2,
2572  0xe3, 0x83, 0xa3, 0xe3, 0x83, 0xa4, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0xa6, 0xe3,
2573  0x83, 0xa7, 0xe3, 0x83, 0xa8, 0xe3, 0x83, 0xa9, 0xe3, 0x83, 0xaa, 0xe3, 0x83,
2574  0xaa, 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0xaa, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0xaa,
2575  0xe3, 0x83, 0xa7, 0xe3, 0x83, 0xab, 0xe3, 0x83, 0xac, 0xe3, 0x83, 0xad, 0xe3,
2576  0x83, 0xae, 0xe3, 0x83, 0xaf, 0xe3, 0x83, 0xb0, 0xe3, 0x83, 0xb1, 0xe3, 0x83,
2577  0xb2, 0xe3, 0x83, 0xb3, 0xe3, 0x83, 0xb3, 0xe3, 0x83, 0xbc, 0xe3, 0x83, 0xb4,
2578  0xe3, 0x83, 0xb4, 0xe3, 0x82, 0xa1, 0xe3, 0x83, 0xb4, 0xe3, 0x82, 0xa3, 0xe3,
2579  0x83, 0xb4, 0xe3, 0x82, 0xa7, 0xe3, 0x83, 0xb4, 0xe3, 0x82, 0xa9, 0xe3, 0x83,
2580  0xb5, 0xe3, 0x83, 0xb6, 0xe3, 0x83, 0xbc
2581 };
2582 static uint16_t rk_tree_idx[] = {
2583  0x001b, 0x0022, 0x0025, 0x0028, 0x002d, 0x0030, 0x0039, 0x003b, 0x003c, 0x003f,
2584  0x0046, 0x0047, 0x004f, 0x0050, 0x0053, 0x005a, 0x005d, 0x0064, 0x0067, 0x006f,
2585  0x0070, 0x0073, 0x007d, 0x007f, 0x0081, 0x0082, 0x0083, 0x0088, 0x008f, 0x0092,
2586  0x00af, 0x00b5, 0x00bc, 0x00bf, 0x00c6, 0x00c9, 0x00d1, 0x00d6, 0x00da, 0x00e4,
2587  0x00e6, 0x00eb, 0x00ec, 0x00f0, 0x00f6, 0x00fc, 0x00fe, 0x0108, 0x010a, 0x010c,
2588  0x010d, 0x010e, 0x0113, 0x0118, 0x011f, 0x0123, 0x0125, 0x0164, 0x0180, 0x0183,
2589  0x0199, 0x01ad
2590 };
2591 static rk_tree_node rk_tree[] = {
2592  {0x2d, 0x00, 0xb2, 0x01}, {0x61, 0x00, 0x01, 0x01}, {0x62, 0x01, 0xff, 0x01},
2593  {0x63, 0x03, 0xff, 0x01}, {0x64, 0x06, 0xff, 0x01}, {0x65, 0x00, 0x24, 0x01},
2594  {0x66, 0x0a, 0xff, 0x01}, {0x67, 0x0c, 0xff, 0x01}, {0x68, 0x0f, 0xff, 0x01},
2595  {0x69, 0x00, 0x03, 0x01}, {0x6a, 0x11, 0xff, 0x01}, {0x6b, 0x13, 0xff, 0x01},
2596  {0x6c, 0x16, 0xff, 0x01}, {0x6d, 0x1c, 0xff, 0x01}, {0x6e, 0x1e, 0xff, 0x01},
2597  {0x6f, 0x00, 0x26, 0x01}, {0x70, 0x20, 0xff, 0x01}, {0x72, 0x22, 0xff, 0x01},
2598  {0x73, 0x24, 0xff, 0x01}, {0x74, 0x27, 0xff, 0x01}, {0x75, 0x00, 0x06, 0x01},
2599  {0x76, 0x2c, 0xff, 0x01}, {0x77, 0x2d, 0xff, 0x01}, {0x78, 0x2f, 0xff, 0x01},
2600  {0x79, 0x35, 0xff, 0x01}, {0x7a, 0x36, 0xff, 0x01}, {0xe3, 0x38, 0xff, 0x01},
2601  {0x61, 0x00, 0x72, 0x01}, {0x62, 0x01, 0x56, 0x01}, {0x65, 0x00, 0x89, 0x01},
2602  {0x69, 0x00, 0x78, 0x01}, {0x6f, 0x00, 0x8c, 0x01}, {0x75, 0x00, 0x86, 0x01},
2603  {0x79, 0x02, 0xff, 0x00}, {0x61, 0x00, 0x79, 0x01}, {0x6f, 0x00, 0x7b, 0x01},
2604  {0x75, 0x00, 0x7a, 0x01}, {0x63, 0x03, 0x56, 0x01}, {0x68, 0x04, 0xff, 0x01},
2605  {0x79, 0x05, 0xff, 0x01}, {0x61, 0x00, 0x4f, 0x00}, {0x65, 0x00, 0x4e, 0x00},
2606  {0x69, 0x00, 0x4d, 0x01}, {0x6f, 0x00, 0x51, 0x00}, {0x75, 0x00, 0x50, 0x00},
2607  {0x61, 0x00, 0x4f, 0x01}, {0x6f, 0x00, 0x51, 0x01}, {0x75, 0x00, 0x50, 0x01},
2608  {0x61, 0x00, 0x4c, 0x01}, {0x64, 0x06, 0x56, 0x01}, {0x65, 0x00, 0x60, 0x01},
2609  {0x68, 0x07, 0xff, 0x00}, {0x69, 0x00, 0x61, 0x00}, {0x6f, 0x00, 0x65, 0x01},
2610  {0x75, 0x00, 0x5c, 0x01}, {0x77, 0x08, 0xff, 0x00}, {0x79, 0x09, 0xff, 0x01},
2611  {0x69, 0x00, 0x61, 0x01}, {0x75, 0x00, 0x62, 0x01}, {0x75, 0x00, 0x66, 0x01},
2612  {0x61, 0x00, 0x53, 0x01}, {0x6f, 0x00, 0x55, 0x01}, {0x75, 0x00, 0x54, 0x01},
2613  {0x61, 0x00, 0x81, 0x00}, {0x65, 0x00, 0x83, 0x00}, {0x66, 0x0a, 0x56, 0x01},
2614  {0x69, 0x00, 0x82, 0x00}, {0x6f, 0x00, 0x84, 0x00}, {0x75, 0x00, 0x80, 0x01},
2615  {0x79, 0x0b, 0xff, 0x00}, {0x75, 0x00, 0x85, 0x01}, {0x61, 0x00, 0x28, 0x01},
2616  {0x65, 0x00, 0x36, 0x01}, {0x67, 0x0c, 0x56, 0x01}, {0x69, 0x00, 0x2d, 0x01},
2617  {0x6f, 0x00, 0x38, 0x01}, {0x75, 0x00, 0x33, 0x01}, {0x77, 0x0d, 0xff, 0x00},
2618  {0x79, 0x0e, 0xff, 0x00}, {0x61, 0x00, 0x34, 0x01}, {0x61, 0x00, 0x2e, 0x01},
2619  {0x6f, 0x00, 0x30, 0x01}, {0x75, 0x00, 0x2f, 0x01}, {0x61, 0x00, 0x71, 0x01},
2620  {0x65, 0x00, 0x88, 0x01}, {0x68, 0x0f, 0x56, 0x01}, {0x69, 0x00, 0x74, 0x01},
2621  {0x6f, 0x00, 0x8b, 0x01}, {0x75, 0x00, 0x80, 0x01}, {0x79, 0x10, 0xff, 0x00},
2622  {0x61, 0x00, 0x75, 0x01}, {0x6f, 0x00, 0x77, 0x01}, {0x75, 0x00, 0x76, 0x01},
2623  {0x61, 0x00, 0x42, 0x00}, {0x65, 0x00, 0x41, 0x00}, {0x69, 0x00, 0x40, 0x01},
2624  {0x6a, 0x11, 0x56, 0x01}, {0x6f, 0x00, 0x44, 0x00}, {0x75, 0x00, 0x43, 0x00},
2625  {0x79, 0x12, 0xff, 0x00}, {0x61, 0x00, 0x42, 0x01}, {0x6f, 0x00, 0x44, 0x01},
2626  {0x75, 0x00, 0x43, 0x01}, {0x61, 0x00, 0x27, 0x01}, {0x65, 0x00, 0x35, 0x01},
2627  {0x69, 0x00, 0x29, 0x01}, {0x6b, 0x13, 0x56, 0x01}, {0x6f, 0x00, 0x37, 0x01},
2628  {0x75, 0x00, 0x31, 0x01}, {0x77, 0x14, 0xff, 0x00}, {0x79, 0x15, 0xff, 0x00},
2629  {0x61, 0x00, 0x32, 0x01}, {0x61, 0x00, 0x2a, 0x01}, {0x6f, 0x00, 0x2c, 0x01},
2630  {0x75, 0x00, 0x2b, 0x01}, {0x61, 0x00, 0x00, 0x01}, {0x65, 0x00, 0x23, 0x01},
2631  {0x69, 0x00, 0x02, 0x01}, {0x6b, 0x17, 0xff, 0x01}, {0x6c, 0x16, 0x56, 0x01},
2632  {0x6f, 0x00, 0x25, 0x01}, {0x74, 0x18, 0xff, 0x01}, {0x75, 0x00, 0x05, 0x01},
2633  {0x77, 0x1a, 0xff, 0x01}, {0x79, 0x1b, 0xff, 0x01}, {0x61, 0x00, 0xb0, 0x01},
2634  {0x65, 0x00, 0xb1, 0x01}, {0x73, 0x19, 0xff, 0x00}, {0x75, 0x00, 0x56, 0x01},
2635  {0x75, 0x00, 0x56, 0x01}, {0x61, 0x00, 0xa4, 0x01}, {0x61, 0x00, 0x96, 0x01},
2636  {0x65, 0x00, 0x23, 0x01}, {0x69, 0x00, 0x02, 0x01}, {0x6f, 0x00, 0x9a, 0x01},
2637  {0x75, 0x00, 0x98, 0x01}, {0x61, 0x00, 0x8e, 0x01}, {0x65, 0x00, 0x94, 0x01},
2638  {0x69, 0x00, 0x8f, 0x01}, {0x6d, 0x1c, 0x56, 0x01}, {0x6f, 0x00, 0x95, 0x01},
2639  {0x75, 0x00, 0x93, 0x01}, {0x79, 0x1d, 0xff, 0x00}, {0x61, 0x00, 0x90, 0x01},
2640  {0x6f, 0x00, 0x92, 0x01}, {0x75, 0x00, 0x91, 0x01}, {0x00, 0x00, 0xa9, 0x01},
2641  {0x27, 0x00, 0xa9, 0x00}, {0x2d, 0x00, 0xaa, 0x00}, {0x61, 0x00, 0x67, 0x01},
2642  {0x62, 0x01, 0xa9, 0x00}, {0x63, 0x03, 0xa9, 0x00}, {0x64, 0x06, 0xa9, 0x00},
2643  {0x65, 0x00, 0x6f, 0x01}, {0x66, 0x0a, 0xa9, 0x00}, {0x67, 0x0c, 0xa9, 0x00},
2644  {0x68, 0x0f, 0xa9, 0x00}, {0x69, 0x00, 0x68, 0x01}, {0x6a, 0x11, 0xa9, 0x00},
2645  {0x6b, 0x13, 0xa9, 0x00}, {0x6c, 0x16, 0xa9, 0x00}, {0x6d, 0x1c, 0xa9, 0x00},
2646  {0x6e, 0x00, 0xa9, 0x00}, {0x6f, 0x00, 0x70, 0x01}, {0x70, 0x20, 0xa9, 0x00},
2647  {0x72, 0x22, 0xa9, 0x00}, {0x73, 0x24, 0xa9, 0x00}, {0x74, 0x27, 0xa9, 0x00},
2648  {0x75, 0x00, 0x6e, 0x01}, {0x76, 0x2c, 0xa9, 0x00}, {0x77, 0x2d, 0xa9, 0x00},
2649  {0x78, 0x2f, 0xa9, 0x00}, {0x79, 0x1f, 0xff, 0x00}, {0x7a, 0x36, 0xa9, 0x00},
2650  {0xe3, 0x38, 0xa9, 0x00}, {0x00, 0x00, 0xa9, 0x01}, {0x61, 0x00, 0x6b, 0x01},
2651  {0x65, 0x00, 0x6a, 0x01}, {0x69, 0x00, 0x69, 0x01}, {0x6f, 0x00, 0x6d, 0x01},
2652  {0x75, 0x00, 0x6c, 0x01}, {0x61, 0x00, 0x73, 0x01}, {0x65, 0x00, 0x8a, 0x01},
2653  {0x69, 0x00, 0x7c, 0x01}, {0x6f, 0x00, 0x8d, 0x01}, {0x70, 0x20, 0x56, 0x01},
2654  {0x75, 0x00, 0x87, 0x01}, {0x79, 0x21, 0xff, 0x00}, {0x61, 0x00, 0x7d, 0x01},
2655  {0x6f, 0x00, 0x7f, 0x01}, {0x75, 0x00, 0x7e, 0x01}, {0x61, 0x00, 0x9c, 0x01},
2656  {0x65, 0x00, 0xa2, 0x01}, {0x69, 0x00, 0x9d, 0x01}, {0x6f, 0x00, 0xa3, 0x01},
2657  {0x72, 0x22, 0x56, 0x01}, {0x75, 0x00, 0xa1, 0x01}, {0x79, 0x23, 0xff, 0x00},
2658  {0x61, 0x00, 0x9e, 0x01}, {0x6f, 0x00, 0xa0, 0x01}, {0x75, 0x00, 0x9f, 0x01},
2659  {0x61, 0x00, 0x39, 0x01}, {0x65, 0x00, 0x47, 0x01}, {0x68, 0x25, 0xff, 0x00},
2660  {0x69, 0x00, 0x3b, 0x01}, {0x6f, 0x00, 0x49, 0x01}, {0x73, 0x24, 0x56, 0x01},
2661  {0x75, 0x00, 0x45, 0x01}, {0x79, 0x26, 0xff, 0x00}, {0x61, 0x00, 0x3d, 0x00},
2662  {0x65, 0x00, 0x3c, 0x00}, {0x69, 0x00, 0x3b, 0x01}, {0x6f, 0x00, 0x3f, 0x00},
2663  {0x75, 0x00, 0x3e, 0x00}, {0x61, 0x00, 0x3d, 0x01}, {0x65, 0x00, 0x3c, 0x01},
2664  {0x6f, 0x00, 0x3f, 0x01}, {0x75, 0x00, 0x3e, 0x01}, {0x61, 0x00, 0x4b, 0x01},
2665  {0x65, 0x00, 0x5d, 0x01}, {0x68, 0x28, 0xff, 0x00}, {0x69, 0x00, 0x4d, 0x01},
2666  {0x6f, 0x00, 0x63, 0x01}, {0x73, 0x29, 0xff, 0x00}, {0x74, 0x27, 0x56, 0x01},
2667  {0x75, 0x00, 0x57, 0x01}, {0x77, 0x2a, 0xff, 0x00}, {0x79, 0x2b, 0xff, 0x00},
2668  {0x69, 0x00, 0x5e, 0x01}, {0x75, 0x00, 0x5f, 0x01}, {0x61, 0x00, 0x58, 0x00},
2669  {0x65, 0x00, 0x5a, 0x00}, {0x69, 0x00, 0x59, 0x00}, {0x6f, 0x00, 0x5b, 0x00},
2670  {0x75, 0x00, 0x57, 0x01}, {0x75, 0x00, 0x64, 0x01}, {0x61, 0x00, 0x4f, 0x01},
2671  {0x65, 0x00, 0x4e, 0x01}, {0x6f, 0x00, 0x51, 0x01}, {0x75, 0x00, 0x50, 0x01},
2672  {0x61, 0x00, 0xac, 0x00}, {0x65, 0x00, 0xae, 0x00}, {0x69, 0x00, 0xad, 0x00},
2673  {0x6f, 0x00, 0xaf, 0x00}, {0x75, 0x00, 0xab, 0x01}, {0x76, 0x2c, 0x56, 0x01},
2674  {0x61, 0x00, 0xa5, 0x01}, {0x65, 0x00, 0x0b, 0x01}, {0x69, 0x00, 0x08, 0x01},
2675  {0x6f, 0x00, 0xa8, 0x01}, {0x77, 0x2d, 0x56, 0x01}, {0x79, 0x2e, 0xff, 0x01},
2676  {0x65, 0x00, 0xa7, 0x01}, {0x69, 0x00, 0xa6, 0x01}, {0x61, 0x00, 0x00, 0x01},
2677  {0x65, 0x00, 0x23, 0x01}, {0x69, 0x00, 0x02, 0x01}, {0x6b, 0x30, 0xff, 0x01},
2678  {0x6f, 0x00, 0x25, 0x01}, {0x74, 0x31, 0xff, 0x01}, {0x75, 0x00, 0x05, 0x01},
2679  {0x77, 0x33, 0xff, 0x01}, {0x78, 0x2f, 0x56, 0x01}, {0x79, 0x34, 0xff, 0x01},
2680  {0x61, 0x00, 0xb0, 0x01}, {0x65, 0x00, 0xb1, 0x01}, {0x73, 0x32, 0xff, 0x00},
2681  {0x75, 0x00, 0x56, 0x01}, {0x75, 0x00, 0x56, 0x01}, {0x61, 0x00, 0xa4, 0x01},
2682  {0x61, 0x00, 0x96, 0x01}, {0x65, 0x00, 0x23, 0x01}, {0x69, 0x00, 0x02, 0x01},
2683  {0x6f, 0x00, 0x9a, 0x01}, {0x75, 0x00, 0x98, 0x01}, {0x61, 0x00, 0x97, 0x01},
2684  {0x65, 0x00, 0x04, 0x01}, {0x6f, 0x00, 0x9b, 0x01}, {0x75, 0x00, 0x99, 0x01},
2685  {0x79, 0x35, 0x56, 0x01}, {0x61, 0x00, 0x3a, 0x01}, {0x65, 0x00, 0x48, 0x01},
2686  {0x69, 0x00, 0x40, 0x01}, {0x6f, 0x00, 0x4a, 0x01}, {0x75, 0x00, 0x46, 0x01},
2687  {0x79, 0x37, 0xff, 0x00}, {0x7a, 0x36, 0x56, 0x01}, {0x61, 0x00, 0x42, 0x01},
2688  {0x65, 0x00, 0x41, 0x01}, {0x6f, 0x00, 0x44, 0x01}, {0x75, 0x00, 0x43, 0x01},
2689  {0x81, 0x39, 0xff, 0x01}, {0x82, 0x3d, 0xff, 0x01}, {0x81, 0x00, 0x00, 0x01},
2690  {0x82, 0x00, 0x01, 0x01}, {0x83, 0x00, 0x02, 0x01}, {0x84, 0x00, 0x03, 0x01},
2691  {0x85, 0x00, 0x05, 0x01}, {0x86, 0x3a, 0xff, 0x01}, {0x87, 0x00, 0x23, 0x01},
2692  {0x88, 0x00, 0x24, 0x01}, {0x89, 0x00, 0x25, 0x01}, {0x8a, 0x00, 0x26, 0x01},
2693  {0x8b, 0x00, 0x27, 0x01}, {0x8c, 0x00, 0x28, 0x01}, {0x8d, 0x00, 0x29, 0x01},
2694  {0x8e, 0x00, 0x2d, 0x01}, {0x8f, 0x00, 0x31, 0x01}, {0x90, 0x00, 0x33, 0x01},
2695  {0x91, 0x00, 0x35, 0x01}, {0x92, 0x00, 0x36, 0x01}, {0x93, 0x00, 0x37, 0x01},
2696  {0x94, 0x00, 0x38, 0x01}, {0x95, 0x00, 0x39, 0x01}, {0x96, 0x00, 0x3a, 0x01},
2697  {0x97, 0x00, 0x3b, 0x01}, {0x98, 0x00, 0x40, 0x01}, {0x99, 0x00, 0x45, 0x01},
2698  {0x9a, 0x00, 0x46, 0x01}, {0x9b, 0x00, 0x47, 0x01}, {0x9c, 0x00, 0x48, 0x01},
2699  {0x9d, 0x00, 0x49, 0x01}, {0x9e, 0x00, 0x4a, 0x01}, {0x9f, 0x00, 0x4b, 0x01},
2700  {0xa0, 0x00, 0x4c, 0x01}, {0xa1, 0x00, 0x4d, 0x01}, {0xa2, 0x00, 0x52, 0x01},
2701  {0xa3, 0x00, 0x56, 0x01}, {0xa4, 0x00, 0x57, 0x01}, {0xa5, 0x00, 0x5c, 0x01},
2702  {0xa6, 0x00, 0x5d, 0x01}, {0xa7, 0x00, 0x60, 0x01}, {0xa8, 0x00, 0x63, 0x01},
2703  {0xa9, 0x00, 0x65, 0x01}, {0xaa, 0x00, 0x67, 0x01}, {0xab, 0x00, 0x68, 0x01},
2704  {0xac, 0x00, 0x6e, 0x01}, {0xad, 0x00, 0x6f, 0x01}, {0xae, 0x00, 0x70, 0x01},
2705  {0xaf, 0x00, 0x71, 0x01}, {0xb0, 0x00, 0x72, 0x01}, {0xb1, 0x00, 0x73, 0x01},
2706  {0xb2, 0x00, 0x74, 0x01}, {0xb3, 0x00, 0x78, 0x01}, {0xb4, 0x00, 0x7c, 0x01},
2707  {0xb5, 0x00, 0x80, 0x01}, {0xb6, 0x00, 0x86, 0x01}, {0xb7, 0x00, 0x87, 0x01},
2708  {0xb8, 0x00, 0x88, 0x01}, {0xb9, 0x00, 0x89, 0x01}, {0xba, 0x00, 0x8a, 0x01},
2709  {0xbb, 0x00, 0x8b, 0x01}, {0xbc, 0x00, 0x8c, 0x01}, {0xbd, 0x00, 0x8d, 0x01},
2710  {0xbe, 0x00, 0x8e, 0x01}, {0xbf, 0x00, 0x8f, 0x01}, {0x00, 0x00, 0x06, 0x00},
2711  {0x2d, 0x00, 0x22, 0x00}, {0x61, 0x00, 0x07, 0x00}, {0x62, 0x01, 0x06, 0x00},
2712  {0x63, 0x03, 0x06, 0x00}, {0x64, 0x06, 0x06, 0x00}, {0x65, 0x00, 0x0c, 0x00},
2713  {0x66, 0x0a, 0x06, 0x00}, {0x67, 0x0c, 0x06, 0x00}, {0x68, 0x0f, 0x06, 0x00},
2714  {0x69, 0x00, 0x09, 0x00}, {0x6a, 0x11, 0x06, 0x00}, {0x6b, 0x13, 0x06, 0x00},
2715  {0x6c, 0x16, 0x06, 0x00}, {0x6d, 0x1c, 0x06, 0x00}, {0x6e, 0x1e, 0x06, 0x00},
2716  {0x6f, 0x00, 0x0d, 0x00}, {0x70, 0x20, 0x06, 0x00}, {0x72, 0x22, 0x06, 0x00},
2717  {0x73, 0x24, 0x06, 0x00}, {0x74, 0x27, 0x06, 0x00}, {0x75, 0x00, 0x0a, 0x00},
2718  {0x76, 0x2c, 0x06, 0x00}, {0x77, 0x2d, 0x06, 0x00}, {0x78, 0x2f, 0x06, 0x00},
2719  {0x79, 0x35, 0x06, 0x00}, {0x7a, 0x36, 0x06, 0x00}, {0xe3, 0x3b, 0xff, 0x01},
2720  {0x00, 0x00, 0x06, 0x00}, {0x81, 0x39, 0x06, 0x00}, {0x82, 0x3c, 0xff, 0x01},
2721  {0x00, 0x00, 0x06, 0x01}, {0x80, 0x00, 0x0e, 0x00}, {0x81, 0x00, 0x0f, 0x00},
2722  {0x82, 0x00, 0x10, 0x00}, {0x83, 0x00, 0x11, 0x00}, {0x84, 0x00, 0x12, 0x00},
2723  {0x85, 0x00, 0x13, 0x00}, {0x86, 0x00, 0x14, 0x00}, {0x87, 0x00, 0x15, 0x00},
2724  {0x88, 0x00, 0x16, 0x00}, {0x89, 0x00, 0x17, 0x00}, {0x8a, 0x00, 0x18, 0x00},
2725  {0x8b, 0x00, 0x19, 0x00}, {0x8c, 0x00, 0x1a, 0x00}, {0x8d, 0x00, 0x1b, 0x00},
2726  {0x8e, 0x00, 0x1c, 0x00}, {0x8f, 0x00, 0x1d, 0x00}, {0x90, 0x00, 0x1e, 0x00},
2727  {0x91, 0x00, 0x1f, 0x00}, {0x92, 0x00, 0x20, 0x00}, {0x93, 0x00, 0x21, 0x00},
2728  {0x9b, 0x00, 0xab, 0x01}, {0x80, 0x00, 0x93, 0x01}, {0x81, 0x00, 0x94, 0x01},
2729  {0x82, 0x00, 0x95, 0x01}, {0x83, 0x00, 0x96, 0x01}, {0x84, 0x00, 0x97, 0x01},
2730  {0x85, 0x00, 0x98, 0x01}, {0x86, 0x00, 0x99, 0x01}, {0x87, 0x00, 0x9a, 0x01},
2731  {0x88, 0x00, 0x9b, 0x01}, {0x89, 0x00, 0x9c, 0x01}, {0x8a, 0x00, 0x9d, 0x01},
2732  {0x8b, 0x00, 0xa1, 0x01}, {0x8c, 0x00, 0xa2, 0x01}, {0x8d, 0x00, 0xa3, 0x01},
2733  {0x8e, 0x00, 0xa4, 0x01}, {0x8f, 0x00, 0xa5, 0x01}, {0x90, 0x00, 0xa6, 0x01},
2734  {0x91, 0x00, 0xa7, 0x01}, {0x92, 0x00, 0xa8, 0x01}, {0x93, 0x00, 0xa9, 0x01}
2735 };
2736 
2737 static rk_tree_node *
2738 rk_lookup(uint8_t state, uint8_t code)
2739 {
2740  if (state < sizeof(rk_tree_idx)/sizeof(uint16_t)) {
2741  uint16_t ns = state ? rk_tree_idx[state - 1] : 0;
2742  uint16_t ne = rk_tree_idx[state];
2743  while (ns < ne) {
2744  uint16_t m = (ns + ne)>>1;
2745  rk_tree_node *rn = &rk_tree[m];
2746  if (rn->code == code) { return rn; }
2747  if (rn->code < code) {
2748  ns = m + 1;
2749  } else {
2750  ne = m;
2751  }
2752  }
2753  }
2754  return NULL;
2755 }
2756 
2757 static uint32_t
2758 rk_emit(rk_tree_node *rn, char **str)
2759 {
2760  if (rn && rn->emit != 0xff) {
2761  uint16_t pos = rn->emit ? rk_str_idx[rn->emit - 1] : 0;
2762  *str = &rk_str[pos];
2763  return (uint32_t)(rk_str_idx[rn->emit] - pos);
2764  } else {
2765  *str = NULL;
2766  return 0;
2767  }
2768 }
2769 
2770 #define RK_OUTPUT(e,l) do {\
2771  if (oc < oe) {\
2772  uint32_t l_ = (oc + (l) < oe) ? (l) : (oe - oc);\
2773  memcpy(oc, (e), l_);\
2774  oc += l_;\
2775  ic_ = ic;\
2776  }\
2777 } while (0)
2778 
2779 static uint32_t
2780 rk_conv(const char *str, uint32_t str_len, uint8_t *buf, uint32_t buf_size, uint8_t *statep)
2781 {
2782  uint32_t l;
2783  uint8_t state = 0;
2784  rk_tree_node *rn;
2785  char *e;
2786  uint8_t *oc = buf, *oe = oc + buf_size;
2787  const uint8_t *ic = (uint8_t *)str, *ic_ = ic, *ie = ic + str_len;
2788  while (ic < ie) {
2789  if ((rn = rk_lookup(state, *ic))) {
2790  ic++;
2791  if ((l = rk_emit(rn, &e))) { RK_OUTPUT(e, l); }
2792  state = rn->next;
2793  } else {
2794  if (!state) { ic++; }
2795  if (ic_ < ic) { RK_OUTPUT(ic_, ic - ic_); }
2796  state = 0;
2797  }
2798  }
2799 #ifdef FLUSH_UNRESOLVED_INPUT
2800  if ((rn = rk_lookup(state, 0))) {
2801  if ((l = rk_emit(rn, &e))) { RK_OUTPUT(e, l); }
2802  state = rn->next;
2803  } else {
2804  if (ic_ < ic) { RK_OUTPUT(ic_, ic - ic_); }
2805  }
2806 #endif /* FLUSH_UNRESOLVED_INPUT */
2807  *statep = state;
2808  return oc - buf;
2809 }
2810 
2811 static grn_id
2812 sub_search(grn_ctx *ctx, grn_pat *pat, grn_id id,
2813  int *c0, uint8_t *key, uint32_t key_len)
2814 {
2815  pat_node *pn;
2816  uint32_t len = key_len * 16;
2817  if (!key_len) { return id; }
2818  PAT_AT(pat, id, pn);
2819  while (pn) {
2820  int ch;
2821  ch = PAT_CHK(pn);
2822  if (*c0 < ch && ch < len - 1) {
2823  if (ch & 1) {
2824  id = (ch + 1 < len) ? pn->lr[1] : pn->lr[0];
2825  } else {
2826  id = pn->lr[nth_bit(key, ch, len)];
2827  }
2828  *c0 = ch;
2829  PAT_AT(pat, id, pn);
2830  } else {
2831  const uint8_t *k = pat_node_get_key(ctx, pat, pn);
2832  return (k && key_len <= PAT_LEN(pn) && !memcmp(k, key, key_len)) ? id : GRN_ID_NIL;
2833  }
2834  }
2835  return GRN_ID_NIL;
2836 }
2837 
2838 static void
2839 search_push(grn_ctx *ctx, grn_pat *pat, grn_pat_cursor *c,
2840  uint8_t *key, uint32_t key_len, uint8_t state, grn_id id, int c0, int flags)
2841 {
2842  if (state) {
2843  int step;
2844  uint16_t ns, ne;
2845  if (flags & GRN_CURSOR_DESCENDING) {
2846  ns = rk_tree_idx[state - 1];
2847  ne = rk_tree_idx[state];
2848  step = 1;
2849  } else {
2850  ns = rk_tree_idx[state] - 1;
2851  ne = rk_tree_idx[state - 1] - 1;
2852  step = -1;
2853  }
2854  for (; ns != ne; ns += step) {
2855  rk_tree_node *rn = &rk_tree[ns];
2856  if (rn->attr) {
2857  char *e;
2858  uint32_t l = rk_emit(rn, &e);
2859  if (l) {
2860  if (l + key_len <= GRN_TABLE_MAX_KEY_SIZE) {
2861  int ch = c0;
2862  grn_id i;
2863  memcpy(key + key_len, e, l);
2864  if ((i = sub_search(ctx, pat, id, &ch, key, key_len + l))) {
2865  search_push(ctx, pat, c, key, key_len + l, rn->next, i, ch, flags);
2866  }
2867  }
2868  } else {
2869  search_push(ctx, pat, c, key, key_len, rn->next, id, c0, flags);
2870  }
2871  }
2872  }
2873  } else {
2874  pat_node *pn;
2875  PAT_AT(pat, id, pn);
2876  if (pn) {
2877  int ch = PAT_CHK(pn);
2878  uint32_t len = key_len * 16;
2879  if (c0 < ch) {
2880  if (flags & GRN_CURSOR_DESCENDING) {
2881  if ((ch > len - 1) || !(flags & GRN_CURSOR_GT)) { push(c, pn->lr[0], ch); }
2882  push(c, pn->lr[1], ch);
2883  } else {
2884  push(c, pn->lr[1], ch);
2885  if ((ch > len - 1) || !(flags & GRN_CURSOR_GT)) { push(c, pn->lr[0], ch); }
2886  }
2887  } else {
2888  if (PAT_LEN(pn) * 16 > len || !(flags & GRN_CURSOR_GT)) { push(c, id, ch); }
2889  }
2890  }
2891  }
2892 }
2893 
2894 static grn_rc
2895 set_cursor_rk(grn_ctx *ctx, grn_pat *pat, grn_pat_cursor *c,
2896  const void *key, uint32_t key_len, int flags)
2897 {
2898  grn_id id;
2899  uint8_t state;
2900  pat_node *pn;
2901  int c0 = -1;
2902  uint32_t len, byte_len;
2903  uint8_t keybuf[GRN_TABLE_MAX_KEY_SIZE];
2904  if (flags & GRN_CURSOR_SIZE_BY_BIT) { return GRN_OPERATION_NOT_SUPPORTED; }
2905  byte_len = rk_conv(key, key_len, keybuf, GRN_TABLE_MAX_KEY_SIZE, &state);
2906  len = byte_len * 16;
2907  PAT_AT(pat, 0, pn);
2908  id = pn->lr[1];
2909  if ((id = sub_search(ctx, pat, id, &c0, keybuf, byte_len))) {
2910  search_push(ctx, pat, c, keybuf, byte_len, state, id, c0, flags);
2911  }
2912  return ctx->rc;
2913 }