/
idx.cpp
1752 lines (1485 loc) · 47.4 KB
/
idx.cpp
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
/*
* PROGRAM: JRD Access Method
* MODULE: idx.cpp
* DESCRIPTION: Index manager
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2003.03.04 Dmitry Yemanov: Added support for NULLs in unique indices.
* Done in two stages:
* 1. Restored old behaviour of having only _one_
* NULL key allowed (i.e. two NULLs are considered
* duplicates). idx_e_nullunique error was removed.
* 2. Changed algorithms in IDX_create_index() and
* check_duplicates() to ignore NULL key duplicates.
*/
#include "firebird.h"
#include <string.h>
#include "../jrd/common.h"
#include "../jrd/jrd.h"
#include "../jrd/val.h"
#include "../jrd/intl.h"
#include "../jrd/req.h"
#include "../jrd/ods.h"
#include "../jrd/btr.h"
#include "../jrd/sort.h"
#include "../jrd/lls.h"
#include "../jrd/tra.h"
#include "gen/iberror.h"
#include "../jrd/sbm.h"
#include "../jrd/exe.h"
#include "../jrd/scl.h"
#include "../jrd/lck.h"
#include "../jrd/rse.h"
#include "../jrd/cch.h"
#include "../jrd/gdsassert.h"
#include "../jrd/btr_proto.h"
#include "../jrd/cch_proto.h"
#include "../jrd/cmp_proto.h"
#include "../jrd/dpm_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/evl_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/idx_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/jrd_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/sort_proto.h"
#include "../jrd/vio_proto.h"
#include "../jrd/tra_proto.h"
using namespace Jrd;
using namespace Ods;
using namespace Firebird;
// Data to be passed to index fast load duplicates routine
struct index_fast_load
{
SLONG ifl_duplicates;
USHORT ifl_key_length;
};
static idx_e check_duplicates(thread_db*, Record*, index_desc*, index_insertion*, jrd_rel*);
static idx_e check_foreign_key(thread_db*, Record*, jrd_rel*, jrd_tra*, index_desc*, jrd_rel**, USHORT *);
static idx_e check_partner_index(thread_db*, jrd_rel*, Record*, jrd_tra*, index_desc*, jrd_rel*, USHORT);
static bool duplicate_key(const UCHAR*, const UCHAR*, void*);
static PageNumber get_root_page(thread_db*, jrd_rel*);
static int index_block_flush(void*);
static idx_e insert_key(thread_db*, jrd_rel*, Record*, jrd_tra*, WIN *, index_insertion*, jrd_rel**, USHORT *);
static bool key_equal(const temporary_key*, const temporary_key*);
static void release_index_block(thread_db*, IndexBlock*);
static void signal_index_deletion(thread_db*, jrd_rel*, USHORT);
void IDX_check_access(thread_db* tdbb, CompilerScratch* csb, jrd_rel* view, jrd_rel* relation)
{
/**************************************
*
* I D X _ c h e c k _ a c c e s s
*
**************************************
*
* Functional description
* Check the various indices in a relation
* to see if we need REFERENCES access to fields
* in the primary key. Don't call this routine for
* views or external relations, since the mechanism
* ain't there.
*
**************************************/
SET_TDBB(tdbb);
index_desc idx;
idx.idx_id = idx_invalid;
RelationPages* relPages = relation->getPages(tdbb);
WIN window(relPages->rel_pg_space_id, -1);
WIN referenced_window(relPages->rel_pg_space_id, -1);
while (BTR_next_index(tdbb, relation, 0, &idx, &window))
{
if (idx.idx_flags & idx_foreign)
{
// find the corresponding primary key index
if (!MET_lookup_partner(tdbb, relation, &idx, 0)) {
continue;
}
jrd_rel* referenced_relation = MET_relation(tdbb, idx.idx_primary_relation);
MET_scan_relation(tdbb, referenced_relation);
const USHORT index_id = idx.idx_primary_index;
// get the description of the primary key index
referenced_window.win_page = get_root_page(tdbb, referenced_relation);
referenced_window.win_flags = 0;
index_root_page* referenced_root =
(index_root_page*) CCH_FETCH(tdbb, &referenced_window, LCK_read, pag_root);
index_desc referenced_idx;
if (!BTR_description(tdbb, referenced_relation, referenced_root,
&referenced_idx, index_id))
{
BUGCHECK(173); // msg 173 referenced index description not found
}
// post references access to each field in the index
const index_desc::idx_repeat* idx_desc = referenced_idx.idx_rpt;
for (USHORT i = 0; i < referenced_idx.idx_count; i++, idx_desc++)
{
const jrd_fld* referenced_field =
MET_get_field(referenced_relation, idx_desc->idx_field);
CMP_post_access(tdbb, csb,
referenced_relation->rel_security_name,
(view ? view->rel_id : 0),
SCL_sql_references, "TABLE",
referenced_relation->rel_name);
CMP_post_access(tdbb, csb,
referenced_field->fld_security_name, 0,
SCL_sql_references, "COLUMN",
referenced_field->fld_name);
}
CCH_RELEASE(tdbb, &referenced_window);
}
}
}
bool IDX_check_master_types (thread_db* tdbb, index_desc& idx, jrd_rel* partner_relation, int& bad_segment)
{
/**********************************************
*
* I D X _ c h e c k _ m a s t e r _ t y p e s
*
**********************************************
*
* Functional description
* Check if both indices of foreign key constraint
* has compatible data types in appropriate segments.
* Called when detail index is created after idx_itype
* was assigned
*
**********************************************/
SET_TDBB(tdbb);
index_desc partner_idx;
// get the index root page for the partner relation
WIN window(get_root_page(tdbb, partner_relation));
index_root_page* root = (index_root_page*) CCH_FETCH(tdbb, &window, LCK_read, pag_root);
// get the description of the partner index
if (!BTR_description(tdbb, partner_relation, root, &partner_idx, idx.idx_primary_index))
BUGCHECK(175); // msg 175 partner index description not found
CCH_RELEASE(tdbb, &window);
// make sure partner index have the same segment count as our
fb_assert(idx.idx_count == partner_idx.idx_count);
for (int i = 0; i < idx.idx_count; i++)
{
if (idx.idx_rpt[i].idx_itype != partner_idx.idx_rpt[i].idx_itype)
{
bad_segment = i;
return false;
}
}
return true;
}
void IDX_create_index(thread_db* tdbb,
jrd_rel* relation,
index_desc* idx,
const TEXT* index_name,
USHORT* index_id,
jrd_tra* transaction,
SelectivityList& selectivity)
{
/**************************************
*
* I D X _ c r e a t e _ i n d e x
*
**************************************
*
* Functional description
* Create and populate index.
*
**************************************/
idx_e result = idx_e_ok;
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
if (relation->rel_file)
{
ERR_post(Arg::Gds(isc_no_meta_update) <<
Arg::Gds(isc_extfile_uns_op) << Arg::Str(relation->rel_name));
}
else if (relation->isVirtual())
{
ERR_post(Arg::Gds(isc_no_meta_update) <<
Arg::Gds(isc_wish_list));
}
get_root_page(tdbb, relation);
fb_assert(transaction);
BTR_reserve_slot(tdbb, relation, transaction, idx);
if (index_id) {
*index_id = idx->idx_id;
}
record_param primary, secondary;
secondary.rpb_relation = relation;
primary.rpb_relation = relation;
primary.rpb_number.setValue(BOF_NUMBER);
//primary.getWindow(tdbb).win_flags = secondary.getWindow(tdbb).win_flags = 0; redundant
const bool isODS11 = (dbb->dbb_ods_version >= ODS_VERSION11);
const bool isDescending = (idx->idx_flags & idx_descending);
const bool isPrimary = (idx->idx_flags & idx_primary);
// hvlad: in ODS11 empty string and NULL values can have the same binary
// representation in index keys. BTR can distinguish it by the key_length
// but SORT module currently don't take it into account. Therefore add to
// the index key one byte prefix with 0 for NULL value and 1 for not-NULL
// value to produce right sorting.
// BTR\fast_load will remove this one byte prefix from the index key.
// Note that this is necessary only for single-segment ascending indexes
// and only for ODS11 and higher.
const int nullIndLen = isODS11 && !isDescending && (idx->idx_count == 1) ? 1 : 0;
const USHORT key_length = ROUNDUP(BTR_key_length(tdbb, relation, idx) + nullIndLen, sizeof(SINT64));
const USHORT max_key_size =
isODS11 ? MAX_KEY_LIMIT : MAX_KEY_PRE_ODS11;
if (key_length >= max_key_size)
{
ERR_post(Arg::Gds(isc_no_meta_update) <<
Arg::Gds(isc_keytoobig) << Arg::Str(index_name));
}
RecordStack stack;
const UCHAR pad = isDescending ? -1 : 0;
index_fast_load ifl_data;
ifl_data.ifl_duplicates = 0;
ifl_data.ifl_key_length = key_length;
bool key_is_null = false;
sort_key_def key_desc[2];
// Key sort description
key_desc[0].skd_dtype = SKD_bytes;
key_desc[0].skd_flags = SKD_ascending;
key_desc[0].skd_length = key_length;
key_desc[0].skd_offset = 0;
key_desc[0].skd_vary_offset = 0;
// RecordNumber sort description
key_desc[1].skd_dtype = SKD_int64;
key_desc[1].skd_flags = SKD_ascending;
key_desc[1].skd_length = sizeof(RecordNumber);
key_desc[1].skd_offset = key_length;
key_desc[1].skd_vary_offset = 0;
FPTR_REJECT_DUP_CALLBACK callback = (idx->idx_flags & idx_unique) ? duplicate_key : NULL;
void* callback_arg = (idx->idx_flags & idx_unique) ? &ifl_data : NULL;
sort_context* sort_handle =
SORT_init(dbb, &transaction->tra_sorts, key_length + sizeof(index_sort_record),
2, 1, key_desc, callback, callback_arg);
try {
jrd_rel* partner_relation = 0;
USHORT partner_index_id = 0;
if (idx->idx_flags & idx_foreign)
{
if (!MET_lookup_partner(tdbb, relation, idx, index_name)) {
BUGCHECK(173); // msg 173 referenced index description not found
}
partner_relation = MET_relation(tdbb, idx->idx_primary_relation);
partner_index_id = idx->idx_primary_index;
}
// Checkout a garbage collect record block for fetching data.
Record* gc_record = VIO_gc_record(tdbb, relation);
// Unless this is the only attachment or a database restore, worry about
// preserving the page working sets of other attachments.
Attachment* attachment = tdbb->getAttachment();
if (attachment && (attachment != dbb->dbb_attachments || attachment->att_next))
{
if (attachment->att_flags & ATT_gbak_attachment ||
DPM_data_pages(tdbb, relation) > (SLONG) dbb->dbb_bcb->bcb_count)
{
primary.getWindow(tdbb).win_flags = secondary.getWindow(tdbb).win_flags = WIN_large_scan;
primary.rpb_org_scans = secondary.rpb_org_scans = relation->rel_scan_count++;
}
}
// Loop thru the relation computing index keys. If there are old versions, find them, too.
temporary_key key;
while (DPM_next(tdbb, &primary, LCK_read,
#ifdef SCROLLABLE_CURSORS
false,
#endif
false))
{
if (!VIO_garbage_collect(tdbb, &primary, transaction))
continue;
if (primary.rpb_flags & rpb_deleted)
CCH_RELEASE(tdbb, &primary.getWindow(tdbb));
else
{
primary.rpb_record = gc_record;
VIO_data(tdbb, &primary, dbb->dbb_permanent);
gc_record = primary.rpb_record;
stack.push(primary.rpb_record);
}
secondary.rpb_page = primary.rpb_b_page;
secondary.rpb_line = primary.rpb_b_line;
secondary.rpb_prior = primary.rpb_prior;
while (secondary.rpb_page)
{
if (!DPM_fetch(tdbb, &secondary, LCK_read))
break; // must be garbage collected
secondary.rpb_record = NULL;
VIO_data(tdbb, &secondary, tdbb->getDefaultPool());
stack.push(secondary.rpb_record);
secondary.rpb_page = secondary.rpb_b_page;
secondary.rpb_line = secondary.rpb_b_line;
}
while (stack.hasData())
{
Record* record = stack.pop();
// If foreign key index is being defined, make sure foreign
// key definition will not be violated
if (idx->idx_flags & idx_foreign)
{
idx_null_state null_state;
// find out if there is a null segment by faking uniqueness --
// if there is one, don't bother to check the primary key
if (!(idx->idx_flags & idx_unique))
{
idx->idx_flags |= idx_unique;
result = BTR_key(tdbb, relation, record, idx, &key, &null_state, false);
idx->idx_flags &= ~idx_unique;
}
else
{
result = BTR_key(tdbb, relation, record, idx, &key, &null_state, false);
}
if (result == idx_e_ok && null_state == idx_nulls_none)
{
result = check_partner_index(tdbb, relation, record, transaction, idx,
partner_relation, partner_index_id);
}
}
if (result == idx_e_ok)
{
idx_null_state null_state;
result = BTR_key(tdbb, relation, record, idx, &key, &null_state, false);
if (result == idx_e_ok)
{
if (isPrimary && null_state != idx_nulls_none)
{
fb_assert(key.key_null_segment < idx->idx_count);
const USHORT bad_id = idx->idx_rpt[key.key_null_segment].idx_field;
const jrd_fld *bad_fld = MET_get_field(relation, bad_id);
ERR_post(Arg::Gds(isc_not_valid) << Arg::Str(bad_fld->fld_name) <<
Arg::Str(NULL_STRING_MARK));
}
key_is_null = (null_state == idx_nulls_all);
}
}
if (result != idx_e_ok)
{
do {
if (record != gc_record)
delete record;
} while (stack.hasData() && (record = stack.pop()));
gc_record->rec_flags &= ~REC_gc_active;
if (primary.getWindow(tdbb).win_flags & WIN_large_scan)
--relation->rel_scan_count;
ERR_duplicate_error(result, relation, idx->idx_id, index_name);
}
if (key.key_length > key_length)
{
do {
if (record != gc_record)
delete record;
} while (stack.hasData() && (record = stack.pop()));
gc_record->rec_flags &= ~REC_gc_active;
if (primary.getWindow(tdbb).win_flags & WIN_large_scan)
--relation->rel_scan_count;
ERR_post(Arg::Gds(isc_key_too_big));
}
UCHAR* p;
SORT_put(tdbb, sort_handle, reinterpret_cast<ULONG**>(&p));
// try to catch duplicates early
if (ifl_data.ifl_duplicates > 0)
{
do {
if (record != gc_record)
delete record;
} while (stack.hasData() && (record = stack.pop()));
gc_record->rec_flags &= ~REC_gc_active;
if (primary.getWindow(tdbb).win_flags & WIN_large_scan)
--relation->rel_scan_count;
ERR_post(Arg::Gds(isc_no_dup) << Arg::Str(index_name));
}
USHORT l = key.key_length;
if (nullIndLen) {
*p++ = (key.key_length == 0) ? 0 : 1;
}
if (l > 0)
{
memcpy(p, key.key_data, l);
p += l;
}
if ( (l = key_length - nullIndLen - key.key_length) )
{
memset(p, pad, l);
p += l;
}
index_sort_record* isr = (index_sort_record*) p;
isr->isr_record_number = primary.rpb_number.getValue();
isr->isr_key_length = key.key_length;
isr->isr_flags = (stack.hasData() ? ISR_secondary : 0) | (key_is_null ? ISR_null : 0);
if (record != gc_record)
delete record;
}
if (--tdbb->tdbb_quantum < 0)
JRD_reschedule(tdbb, 0, true);
}
gc_record->rec_flags &= ~REC_gc_active;
if (primary.getWindow(tdbb).win_flags & WIN_large_scan)
--relation->rel_scan_count;
SORT_sort(tdbb, sort_handle);
if (ifl_data.ifl_duplicates > 0) {
ERR_post(Arg::Gds(isc_no_dup) << Arg::Str(index_name));
}
}
catch (const Firebird::Exception& ex)
{
Firebird::stuff_exception(tdbb->tdbb_status_vector, ex);
SORT_fini(sort_handle);
ERR_punt();
}
BTR_create(tdbb, relation, idx, key_length, sort_handle, selectivity);
if (ifl_data.ifl_duplicates > 0)
{
// we don't need SORT_fini() here, as it's called inside BTR_create()
ERR_post(Arg::Gds(isc_no_dup) << Arg::Str(index_name));
}
if ((relation->rel_flags & REL_temp_conn) &&
(relation->getPages(tdbb)->rel_instance_id != 0))
{
IndexLock* idx_lock = CMP_get_index_lock(tdbb, relation, idx->idx_id);
if (idx_lock)
{
++idx_lock->idl_count;
if (idx_lock->idl_count == 1) {
LCK_lock(tdbb, idx_lock->idl_lock, LCK_SR, LCK_WAIT);
}
}
}
}
IndexBlock* IDX_create_index_block(thread_db* tdbb, jrd_rel* relation, USHORT id)
{
/**************************************
*
* I D X _ c r e a t e _ i n d e x _ b l o c k
*
**************************************
*
* Functional description
* Create an index block and an associated
* lock block for the specified index.
*
**************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
IndexBlock* index_block = FB_NEW(*dbb->dbb_permanent) IndexBlock();
index_block->idb_id = id;
// link the block in with the relation linked list
index_block->idb_next = relation->rel_index_blocks;
relation->rel_index_blocks = index_block;
// create a shared lock for the index, to coordinate
// any modification to the index so that the cached information
// about the index will be discarded
Lock* lock = FB_NEW_RPT(*dbb->dbb_permanent, 0) Lock;
index_block->idb_lock = lock;
lock->lck_parent = dbb->dbb_lock;
lock->lck_dbb = dbb;
lock->lck_key.lck_long = (relation->rel_id << 16) | index_block->idb_id;
lock->lck_length = sizeof(lock->lck_key.lck_long);
lock->lck_type = LCK_expression;
lock->lck_owner_handle = LCK_get_owner_handle(tdbb, lock->lck_type);
lock->lck_ast = index_block_flush;
lock->lck_object = index_block;
return index_block;
}
void IDX_delete_index(thread_db* tdbb, jrd_rel* relation, USHORT id)
{
/**************************************
*
* I D X _ d e l e t e _ i n d e x
*
**************************************
*
* Functional description
* Delete a single index.
*
**************************************/
SET_TDBB(tdbb);
signal_index_deletion(tdbb, relation, id);
WIN window(get_root_page(tdbb, relation));
CCH_FETCH(tdbb, &window, LCK_write, pag_root);
const bool tree_exists = BTR_delete_index(tdbb, &window, id);
if ((relation->rel_flags & REL_temp_conn) && (relation->getPages(tdbb)->rel_instance_id != 0) &&
tree_exists)
{
IndexLock* idx_lock = CMP_get_index_lock(tdbb, relation, id);
if (idx_lock)
{
if (!--idx_lock->idl_count) {
LCK_release(tdbb, idx_lock->idl_lock);
}
}
}
}
void IDX_delete_indices(thread_db* tdbb, jrd_rel* relation, RelationPages* relPages)
{
/**************************************
*
* I D X _ d e l e t e _ i n d i c e s
*
**************************************
*
* Functional description
* Delete all known indices in preparation for deleting a
* complete relation.
*
**************************************/
SET_TDBB(tdbb);
fb_assert(relPages->rel_index_root);
WIN window(relPages->rel_pg_space_id, relPages->rel_index_root);
index_root_page* root = (index_root_page*) CCH_FETCH(tdbb, &window, LCK_write, pag_root);
const bool is_temp = (relation->rel_flags & REL_temp_conn) && (relPages->rel_instance_id != 0);
for (USHORT i = 0; i < root->irt_count; i++)
{
const bool tree_exists = BTR_delete_index(tdbb, &window, i);
root = (index_root_page*) CCH_FETCH(tdbb, &window, LCK_write, pag_root);
if (is_temp && tree_exists)
{
IndexLock* idx_lock = CMP_get_index_lock(tdbb, relation, i);
if (idx_lock)
{
if (!--idx_lock->idl_count) {
LCK_release(tdbb, idx_lock->idl_lock);
}
}
}
}
CCH_RELEASE(tdbb, &window);
}
idx_e IDX_erase(thread_db* tdbb, record_param* rpb,
jrd_tra* transaction, jrd_rel** bad_relation, USHORT* bad_index)
{
/**************************************
*
* I D X _ e r a s e
*
**************************************
*
* Functional description
* Check the various indices prior to an ERASE operation.
* If one is a primary key, check its partner for
* a duplicate record.
*
**************************************/
index_desc idx;
SET_TDBB(tdbb);
idx_e error_code = idx_e_ok;
idx.idx_id = idx_invalid;
RelationPages* relPages = rpb->rpb_relation->getPages(tdbb);
WIN window(relPages->rel_pg_space_id, -1);
while (BTR_next_index(tdbb, rpb->rpb_relation, transaction, &idx, &window))
{
if (idx.idx_flags & (idx_primary | idx_unique))
{
error_code = check_foreign_key(tdbb, rpb->rpb_record, rpb->rpb_relation, transaction,
&idx, bad_relation, bad_index);
if (idx_e_ok != error_code)
{
CCH_RELEASE(tdbb, &window);
break;
}
}
}
return error_code;
}
void IDX_garbage_collect(thread_db* tdbb,
record_param* rpb,
RecordStack& going,
RecordStack& staying)
{
/**************************************
*
* I D X _ g a r b a g e _ c o l l e c t
*
**************************************
*
* Functional description
* Perform garbage collection for a bunch of records. Scan
* through the indices defined for a relation. Garbage collect
* each.
*
**************************************/
index_desc idx;
temporary_key key1, key2;
SET_TDBB(tdbb);
index_insertion insertion;
insertion.iib_descriptor = &idx;
insertion.iib_number = rpb->rpb_number;
insertion.iib_relation = rpb->rpb_relation;
insertion.iib_key = &key1;
WIN window(get_root_page(tdbb, rpb->rpb_relation));
index_root_page* root = (index_root_page*) CCH_FETCH(tdbb, &window, LCK_read, pag_root);
for (USHORT i = 0; i < root->irt_count; i++)
{
if (BTR_description(tdbb, rpb->rpb_relation, root, &idx, i))
{
for (RecordStack::iterator stack1(going); stack1.hasData(); ++stack1)
{
Record* rec1 = stack1.object();
BTR_key(tdbb, rpb->rpb_relation, rec1, &idx, &key1, 0, false);
// Cancel index if there are duplicates in the remaining records
RecordStack::iterator stack2(stack1);
for (++stack2; stack2.hasData(); ++stack2)
{
Record* rec2 = stack2.object();
BTR_key(tdbb, rpb->rpb_relation, rec2, &idx, &key2, 0, false);
if (key_equal(&key1, &key2))
break;
}
if (stack2.hasData())
continue;
// Make sure the index doesn't exist in any record remaining
RecordStack::iterator stack3(staying);
for (; stack3.hasData(); ++stack3)
{
Record* rec3 = stack3.object();
BTR_key(tdbb, rpb->rpb_relation, rec3, &idx, &key2, 0, false);
if (key_equal(&key1, &key2))
break;
}
if (stack3.hasData())
continue;
// Get rid of index node
BTR_remove(tdbb, &window, &insertion);
root = (index_root_page*) CCH_FETCH(tdbb, &window, LCK_read, pag_root);
if (stack1.hasMore(1))
{
BTR_description(tdbb, rpb->rpb_relation, root, &idx, i);
}
}
}
}
CCH_RELEASE(tdbb, &window);
}
idx_e IDX_modify(thread_db* tdbb,
record_param* org_rpb,
record_param* new_rpb,
jrd_tra* transaction, jrd_rel** bad_relation, USHORT* bad_index)
{
/**************************************
*
* I D X _ m o d i f y
*
**************************************
*
* Functional description
* Update the various indices after a MODIFY operation. If a duplicate
* index is violated, return the index number. If successful, return
* -1.
*
**************************************/
SET_TDBB(tdbb);
temporary_key key1;
index_desc idx;
index_insertion insertion;
insertion.iib_relation = org_rpb->rpb_relation;
insertion.iib_number = org_rpb->rpb_number;
insertion.iib_key = &key1;
insertion.iib_descriptor = &idx;
insertion.iib_transaction = transaction;
idx_e error_code = idx_e_ok;
idx.idx_id = idx_invalid;
RelationPages* relPages = org_rpb->rpb_relation->getPages(tdbb);
WIN window(relPages->rel_pg_space_id, -1);
temporary_key key2;
while (BTR_next_index(tdbb, org_rpb->rpb_relation, transaction, &idx, &window))
{
*bad_index = idx.idx_id;
*bad_relation = new_rpb->rpb_relation;
if ( (error_code =
BTR_key(tdbb, new_rpb->rpb_relation, new_rpb->rpb_record, &idx, &key1, 0, false)) )
{
CCH_RELEASE(tdbb, &window);
break;
}
BTR_key(tdbb, org_rpb->rpb_relation, org_rpb->rpb_record, &idx, &key2, 0, false);
if (!key_equal(&key1, &key2))
{
if (( error_code = insert_key(tdbb, new_rpb->rpb_relation, new_rpb->rpb_record,
transaction, &window, &insertion, bad_relation,
bad_index)) )
{
return error_code;
}
}
}
return error_code;
}
idx_e IDX_modify_check_constraints(thread_db* tdbb,
record_param* org_rpb,
record_param* new_rpb,
jrd_tra* transaction,
jrd_rel** bad_relation, USHORT* bad_index)
{
/**************************************
*
* I D X _ m o d i f y _ c h e c k _ c o n s t r a i n t
*
**************************************
*
* Functional description
* Check for foreign key constraint after a modify statement
*
**************************************/
temporary_key key1, key2;
SET_TDBB(tdbb);
idx_e error_code = idx_e_ok;
index_desc idx;
idx.idx_id = idx_invalid;
RelationPages* relPages = org_rpb->rpb_relation->getPages(tdbb);
WIN window(relPages->rel_pg_space_id, -1);
// If relation's primary/unique keys have no dependencies by other
// relations' foreign keys then don't bother cycling thru all index descriptions.
if (!(org_rpb->rpb_relation->rel_flags & REL_check_partners) &&
!(org_rpb->rpb_relation->rel_primary_dpnds.prim_reference_ids))
{
return error_code;
}
// Now check all the foreign key constraints. Referential integrity relation
// could be established by primary key/foreign key or unique key/foreign key
while (BTR_next_index(tdbb, org_rpb->rpb_relation, transaction, &idx, &window))
{
if (!(idx.idx_flags & (idx_primary | idx_unique)) ||
!MET_lookup_partner(tdbb, org_rpb->rpb_relation, &idx, 0))
{
continue;
}
*bad_index = idx.idx_id;
*bad_relation = new_rpb->rpb_relation;
if ((error_code =
BTR_key(tdbb, new_rpb->rpb_relation, new_rpb->rpb_record, &idx, &key1, 0, false)) ||
(error_code =
BTR_key(tdbb, org_rpb->rpb_relation, org_rpb->rpb_record, &idx, &key2, 0, false)))
{
CCH_RELEASE(tdbb, &window);
break;
}
if (!key_equal(&key1, &key2))
{
error_code = check_foreign_key(tdbb, org_rpb->rpb_record, org_rpb->rpb_relation,
transaction, &idx, bad_relation, bad_index);
if (idx_e_ok != error_code)
{
CCH_RELEASE(tdbb, &window);
return error_code;
}
}
}
return error_code;
}
void IDX_statistics(thread_db* tdbb, jrd_rel* relation, USHORT id, SelectivityList& selectivity)
{
/**************************************
*
* I D X _ s t a t i s t i c s
*
**************************************
*
* Functional description
* Scan index pages recomputing
* selectivity.
*
**************************************/
SET_TDBB(tdbb);
BTR_selectivity(tdbb, relation, id, selectivity);
}
idx_e IDX_store(thread_db* tdbb, record_param* rpb,
jrd_tra* transaction, jrd_rel** bad_relation, USHORT* bad_index)
{
/**************************************
*
* I D X _ s t o r e
*
**************************************
*
* Functional description
* Update the various indices after a STORE operation. If a duplicate
* index is violated, return the index number. If successful, return
* -1.
*
**************************************/
SET_TDBB(tdbb);
temporary_key key;
index_desc idx;
index_insertion insertion;
insertion.iib_relation = rpb->rpb_relation;
insertion.iib_number = rpb->rpb_number;
insertion.iib_key = &key;
insertion.iib_descriptor = &idx;
insertion.iib_transaction = transaction;
idx_e error_code = idx_e_ok;
idx.idx_id = idx_invalid;
RelationPages* relPages = rpb->rpb_relation->getPages(tdbb);
WIN window(relPages->rel_pg_space_id, -1);
while (BTR_next_index(tdbb, rpb->rpb_relation, transaction, &idx, &window))
{
*bad_index = idx.idx_id;
*bad_relation = rpb->rpb_relation;
if ( (error_code = BTR_key(tdbb, rpb->rpb_relation, rpb->rpb_record, &idx, &key, 0, false)) )
{
CCH_RELEASE(tdbb, &window);
break;
}
if ( (error_code = insert_key(tdbb, rpb->rpb_relation, rpb->rpb_record, transaction,
&window, &insertion, bad_relation, bad_index)) )
{
return error_code;
}
}
return error_code;
}
static idx_e check_duplicates(thread_db* tdbb,
Record* record,