-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbindex.c
3016 lines (2719 loc) · 94.5 KB
/
dbindex.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
/*
* $Id: $
* $Version: $
*
* Copyright (c) Enar Reilent 2009, Priit Järv 2010,2011,2013,2014
*
* This file is part of WhiteDB
*
* WhiteDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WhiteDB 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WhiteDB. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** @file dbindex.c
* Implementation of T-tree index
*/
/* ====== Includes =============== */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#include "config-w32.h"
#else
#include "config-gcc.h"
#endif
#include "dbdata.h"
#include "dbindex.h"
#include "dbcompare.h"
#include "dbhash.h"
/* ====== Private defs =========== */
#define LL_CASE 0
#define LR_CASE 1
#define RL_CASE 2
#define RR_CASE 3
#ifndef max
#define max(a,b) (a>b ? a : b)
#endif
#define HASHIDX_OP_STORE 1
#define HASHIDX_OP_REMOVE 2
#define HASHIDX_OP_FIND 3
/* ======= Private protos ================ */
#ifndef TTREE_SINGLE_COMPARE
static gint db_find_bounding_tnode(void *db, gint rootoffset, gint key,
gint *result, struct wg_tnode *rb_node);
#endif
static int db_which_branch_causes_overweight(void *db, struct wg_tnode *root);
static int db_rotate_ttree(void *db, gint index_id, struct wg_tnode *root,
int overw);
static gint ttree_add_row(void *db, gint index_id, void *rec);
static gint ttree_remove_row(void *db, gint index_id, void * rec);
static gint create_ttree_index(void *db, gint index_id);
static gint drop_ttree_index(void *db, gint column);
static gint insert_into_list(void *db, gint *head, gint value);
static void delete_from_list(void *db, gint *head);
#ifdef USE_INDEX_TEMPLATE
static gint add_index_template(void *db, gint *matchrec, gint reclen);
static gint find_index_template(void *db, gint *matchrec, gint reclen);
static gint remove_index_template(void *db, gint template_offset);
#endif
static gint hash_add_row(void *db, gint index_id, void *rec);
static gint hash_remove_row(void *db, gint index_id, void *rec);
static gint hash_recurse(void *db, wg_index_header *hdr, char *prefix,
gint prefixlen, gint *values, gint count, void *rec, gint op, gint expand);
static gint hash_extend_prefix(void *db, wg_index_header *hdr, char *prefix,
gint prefixlen, gint nextval, gint *values, gint count, void *rec, gint op,
gint expand);
static gint create_hash_index(void *db, gint index_id);
static gint drop_hash_index(void *db, gint index_id);
static gint sort_columns(gint *sorted_cols, gint *columns, gint col_count);
static gint show_index_error(void* db, char* errmsg);
static gint show_index_error_nr(void* db, char* errmsg, gint nr);
/* ====== Functions ============== */
/*
* Index implementation:
* - T-Tree, as described by Lehman & Carey '86
* This includes search with a single compare per node, enabled by
* defining TTREE_SINGLE_COMPARE
*
* - improvements loosely based on T* tree (Kim & Choi '96)
* Nodes have predecessor and successor pointers. This is turned
* on by defining TTREE_CHAINED_NODES. Other alterations described in
* the original T* tree paper were not implemented.
*
* - hash index (allows multi-column indexes) (not done yet)
*
* Index metainfo:
* data about indexes in system is stored in dbh->index_control_area_header
*
* index_table[] - 0 - 0 - v - 0 - 0 - v - 0
* | |
* index hdr A <--- list elem list elem ---> index hdr B
* ^ 0 v
* | |
* ----------------------- list elem
* 0
*
* index_table is a fixed size array that contains offsets to index
* lists by database field (column) number. Index lists themselves contain
* offsets to index headers. This arrangement is used so that one
* index can be referred to from several fields (index headers are
* unique, index list elements are not).
*
* In the above example, A is a (hash) index on columns 2 and 5, while B
* is an index on column 5.
*
* Note: offset to index header struct is also used as an index id.
*/
/* ------------------- T-tree private functions ------------- */
#ifndef TTREE_SINGLE_COMPARE
/**
* returns bounding node offset or if no really bounding node exists, then the closest node
*/
static gint db_find_bounding_tnode(void *db, gint rootoffset, gint key,
gint *result, struct wg_tnode *rb_node) {
struct wg_tnode * node = (struct wg_tnode *)offsettoptr(db,rootoffset);
/* Original tree search algorithm: compares both bounds of
* the node to determine immediately if the value falls between them.
*/
if(WG_COMPARE(db, key, node->current_min) == WG_LESSTHAN) {
/* if(key < node->current_max) */
if(node->left_child_offset != 0)
return db_find_bounding_tnode(db, node->left_child_offset,
key, result, NULL);
else {
*result = DEAD_END_LEFT_NOT_BOUNDING;
return rootoffset;
}
} else if(WG_COMPARE(db, key, node->current_max) != WG_GREATER) {
*result = REALLY_BOUNDING_NODE;
return rootoffset;
}
else { /* if(key > node->current_max) */
if(node->right_child_offset != 0)
return db_find_bounding_tnode(db, node->right_child_offset,
key, result, NULL);
else{
*result = DEAD_END_RIGHT_NOT_BOUNDING;
return rootoffset;
}
}
}
#else
/* "rightmost" node search is the improved tree search described in
* the original T-tree paper.
*/
#define db_find_bounding_tnode wg_search_ttree_rightmost
#endif
/**
* returns the description of imbalance - 4 cases possible
* LL - left child of the left child is overweight
* LR - right child of the left child is overweight
* etc
*/
static int db_which_branch_causes_overweight(void *db, struct wg_tnode *root){
struct wg_tnode *child;
if(root->left_subtree_height > root->right_subtree_height){
child = (struct wg_tnode *)offsettoptr(db,root->left_child_offset);
if(child->left_subtree_height >= child->right_subtree_height)return LL_CASE;
else return LR_CASE;
}else{
child = (struct wg_tnode *)offsettoptr(db,root->right_child_offset);
if(child->left_subtree_height > child->right_subtree_height)return RL_CASE;
else return RR_CASE;
}
}
static int db_rotate_ttree(void *db, gint index_id, struct wg_tnode *root, int overw){
gint grandparent = root->parent_offset;
gint initialrootoffset = ptrtooffset(db,root);
struct wg_tnode *r = NULL;
struct wg_tnode *g = (struct wg_tnode *)offsettoptr(db,grandparent);
wg_index_header *hdr = (wg_index_header *)offsettoptr(db,index_id);
gint column = hdr->rec_field_index[0]; /* always one column for T-tree */
if(overw == LL_CASE){
/* A B
* B C D A
* D E -> N E C
* N
*/
//printf("LL_CASE\n");
//save some stuff into variables for later use
gint offset_left_child = root->left_child_offset;
gint offset_right_grandchild = ((struct wg_tnode *)offsettoptr(db,offset_left_child))->right_child_offset;
gint right_grandchild_height = ((struct wg_tnode *)offsettoptr(db,offset_left_child))->right_subtree_height;
//first switch: E goes to A's left child
root->left_child_offset = offset_right_grandchild;
root->left_subtree_height = right_grandchild_height;
if(offset_right_grandchild != 0){
((struct wg_tnode *)offsettoptr(db,offset_right_grandchild))->parent_offset = ptrtooffset(db,root);
}
//second switch: A goes to B's right child
((struct wg_tnode *)offsettoptr(db,offset_left_child)) -> right_child_offset = ptrtooffset(db,root);
((struct wg_tnode *)offsettoptr(db,offset_left_child)) -> right_subtree_height = max(root->left_subtree_height,root->right_subtree_height)+1;
root->parent_offset = offset_left_child;
//for later grandparent fix
r = (struct wg_tnode *)offsettoptr(db,offset_left_child);
}else if(overw == RR_CASE){
/* A C
* B C A E
* D E -> B D N
* N
*/
//printf("RR_CASE\n");
//save some stuff into variables for later use
gint offset_right_child = root->right_child_offset;
gint offset_left_grandchild = ((struct wg_tnode *)offsettoptr(db,offset_right_child))->left_child_offset;
gint left_grandchild_height = ((struct wg_tnode *)offsettoptr(db,offset_right_child))->left_subtree_height;
//first switch: D goes to A's right child
root->right_child_offset = offset_left_grandchild;
root->right_subtree_height = left_grandchild_height;
if(offset_left_grandchild != 0){
((struct wg_tnode *)offsettoptr(db,offset_left_grandchild))->parent_offset = ptrtooffset(db,root);
}
//second switch: A goes to C's left child
((struct wg_tnode *)offsettoptr(db,offset_right_child)) -> left_child_offset = ptrtooffset(db,root);
((struct wg_tnode *)offsettoptr(db,offset_right_child)) -> left_subtree_height = max(root->right_subtree_height,root->left_subtree_height)+1;
root->parent_offset = offset_right_child;
//for later grandparent fix
r = (struct wg_tnode *)offsettoptr(db,offset_right_child);
}else if(overw == LR_CASE){
/* A E
* B C B A
* D E -> D F G C
* F G N
* N
*/
struct wg_tnode *bb, *ee;
//save some stuff into variables for later use
gint offset_left_child = root->left_child_offset;
gint offset_right_grandchild = ((struct wg_tnode *)offsettoptr(db,offset_left_child))->right_child_offset;
//first swtich: G goes to A's left child
ee = (struct wg_tnode *)offsettoptr(db,offset_right_grandchild);
root -> left_child_offset = ee -> right_child_offset;
root -> left_subtree_height = ee -> right_subtree_height;
if(ee -> right_child_offset != 0){
((struct wg_tnode *)offsettoptr(db,ee->right_child_offset))->parent_offset = ptrtooffset(db, root);
}
//second switch: F goes to B's right child
bb = (struct wg_tnode *)offsettoptr(db,offset_left_child);
bb -> right_child_offset = ee -> left_child_offset;
bb -> right_subtree_height = ee -> left_subtree_height;
if(ee -> left_child_offset != 0){
((struct wg_tnode *)offsettoptr(db,ee->left_child_offset))->parent_offset = offset_left_child;
}
//third switch: B goes to E's left child
/* The Lehman/Carey "special" LR rotation - instead of creating
* an internal node with one element, the values of what will become the
* left child will be moved over to the parent, thus ensuring the internal
* node is adequately filled. This is only allowed if E is a leaf.
*/
if(ee->number_of_elements == 1 && !ee->right_child_offset &&\
!ee->left_child_offset && bb->number_of_elements == WG_TNODE_ARRAY_SIZE){
int i;
/* Create space for elements from B */
ee->array_of_values[bb->number_of_elements - 1] = ee->array_of_values[0];
/* All the values moved are smaller than in E */
for(i=1; i<bb->number_of_elements; i++)
ee->array_of_values[i-1] = bb->array_of_values[i];
ee->number_of_elements = bb->number_of_elements;
/* Examine the new leftmost element to find current_min */
ee->current_min = wg_get_field(db, (void *)offsettoptr(db,
ee->array_of_values[0]), column);
bb -> number_of_elements = 1;
bb -> current_max = bb -> current_min;
}
//then switch the nodes
ee -> left_child_offset = offset_left_child;
ee -> left_subtree_height = max(bb->right_subtree_height,bb->left_subtree_height)+1;
bb -> parent_offset = offset_right_grandchild;
//fourth switch: A goes to E's right child
ee -> right_child_offset = ptrtooffset(db, root);
ee -> right_subtree_height = max(root->right_subtree_height,root->left_subtree_height)+1;
root -> parent_offset = offset_right_grandchild;
//for later grandparent fix
r = ee;
}else if(overw == RL_CASE){
/* A E
* C B A B
* E D -> C G F D
* G F N
* N
*/
struct wg_tnode *bb, *ee;
//save some stuff into variables for later use
gint offset_right_child = root->right_child_offset;
gint offset_left_grandchild = ((struct wg_tnode *)offsettoptr(db,offset_right_child))->left_child_offset;
//first swtich: G goes to A's left child
ee = (struct wg_tnode *)offsettoptr(db,offset_left_grandchild);
root -> right_child_offset = ee -> left_child_offset;
root -> right_subtree_height = ee -> left_subtree_height;
if(ee -> left_child_offset != 0){
((struct wg_tnode *)offsettoptr(db,ee->left_child_offset))->parent_offset = ptrtooffset(db, root);
}
//second switch: F goes to B's right child
bb = (struct wg_tnode *)offsettoptr(db,offset_right_child);
bb -> left_child_offset = ee -> right_child_offset;
bb -> left_subtree_height = ee -> right_subtree_height;
if(ee -> right_child_offset != 0){
((struct wg_tnode *)offsettoptr(db,ee->right_child_offset))->parent_offset = offset_right_child;
}
//third switch: B goes to E's right child
/* "special" RL rotation - see comments for LR_CASE */
if(ee->number_of_elements == 1 && !ee->right_child_offset &&\
!ee->left_child_offset && bb->number_of_elements == WG_TNODE_ARRAY_SIZE){
int i;
/* All the values moved are larger than in E */
for(i=1; i<bb->number_of_elements; i++)
ee->array_of_values[i] = bb->array_of_values[i-1];
ee->number_of_elements = bb->number_of_elements;
/* Examine the new rightmost element to find current_max */
ee->current_max = wg_get_field(db, (void *)offsettoptr(db,
ee->array_of_values[ee->number_of_elements - 1]), column);
/* Remaining B node array element should sit in slot 0 */
bb->array_of_values[0] = \
bb->array_of_values[bb->number_of_elements - 1];
bb -> number_of_elements = 1;
bb -> current_min = bb -> current_max;
}
ee -> right_child_offset = offset_right_child;
ee -> right_subtree_height = max(bb->right_subtree_height,bb->left_subtree_height)+1;
bb -> parent_offset = offset_left_grandchild;
//fourth switch: A goes to E's right child
ee -> left_child_offset = ptrtooffset(db, root);
ee -> left_subtree_height = max(root->right_subtree_height,root->left_subtree_height)+1;
root -> parent_offset = offset_left_grandchild;
//for later grandparent fix
r = ee;
} else {
/* catch an error case (can't really happen) */
show_index_error(db, "tree rotate called with invalid argument, "\
"index may have become corrupt");
return -1;
}
//fix grandparent - regardless of current 'overweight' case
if(grandparent == 0){//'grandparent' is index header data
r->parent_offset = 0;
//TODO more error check here
TTREE_ROOT_NODE(hdr) = ptrtooffset(db,r);
}else{//grandparent is usual node
//printf("change grandparent node\n");
r -> parent_offset = grandparent;
if(g->left_child_offset == initialrootoffset){//new subtree must replace the left child of grandparent
g->left_child_offset = ptrtooffset(db,r);
g->left_subtree_height = max(r->left_subtree_height,r->right_subtree_height)+1;
}else{
g->right_child_offset = ptrtooffset(db,r);
g->right_subtree_height = max(r->left_subtree_height,r->right_subtree_height)+1;
}
}
return 0;
}
/** inserts pointer to data row into index tree structure
*
* returns:
* 0 - on success
* -1 - if error
*/
static gint ttree_add_row(void *db, gint index_id, void *rec) {
gint rootoffset, column;
gint newvalue, boundtype, bnodeoffset, newoffset;
struct wg_tnode *node;
wg_index_header *hdr = (wg_index_header *)offsettoptr(db,index_id);
db_memsegment_header* dbh = dbmemsegh(db);
rootoffset = TTREE_ROOT_NODE(hdr);
#ifdef CHECK
if(rootoffset == 0){
#ifdef WG_NO_ERRPRINT
#else
fprintf(stderr,"index at offset %d does not exist\n", (int) index_id);
#endif
return -1;
}
#endif
column = hdr->rec_field_index[0]; /* always one column for T-tree */
//extract real value from the row (rec)
newvalue = wg_get_field(db, rec, column);
//find bounding node for the value
bnodeoffset = db_find_bounding_tnode(db, rootoffset, newvalue, &boundtype, NULL);
node = (struct wg_tnode *)offsettoptr(db,bnodeoffset);
newoffset = 0;//save here the offset of newly created tnode - 0 if no node added into the tree
//if bounding node exists - follow one algorithm, else the other
if(boundtype == REALLY_BOUNDING_NODE){
//check if the node has room for a new entry
if(node->number_of_elements < WG_TNODE_ARRAY_SIZE){
int i, j;
gint cr;
/* add array entry and update control data. We keep the
* array sorted, smallest values left. */
for(i=0; i<node->number_of_elements; i++) {
/* The node is small enough for naive scans to be
* "good enough" inside the node. Note that we
* branch into re-sort loop as early as possible
* with >= operator (> would be algorithmically correct too)
* since here the compare is more expensive than the slot
* copying.
*/
cr = WG_COMPARE(db, wg_get_field(db,
(void *)offsettoptr(db,node->array_of_values[i]), column),
newvalue);
if(cr != WG_LESSTHAN) { /* value >= newvalue */
/* Push remaining values to the right */
for(j=node->number_of_elements; j>i; j--)
node->array_of_values[j] = node->array_of_values[j-1];
break;
}
}
/* i is either number_of_elements or a vacated slot
* in the array now. */
node->array_of_values[i] = ptrtooffset(db,rec);
node->number_of_elements++;
/* Update min. Due to the >= comparison max is preserved
* in this case. Note that we are overwriting values that
* WG_COMPARE() may deem equal. This is intentional, because other
* parts of T-tree algorithm rely on encoded values of min/max fields
* to be in sync with the leftmost/rightmost slots.
*/
if(i==0) {
node->current_min = newvalue;
}
}
else{
//still, insert the value here, but move minimum out of this node
//get the minimum element from this node
int i, j;
gint cr, minvalue, minvaluerowoffset;
minvalue = node->current_min;
minvaluerowoffset = node->array_of_values[0];
/* Now scan for the matching slot. However, since
* we already know the 0 slot will be re-filled, we
* do this scan (and sort) in reverse order, compared to the case
* where array had some space left. */
for(i=WG_TNODE_ARRAY_SIZE-1; i>0; i--) {
cr = WG_COMPARE(db, wg_get_field(db,
(void *)offsettoptr(db,node->array_of_values[i]), column),
newvalue);
if(cr != WG_GREATER) { /* value <= newvalue */
/* Push remaining values to the left */
for(j=0; j<i; j++)
node->array_of_values[j] = node->array_of_values[j+1];
break;
}
}
/* i is either 0 or a freshly vacated slot */
node->array_of_values[i] = ptrtooffset(db,rec);
/* Update minimum. Thanks to the sorted array, we know for a fact
* that the minimum sits in slot 0. */
if(i==0) {
node->current_min = newvalue;
} else {
node->current_min = wg_get_field(db,
(void *)offsettoptr(db,node->array_of_values[0]), column);
/* The scan for the free slot starts from the right and
* tries to exit as fast as possible. So it's possible that
* the rightmost slot was changed.
*/
if(i == WG_TNODE_ARRAY_SIZE-1) {
node->current_max = newvalue;
}
}
//proceed to the node that holds greatest lower bound - must be leaf (can be the initial bounding node)
if(node->left_child_offset != 0){
#ifndef TTREE_CHAINED_NODES
gint greatestlb = wg_ttree_find_glb_node(db,node->left_child_offset);
#else
gint greatestlb = node->pred_offset;
#endif
node = (struct wg_tnode *)offsettoptr(db, greatestlb);
}
//if the greatest lower bound node has room, insert value
//otherwise make the new node as right child and put the value there
if(node->number_of_elements < WG_TNODE_ARRAY_SIZE){
//add array entry and update control data
node->array_of_values[node->number_of_elements] = minvaluerowoffset;//save offset, use first free slot
node->number_of_elements++;
node->current_max = minvalue;
}else{
//create, initialize and save first value
struct wg_tnode *leaf;
gint newnode = wg_alloc_fixlen_object(db, &dbh->tnode_area_header);
if(newnode == 0)return -1;
leaf =(struct wg_tnode *)offsettoptr(db,newnode);
leaf->parent_offset = ptrtooffset(db,node);
leaf->left_subtree_height = 0;
leaf->right_subtree_height = 0;
leaf->current_max = minvalue;
leaf->current_min = minvalue;
leaf->number_of_elements = 1;
leaf->left_child_offset = 0;
leaf->right_child_offset = 0;
leaf->array_of_values[0] = minvaluerowoffset;
/* If the original, full node did not have a left child, then
* there also wasn't a separate GLB node, so we are adding one now
* as the left child. Otherwise, the new node is added as the right
* child to the current GLB node.
*/
if(bnodeoffset == ptrtooffset(db,node)) {
node->left_child_offset = newnode;
#ifdef TTREE_CHAINED_NODES
/* Create successor / predecessor relationship */
leaf->succ_offset = ptrtooffset(db, node);
leaf->pred_offset = node->pred_offset;
if(node->pred_offset) {
struct wg_tnode *pred = \
(struct wg_tnode *) offsettoptr(db, node->pred_offset);
pred->succ_offset = newnode;
} else {
TTREE_MIN_NODE(hdr) = newnode;
}
node->pred_offset = newnode;
#endif
} else {
#ifdef TTREE_CHAINED_NODES
struct wg_tnode *succ;
#endif
node->right_child_offset = newnode;
#ifdef TTREE_CHAINED_NODES
/* Insert the new node in the sequential chain between
* the original node and the GLB node found */
leaf->succ_offset = node->succ_offset;
leaf->pred_offset = ptrtooffset(db, node);
#ifdef CHECK
if(!node->succ_offset) {
show_index_error(db, "GLB with no successor, panic");
return -1;
} else {
#endif
succ = (struct wg_tnode *) offsettoptr(db, leaf->succ_offset);
succ->pred_offset = newnode;
#ifdef CHECK
}
#endif
node->succ_offset = newnode;
#endif /* TTREE_CHAINED_NODES */
}
newoffset = newnode;
}
}
}//the bounding node existed - first algorithm
else{// bounding node does not exist
//try to insert the new value to that node - becoming new min or max
//if the node has room for a new entry
if(node->number_of_elements < WG_TNODE_ARRAY_SIZE){
int i;
/* add entry, keeping the array sorted (see also notes for the
* bounding node case. The difference this time is that we already
* know if this value is becoming the new min or max).
*/
if(boundtype == DEAD_END_LEFT_NOT_BOUNDING) {
/* our new value is the new min, push everything right */
for(i=node->number_of_elements; i>0; i--)
node->array_of_values[i] = node->array_of_values[i-1];
node->array_of_values[0] = ptrtooffset(db,rec);
node->current_min = newvalue;
} else { /* DEAD_END_RIGHT_NOT_BOUNDING */
/* even simpler case, new value is added to the right */
node->array_of_values[node->number_of_elements] = ptrtooffset(db,rec);
node->current_max = newvalue;
}
node->number_of_elements++;
/* XXX: not clear if the empty node can occur here. Until this
* is checked, we'll be paranoid and overwrite both min and max. */
if(node->number_of_elements==1) {
node->current_max = newvalue;
node->current_min = newvalue;
}
}else{
//make a new node and put data there
struct wg_tnode *leaf;
gint newnode = wg_alloc_fixlen_object(db, &dbh->tnode_area_header);
if(newnode == 0)return -1;
leaf =(struct wg_tnode *)offsettoptr(db,newnode);
leaf->parent_offset = ptrtooffset(db,node);
leaf->left_subtree_height = 0;
leaf->right_subtree_height = 0;
leaf->current_max = newvalue;
leaf->current_min = newvalue;
leaf->number_of_elements = 1;
leaf->left_child_offset = 0;
leaf->right_child_offset = 0;
leaf->array_of_values[0] = ptrtooffset(db,rec);
newoffset = newnode;
//set new node as left or right leaf
if(boundtype == DEAD_END_LEFT_NOT_BOUNDING){
node->left_child_offset = newnode;
#ifdef TTREE_CHAINED_NODES
/* Set the new node as predecessor of the parent */
leaf->succ_offset = ptrtooffset(db, node);
leaf->pred_offset = node->pred_offset;
if(node->pred_offset) {
/* Notify old predecessor that the node following
* it changed */
struct wg_tnode *pred = \
(struct wg_tnode *) offsettoptr(db, node->pred_offset);
pred->succ_offset = newnode;
} else {
TTREE_MIN_NODE(hdr) = newnode;
}
node->pred_offset = newnode;
#endif
}else if(boundtype == DEAD_END_RIGHT_NOT_BOUNDING){
node->right_child_offset = newnode;
#ifdef TTREE_CHAINED_NODES
/* Set the new node as successor of the parent */
leaf->succ_offset = node->succ_offset;
leaf->pred_offset = ptrtooffset(db, node);
if(node->succ_offset) {
/* Notify old successor that the node preceding
* it changed */
struct wg_tnode *succ = \
(struct wg_tnode *) offsettoptr(db, node->succ_offset);
succ->pred_offset = newnode;
} else {
TTREE_MAX_NODE(hdr) = newnode;
}
node->succ_offset = newnode;
#endif
}
}
}//no bounding node found - algorithm 2
//if new node was added to tree - must update child height data in nodes from leaf to root
//or until find a node with imbalance
//then determine the bad balance case: LL, LR, RR or RL and execute proper rotation
if(newoffset){
struct wg_tnode *child = (struct wg_tnode *)offsettoptr(db,newoffset);
struct wg_tnode *parent;
int left = 0;
while(child->parent_offset != 0){//this is not a root
int balance;
parent = (struct wg_tnode *)offsettoptr(db,child->parent_offset);
//determine which child the child is, left or right one
if(parent->left_child_offset == ptrtooffset(db,child)) left = 1;
else left = 0;
//increment parent left or right subtree height
if(left)parent->left_subtree_height++;
else parent->right_subtree_height++;
//check balance
balance = parent->left_subtree_height - parent->right_subtree_height;
if(balance == 0) {
/* As a result of adding a new node somewhere below, left
* and right subtrees of the node we're checking became
* of EQUAL height. This means that changes in subtree heights
* do not propagate any further (the max depth in this node
* dit NOT change).
*/
break;
}
if(balance > 1 || balance < -1){//must rebalance
//the current parent is root for balancing operation
//determine the branch that causes overweight
int overw = db_which_branch_causes_overweight(db,parent);
//fix balance
db_rotate_ttree(db,index_id,parent,overw);
break;//while loop because balance does not change in the next levels
}else{//just proceed to the parent node
child = parent;
}
}
}
return 0;
}
/** removes pointer to data row from index tree structure
*
* returns:
* 0 - on success
* -1 - if error, index doesnt exist
* -2 - if error, no bounding node for key
* -3 - if error, boundig node exists, value not
* -4 - if error, tree not in balance
*/
static gint ttree_remove_row(void *db, gint index_id, void * rec) {
int i, found;
gint key, rootoffset, column, boundtype, bnodeoffset;
gint rowoffset;
struct wg_tnode *node, *parent;
wg_index_header *hdr = (wg_index_header *)offsettoptr(db,index_id);
rootoffset = TTREE_ROOT_NODE(hdr);
#ifdef CHECK
if(rootoffset == 0){
#ifdef WG_NO_ERRPRINT
#else
fprintf(stderr,"index at offset %d does not exist\n", (int) index_id);
#endif
return -1;
}
#endif
column = hdr->rec_field_index[0]; /* always one column for T-tree */
key = wg_get_field(db, rec, column);
rowoffset = ptrtooffset(db, rec);
/* find bounding node for the value. Since non-unique values
* are allowed, we will find the leftmost node and scan
* right from there (we *need* the exact row offset).
*/
bnodeoffset = wg_search_ttree_leftmost(db,
rootoffset, key, &boundtype, NULL);
node = (struct wg_tnode *)offsettoptr(db,bnodeoffset);
//if bounding node does not exist - error
if(boundtype != REALLY_BOUNDING_NODE) return -2;
/* find the record inside the node. This is an expensive loop if there
* are many repeated values, so unnecessary deleting should be avoided
* on higher level.
*/
found = -1;
for(;;) {
for(i=0;i<node->number_of_elements;i++){
if(node->array_of_values[i] == rowoffset) {
found = i;
goto found_row;
}
}
bnodeoffset = TNODE_SUCCESSOR(db, node);
if(!bnodeoffset)
break; /* no more successors */
node = (struct wg_tnode *)offsettoptr(db,bnodeoffset);
if(WG_COMPARE(db, node->current_min, key) == WG_GREATER)
break; /* successor is not a bounding node */
}
found_row:
if(found == -1) return -3;
//delete the key and rearrange other elements
node->number_of_elements--;
if(found < node->number_of_elements) { /* not the last element */
/* slide the elements to the right of the found value
* one step to the left */
for(i=found; i<node->number_of_elements; i++)
node->array_of_values[i] = node->array_of_values[i+1];
}
/* Update min/max */
if(found==node->number_of_elements && node->number_of_elements != 0) {
/* Rightmost element was removed, so new max should be updated to
* the new rightmost value */
node->current_max = wg_get_field(db, (void *)offsettoptr(db,
node->array_of_values[node->number_of_elements - 1]), column);
} else if(found==0 && node->number_of_elements != 0) {
/* current_min removed, update to new leftmost value */
node->current_min = wg_get_field(db, (void *)offsettoptr(db,
node->array_of_values[0]), column);
}
//check underflow and take some actions if needed
if(node->number_of_elements < 5){//TODO use macro
//if the node is internal node - borrow its gratest lower bound from the node where it is
if(node->left_child_offset != 0 && node->right_child_offset != 0){//internal node
#ifndef TTREE_CHAINED_NODES
gint greatestlb = wg_ttree_find_glb_node(db,node->left_child_offset);
#else
gint greatestlb = node->pred_offset;
#endif
struct wg_tnode *glbnode = (struct wg_tnode *)offsettoptr(db, greatestlb);
/* Make space for a new min value */
for(i=node->number_of_elements; i>0; i--)
node->array_of_values[i] = node->array_of_values[i-1];
/* take the glb value (always the rightmost in the array) and
* insert it in our node */
node -> array_of_values[0] = \
glbnode->array_of_values[glbnode->number_of_elements-1];
node -> number_of_elements++;
node -> current_min = glbnode -> current_max;
if(node->number_of_elements == 1) /* we just got our first element */
node->current_max = glbnode -> current_max;
glbnode -> number_of_elements--;
//reset new max for glbnode
if(glbnode->number_of_elements != 0) {
glbnode->current_max = wg_get_field(db, (void *)offsettoptr(db,
glbnode->array_of_values[glbnode->number_of_elements - 1]), column);
}
node = glbnode;
}
}
//now variable node points to the node which really lost an element
//this is definitely leaf or half-leaf
//if the node is empty - free it and rebalanc the tree
parent = NULL;
//delete the empty leaf
if(node->left_child_offset == 0 && node->right_child_offset == 0 && node->number_of_elements == 0){
if(node->parent_offset != 0){
parent = (struct wg_tnode *)offsettoptr(db, node->parent_offset);
//was it left or right child
if(parent->left_child_offset == ptrtooffset(db,node)){
parent->left_child_offset=0;
parent->left_subtree_height=0;
}else{
parent->right_child_offset=0;
parent->right_subtree_height=0;
}
}
#ifdef TTREE_CHAINED_NODES
/* Remove the node from sequential chain */
if(node->succ_offset) {
struct wg_tnode *succ = \
(struct wg_tnode *) offsettoptr(db, node->succ_offset);
succ->pred_offset = node->pred_offset;
} else {
TTREE_MAX_NODE(hdr) = node->pred_offset;
}
if(node->pred_offset) {
struct wg_tnode *pred = \
(struct wg_tnode *) offsettoptr(db, node->pred_offset);
pred->succ_offset = node->succ_offset;
} else {
TTREE_MIN_NODE(hdr) = node->succ_offset;
}
#endif
/* Free the node, unless it's the root node */
if(node != offsettoptr(db, TTREE_ROOT_NODE(hdr))) {
wg_free_tnode(db, ptrtooffset(db,node));
} else {
/* Set empty state of root node */
node->current_max = WG_ILLEGAL;
node->current_min = WG_ILLEGAL;
#ifdef TTREE_CHAINED_NODES
TTREE_MAX_NODE(hdr) = TTREE_ROOT_NODE(hdr);
TTREE_MIN_NODE(hdr) = TTREE_ROOT_NODE(hdr);
#endif
}
//rebalance if needed
}
//or if the node was a half-leaf, see if it can be merged with its leaf
if((node->left_child_offset == 0 && node->right_child_offset != 0) || (node->left_child_offset != 0 && node->right_child_offset == 0)){
int elements = node->number_of_elements;
int left;
struct wg_tnode *child;
if(node->left_child_offset != 0){
child = (struct wg_tnode *)offsettoptr(db, node->left_child_offset);
left = 1;//true
}else{
child = (struct wg_tnode *)offsettoptr(db, node->right_child_offset);
left = 0;//false
}
elements += child->number_of_elements;
if(!(child->left_subtree_height == 0 && child->right_subtree_height == 0)){
show_index_error(db,
"index tree is not balanced, deleting algorithm doesn't work");
return -4;
}
//if possible move all elements from child to node and free child
if(elements <= WG_TNODE_ARRAY_SIZE){
int i = node->number_of_elements;
int j;
node->number_of_elements = elements;
if(left){
/* Left child elements are all smaller than in current node */
for(j=i-1; j>=0; j--){
node->array_of_values[j + child->number_of_elements] = \
node->array_of_values[j];
}
for(j=0;j<child->number_of_elements;j++){
node->array_of_values[j]=child->array_of_values[j];
}
node->left_subtree_height=0;
node->left_child_offset=0;
node->current_min=child->current_min;
if(!i) node->current_max=child->current_max; /* parent was empty */
}else{
/* Right child elements are all larger than in current node */
for(j=0;j<child->number_of_elements;j++){
node->array_of_values[i+j]=child->array_of_values[j];
}
node->right_subtree_height=0;
node->right_child_offset=0;
node->current_max=child->current_max;
if(!i) node->current_min=child->current_min; /* parent was empty */
}
#ifdef TTREE_CHAINED_NODES
/* Remove the child from sequential chain */
if(child->succ_offset) {
struct wg_tnode *succ = \
(struct wg_tnode *) offsettoptr(db, child->succ_offset);
succ->pred_offset = child->pred_offset;
} else {
TTREE_MAX_NODE(hdr) = child->pred_offset;
}
if(child->pred_offset) {
struct wg_tnode *pred = \
(struct wg_tnode *) offsettoptr(db, child->pred_offset);
pred->succ_offset = child->succ_offset;
} else {
TTREE_MIN_NODE(hdr) = child->succ_offset;
}
#endif
wg_free_tnode(db, ptrtooffset(db, child));
if(node->parent_offset) {
parent = (struct wg_tnode *)offsettoptr(db, node->parent_offset);
if(parent->left_child_offset==ptrtooffset(db,node)){
parent->left_subtree_height=1;
}else{
parent->right_subtree_height=1;
}
}
}
}
//check balance and update subtree height data
//stop when find a node where subtree heights dont change
if(parent != NULL){