forked from groonga/groonga
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.c
More file actions
5530 lines (5162 loc) · 159 KB
/
Copy pathhash.c
File metadata and controls
5530 lines (5162 loc) · 159 KB
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
/*
Copyright (C) 2009-2018 Brazil
Copyright (C) 2018-2024 Sutou Kouhei <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
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_hash.h"
#include "grn_normalizer.h"
#include "grn_obj.h"
#include "grn_output.h"
#include "grn_posting.h"
#include "grn_selector.h"
#include "grn_store.h"
#include "grn_wal.h"
#include <string.h>
#include <limits.h>
/* grn_tiny_array */
/* Requirements: id != GRN_ID_NIL. */
inline static int
grn_tiny_array_get_block_id(grn_id id)
{
grn_bit_scan_rev_result most_significant_one_bit_offset;
GRN_BIT_SCAN_REV(id, most_significant_one_bit_offset);
return most_significant_one_bit_offset >> GRN_TINY_ARRAY_FACTOR;
}
/* Requirements: id != GRN_ID_NIL. */
inline static void *
grn_tiny_array_get(grn_tiny_array *array, grn_id id) {
const int block_id = grn_tiny_array_get_block_id(id);
uint8_t * const block = (uint8_t *)array->blocks[block_id];
if (block) {
const size_t offset = GRN_TINY_ARRAY_GET_OFFSET(block_id);
return block + (id - offset) * array->element_size;
}
return NULL;
}
/* Requirements: id != GRN_ID_NIL. */
inline static void *
grn_tiny_array_put(grn_tiny_array *array, grn_id id) {
const int block_id = grn_tiny_array_get_block_id(id);
void ** const block = &array->blocks[block_id];
const size_t offset = GRN_TINY_ARRAY_GET_OFFSET(block_id);
if (!*block) {
grn_ctx * const ctx = array->ctx;
if (array->flags & GRN_TINY_ARRAY_THREADSAFE) {
CRITICAL_SECTION_ENTER(array->lock);
}
if (!*block) {
const size_t block_size =
GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id) * array->element_size;
if (array->flags & GRN_TINY_ARRAY_USE_MALLOC) {
if (array->flags & GRN_TINY_ARRAY_CLEAR) {
*block = GRN_CALLOC(block_size);
} else {
*block = GRN_MALLOC(block_size);
}
} else {
*block = GRN_CTX_ALLOC(ctx, block_size);
}
}
if (array->flags & GRN_TINY_ARRAY_THREADSAFE) {
CRITICAL_SECTION_LEAVE(array->lock);
}
if (!*block) {
return NULL;
}
}
if (id > array->max) {
array->max = id;
}
return (uint8_t *)*block + (id - offset) * array->element_size;
}
inline static void *
grn_tiny_array_at_inline(grn_tiny_array *array, grn_id id)
{
return id ? grn_tiny_array_put(array, id) : NULL;
}
void
grn_tiny_array_init(grn_ctx *ctx, grn_tiny_array *array,
uint16_t element_size, uint16_t flags)
{
array->ctx = ctx;
array->max = 0;
array->element_size = element_size;
array->flags = flags;
memset(array->blocks, 0, sizeof(array->blocks));
if (flags & GRN_TINY_ARRAY_THREADSAFE) {
CRITICAL_SECTION_INIT(array->lock);
}
}
void
grn_tiny_array_fin(grn_tiny_array *array)
{
int block_id;
grn_ctx * const ctx = array->ctx;
for (block_id = 0; block_id < GRN_TINY_ARRAY_NUM_BLOCKS; block_id++) {
if (array->blocks[block_id]) {
if (array->flags & GRN_TINY_ARRAY_USE_MALLOC) {
GRN_FREE(array->blocks[block_id]);
} else {
GRN_CTX_FREE(ctx, array->blocks[block_id]);
}
array->blocks[block_id] = NULL;
}
}
if (array->flags & GRN_TINY_ARRAY_THREADSAFE) {
CRITICAL_SECTION_FIN(array->lock);
}
}
void *
grn_tiny_array_at(grn_tiny_array *array, grn_id id)
{
return grn_tiny_array_at_inline(array, id);
}
grn_id
grn_tiny_array_id(grn_tiny_array *array, const void *element_address)
{
const uint8_t * const ptr = (const uint8_t *)element_address;
uint32_t block_id;
grn_id offset = 1;
for (block_id = 0; block_id < GRN_TINY_ARRAY_NUM_BLOCKS; block_id++) {
const uint32_t block_size = GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id);
const uint8_t * const block = (const uint8_t *)array->blocks[block_id];
if (block) {
if (block <= ptr && ptr < (block + block_size * array->element_size)) {
return offset + (grn_id)((ptr - block) / array->element_size);
}
}
offset += block_size;
}
return GRN_ID_NIL;
}
/* grn_tiny_bitmap */
static void
grn_tiny_bitmap_init(grn_ctx *ctx, grn_tiny_bitmap *bitmap)
{
bitmap->ctx = ctx;
memset(bitmap->blocks, 0, sizeof(bitmap->blocks));
}
static void
grn_tiny_bitmap_fin(grn_tiny_bitmap *bitmap)
{
int block_id;
grn_ctx * const ctx = bitmap->ctx;
for (block_id = 0; block_id < GRN_TINY_ARRAY_NUM_BLOCKS; block_id++) {
if (bitmap->blocks[block_id]) {
GRN_CTX_FREE(ctx, bitmap->blocks[block_id]);
bitmap->blocks[block_id] = NULL;
}
}
}
/* Requirements: bit_id != GRN_ID_NIL. */
inline static uint8_t *
grn_tiny_bitmap_get_byte(grn_tiny_bitmap *bitmap, grn_id bit_id) {
const uint32_t byte_id = (bit_id >> 3) + 1;
const int block_id = grn_tiny_array_get_block_id(byte_id);
uint8_t * const block = (uint8_t *)bitmap->blocks[block_id];
if (block) {
const size_t offset = GRN_TINY_ARRAY_GET_OFFSET(block_id);
return block + byte_id - offset;
}
return NULL;
}
/* Requirements: bit_id != GRN_ID_NIL. */
inline static uint8_t *
grn_tiny_bitmap_put_byte(grn_tiny_bitmap *bitmap, grn_id bit_id) {
const uint32_t byte_id = (bit_id >> 3) + 1;
const int block_id = grn_tiny_array_get_block_id(byte_id);
void ** const block = &bitmap->blocks[block_id];
const size_t offset = GRN_TINY_ARRAY_GET_OFFSET(block_id);
if (!*block) {
grn_ctx * const ctx = bitmap->ctx;
*block = GRN_CTX_ALLOC(ctx, GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id));
if (!*block) {
return NULL;
}
}
return (uint8_t *)*block + byte_id - offset;
}
/* Requirements: bit_id != GRN_ID_NIL. */
/* Return value: 1/0 on success, -1 on failure. */
/* Note: A bitmap is extended if needed. */
inline static int
grn_tiny_bitmap_put(grn_tiny_bitmap *bitmap, grn_id bit_id)
{
uint8_t * const ptr = grn_tiny_bitmap_put_byte(bitmap, bit_id);
return ptr ? ((*ptr >> (bit_id & 7)) & 1) : -1;
}
/* Requirements: bit_id != GRN_ID_NIL. */
inline static uint8_t *
grn_tiny_bitmap_get_and_set(grn_tiny_bitmap *bitmap, grn_id bit_id,
bool bit)
{
uint8_t * const ptr = grn_tiny_bitmap_get_byte(bitmap, bit_id);
if (ptr) {
/* This branch will be removed because the given `bit' is constant. */
if (bit) {
*ptr |= 1 << (bit_id & 7);
} else {
*ptr &= ~((uint8_t)(1 << (bit_id & 7)));
}
}
return ptr;
}
/* Requirements: bit_id != GRN_ID_NIL. */
/* Note: A bitmap is extended if needed. */
inline static uint8_t *
grn_tiny_bitmap_put_and_set(grn_tiny_bitmap *bitmap, grn_id bit_id,
bool bit)
{
uint8_t * const ptr = grn_tiny_bitmap_put_byte(bitmap, bit_id);
if (ptr) {
/* This branch will be removed because the given `bit' is constant. */
if (bit) {
*ptr |= 1 << (bit_id & 7);
} else {
*ptr &= ~((uint8_t)(1 << (bit_id & 7)));
}
}
return ptr;
}
/* grn_io_array */
#define GRN_ARRAY_MAX (GRN_ID_MAX - 8)
inline static grn_rc
grn_io_array_wal_touch(grn_ctx *ctx, grn_array *array, const char *tag)
{
if (array->wal_touched) {
return GRN_SUCCESS;
}
if (GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_NONE) {
return GRN_SUCCESS;
}
grn_rc rc = grn_wal_touch(ctx, (grn_obj *)array, false, tag);
if (rc == GRN_SUCCESS) {
array->wal_touched = true;
}
return rc;
}
inline static void *
grn_io_array_at_inline(grn_ctx *ctx, grn_io *io, uint32_t segment_id,
uint64_t offset, int flags)
{
return grn_io_array_at(ctx, io, segment_id, offset, &flags);
}
/*
* grn_io_array_bit_at() returns 1/0 on success, -1 on failure.
*/
inline static int
grn_io_array_bit_at(grn_ctx *ctx, grn_io *io,
uint32_t segment_id, uint64_t offset)
{
uint8_t * const ptr = (uint8_t *)grn_io_array_at_inline(
ctx, io, segment_id, (offset >> 3) + 1, 0);
return ptr ? ((*ptr >> (offset & 7)) & 1) : -1;
}
/*
* The following functions, grn_io_array_bit_*(), return a non-NULL pointer on
* success, a NULL pointer on failure.
*/
inline static void *
grn_io_array_bit_on(grn_ctx *ctx, grn_io *io,
uint32_t segment_id, uint64_t offset)
{
uint8_t * const ptr = (uint8_t *)grn_io_array_at_inline(
ctx, io, segment_id, (offset >> 3) + 1, GRN_TABLE_ADD);
if (ptr) {
*ptr |= 1 << (offset & 7);
}
return ptr;
}
inline static void *
grn_io_array_bit_off(grn_ctx *ctx, grn_io *io,
uint32_t segment_id, uint64_t offset)
{
uint8_t *const ptr =
(uint8_t *)grn_io_array_at_inline(
ctx, io, segment_id, (offset >> 3) + 1, GRN_TABLE_ADD);
if (ptr) {
*ptr &= ~((uint8_t)(1 << (offset & 7)));
}
return ptr;
}
/* grn_array */
#define GRN_ARRAY_SEGMENT_SIZE 0x400000
#define GRN_ARRAY_STATUS_TRUNCATED (0x01<<0)
#define GRN_ARRAY_STATUS_GARBAGES_BUFFER_INITIALIZED (0x01<<1)
#define GRN_ARRAY_GARBAGES_BUFFER_SIZE 5
/* Header of grn_io-based grn_array. */
struct grn_array_header {
uint32_t flags;
uint32_t curr_rec;
uint32_t value_size;
uint32_t n_entries;
uint32_t n_garbages;
grn_id garbages;
uint32_t lock;
uint32_t status;
uint32_t n_garbages_in_buffer;
grn_id garbages_buffer[GRN_ARRAY_GARBAGES_BUFFER_SIZE];
uint32_t reserved[1];
};
/*
* A grn_io-based grn_array consists of the following 2 segments.
* GRN_ARRAY_VALUE_SEGMENT: stores values.
* GRN_ARRAY_BITMAP_SEGMENT: stores whether entries are valid or not.
*/
enum {
GRN_ARRAY_VALUE_SEGMENT = 0,
GRN_ARRAY_BITMAP_SEGMENT = 1
};
inline static bool
grn_array_is_io_array(grn_array *array)
{
return array->io != NULL;
}
inline static bool
grn_array_can_use_value_for_garbage(grn_array *array)
{
return array->value_size >= sizeof(grn_id);
}
/*
* array must be grn_array_is_io_array(array) &&
* !grn_array_can_use_value_for_garbage(array). The
* grn_array_is_io_array(array) restriction may be removed when we
* need to implement this for tiny array.
*/
inline static void
grn_array_put_garbage_to_buffer(grn_ctx *ctx,
grn_array *array,
grn_id garbage_id)
{
struct grn_array_header * const header = array->header;
if (header->garbages != GRN_ID_NIL &&
header->n_garbages_in_buffer < GRN_ARRAY_GARBAGES_BUFFER_SIZE) {
int i;
for (i = 0; i < GRN_ARRAY_GARBAGES_BUFFER_SIZE; i++) {
if (header->garbages_buffer[i] == GRN_ID_NIL) {
header->garbages_buffer[i] = header->garbages;
header->n_garbages_in_buffer++;
header->garbages = GRN_ID_NIL;
break;
}
}
}
if (header->garbages == GRN_ID_NIL) {
header->garbages = garbage_id;
}
}
inline static void *
grn_array_io_entry_at(grn_ctx *ctx, grn_array *array, grn_id id, int flags)
{
return grn_io_array_at_inline(ctx, array->io, GRN_ARRAY_VALUE_SEGMENT, id, flags);
}
inline static void *
grn_array_entry_at(grn_ctx *ctx, grn_array *array, grn_id id, int flags)
{
if (grn_array_is_io_array(array)) {
return grn_array_io_entry_at(ctx, array, id, flags);
} else {
return grn_tiny_array_at_inline(&array->array, id);
}
}
/* grn_array_bitmap_at() returns 1/0 on success, -1 on failure. */
inline static int
grn_array_bitmap_at(grn_ctx *ctx, grn_array *array, grn_id id)
{
if (grn_array_is_io_array(array)) {
int bit = grn_io_array_bit_at(ctx, array->io, GRN_ARRAY_BITMAP_SEGMENT, id);
if (bit == 0 && !grn_array_can_use_value_for_garbage(array)) {
grn_array_put_garbage_to_buffer(ctx, array, id);
}
return bit;
} else {
return grn_tiny_bitmap_put(&array->bitmap, id);
}
}
static grn_rc
grn_array_init_tiny_array(grn_ctx *ctx, grn_array *array, const char *path,
uint32_t value_size, uint32_t flags)
{
if (path) {
ERR(GRN_INVALID_ARGUMENT, "failed to create tiny array");
return ctx->rc;
}
array->obj.header.flags = (grn_obj_flags)flags;
array->ctx = ctx;
array->value_size = value_size;
array->n_keys = 0;
array->keys = NULL;
array->n_garbages = &array->n_garbages_buf;
array->n_entries = &array->n_entries_buf;
array->n_garbages_buf = 0;
array->n_entries_buf = 0;
array->io = NULL;
array->header = NULL;
array->garbages = GRN_ID_NIL;
grn_tiny_array_init(ctx,
&array->array,
(uint16_t)value_size,
GRN_TINY_ARRAY_CLEAR);
grn_tiny_bitmap_init(ctx, &array->bitmap);
return GRN_SUCCESS;
}
static grn_io *
grn_array_create_io_array(grn_ctx *ctx, const char *path, uint32_t value_size)
{
uint32_t w_of_element = 0;
grn_io_array_spec array_spec[2];
while ((1U << w_of_element) < value_size) {
w_of_element++;
}
array_spec[GRN_ARRAY_VALUE_SEGMENT].w_of_element = w_of_element;
array_spec[GRN_ARRAY_VALUE_SEGMENT].max_n_segments =
1U << (30 - (22 - w_of_element));
array_spec[GRN_ARRAY_BITMAP_SEGMENT].w_of_element = 0;
array_spec[GRN_ARRAY_BITMAP_SEGMENT].max_n_segments = 1U << (30 - (22 + 3));
return grn_io_create_with_array(ctx, path, sizeof(struct grn_array_header),
GRN_ARRAY_SEGMENT_SIZE, GRN_IO_AUTO,
2, array_spec);
}
static grn_rc
grn_array_init_io_array(grn_ctx *ctx, grn_array *array, const char *path,
uint32_t value_size, uint32_t flags)
{
grn_io *io;
struct grn_array_header *header;
io = grn_array_create_io_array(ctx, path, value_size);
if (!io) {
return ctx->rc;
}
grn_io_set_type(io, GRN_TABLE_NO_KEY);
header = grn_io_header(io);
header->flags = flags;
header->curr_rec = 0;
header->lock = 0;
header->value_size = value_size;
header->n_entries = 0;
header->n_garbages = 0;
header->garbages = GRN_ID_NIL;
header->status = 0;
header->n_garbages_in_buffer = 0;
{
int i;
for (i = 0; i < GRN_ARRAY_GARBAGES_BUFFER_SIZE; i++) {
header->garbages_buffer[i] = GRN_ID_NIL;
}
}
header->status |= GRN_ARRAY_STATUS_GARBAGES_BUFFER_INITIALIZED;
array->obj.header.flags = (grn_obj_flags)flags;
array->ctx = ctx;
array->value_size = value_size;
array->n_keys = 0;
array->keys = NULL;
array->n_garbages = &header->n_garbages;
array->n_entries = &header->n_entries;
array->io = io;
array->header = header;
array->lock = &header->lock;
array->wal_touched = false;
return GRN_SUCCESS;
}
static grn_rc
grn_array_init(grn_ctx *ctx, grn_array *array,
const char *path, uint32_t value_size, uint32_t flags)
{
if (flags & GRN_ARRAY_TINY) {
return grn_array_init_tiny_array(ctx, array, path, value_size, flags);
} else {
return grn_array_init_io_array(ctx, array, path, value_size, flags);
}
}
grn_array *
grn_array_create(grn_ctx *ctx, const char *path, uint32_t value_size, uint32_t flags)
{
if (ctx) {
grn_array * const array = (grn_array *)GRN_CALLOC(sizeof(grn_array));
if (array) {
GRN_DB_OBJ_SET_TYPE(array, GRN_TABLE_NO_KEY);
if (!grn_array_init(ctx, array, path, value_size, flags)) {
return array;
}
GRN_FREE(array);
}
}
return NULL;
}
grn_array *
grn_array_open(grn_ctx *ctx, const char *path)
{
if (ctx) {
grn_io * const io = grn_io_open(ctx, path, GRN_IO_AUTO);
if (io) {
struct grn_array_header * const header = grn_io_header(io);
uint32_t io_type = grn_io_get_type(io);
if (io_type == GRN_TABLE_NO_KEY) {
grn_array * const array = (grn_array *)GRN_CALLOC(sizeof(grn_array));
if (array) {
if (!(header->flags & GRN_ARRAY_TINY)) {
GRN_DB_OBJ_SET_TYPE(array, GRN_TABLE_NO_KEY);
array->obj.header.flags = (grn_obj_flags)(header->flags);
array->ctx = ctx;
array->value_size = header->value_size;
array->n_keys = 0;
array->keys = NULL;
array->n_garbages = &header->n_garbages;
array->n_entries = &header->n_entries;
array->io = io;
array->header = header;
array->lock = &header->lock;
array->wal_touched = false;
/* For old array created by Groonga that doesn't have
* garbages_buffer. */
if (!grn_array_can_use_value_for_garbage(array) &&
!(header->status & GRN_ARRAY_STATUS_GARBAGES_BUFFER_INITIALIZED)) {
header->n_garbages_in_buffer = 0;
int i;
for (i = 0; i < GRN_ARRAY_GARBAGES_BUFFER_SIZE; i++) {
header->garbages_buffer[i] = GRN_ID_NIL;
}
header->status |= GRN_ARRAY_STATUS_GARBAGES_BUFFER_INITIALIZED;
}
return array;
} else {
GRN_LOG(ctx, GRN_LOG_NOTICE, "invalid array flags. (%x)", header->flags);
}
GRN_FREE(array);
}
} else {
ERR(GRN_INVALID_FORMAT,
"[table][array] file type must be %#04x: <%#04x>",
GRN_TABLE_NO_KEY, io_type);
}
grn_io_close(ctx, io);
}
}
return NULL;
}
/*
* grn_array_error_if_truncated() logs an error and returns its error code if
* an array is truncated by another process.
* Otherwise, this function returns GRN_SUCCESS.
* Note that `ctx` and `array` must be valid.
*
* FIXME: An array should be reopened if possible.
*/
static grn_rc
grn_array_error_if_truncated(grn_ctx *ctx, grn_array *array)
{
if (array->header && (array->header->status & GRN_ARRAY_STATUS_TRUNCATED)) {
ERR(GRN_FILE_CORRUPT,
"array is truncated, please unmap or reopen the database");
return GRN_FILE_CORRUPT;
}
return GRN_SUCCESS;
}
grn_rc
grn_array_close(grn_ctx *ctx, grn_array *array)
{
grn_rc rc = GRN_SUCCESS;
if (!ctx || !array) { return GRN_INVALID_ARGUMENT; }
if (array->keys) { GRN_FREE(array->keys); }
if (grn_array_is_io_array(array)) {
if (array->io->path[0] != '\0' &&
GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_PRIMARY) {
grn_obj_flush(ctx, (grn_obj *)array);
}
rc = grn_io_close(ctx, array->io);
} else {
GRN_ASSERT(ctx == array->ctx);
grn_tiny_array_fin(&array->array);
grn_tiny_bitmap_fin(&array->bitmap);
}
GRN_FREE(array);
return rc;
}
grn_rc
grn_array_remove(grn_ctx *ctx, const char *path)
{
if (!ctx || !path) { return GRN_INVALID_ARGUMENT; }
grn_rc wal_rc = grn_wal_remove(ctx, path, "[array]");
grn_rc io_rc = grn_io_remove(ctx, path);
grn_rc rc = io_rc;
if (rc == GRN_SUCCESS) {
rc = wal_rc;
}
return rc;
}
uint32_t
grn_array_size(grn_ctx *ctx, grn_array *array)
{
if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) {
return 0;
}
return *array->n_entries;
}
uint32_t
grn_array_get_flags(grn_ctx *ctx, grn_array *array)
{
return array->header->flags;
}
grn_rc
grn_array_truncate(grn_ctx *ctx, grn_array *array)
{
grn_rc rc;
char *path = NULL;
uint32_t value_size, flags;
if (!ctx || !array) { return GRN_INVALID_ARGUMENT; }
rc = grn_array_error_if_truncated(ctx, array);
if (rc != GRN_SUCCESS) {
return rc;
}
if (grn_array_is_io_array(array)) {
const char * const io_path = grn_io_path(array->io);
if (io_path && *io_path) {
path = GRN_STRDUP(io_path);
if (!path) {
ERR(GRN_NO_MEMORY_AVAILABLE, "cannot duplicate path: <%s>", io_path);
return GRN_NO_MEMORY_AVAILABLE;
}
}
}
value_size = array->value_size;
flags = array->obj.header.flags;
if (grn_array_is_io_array(array)) {
if (path) {
/* Only an I/O array with a valid path uses the `truncated` flag. */
array->header->status |= GRN_ARRAY_STATUS_TRUNCATED;
}
rc = grn_io_close(ctx, array->io);
if (!rc) {
array->io = NULL;
if (path) {
rc = grn_array_remove(ctx, path);
}
}
}
if (!rc) {
rc = grn_array_init(ctx, array, path, value_size, flags);
}
if (path) { GRN_FREE(path); }
return rc;
}
inline static grn_id
grn_array_get_max_id(grn_array *array)
{
return grn_array_is_io_array(array) ? array->header->curr_rec : array->array.max;
}
inline static void *
grn_array_get_value_inline(grn_ctx *ctx, grn_array *array, grn_id id)
{
if (!ctx || !array) {
return NULL;
}
if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) {
return NULL;
}
if (*array->n_garbages) {
/*
* grn_array_bitmap_at() is a time-consuming function, so it is called only
* when there are garbages in the array.
*/
if (grn_array_bitmap_at(ctx, array, id) != 1) {
return NULL;
}
} else if (id == 0 || id > grn_array_get_max_id(array)) {
return NULL;
}
return grn_array_entry_at(ctx, array, id, 0);
}
int
grn_array_get_value(grn_ctx *ctx, grn_array *array, grn_id id, void *valuebuf)
{
void * const value = grn_array_get_value_inline(ctx, array, id);
if (value) {
if (valuebuf) {
grn_memcpy(valuebuf, value, array->value_size);
}
return (int)(array->value_size);
}
return 0;
}
void *
_grn_array_get_value(grn_ctx *ctx, grn_array *array, grn_id id)
{
return grn_array_get_value_inline(ctx, array, id);
}
inline static grn_rc
grn_array_set_value_inline(grn_ctx *ctx, grn_array *array, grn_id id,
const void *value, int flags)
{
void *entry;
entry = grn_array_entry_at(ctx, array, id, 0);
if (!entry) {
return GRN_NO_MEMORY_AVAILABLE;
}
if (grn_array_is_io_array(array)) {
grn_rc rc = grn_io_array_wal_touch(ctx, array, "[array][add]");
if (rc != GRN_SUCCESS) {
return rc;
}
}
switch ((flags & GRN_OBJ_SET_MASK)) {
case GRN_OBJ_SET :
grn_memcpy(entry, value, array->value_size);
return GRN_SUCCESS;
case GRN_OBJ_INCR :
switch (array->value_size) {
case sizeof(int32_t) :
*((int32_t *)entry) += *((int32_t *)value);
return GRN_SUCCESS;
case sizeof(int64_t) :
*((int64_t *)entry) += *((int64_t *)value);
return GRN_SUCCESS;
default :
return GRN_INVALID_ARGUMENT;
}
break;
case GRN_OBJ_DECR :
switch (array->value_size) {
case sizeof(int32_t) :
*((int32_t *)entry) -= *((int32_t *)value);
return GRN_SUCCESS;
case sizeof(int64_t) :
*((int64_t *)entry) -= *((int64_t *)value);
return GRN_SUCCESS;
default :
return GRN_INVALID_ARGUMENT;
}
break;
default :
/* todo : support other types. */
return GRN_INVALID_ARGUMENT;
}
}
grn_rc
grn_array_set_value(grn_ctx *ctx, grn_array *array, grn_id id,
const void *value, int flags)
{
grn_rc rc;
if (!ctx || !array || !value) {
return GRN_INVALID_ARGUMENT;
}
rc = grn_array_error_if_truncated(ctx, array);
if (rc != GRN_SUCCESS) {
return rc;
}
if (*array->n_garbages) {
/*
* grn_array_bitmap_at() is a time-consuming function, so it is called only
* when there are garbages in the array.
*/
if (grn_array_bitmap_at(ctx, array, id) != 1) {
return GRN_INVALID_ARGUMENT;
}
} else if (id == 0 || id > grn_array_get_max_id(array)) {
return GRN_INVALID_ARGUMENT;
}
return grn_array_set_value_inline(ctx, array, id, value, flags);
}
grn_rc
grn_array_delete_by_id(grn_ctx *ctx, grn_array *array, grn_id id,
grn_table_delete_optarg *optarg)
{
grn_rc rc;
if (!ctx || !array) {
return GRN_INVALID_ARGUMENT;
}
rc = grn_array_error_if_truncated(ctx, array);
if (rc != GRN_SUCCESS) {
return rc;
}
if (grn_array_bitmap_at(ctx, array, id) != 1) {
return GRN_INVALID_ARGUMENT;
}
{
rc = GRN_SUCCESS;
/* lock */
if (grn_array_is_io_array(array)) {
rc = grn_io_array_wal_touch(ctx, array, "[array][delete]");
if (rc != GRN_SUCCESS) {
return rc;
}
if (grn_array_can_use_value_for_garbage(array)) {
struct grn_array_header * const header = array->header;
void * const entry = grn_array_io_entry_at(ctx, array, id, 0);
if (!entry) {
rc = GRN_INVALID_ARGUMENT;
} else {
*((grn_id *)entry) = header->garbages;
header->garbages = id;
}
} else {
grn_array_put_garbage_to_buffer(ctx, array, id);
}
if (!rc) {
(*array->n_entries)--;
(*array->n_garbages)++;
/*
* The following grn_io_array_bit_off() fails iff a problem has
* occurred after the above grn_array_bitmap_at(). That is to say,
* an unexpected case.
*/
grn_io_array_bit_off(ctx, array->io, GRN_ARRAY_BITMAP_SEGMENT, id);
}
} else {
if (grn_array_can_use_value_for_garbage(array)) {
void * const entry = grn_tiny_array_get(&array->array, id);
if (!entry) {
rc = GRN_INVALID_ARGUMENT;
ERR(rc,
"[array][delete][tiny] failed to get garbage entry: %p: %u(%u)",
array,
id,
*(array->n_garbages));
} else {
*((grn_id *)entry) = array->garbages;
array->garbages = id;
}
}
if (rc == GRN_SUCCESS) {
(*array->n_entries)--;
(*array->n_garbages)++;
/*
* The following grn_io_array_bit_off() fails iff a problem has
* occurred after the above grn_array_bitmap_at(). That is to say,
* an unexpected case.
*/
grn_tiny_bitmap_get_and_set(&array->bitmap, id, 0);
}
}
/* unlock */
return rc;
}
}
grn_id
grn_array_at(grn_ctx *ctx, grn_array *array, grn_id id)
{
if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) {
return GRN_ID_NIL;
}
if (*array->n_garbages) {
/*
* grn_array_bitmap_at() is a time-consuming function, so it is called only
* when there are garbages in the array.
*/
if (grn_array_bitmap_at(ctx, array, id) != 1) {
return GRN_ID_NIL;
}
} else if (id > grn_array_get_max_id(array)) {
return GRN_ID_NIL;
}
return id;
}
grn_rc
grn_array_copy_sort_key(grn_ctx *ctx, grn_array *array,
grn_table_sort_key *keys, size_t n_keys)
{
array->keys = (grn_table_sort_key *)GRN_MALLOCN(grn_table_sort_key, n_keys);
if (!array->keys) {
return ctx->rc;
}
grn_memcpy(array->keys, keys, sizeof(grn_table_sort_key) * n_keys);
array->n_keys = n_keys;
return GRN_SUCCESS;
}
void
grn_array_cursor_close(grn_ctx *ctx, grn_array_cursor *cursor)
{
GRN_ASSERT(cursor->ctx == ctx);
GRN_FREE(cursor);
}
grn_array_cursor *
grn_array_cursor_open(grn_ctx *ctx, grn_array *array, grn_id min, grn_id max,
int offset, int limit, int flags)
{
grn_array_cursor *cursor;
if (!array || !ctx) { return NULL; }
if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) {
return NULL;
}
cursor = (grn_array_cursor *)GRN_MALLOCN(grn_array_cursor, 1);
if (!cursor) { return NULL; }
GRN_DB_OBJ_SET_TYPE(cursor, GRN_CURSOR_TABLE_NO_KEY);
cursor->array = array;
cursor->ctx = ctx;
cursor->obj.header.flags = (grn_obj_flags)(flags);
cursor->obj.header.domain = GRN_ID_NIL;
if (flags & GRN_CURSOR_DESCENDING) {
cursor->dir = -1;
if (max) {
cursor->curr_rec = max;
if (!(flags & GRN_CURSOR_LT)) { cursor->curr_rec++; }
} else {
cursor->curr_rec = grn_array_get_max_id(array) + 1;
}
if (min) {
cursor->tail = min;
if ((flags & GRN_CURSOR_GT)) { cursor->tail++; }
} else {
cursor->tail = GRN_ID_NIL + 1;
}
if (cursor->curr_rec < cursor->tail) { cursor->tail = cursor->curr_rec; }
} else {
cursor->dir = 1;
if (min) {
cursor->curr_rec = min;
if (!(flags & GRN_CURSOR_GT)) { cursor->curr_rec--; }
} else {
cursor->curr_rec = GRN_ID_NIL;
}
if (max) {
cursor->tail = max;
if ((flags & GRN_CURSOR_LT)) { cursor->tail--; }