-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrngo.c
1319 lines (1256 loc) · 36.7 KB
/
grngo.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
#include "grngo.h"
#include <math.h>
#include <string.h>
// -- debug --
#include <stdio.h>
#define GRNGO_DEBUG(fmt, ...)\
fprintf(stderr, "%s:%d: In %s: " fmt "\n",\
__FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__)
// -- miscellaneous --
#define GRNGO_MAX_BUILTIN_TYPE_ID GRN_DB_WGS84_GEO_POINT
#define GRNGO_MAX_SHORT_TEXT_LEN 4095
#define GRNGO_MAX_TEXT_LEN 65535
#define GRNGO_MAX_LONG_TEXT_LEN 2147484647
#define GRNGO_BOOL_DB_TYPE grn_bool
#define GRNGO_INT8_DB_TYPE int8_t
#define GRNGO_INT16_DB_TYPE int16_t
#define GRNGO_INT32_DB_TYPE int32_t
#define GRNGO_INT64_DB_TYPE int64_t
#define GRNGO_UINT8_DB_TYPE uint8_t
#define GRNGO_UINT16_DB_TYPE uint16_t
#define GRNGO_UINT32_DB_TYPE uint32_t
#define GRNGO_UINT64_DB_TYPE uint64_t
#define GRNGO_FLOAT_DB_TYPE double
#define GRNGO_TIME_DB_TYPE int64_t
#define GRNGO_TEXT_DB_TYPE grngo_text
#define GRNGO_TOKYO_GEO_POINT_DB_TYPE grn_geo_point
#define GRNGO_WGS84_GEO_POINT_DB_TYPE grn_geo_point
#define GRNGO_DB_TYPE(type) GRNGO_ ## type ## _DB_TYPE
#define GRNGO_BOOL_C_TYPE grn_bool
#define GRNGO_INT8_C_TYPE int64_t
#define GRNGO_INT16_C_TYPE int64_t
#define GRNGO_INT32_C_TYPE int64_t
#define GRNGO_INT64_C_TYPE int64_t
#define GRNGO_UINT8_C_TYPE int64_t
#define GRNGO_UINT16_C_TYPE int64_t
#define GRNGO_UINT32_C_TYPE int64_t
#define GRNGO_UINT64_C_TYPE int64_t
#define GRNGO_FLOAT_C_TYPE double
#define GRNGO_TIME_C_TYPE int64_t
#define GRNGO_TEXT_C_TYPE grngo_text
#define GRNGO_TOKYO_GEO_POINT_C_TYPE grn_geo_point
#define GRNGO_WGS84_GEO_POINT_C_TYPE grn_geo_point
#define GRNGO_C_TYPE(type) GRNGO_ ## type ## _C_TYPE
#define GRNGO_TEST_BOOL(value) (1)
#define GRNGO_TEST_INT8(value) \
(((value) >= INT8_MIN) && ((value) <= INT8_MAX))
#define GRNGO_TEST_INT16(value) \
(((value) >= INT16_MIN) && ((value) <= INT16_MAX))
#define GRNGO_TEST_INT32(value) \
(((value) >= INT32_MIN) && ((value) <= INT32_MAX))
#define GRNGO_TEST_INT64(value) (1)
#define GRNGO_TEST_UINT8(value) \
(((value) >= 0) && ((value) <= (int64_t)UINT8_MAX))
#define GRNGO_TEST_UINT16(value) \
(((value) >= 0) && ((value) <= (int64_t)UINT16_MAX))
#define GRNGO_TEST_UINT32(value) \
(((value) >= 0) && ((value) <= (int64_t)UINT32_MAX))
#define GRNGO_TEST_UINT64(value) ((value) >= 0)
#define GRNGO_TEST_TIME(value) (1)
#define GRNGO_TEST_FLOAT(value) (!isnan(value))
#define GRNGO_TEST_SHORT_TEXT(value) \
(((value).ptr && ((value).size < GRNGO_MAX_SHORT_TEXT_LEN)) ||\
(!(value).ptr && !(value).size))
#define GRNGO_TEST_TEXT(value) \
(((value).ptr && ((value).size < GRNGO_MAX_TEXT_LEN)) ||\
(!(value).ptr && !(value).size))
#define GRNGO_TEST_LONG_TEXT(value) \
(((value).ptr && ((value).size < GRNGO_MAX_LONG_TEXT_LEN)) ||\
(!(value).ptr && !(value).size))
#define GRNGO_TEST_GEO_POINT(value) \
(((value).latitude >= ( -90 * 60 * 60 * 1000)) &&\
((value).latitude <= ( 90 * 60 * 60 * 1000)) &&\
((value).longitude >= (-180 * 60 * 60 * 1000)) &&\
((value).longitude <= ( 180 * 60 * 60 * 1000)))
#define GRNGO_TEST_VECTOR(value) \
((value.ptr) || (!(value).ptr && !(value).size))
static void *
_grngo_malloc(grngo_db *db, size_t size,
const char *file, int line, const char *func) {
void *buf = malloc(size);
if (!buf && db) {
// TODO: Error!
}
return buf;
}
#define GRNGO_MALLOC(db, size)\
_grngo_malloc(db, size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
static void *
_grngo_realloc(grngo_db *db, void *ptr, size_t size,
const char *file, int line, const char *func) {
void *buf = realloc(ptr, size);
if (!buf && db) {
// TODO: Error!
}
return buf;
}
#define GRNGO_REALLOC(db, ptr, size)\
_grngo_realloc(db, ptr, size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
static void
_grngo_free(grngo_db *db, void *buf,
const char *file, int line, const char *func) {
free(buf);
}
#define GRNGO_FREE(db, buf)\
_grngo_free(db, buf, __FILE__, __LINE__, __PRETTY_FUNCTION__)
static grn_bool
_grngo_is_vector(grn_obj *obj) {
if (obj->header.type != GRN_COLUMN_VAR_SIZE) {
return GRN_FALSE;
}
grn_obj_flags type = obj->header.flags & GRN_OBJ_COLUMN_TYPE_MASK;
return type == GRN_OBJ_COLUMN_VECTOR;
}
// -- grngo_db --
static grngo_db *
_grngo_new_db(void) {
grngo_db *db = (grngo_db *)GRNGO_MALLOC(NULL, sizeof(*db));
if (!db) {
return NULL;
}
memset(db, 0, sizeof(*db));
db->ctx = NULL;
db->obj = NULL;
db->estr = db->estr_buf;
return db;
}
static void
_grngo_delete_db(grngo_db *db) {
if (db->obj) {
grn_obj_close(db->ctx, db->obj);
}
if (db->ctx) {
grn_ctx_close(db->ctx);
}
GRNGO_FREE(NULL, db);
}
static grn_rc
_grngo_create_db(grngo_db *db, const char *path) {
db->ctx = grn_ctx_open(0);
if (!db->ctx) {
return GRN_NO_MEMORY_AVAILABLE;
}
db->obj = grn_db_create(db->ctx, path, NULL);
if (!db->obj) {
if (db->ctx->rc != GRN_SUCCESS) {
return db->ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
return GRN_SUCCESS;
}
static grn_rc
_grngo_open_db(grngo_db *db, const char *path) {
db->ctx = grn_ctx_open(0);
if (!db->ctx) {
return GRN_NO_MEMORY_AVAILABLE;
}
db->obj = grn_db_open(db->ctx, path);
if (!db->obj) {
if (db->ctx->rc != GRN_SUCCESS) {
return db->ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
return GRN_SUCCESS;
}
grn_rc
grngo_create_db(const char *path, size_t path_len, grngo_db **db) {
if ((!path && path_len) || !db) {
return GRN_INVALID_ARGUMENT;
}
// Create a zero-terminated path.
char *path_cstr = NULL;
if (path) {
path_cstr = GRNGO_MALLOC(NULL, path_len + 1);
if (!path_cstr) {
return GRN_NO_MEMORY_AVAILABLE;
}
memcpy(path_cstr, path, path_len);
path_cstr[path_len] = '\0';
}
// Create a DB.
grngo_db *new_db = _grngo_new_db();
grn_rc rc = new_db ? GRN_SUCCESS : GRN_NO_MEMORY_AVAILABLE;
if (rc == GRN_SUCCESS) {
rc = _grngo_create_db(new_db, path_cstr);
if (rc == GRN_SUCCESS) {
*db = new_db;
} else {
_grngo_delete_db(new_db);
}
}
GRNGO_FREE(NULL, path_cstr);
return rc;
}
grn_rc
grngo_open_db(const char *path, size_t path_len, grngo_db **db) {
if ((!path && path_len) || !db) {
return GRN_INVALID_ARGUMENT;
}
// Create a zero-terminated path.
char *path_cstr = NULL;
if (path) {
path_cstr = GRNGO_MALLOC(NULL, path_len + 1);
if (!path_cstr) {
return GRN_NO_MEMORY_AVAILABLE;
}
memcpy(path_cstr, path, path_len);
path_cstr[path_len] = '\0';
}
// Open a DB.
grngo_db *new_db = _grngo_new_db();
grn_rc rc = new_db ? GRN_SUCCESS : GRN_NO_MEMORY_AVAILABLE;
if (rc == GRN_SUCCESS) {
rc = _grngo_open_db(new_db, path_cstr);
if (rc == GRN_SUCCESS) {
*db = new_db;
} else {
_grngo_delete_db(new_db);
}
}
GRNGO_FREE(NULL, path_cstr);
return rc;
}
void
grngo_close_db(grngo_db *db) {
if (db) {
_grngo_delete_db(db);
}
}
grn_rc
grngo_send(grngo_db *db, const char *cmd, size_t cmd_len) {
if (!db || (!cmd && cmd_len)) {
return GRN_INVALID_ARGUMENT;
}
grn_rc rc = grn_ctx_send(db->ctx, cmd, cmd_len, 0);
if (rc != GRN_SUCCESS) {
return rc;
}
return db->ctx->rc;
}
grn_rc
grngo_recv(grngo_db *db, char **res, unsigned int *res_len) {
if (!db || !res || !res_len) {
return GRN_INVALID_ARGUMENT;
}
int flags;
grn_rc rc = grn_ctx_recv(db->ctx, res, res_len, &flags);
if (rc != GRN_SUCCESS) {
return rc;
}
return db->ctx->rc;
}
// -- grngo_table --
static grngo_table *
_grngo_new_table(grngo_db *db) {
grngo_table *table = (grngo_table *)GRNGO_MALLOC(db, sizeof(*table));
if (!table) {
return NULL;
}
memset(table, 0, sizeof(*table));
table->db = db;
table->objs = NULL;
return table;
}
static void
_grngo_delete_table(grngo_table *table) {
if (table->objs) {
size_t i;
for (i = 0; i < table->n_objs; i++) {
if (table->objs[i]) {
grn_obj_unlink(table->db->ctx, table->objs[i]);
}
}
GRNGO_FREE(table->db, table->objs);
}
GRNGO_FREE(table->db, table);
}
static grn_rc
_grngo_open_table(grngo_table *table, const char *name, size_t name_len) {
grn_ctx *ctx = table->db->ctx;
grn_obj *obj = grn_ctx_get(ctx, name, name_len);
while (obj) {
// Register an object.
size_t new_size = sizeof(grn_obj *) * (table->n_objs + 1);
grn_obj **new_objs = (grn_obj **)GRNGO_REALLOC(table->db, table->objs,
new_size);
if (!new_objs) {
grn_obj_unlink(ctx, obj);
return GRN_NO_MEMORY_AVAILABLE;
}
table->objs = new_objs;
table->objs[table->n_objs] = obj;
table->n_objs++;
// Detect the builtin type of _key or dereference _key.
grn_id domain = obj->header.domain;
if (obj->header.type == GRN_TABLE_NO_KEY) {
domain = GRN_DB_VOID;
}
if (domain <= GRNGO_MAX_BUILTIN_TYPE_ID) {
table->key_type = domain;
return GRN_SUCCESS;
}
obj = grn_ctx_at(ctx, domain);
}
if (!obj) {
if (ctx->rc != GRN_SUCCESS) {
return ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
return GRN_SUCCESS;
}
grn_rc
grngo_open_table(grngo_db *db, const char *name, size_t name_len,
grngo_table **table) {
if (!db || !name || (name_len == 0) || !table) {
return GRN_INVALID_ARGUMENT;
}
grngo_table *new_table = _grngo_new_table(db);
grn_rc rc = new_table ? GRN_SUCCESS : GRN_NO_MEMORY_AVAILABLE;
if (rc == GRN_SUCCESS) {
rc = _grngo_open_table(new_table, name, name_len);
if (rc == GRN_SUCCESS) {
*table = new_table;
} else {
_grngo_delete_table(new_table);
}
}
return rc;
}
void
grngo_close_table(grngo_table *table) {
if (table) {
_grngo_delete_table(table);
}
}
static grn_rc
_grngo_insert_row(grngo_table *table, const void *key, size_t key_size,
grn_bool *inserted, grn_id *id) {
grn_ctx *ctx = table->db->ctx;
size_t i = table->n_objs - 1;
int tmp_inserted;
grn_id tmp_id = grn_table_add(ctx, table->objs[i], key, key_size,
&tmp_inserted);
if (tmp_id == GRN_ID_NIL) {
if (ctx->rc != GRN_SUCCESS) {
return ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
// Resolve table references.
while (i > 0) {
i--;
tmp_id = grn_table_add(ctx, table->objs[i], &tmp_id, sizeof(tmp_id),
&tmp_inserted);
if (tmp_id == GRN_ID_NIL) {
if (ctx->rc != GRN_SUCCESS) {
return ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
}
*inserted = (grn_bool)tmp_inserted;
*id = tmp_id;
return GRN_SUCCESS;
}
grn_rc
grngo_insert_void(grngo_table *table, grn_bool *inserted, grn_id *id) {
if (!table || !inserted || !id) {
return GRN_INVALID_ARGUMENT;
}
if (table->key_type != GRN_DB_VOID) {
return GRN_INVALID_ARGUMENT;
}
return _grngo_insert_row(table, NULL, 0, inserted, id);
}
grn_rc
grngo_insert_bool(grngo_table *table, grn_bool key,
grn_bool *inserted, grn_id *id) {
if (!table || !inserted || !id) {
return GRN_INVALID_ARGUMENT;
}
if ((table->key_type != GRN_DB_BOOL) || !GRNGO_TEST_BOOL(key)) {
return GRN_INVALID_ARGUMENT;
}
return _grngo_insert_row(table, &key, sizeof(key), inserted, id);
}
#define GRNGO_INSERT_INT_CASE_BLOCK(type)\
case GRN_DB_ ## type: {\
if (!GRNGO_TEST_ ## type(key)) {\
return GRN_INVALID_ARGUMENT;\
}\
GRNGO_DB_TYPE(type) tmp_key = (GRNGO_DB_TYPE(type))key;\
return _grngo_insert_row(table, &tmp_key, sizeof(tmp_key), inserted, id);\
}
grn_rc
grngo_insert_int(grngo_table *table, int64_t key,
grn_bool *inserted, grn_id *id) {
if (!table || !inserted || !id) {
return GRN_INVALID_ARGUMENT;
}
switch (table->key_type) {
GRNGO_INSERT_INT_CASE_BLOCK(INT8)
GRNGO_INSERT_INT_CASE_BLOCK(INT16)
GRNGO_INSERT_INT_CASE_BLOCK(INT32)
GRNGO_INSERT_INT_CASE_BLOCK(INT64)
GRNGO_INSERT_INT_CASE_BLOCK(UINT8)
GRNGO_INSERT_INT_CASE_BLOCK(UINT16)
GRNGO_INSERT_INT_CASE_BLOCK(UINT32)
GRNGO_INSERT_INT_CASE_BLOCK(UINT64)
GRNGO_INSERT_INT_CASE_BLOCK(TIME)
default: {
return GRN_INVALID_ARGUMENT;
}
}
}
#undef GRNGO_INSERT_INT_CASE_BLOCK
grn_rc
grngo_insert_float(grngo_table *table, double key,
grn_bool *inserted, grn_id *id) {
if (!table || !inserted || !id) {
return GRN_INVALID_ARGUMENT;
}
if ((table->key_type != GRN_DB_FLOAT) || !GRNGO_TEST_FLOAT(key)) {
return GRN_INVALID_ARGUMENT;
}
return _grngo_insert_row(table, &key, sizeof(key), inserted, id);
}
grn_rc
grngo_insert_text(grngo_table *table, grngo_text key,
grn_bool *inserted, grn_id *id) {
if (!table || !inserted || !id) {
return GRN_INVALID_ARGUMENT;
}
if ((table->key_type != GRN_DB_SHORT_TEXT) || !GRNGO_TEST_SHORT_TEXT(key)) {
return GRN_INVALID_ARGUMENT;
}
return _grngo_insert_row(table, key.ptr, key.size, inserted, id);
}
grn_rc
grngo_insert_geo_point(grngo_table *table, grn_geo_point key,
grn_bool *inserted, grn_id *id) {
if (!table || !inserted || !id) {
return GRN_INVALID_ARGUMENT;
}
switch (table->key_type) {
case GRN_DB_TOKYO_GEO_POINT:
case GRN_DB_WGS84_GEO_POINT: {
if (!GRNGO_TEST_GEO_POINT(key)) {
return GRN_INVALID_ARGUMENT;
}
break;
}
default: {
return GRN_INVALID_ARGUMENT;
}
}
return _grngo_insert_row(table, &key, sizeof(key), inserted, id);
}
// -- grngo_column --
static grngo_column *
_grngo_new_column(grngo_table *table) {
grngo_column *column = (grngo_column *)GRNGO_MALLOC(table->db,
sizeof(*column));
if (!column) {
return NULL;
}
memset(column, 0, sizeof(*column));
column->db = table->db;
column->table = table;
column->srcs = NULL;
column->src_bufs = NULL;
column->text_buf = NULL;
column->vector_buf = NULL;
return column;
}
static void
_grngo_delete_column(grngo_column *column) {
grn_ctx *ctx = column->db->ctx;
if (column->srcs) {
size_t i;
for (i = 0; i < column->n_srcs; i++) {
grn_obj_unlink(ctx, column->srcs[i]);
}
GRNGO_FREE(column->db, column->srcs);
}
if (column->src_bufs) {
size_t i;
for (i = 0; i < column->n_srcs; i++) {
if (column->src_bufs[i]) {
grn_obj_close(ctx, column->src_bufs[i]);
}
}
GRNGO_FREE(column->db, column->src_bufs);
}
if (column->text_buf) {
grn_obj_close(ctx, column->text_buf);
}
if (column->vector_buf) {
grn_obj_close(ctx, column->vector_buf);
}
GRNGO_FREE(column->db, column);
}
static grn_rc
_grngo_open_src(grngo_db *db, grn_obj *table,
const char *name, size_t name_len, grn_obj **src) {
if ((name_len == GRN_COLUMN_NAME_KEY_LEN) &&
!memcmp(name, GRN_COLUMN_NAME_KEY, name_len) &&
(table->header.type == GRN_TABLE_NO_KEY)) {
return GRN_INVALID_ARGUMENT;
}
grn_obj *new_src;
if ((name_len == GRN_COLUMN_NAME_VALUE_LEN) &&
!memcmp(name, GRN_COLUMN_NAME_VALUE, name_len)) {
new_src = grn_ctx_at(db->ctx, grn_obj_id(db->ctx, table));
} else {
new_src = grn_obj_column(db->ctx, table, name, name_len);
}
if (!new_src) {
if (db->ctx->rc != GRN_SUCCESS) {
return db->ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
*src = new_src;
return GRN_SUCCESS;
}
static grn_rc
_grngo_push_src(grngo_column *column, grn_obj *table,
const char *name, size_t name_len, grn_obj **next_table) {
grn_obj *src;
grn_rc rc = _grngo_open_src(column->db, table, name, name_len, &src);
if (rc != GRN_SUCCESS) {
return rc;
}
grn_ctx *ctx = column->db->ctx;
switch (src->header.type) {
case GRN_COLUMN_VAR_SIZE: {
grn_obj_flags type = src->header.flags & GRN_OBJ_COLUMN_TYPE_MASK;
if (type == GRN_OBJ_COLUMN_VECTOR) {
column->dimension++;
}
// Fallthrough.
}
case GRN_TABLE_HASH_KEY: // _value.
case GRN_TABLE_PAT_KEY: // _value.
case GRN_TABLE_NO_KEY: // _value.
case GRN_ACCESSOR: // _id or _key.
case GRN_COLUMN_FIX_SIZE: {
grn_id range = grn_obj_get_range(ctx, src);
if (range == GRN_DB_VOID) {
grn_obj_unlink(ctx, src);
return GRN_INVALID_ARGUMENT;
} else if (range <= GRNGO_MAX_BUILTIN_TYPE_ID) {
column->value_type = range;
*next_table = NULL;
} else {
grn_obj *range_obj = grn_ctx_at(ctx, range);
if (!grn_obj_is_table(ctx, range_obj)) {
grn_obj_unlink(ctx, range_obj);
return GRN_INVALID_ARGUMENT;
}
*next_table = range_obj;
}
break;
}
default: {
grn_obj_unlink(ctx, src);
return GRN_INVALID_ARGUMENT;
}
}
// Append a source.
size_t new_size = sizeof(grn_obj *) * (column->n_srcs + 1);
grn_obj **new_srcs = (grn_obj **)GRNGO_REALLOC(column->db, column->srcs,
new_size);
if (!new_srcs) {
if (*next_table) {
grn_obj_unlink(ctx, *next_table);
}
grn_obj_unlink(ctx, src);
return GRN_NO_MEMORY_AVAILABLE;
}
column->srcs = new_srcs;
column->srcs[column->n_srcs] = src;
column->n_srcs++;
return GRN_SUCCESS;
}
static grn_rc
_grngo_open_bufs(grngo_column *column) {
size_t size = sizeof(grn_obj *) * column->n_srcs;
column->src_bufs = (grn_obj **)GRNGO_MALLOC(column->db, size);
if (!column->src_bufs) {
return GRN_NO_MEMORY_AVAILABLE;
}
size_t i = 0;
for (i = 0; i < column->n_srcs; i++) {
column->src_bufs[i] = NULL;
}
grn_ctx *ctx = column->db->ctx;
// Open buffers for table references.
for (i = 0; i < (column->n_srcs - 1); i++) {
grn_id range = grn_obj_get_range(ctx, column->srcs[i]);
column->src_bufs[i] = grn_obj_open(ctx, GRN_UVECTOR, 0, range);
if (!column->src_bufs[i]) {
if (ctx->rc != GRN_SUCCESS) {
return ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
}
// Open buffers for values.
grn_builtin_type value_type = column->value_type;
switch (value_type) {
case GRN_DB_SHORT_TEXT:
case GRN_DB_TEXT:
case GRN_DB_LONG_TEXT: {
if (_grngo_is_vector(column->srcs[i])) {
column->src_bufs[i] = grn_obj_open(ctx, GRN_VECTOR, 0, value_type);
} else {
column->src_bufs[i] = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_LONG_TEXT);
}
if (!column->src_bufs[i]) {
if (ctx->rc != GRN_SUCCESS) {
return ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
column->text_buf = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_LONG_TEXT);
if (!column->text_buf) {
if (ctx->rc != GRN_SUCCESS) {
return ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
break;
}
default: {
column->src_bufs[i] = grn_obj_open(ctx, GRN_UVECTOR, 0, value_type);
if (!column->src_bufs[i]) {
if (ctx->rc != GRN_SUCCESS) {
return ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
break;
}
}
// Open a buffer for vectors.
if (column->dimension != 0) {
column->vector_buf = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_LONG_TEXT);
if (!column->vector_buf) {
if (ctx->rc != GRN_SUCCESS) {
return ctx->rc;
}
return GRN_UNKNOWN_ERROR;
}
}
return GRN_SUCCESS;
}
static grn_rc
_grngo_open_column(grngo_table *table, grngo_column *column,
const char *name, size_t name_len) {
// Tokenize the given name and push sources.
grn_obj *owner = table->objs[0];
while (name_len) {
if (!owner) {
return GRN_INVALID_ARGUMENT;
}
const char *token = name;
size_t token_len = 0;
while (name_len) {
name_len--;
if (*name++ == '.') {
break;
}
token_len++;
}
grn_obj *new_owner;
grn_rc rc = _grngo_push_src(column, owner, token, token_len, &new_owner);
if (rc != GRN_SUCCESS) {
return rc;
}
if (column->n_srcs != 0) {
grn_obj_unlink(column->db->ctx, owner);
}
owner = new_owner;
}
// Check whether the column is writable or not.
if (column->n_srcs == 1) {
switch (column->srcs[0]->header.type) {
case GRN_TABLE_HASH_KEY: // _value.
case GRN_TABLE_PAT_KEY: // _value.
case GRN_TABLE_NO_KEY: // _value.
case GRN_COLUMN_FIX_SIZE:
case GRN_COLUMN_VAR_SIZE: {
column->writable = GRN_TRUE;
break;
}
default: {
break;
}
}
}
// Resolve the _key chain if _key is table reference.
while (owner) {
grn_obj *new_owner;
grn_rc rc = _grngo_push_src(column, owner, GRN_COLUMN_NAME_KEY,
GRN_COLUMN_NAME_KEY_LEN, &new_owner);
if (rc != GRN_SUCCESS) {
return rc;
}
grn_obj_unlink(column->db->ctx, owner);
owner = new_owner;
}
return _grngo_open_bufs(column);
}
grn_rc
grngo_open_column(grngo_table *table, const char *name, size_t name_len,
grngo_column **column) {
if (!table || !name || (name_len == 0) || !column) {
return GRN_INVALID_ARGUMENT;
}
grngo_column *new_column = _grngo_new_column(table);
grn_rc rc = new_column ? GRN_SUCCESS : GRN_NO_MEMORY_AVAILABLE;
if (rc == GRN_SUCCESS) {
rc = _grngo_open_column(table, new_column, name, name_len);
if (rc == GRN_SUCCESS) {
*column = new_column;
} else {
_grngo_delete_column(new_column);
}
}
return rc;
}
void
grngo_close_column(grngo_column *column) {
if (column) {
_grngo_delete_column(column);
}
}
grn_rc
grngo_set_bool(grngo_column *column, grn_id id, grn_bool value) {
if (!column || !column->writable || !GRNGO_TEST_BOOL(value)) {
return GRN_INVALID_ARGUMENT;
}
grn_ctx *ctx = column->db->ctx;
if (grn_table_at(ctx, column->table->objs[0], id) == GRN_ID_NIL) {
return GRN_INVALID_ARGUMENT;
}
grn_obj obj;
GRN_BOOL_INIT(&obj, 0);
grn_rc rc = grn_bulk_write(ctx, &obj, (const char *)&value, sizeof(value));
if (rc == GRN_SUCCESS) {
rc = grn_obj_set_value(ctx, column->srcs[0], id, &obj, GRN_OBJ_SET);
}
GRN_OBJ_FIN(ctx, &obj);
return rc;
}
#define GRNGO_SET_INT_CASE_BLOCK(type)\
case GRN_DB_ ## type: {\
if (!GRNGO_TEST_ ## type(value)) {\
return GRN_INVALID_ARGUMENT;\
}\
GRN_ ## type ## _INIT(&obj, 0);\
GRNGO_DB_TYPE(type) db_value = (GRNGO_DB_TYPE(type))value;\
rc = grn_bulk_write(ctx, &obj, (const char *)&db_value, sizeof(db_value));\
break;\
}
grn_rc
grngo_set_int(grngo_column *column, grn_id id, int64_t value) {
if (!column || !column->writable) {
return GRN_INVALID_ARGUMENT;
}
grn_ctx *ctx = column->db->ctx;
if (grn_table_at(ctx, column->table->objs[0], id) == GRN_ID_NIL) {
return GRN_INVALID_ARGUMENT;
}
grn_obj obj;
grn_rc rc;
switch (column->value_type) {
GRNGO_SET_INT_CASE_BLOCK(INT8)
GRNGO_SET_INT_CASE_BLOCK(INT16)
GRNGO_SET_INT_CASE_BLOCK(INT32)
GRNGO_SET_INT_CASE_BLOCK(INT64)
GRNGO_SET_INT_CASE_BLOCK(UINT8)
GRNGO_SET_INT_CASE_BLOCK(UINT16)
GRNGO_SET_INT_CASE_BLOCK(UINT32)
GRNGO_SET_INT_CASE_BLOCK(UINT64)
GRNGO_SET_INT_CASE_BLOCK(TIME)
default: {
return GRN_INVALID_ARGUMENT;
}
}
if (rc == GRN_SUCCESS) {
rc = grn_obj_set_value(ctx, column->srcs[0], id, &obj, GRN_OBJ_SET);
}
GRN_OBJ_FIN(ctx, &obj);
return rc;
}
#undef GRNGO_SET_INT_CASE_BLOCK
grn_rc
grngo_set_float(grngo_column *column, grn_id id, double value) {
if (!column || !column->writable || !GRNGO_TEST_FLOAT(value)) {
return GRN_INVALID_ARGUMENT;
}
grn_ctx *ctx = column->db->ctx;
if (grn_table_at(ctx, column->table->objs[0], id) == GRN_ID_NIL) {
return GRN_INVALID_ARGUMENT;
}
grn_obj obj;
GRN_FLOAT_INIT(&obj, 0);
grn_rc rc = grn_bulk_write(ctx, &obj, (const char *)&value, sizeof(value));
if (rc == GRN_SUCCESS) {
rc = grn_obj_set_value(ctx, column->srcs[0], id, &obj, GRN_OBJ_SET);
}
GRN_OBJ_FIN(ctx, &obj);
return rc;
}
#define GRNGO_SET_TEXT_CASE_BLOCK(type)\
case GRN_DB_ ## type: {\
if (!GRNGO_TEST_ ## type(value)) {\
return GRN_INVALID_ARGUMENT;\
}\
GRN_ ## type ## _INIT(&obj, 0);\
break;\
}
grn_rc
grngo_set_text(grngo_column *column, grn_id id, grngo_text value) {
if (!column || !column->writable) {
return GRN_INVALID_ARGUMENT;
}
grn_ctx *ctx = column->db->ctx;
if (grn_table_at(ctx, column->table->objs[0], id) == GRN_ID_NIL) {
return GRN_INVALID_ARGUMENT;
}
grn_obj obj;
switch (column->value_type) {
GRNGO_SET_TEXT_CASE_BLOCK(SHORT_TEXT)
GRNGO_SET_TEXT_CASE_BLOCK(TEXT)
GRNGO_SET_TEXT_CASE_BLOCK(LONG_TEXT)
default: {
return GRN_UNKNOWN_ERROR;
}
}
grn_rc rc = grn_bulk_write(ctx, &obj, value.ptr, value.size);
if (rc == GRN_SUCCESS) {
rc = grn_obj_set_value(ctx, column->srcs[0], id, &obj, GRN_OBJ_SET);
}
GRN_OBJ_FIN(ctx, &obj);
return rc;
}
#undef GRNGO_SET_TEXT_CASE_BLOCK
grn_rc
grngo_set_geo_point(grngo_column *column, grn_id id, grn_geo_point value) {
if (!column || !column->writable || !GRNGO_TEST_GEO_POINT(value)) {
return GRN_INVALID_ARGUMENT;
}
grn_ctx *ctx = column->db->ctx;
if (grn_table_at(ctx, column->table->objs[0], id) == GRN_ID_NIL) {
return GRN_INVALID_ARGUMENT;
}
grn_obj obj;
switch (column->value_type) {
case GRN_DB_TOKYO_GEO_POINT: {
GRN_TOKYO_GEO_POINT_INIT(&obj, 0);
break;
}
case GRN_DB_WGS84_GEO_POINT: {
GRN_WGS84_GEO_POINT_INIT(&obj, 0);
break;
}
default: {
return GRN_UNKNOWN_ERROR;
}
}
grn_rc rc = grn_bulk_write(ctx, &obj, (const char *)&value, sizeof(value));
if (rc == GRN_SUCCESS) {
rc = grn_obj_set_value(ctx, column->srcs[0], id, &obj, GRN_OBJ_SET);
}
GRN_OBJ_FIN(ctx, &obj);
return rc;
}
grn_rc
grngo_set_bool_vector(grngo_column *column, grn_id id, grngo_vector value) {
if (!column || !column->writable || !GRNGO_TEST_VECTOR(value)) {
return GRN_INVALID_ARGUMENT;
}
grn_ctx *ctx = column->db->ctx;
if (grn_table_at(ctx, column->table->objs[0], id) == GRN_ID_NIL) {
return GRN_INVALID_ARGUMENT;
}
grn_obj obj;
GRN_BOOL_INIT(&obj, GRN_OBJ_VECTOR);
grn_rc rc = grn_bulk_write(ctx, &obj, (const char *)value.ptr,
sizeof(grn_bool) * value.size);
if (rc == GRN_SUCCESS) {
rc = grn_obj_set_value(ctx, column->srcs[0], id, &obj, GRN_OBJ_SET);
}
GRN_OBJ_FIN(ctx, &obj);
return rc;
}
#define GRNGO_SET_INT_VECTOR_CASE_BLOCK(type)\
case GRN_DB_ ## type: {\
for (i = 0; i < value.size; i++) {\
if (!GRNGO_TEST_ ## type(values[i])) {\
return GRN_INVALID_ARGUMENT;\
}\
}\
GRN_ ## type ## _INIT(&obj, GRN_OBJ_VECTOR);\
rc = grn_bulk_space(ctx, &obj, sizeof(GRNGO_DB_TYPE(type)) * value.size);\
if (rc != GRN_SUCCESS) {\
break;\
}\
GRNGO_DB_TYPE(type) *head = (GRNGO_DB_TYPE(type) *)GRN_BULK_HEAD(&obj);\
for (i = 0; i < value.size; i++) {\
head[i] = (GRNGO_DB_TYPE(type))values[i];\
}\
break;\
}
grn_rc
grngo_set_int_vector(grngo_column *column, grn_id id, grngo_vector value) {
if (!column || !column->writable || !GRNGO_TEST_VECTOR(value)) {
return GRN_INVALID_ARGUMENT;
}
grn_ctx *ctx = column->db->ctx;
if (grn_table_at(ctx, column->table->objs[0], id) == GRN_ID_NIL) {
return GRN_INVALID_ARGUMENT;
}
grn_obj obj;
size_t i;
const int64_t *values = (const int64_t *)value.ptr;
grn_rc rc = GRN_SUCCESS;
switch (column->value_type) {
GRNGO_SET_INT_VECTOR_CASE_BLOCK(INT8)
GRNGO_SET_INT_VECTOR_CASE_BLOCK(INT16)
GRNGO_SET_INT_VECTOR_CASE_BLOCK(INT32)
GRNGO_SET_INT_VECTOR_CASE_BLOCK(INT64)
GRNGO_SET_INT_VECTOR_CASE_BLOCK(UINT8)
GRNGO_SET_INT_VECTOR_CASE_BLOCK(UINT16)
GRNGO_SET_INT_VECTOR_CASE_BLOCK(UINT32)
GRNGO_SET_INT_VECTOR_CASE_BLOCK(UINT64)
GRNGO_SET_INT_VECTOR_CASE_BLOCK(TIME)
default: {