/
db.c
12084 lines (11518 loc) · 355 KB
/
db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- c-basic-offset: 2 -*- */
/* Copyright(C) 2009-2015 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "grn.h"
#include "grn_db.h"
#include "grn_hash.h"
#include "grn_pat.h"
#include "grn_dat.h"
#include "grn_ii.h"
#include "grn_ctx_impl.h"
#include "grn_token_cursor.h"
#include "grn_tokenizers.h"
#include "grn_proc.h"
#include "grn_plugin.h"
#include "grn_geo.h"
#include "grn_snip.h"
#include "grn_string.h"
#include "grn_normalizer.h"
#include "grn_util.h"
#include <string.h>
#include <float.h>
typedef struct {
grn_id id;
unsigned int weight;
} weight_uvector_entry;
#define IS_WEIGHT_UVECTOR(obj) ((obj)->header.flags & GRN_OBJ_WITH_WEIGHT)
#define NEXT_ADDR(p) (((byte *)(p)) + sizeof(*(p)))
#define GRN_TABLE_GROUPED (0x01<<0)
#define GRN_TABLE_IS_GROUPED(table)\
((table)->header.impl_flags & GRN_TABLE_GROUPED)
#define GRN_TABLE_GROUPED_ON(table)\
((table)->header.impl_flags |= GRN_TABLE_GROUPED)
#define GRN_TABLE_IS_MULTI_KEYS_GROUPED(table)\
(GRN_TABLE_IS_GROUPED(table) &&\
table->header.domain == GRN_ID_NIL)
#define WITH_NORMALIZE(table,key,key_size,block) do {\
if ((table)->normalizer && key && key_size > 0) {\
grn_obj *nstr;\
if ((nstr = grn_string_open(ctx, key, key_size,\
(table)->normalizer, 0))) {\
const char *key;\
unsigned int key_size;\
grn_string_get_normalized(ctx, nstr, &key, &key_size, NULL);\
block\
grn_obj_close(ctx, nstr);\
}\
} else {\
block\
}\
} while (0)
inline static grn_id
grn_table_add_v_inline(grn_ctx *ctx, grn_obj *table, const void *key, int key_size,
void **value, int *added);
inline static void
grn_table_add_subrec_inline(grn_obj *table, grn_rset_recinfo *ri, int score,
grn_rset_posinfo *pi, int dir);
inline static grn_id
grn_table_cursor_next_inline(grn_ctx *ctx, grn_table_cursor *tc);
inline static int
grn_table_cursor_get_value_inline(grn_ctx *ctx, grn_table_cursor *tc, void **value);
static void grn_obj_ensure_bulk(grn_ctx *ctx, grn_obj *obj);
static void grn_obj_ensure_vector(grn_ctx *ctx, grn_obj *obj);
inline static void
grn_obj_get_range_info(grn_ctx *ctx, grn_obj *obj,
grn_id *range_id, grn_obj_flags *range_flags);
inline static void
gen_pathname(const char *path, char *buffer, int fno)
{
size_t len = strlen(path);
memcpy(buffer, path, len);
if (fno >= 0) {
buffer[len] = '.';
grn_itoh(fno, buffer + len + 1, 7);
buffer[len + 8] = '\0';
} else {
buffer[len] = '\0';
}
}
static grn_bool
is_text_object(grn_obj *object)
{
if (!object) {
return GRN_FALSE;
}
if (object->header.type != GRN_BULK) {
return GRN_FALSE;
}
switch (object->header.domain) {
case GRN_DB_SHORT_TEXT:
case GRN_DB_TEXT:
case GRN_DB_LONG_TEXT:
return GRN_TRUE;
default:
return GRN_FALSE;
}
}
static void
limited_size_inspect(grn_ctx *ctx, grn_obj *buffer, grn_obj *object)
{
unsigned int original_size = 0;
unsigned int max_size = GRN_CTX_MSGSIZE / 2;
if (object) {
original_size = GRN_BULK_VSIZE(object);
}
if (original_size > max_size && is_text_object(object)) {
grn_text_esc(ctx, buffer, GRN_TEXT_VALUE(object), max_size);
GRN_TEXT_PUTS(ctx, buffer, "...(");
grn_text_lltoa(ctx, buffer, original_size);
GRN_TEXT_PUTS(ctx, buffer, ")");
} else {
grn_inspect(ctx, buffer, object);
}
}
typedef struct {
grn_obj *ptr;
uint32_t lock;
uint32_t done;
} db_value;
grn_obj *
grn_db_create(grn_ctx *ctx, const char *path, grn_db_create_optarg *optarg)
{
grn_db *s;
GRN_API_ENTER;
if (!path || strlen(path) <= PATH_MAX - 14) {
if ((s = GRN_MALLOC(sizeof(grn_db)))) {
grn_bool use_default_db_key = GRN_TRUE;
grn_bool use_pat_as_db_keys = GRN_FALSE;
if (getenv("GRN_DB_KEY")) {
if (!strcmp(getenv("GRN_DB_KEY"), "pat")) {
use_default_db_key = GRN_FALSE;
use_pat_as_db_keys = GRN_TRUE;
} else if (!strcmp(getenv("GRN_DB_KEY"), "dat")) {
use_default_db_key = GRN_FALSE;
}
}
if (use_default_db_key && !strcmp(GRN_DEFAULT_DB_KEY, "pat")) {
use_pat_as_db_keys = GRN_TRUE;
}
grn_tiny_array_init(ctx, &s->values, sizeof(db_value),
GRN_TINY_ARRAY_CLEAR|
GRN_TINY_ARRAY_THREADSAFE|
GRN_TINY_ARRAY_USE_MALLOC);
if (use_pat_as_db_keys) {
s->keys = (grn_obj *)grn_pat_create(ctx, path, GRN_TABLE_MAX_KEY_SIZE,
0, GRN_OBJ_KEY_VAR_SIZE);
} else {
s->keys = (grn_obj *)grn_dat_create(ctx, path, GRN_TABLE_MAX_KEY_SIZE,
0, GRN_OBJ_KEY_VAR_SIZE);
}
if (s->keys) {
CRITICAL_SECTION_INIT(s->lock);
GRN_DB_OBJ_SET_TYPE(s, GRN_DB);
s->obj.db = (grn_obj *)s;
s->obj.header.domain = GRN_ID_NIL;
DB_OBJ(&s->obj)->range = GRN_ID_NIL;
// prepare builtin classes and load builtin plugins.
if (path) {
char specs_path[PATH_MAX];
gen_pathname(path, specs_path, 0);
if ((s->specs = grn_ja_create(ctx, specs_path, 65536, 0))) {
grn_ctx_use(ctx, (grn_obj *)s);
grn_db_init_builtin_types(ctx);
GRN_API_RETURN((grn_obj *)s);
} else {
ERR(GRN_NO_MEMORY_AVAILABLE,
"failed to create specs: <%s>", specs_path);
}
} else {
s->specs = NULL;
grn_ctx_use(ctx, (grn_obj *)s);
grn_db_init_builtin_types(ctx);
GRN_API_RETURN((grn_obj *)s);
}
if (use_pat_as_db_keys) {
grn_pat_close(ctx, (grn_pat *)s->keys);
grn_pat_remove(ctx, path);
} else {
grn_dat_close(ctx, (grn_dat *)s->keys);
grn_dat_remove(ctx, path);
}
}
grn_tiny_array_fin(&s->values);
GRN_FREE(s);
} else {
ERR(GRN_NO_MEMORY_AVAILABLE, "grn_db alloc failed");
}
} else {
ERR(GRN_INVALID_ARGUMENT, "too long path");
}
GRN_API_RETURN(NULL);
}
grn_obj *
grn_db_open(grn_ctx *ctx, const char *path)
{
grn_db *s;
GRN_API_ENTER;
if (path && strlen(path) <= PATH_MAX - 14) {
if ((s = GRN_MALLOC(sizeof(grn_db)))) {
uint32_t type = grn_io_detect_type(ctx, path);
grn_tiny_array_init(ctx, &s->values, sizeof(db_value),
GRN_TINY_ARRAY_CLEAR|
GRN_TINY_ARRAY_THREADSAFE|
GRN_TINY_ARRAY_USE_MALLOC);
switch (type) {
case GRN_TABLE_PAT_KEY :
s->keys = (grn_obj *)grn_pat_open(ctx, path);
break;
case GRN_TABLE_DAT_KEY :
s->keys = (grn_obj *)grn_dat_open(ctx, path);
break;
default :
s->keys = NULL;
break;
}
if (s->keys) {
char specs_path[PATH_MAX];
gen_pathname(path, specs_path, 0);
if ((s->specs = grn_ja_open(ctx, specs_path))) {
CRITICAL_SECTION_INIT(s->lock);
GRN_DB_OBJ_SET_TYPE(s, GRN_DB);
s->obj.db = (grn_obj *)s;
s->obj.header.domain = GRN_ID_NIL;
DB_OBJ(&s->obj)->range = GRN_ID_NIL;
grn_ctx_use(ctx, (grn_obj *)s);
#ifdef GRN_WITH_MECAB
if (grn_db_init_mecab_tokenizer(ctx)) {
ERRCLR(ctx);
}
#endif
grn_db_init_builtin_tokenizers(ctx);
grn_db_init_builtin_normalizers(ctx);
grn_db_init_builtin_query(ctx);
GRN_API_RETURN((grn_obj *)s);
}
switch (type) {
case GRN_TABLE_PAT_KEY :
grn_pat_close(ctx, (grn_pat *)s->keys);
break;
case GRN_TABLE_DAT_KEY :
grn_dat_close(ctx, (grn_dat *)s->keys);
break;
}
}
grn_tiny_array_fin(&s->values);
GRN_FREE(s);
} else {
ERR(GRN_NO_MEMORY_AVAILABLE, "grn_db alloc failed");
}
} else {
ERR(GRN_INVALID_ARGUMENT, "inappropriate path");
}
GRN_API_RETURN(NULL);
}
static grn_id
grn_db_curr_id(grn_ctx *ctx, grn_obj *db)
{
grn_id curr_id = GRN_ID_NIL;
grn_db *s = (grn_db *)db;
switch (s->keys->header.type) {
case GRN_TABLE_PAT_KEY :
curr_id = grn_pat_curr_id(ctx, (grn_pat *)s->keys);
break;
case GRN_TABLE_DAT_KEY :
curr_id = grn_dat_curr_id(ctx, (grn_dat *)s->keys);
break;
}
return curr_id;
}
/* s must be validated by caller */
grn_rc
grn_db_close(grn_ctx *ctx, grn_obj *db)
{
grn_id id;
db_value *vp;
grn_db *s = (grn_db *)db;
grn_bool ctx_used_db;
if (!s) { return GRN_INVALID_ARGUMENT; }
GRN_API_ENTER;
ctx_used_db = ctx->impl && ctx->impl->db == db;
if (ctx_used_db) {
grn_ctx_loader_clear(ctx);
if (ctx->impl->parser) {
grn_expr_parser_close(ctx);
}
}
GRN_TINY_ARRAY_EACH(&s->values, 1, grn_db_curr_id(ctx, db), id, vp, {
if (vp->ptr) { grn_obj_close(ctx, vp->ptr); }
});
if (ctx_used_db) {
if (ctx->impl->values) {
grn_db_obj *o;
GRN_ARRAY_EACH(ctx, ctx->impl->values, 0, 0, id, &o, {
grn_obj_close(ctx, *((grn_obj **)o));
});
grn_array_truncate(ctx, ctx->impl->values);
}
}
/* grn_tiny_array_fin should be refined.. */
#ifdef WIN32
{
grn_tiny_array *a = &s->values;
CRITICAL_SECTION_FIN(a->lock);
}
#endif
grn_tiny_array_fin(&s->values);
switch (s->keys->header.type) {
case GRN_TABLE_PAT_KEY :
grn_pat_close(ctx, (grn_pat *)s->keys);
break;
case GRN_TABLE_DAT_KEY :
grn_dat_close(ctx, (grn_dat *)s->keys);
break;
}
CRITICAL_SECTION_FIN(s->lock);
if (s->specs) { grn_ja_close(ctx, s->specs); }
GRN_FREE(s);
if (ctx_used_db) {
grn_cache *cache;
cache = grn_cache_current_get(ctx);
if (cache) {
grn_cache_expire(cache, -1);
}
ctx->impl->db = NULL;
}
GRN_API_RETURN(GRN_SUCCESS);
}
grn_obj *
grn_ctx_get(grn_ctx *ctx, const char *name, int name_size)
{
grn_id id;
grn_obj *obj = NULL;
grn_obj *db;
if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
return NULL;
}
GRN_API_ENTER;
if (GRN_DB_P(db)) {
grn_db *s = (grn_db *)db;
if (name_size < 0) {
name_size = strlen(name);
}
if ((id = grn_table_get(ctx, s->keys, name, name_size))) {
obj = grn_ctx_at(ctx, id);
}
}
GRN_API_RETURN(obj);
}
grn_obj *
grn_ctx_db(grn_ctx *ctx)
{
return (ctx && ctx->impl) ? ctx->impl->db : NULL;
}
grn_obj *
grn_db_keys(grn_obj *s)
{
return (grn_obj *)(((grn_db *)s)->keys);
}
static grn_io*
grn_obj_io(grn_obj *obj)
{
grn_io *io = NULL;
if (obj) {
if (obj->header.type == GRN_DB) { obj = ((grn_db *)obj)->keys; }
switch (obj->header.type) {
case GRN_TABLE_PAT_KEY :
io = ((grn_pat *)obj)->io;
break;
case GRN_TABLE_DAT_KEY :
io = ((grn_dat *)obj)->io;
break;
case GRN_TABLE_HASH_KEY :
io = ((grn_hash *)obj)->io;
break;
case GRN_TABLE_NO_KEY :
io = ((grn_array *)obj)->io;
break;
case GRN_COLUMN_VAR_SIZE :
io = ((grn_ja *)obj)->io;
break;
case GRN_COLUMN_FIX_SIZE :
io = ((grn_ra *)obj)->io;
break;
case GRN_COLUMN_INDEX :
io = ((grn_ii *)obj)->seg;
break;
}
}
return io;
}
uint32_t
grn_db_lastmod(grn_obj *s)
{
return grn_obj_io(((grn_db *)s)->keys)->header->lastmod;
}
void
grn_db_touch(grn_ctx *ctx, grn_obj *s)
{
grn_timeval tv;
grn_timeval_now(ctx, &tv);
grn_obj_io(s)->header->lastmod = tv.tv_sec;
}
#define IS_TEMP(obj) (DB_OBJ(obj)->id & GRN_OBJ_TMP_OBJECT)
void
grn_obj_touch(grn_ctx *ctx, grn_obj *obj, grn_timeval *tv)
{
grn_timeval tv_;
if (!tv) {
grn_timeval_now(ctx, &tv_);
tv = &tv_;
}
if (obj) {
switch (obj->header.type) {
case GRN_DB :
grn_obj_io(obj)->header->lastmod = tv->tv_sec;
break;
case GRN_TABLE_HASH_KEY :
case GRN_TABLE_PAT_KEY :
case GRN_TABLE_DAT_KEY :
case GRN_TABLE_NO_KEY :
case GRN_COLUMN_VAR_SIZE :
case GRN_COLUMN_FIX_SIZE :
case GRN_COLUMN_INDEX :
if (!IS_TEMP(obj)) {
grn_obj_io(DB_OBJ(obj)->db)->header->lastmod = tv->tv_sec;
}
break;
}
}
}
grn_rc
grn_db_check_name(grn_ctx *ctx, const char *name, unsigned int name_size)
{
int len;
const char *name_end = name + name_size;
if (name_size > 0 &&
*name == GRN_DB_PSEUDO_COLUMN_PREFIX) {
return GRN_INVALID_ARGUMENT;
}
while (name < name_end) {
char c = *name;
if ((unsigned int)((c | 0x20) - 'a') >= 26u &&
(unsigned int)(c - '0') >= 10u &&
c != '_' &&
c != '-' &&
c != '#' &&
c != '@') {
return GRN_INVALID_ARGUMENT;
}
if (!(len = grn_charlen(ctx, name, name_end))) { break; }
name += len;
}
return GRN_SUCCESS;
}
grn_obj *
grn_type_create(grn_ctx *ctx, const char *name, unsigned int name_size,
grn_obj_flags flags, unsigned int size)
{
grn_id id;
struct _grn_type *res = NULL;
grn_obj *db;
if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
ERR(GRN_INVALID_ARGUMENT, "db not initialized");
return NULL;
}
GRN_API_ENTER;
if (grn_db_check_name(ctx, name, name_size)) {
GRN_DB_CHECK_NAME_ERR("[type][create]", name, name_size);
GRN_API_RETURN(NULL);
}
if (!GRN_DB_P(db)) {
ERR(GRN_INVALID_ARGUMENT, "invalid db assigned");
GRN_API_RETURN(NULL);
}
id = grn_obj_register(ctx, db, name, name_size);
if (id && (res = GRN_MALLOC(sizeof(grn_db_obj)))) {
GRN_DB_OBJ_SET_TYPE(res, GRN_TYPE);
res->obj.header.flags = flags;
res->obj.header.domain = GRN_ID_NIL;
GRN_TYPE_SIZE(&res->obj) = size;
if (grn_db_obj_init(ctx, db, id, DB_OBJ(res))) {
// grn_obj_delete(ctx, db, id);
GRN_FREE(res);
GRN_API_RETURN(NULL);
}
}
GRN_API_RETURN((grn_obj *)res);
}
static grn_obj *
grn_type_open(grn_ctx *ctx, grn_obj_spec *spec)
{
struct _grn_type *res;
res = GRN_MALLOC(sizeof(struct _grn_type));
if (res) {
GRN_DB_OBJ_SET_TYPE(res, GRN_TYPE);
res->obj.header = spec->header;
GRN_TYPE_SIZE(&res->obj) = GRN_TYPE_SIZE(spec);
}
return (grn_obj *)res;
}
grn_obj *
grn_proc_create(grn_ctx *ctx, const char *name, int name_size, grn_proc_type type,
grn_proc_func *init, grn_proc_func *next, grn_proc_func *fin,
unsigned int nvars, grn_expr_var *vars)
{
grn_proc *res = NULL;
grn_id id = GRN_ID_NIL;
grn_id range = GRN_ID_NIL;
int added = 0;
grn_obj *db;
const char *path = ctx->impl->plugin_path;
if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
ERR(GRN_INVALID_ARGUMENT, "db not initialized");
return NULL;
}
GRN_API_ENTER;
if (path) {
range = grn_plugin_reference(ctx, path);
}
if (name_size < 0) {
name_size = strlen(name);
}
if (grn_db_check_name(ctx, name, name_size)) {
GRN_DB_CHECK_NAME_ERR("[proc][create]", name, name_size);
GRN_API_RETURN(NULL);
}
if (!GRN_DB_P(db)) {
ERR(GRN_INVALID_ARGUMENT, "invalid db assigned");
GRN_API_RETURN(NULL);
}
if (name && name_size) {
grn_db *s = (grn_db *)db;
if (!(id = grn_table_get(ctx, s->keys, name, name_size))) {
if (!(id = grn_table_add(ctx, s->keys, name, name_size, &added))) {
ERR(GRN_NO_MEMORY_AVAILABLE, "grn_table_add failed");
GRN_API_RETURN(NULL);
}
}
if (!added) {
db_value *vp;
if ((vp = grn_tiny_array_at(&s->values, id)) && (res = (grn_proc *)vp->ptr)) {
/* TODO: Do more robust check. */
if (res->funcs[PROC_INIT] ||
res->funcs[PROC_NEXT] ||
res->funcs[PROC_FIN]) {
ERR(GRN_INVALID_ARGUMENT, "already used name");
GRN_API_RETURN(NULL);
}
GRN_API_RETURN((grn_obj *)res);
} else {
added = 1;
}
}
} else if (ctx->impl && ctx->impl->values) {
id = grn_array_add(ctx, ctx->impl->values, NULL) | GRN_OBJ_TMP_OBJECT;
added = 1;
}
if (!res) { res = GRN_MALLOCN(grn_proc, 1); }
if (res) {
GRN_DB_OBJ_SET_TYPE(res, GRN_PROC);
res->obj.db = db;
res->obj.id = id;
res->obj.header.domain = GRN_ID_NIL;
res->obj.header.flags = path ? GRN_OBJ_CUSTOM_NAME : 0;
res->obj.range = range;
res->type = type;
res->funcs[PROC_INIT] = init;
res->funcs[PROC_NEXT] = next;
res->funcs[PROC_FIN] = fin;
res->selector = NULL;
memset(&(res->callbacks), 0, sizeof(res->callbacks));
GRN_TEXT_INIT(&res->name_buf, 0);
res->vars = NULL;
res->nvars = 0;
if (added) {
if (grn_db_obj_init(ctx, db, id, DB_OBJ(res))) {
// grn_obj_delete(ctx, db, id);
GRN_FREE(res);
GRN_API_RETURN(NULL);
}
}
while (nvars--) {
grn_obj *v = grn_expr_add_var(ctx, (grn_obj *)res, vars->name, vars->name_size);
GRN_OBJ_INIT(v, vars->value.header.type, 0, vars->value.header.domain);
GRN_TEXT_PUT(ctx, v, GRN_TEXT_VALUE(&vars->value), GRN_TEXT_LEN(&vars->value));
vars++;
}
}
GRN_API_RETURN((grn_obj *)res);
}
/* grn_table */
static void
calc_rec_size(grn_obj_flags flags, uint32_t max_n_subrecs, uint32_t range_size,
uint32_t additional_value_size,
uint8_t *subrec_size, uint8_t *subrec_offset,
uint32_t *key_size, uint32_t *value_size)
{
*subrec_size = 0;
*subrec_offset = 0;
if (flags & GRN_OBJ_WITH_SUBREC) {
switch (flags & GRN_OBJ_UNIT_MASK) {
case GRN_OBJ_UNIT_DOCUMENT_NONE :
break;
case GRN_OBJ_UNIT_DOCUMENT_SECTION :
*subrec_offset = sizeof(grn_id);
*subrec_size = sizeof(uint32_t);
break;
case GRN_OBJ_UNIT_DOCUMENT_POSITION :
*subrec_offset = sizeof(grn_id);
*subrec_size = sizeof(uint32_t) + sizeof(uint32_t);
break;
case GRN_OBJ_UNIT_SECTION_NONE :
*key_size += sizeof(uint32_t);
break;
case GRN_OBJ_UNIT_SECTION_POSITION :
*key_size += sizeof(uint32_t);
*subrec_offset = sizeof(grn_id) + sizeof(uint32_t);
*subrec_size = sizeof(uint32_t);
break;
case GRN_OBJ_UNIT_POSITION_NONE :
*key_size += sizeof(uint32_t) + sizeof(uint32_t);
break;
case GRN_OBJ_UNIT_USERDEF_DOCUMENT :
*subrec_size = range_size;
break;
case GRN_OBJ_UNIT_USERDEF_SECTION :
*subrec_size = range_size + sizeof(uint32_t);
break;
case GRN_OBJ_UNIT_USERDEF_POSITION :
*subrec_size = range_size + sizeof(uint32_t) + sizeof(uint32_t);
break;
}
*value_size = (uintptr_t)GRN_RSET_SUBRECS_NTH((((grn_rset_recinfo *)0)->subrecs),
*subrec_size, max_n_subrecs);
} else {
*value_size = range_size;
}
*value_size += additional_value_size;
}
static void _grn_obj_remove(grn_ctx *ctx, grn_obj *obj);
static grn_rc
grn_table_create_validate(grn_ctx *ctx, const char *name, unsigned int name_size,
const char *path, grn_obj_flags flags,
grn_obj *key_type, grn_obj *value_type)
{
switch (flags & GRN_OBJ_TABLE_TYPE_MASK) {
case GRN_OBJ_TABLE_HASH_KEY :
if (flags & GRN_OBJ_KEY_WITH_SIS) {
ERR(GRN_INVALID_ARGUMENT,
"[table][create] "
"key with SIS isn't available for hash table: <%.*s>",
name_size, name);
}
break;
case GRN_OBJ_TABLE_PAT_KEY :
break;
case GRN_OBJ_TABLE_DAT_KEY :
break;
case GRN_OBJ_TABLE_NO_KEY :
if (key_type) {
int key_name_size;
char key_name[GRN_TABLE_MAX_KEY_SIZE];
key_name_size = grn_obj_name(ctx, key_type, key_name,
GRN_TABLE_MAX_KEY_SIZE);
ERR(GRN_INVALID_ARGUMENT,
"[table][create] "
"key isn't available for no key table: <%.*s> (%.*s)",
name_size, name, key_name_size, key_name);
} else if (flags & GRN_OBJ_KEY_WITH_SIS) {
ERR(GRN_INVALID_ARGUMENT,
"[table][create] "
"key with SIS isn't available for no key table: <%.*s>",
name_size, name);
} else if (flags & GRN_OBJ_KEY_NORMALIZE) {
ERR(GRN_INVALID_ARGUMENT,
"[table][create] "
"key normalization isn't available for no key table: <%.*s>",
name_size, name);
}
break;
}
return ctx->rc;
}
static grn_obj *
grn_table_create_with_max_n_subrecs(grn_ctx *ctx, const char *name,
unsigned int name_size, const char *path,
grn_obj_flags flags, grn_obj *key_type,
grn_obj *value_type,
uint32_t max_n_subrecs,
uint32_t additional_value_size)
{
grn_id id;
grn_id domain = GRN_ID_NIL, range = GRN_ID_NIL;
uint32_t key_size, value_size = 0, range_size = 0;
uint8_t subrec_size, subrec_offset;
grn_obj *res = NULL;
grn_obj *db;
char buffer[PATH_MAX];
if (!ctx->impl || !(db = ctx->impl->db)) {
ERR(GRN_INVALID_ARGUMENT, "[table][create] db not initialized");
return NULL;
}
if (grn_db_check_name(ctx, name, name_size)) {
GRN_DB_CHECK_NAME_ERR("[table][create]", name, name_size);
return NULL;
}
if (!GRN_DB_P(db)) {
ERR(GRN_INVALID_ARGUMENT, "[table][create] invalid db assigned");
return NULL;
}
if (grn_table_create_validate(ctx, name, name_size, path, flags,
key_type, value_type)) {
return NULL;
}
if (key_type) {
domain = DB_OBJ(key_type)->id;
switch (key_type->header.type) {
case GRN_TYPE :
{
grn_db_obj *t = (grn_db_obj *)key_type;
flags |= t->header.flags;
key_size = GRN_TYPE_SIZE(t);
if (key_size > GRN_TABLE_MAX_KEY_SIZE) {
int type_name_size;
char type_name[GRN_TABLE_MAX_KEY_SIZE];
type_name_size = grn_obj_name(ctx, key_type, type_name,
GRN_TABLE_MAX_KEY_SIZE);
ERR(GRN_INVALID_ARGUMENT,
"[table][create] key size too big: <%.*s> <%.*s>(%u) (max:%u)",
name_size, name,
type_name_size, type_name,
key_size, GRN_TABLE_MAX_KEY_SIZE);
return NULL;
}
}
break;
case GRN_TABLE_HASH_KEY :
case GRN_TABLE_PAT_KEY :
case GRN_TABLE_DAT_KEY :
case GRN_TABLE_NO_KEY :
key_size = sizeof(grn_id);
break;
default :
{
int key_name_size;
char key_name[GRN_TABLE_MAX_KEY_SIZE];
key_name_size = grn_obj_name(ctx, key_type, key_name,
GRN_TABLE_MAX_KEY_SIZE);
ERR(GRN_INVALID_ARGUMENT,
"[table][create] key type must be type or table: <%.*s> (%.*s)",
name_size, name, key_name_size, key_name);
return NULL;
}
break;
}
} else {
key_size = (flags & GRN_OBJ_KEY_VAR_SIZE) ? GRN_TABLE_MAX_KEY_SIZE : sizeof(grn_id);
}
if (value_type) {
range = DB_OBJ(value_type)->id;
switch (value_type->header.type) {
case GRN_TYPE :
{
grn_db_obj *t = (grn_db_obj *)value_type;
if (t->header.flags & GRN_OBJ_KEY_VAR_SIZE) {
int type_name_size;
char type_name[GRN_TABLE_MAX_KEY_SIZE];
type_name_size = grn_obj_name(ctx, value_type, type_name,
GRN_TABLE_MAX_KEY_SIZE);
ERR(GRN_INVALID_ARGUMENT,
"[table][create] value type must be fixed size: <%.*s> (%.*s)",
name_size, name, type_name_size, type_name);
return NULL;
}
range_size = GRN_TYPE_SIZE(t);
}
break;
case GRN_TABLE_HASH_KEY :
case GRN_TABLE_PAT_KEY :
case GRN_TABLE_DAT_KEY :
case GRN_TABLE_NO_KEY :
range_size = sizeof(grn_id);
break;
default :
{
int value_name_size;
char value_name[GRN_TABLE_MAX_KEY_SIZE];
value_name_size = grn_obj_name(ctx, value_type, value_name,
GRN_TABLE_MAX_KEY_SIZE);
ERR(GRN_INVALID_ARGUMENT,
"[table][create] value type must be type or table: <%.*s> (%.*s)",
name_size, name, value_name_size, value_name);
return NULL;
}
break;
}
}
id = grn_obj_register(ctx, db, name, name_size);
if (ERRP(ctx, GRN_ERROR)) { return NULL; }
if (GRN_OBJ_PERSISTENT & flags) {
GRN_LOG(ctx, GRN_LOG_NOTICE, "DDL:table_create %.*s", name_size, name);
if (!path) {
if (GRN_DB_PERSISTENT_P(db)) {
gen_pathname(grn_obj_io(db)->path, buffer, id);
path = buffer;
} else {
ERR(GRN_INVALID_ARGUMENT, "path not assigned for persistent table");
return NULL;
}
} else {
flags |= GRN_OBJ_CUSTOM_NAME;
}
} else {
if (path) {
ERR(GRN_INVALID_ARGUMENT, "path assigned for temporary table");
return NULL;
}
if (GRN_DB_PERSISTENT_P(db) && name && name_size) {
ERR(GRN_INVALID_ARGUMENT, "name assigned for temporary table");
return NULL;
}
}
calc_rec_size(flags, max_n_subrecs, range_size, additional_value_size,
&subrec_size, &subrec_offset, &key_size, &value_size);
switch (flags & GRN_OBJ_TABLE_TYPE_MASK) {
case GRN_OBJ_TABLE_HASH_KEY :
res = (grn_obj *)grn_hash_create(ctx, path, key_size, value_size, flags);
break;
case GRN_OBJ_TABLE_PAT_KEY :
res = (grn_obj *)grn_pat_create(ctx, path, key_size, value_size, flags);
break;
case GRN_OBJ_TABLE_DAT_KEY :
res = (grn_obj *)grn_dat_create(ctx, path, key_size, value_size, flags);
break;
case GRN_OBJ_TABLE_NO_KEY :
domain = range;
res = (grn_obj *)grn_array_create(ctx, path, value_size, flags);
break;
}
if (res) {
DB_OBJ(res)->header.impl_flags = 0;
DB_OBJ(res)->header.domain = domain;
DB_OBJ(res)->range = range;
DB_OBJ(res)->max_n_subrecs = max_n_subrecs;
DB_OBJ(res)->subrec_size = subrec_size;
DB_OBJ(res)->subrec_offset = subrec_offset;
DB_OBJ(res)->flags.group = 0;
if (grn_db_obj_init(ctx, db, id, DB_OBJ(res))) {
_grn_obj_remove(ctx, res);
res = NULL;
}
} else {
grn_obj_delete_by_id(ctx, db, id, GRN_TRUE);
}
return res;
}
grn_obj *
grn_table_create(grn_ctx *ctx, const char *name, unsigned int name_size,
const char *path, grn_obj_flags flags,
grn_obj *key_type, grn_obj *value_type)
{
grn_obj *res;
GRN_API_ENTER;
res = grn_table_create_with_max_n_subrecs(ctx, name, name_size, path,
flags, key_type, value_type,
0, 0);
GRN_API_RETURN(res);
}
grn_obj *
grn_table_create_for_group(grn_ctx *ctx, const char *name,
unsigned int name_size, const char *path,
grn_obj *group_key, grn_obj *value_type,
unsigned int max_n_subrecs)
{
grn_obj *res = NULL;
GRN_API_ENTER;
if (group_key) {
grn_obj *key_type;
key_type = grn_ctx_at(ctx, grn_obj_get_range(ctx, group_key));
if (key_type) {
res = grn_table_create_with_max_n_subrecs(ctx, name, name_size, path,
GRN_TABLE_HASH_KEY|
GRN_OBJ_WITH_SUBREC|
GRN_OBJ_UNIT_USERDEF_DOCUMENT,
key_type, value_type,
max_n_subrecs, 0);
grn_obj_unlink(ctx, key_type);
}
} else {
res = grn_table_create_with_max_n_subrecs(ctx, name, name_size, path,
GRN_TABLE_HASH_KEY|
GRN_OBJ_KEY_VAR_SIZE|
GRN_OBJ_WITH_SUBREC|
GRN_OBJ_UNIT_USERDEF_DOCUMENT,
NULL, value_type,
max_n_subrecs, 0);
}
GRN_API_RETURN(res);
}
unsigned int
grn_table_get_subrecs(grn_ctx *ctx, grn_obj *table, grn_id id,
grn_id *subrecbuf, int *scorebuf, int buf_size)
{
unsigned int count = 0;
GRN_API_ENTER;
if (GRN_OBJ_TABLEP(table)) {
uint32_t value_size;
grn_rset_recinfo *ri;
uint32_t subrec_size = DB_OBJ(table)->subrec_size;
uint32_t max_n_subrecs = DB_OBJ(table)->max_n_subrecs;
if (subrec_size < sizeof(grn_id)) { goto exit; }
if (!max_n_subrecs) { goto exit; }
ri = (grn_rset_recinfo *)grn_obj_get_value_(ctx, table, id, &value_size);
if (ri) {
byte *psubrec = (byte *)ri->subrecs;
uint32_t n_subrecs = (uint32_t)GRN_RSET_N_SUBRECS(ri);
uint32_t limit = value_size / (GRN_RSET_SCORE_SIZE + subrec_size);
if (limit > buf_size) {
limit = buf_size;
}
if (limit > n_subrecs) {
limit = n_subrecs;
}
if (limit > max_n_subrecs) {
limit = max_n_subrecs;
}
for (; count < limit; count++) {
if (scorebuf) {
scorebuf[count] = *((int *)psubrec);
}
psubrec += GRN_RSET_SCORE_SIZE;
if (subrecbuf) {
subrecbuf[count] = *((grn_id *)psubrec);
}
psubrec += subrec_size;
}
}
}
exit :
GRN_API_RETURN(count);