-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
dfw.epp
5565 lines (4717 loc) · 138 KB
/
dfw.epp
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: dfw.epp
* DESCRIPTION: Deferred Work handler
*
* 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): ______________________________________.
*
* 2001.6.25 Claudio Valderrama: Implement deferred check for udf usage
* inside a procedure before dropping the udf and creating stub for future
* processing of dependencies from dropped generators.
*
* 2001.8.12 Claudio Valderrama: find_depend_in_dfw() and other functions
* should respect identifiers with embedded blanks instead of chopping them
*.
* 2001.10.01 Claudio Valderrama: check constraints should fire AFTER the
* BEFORE <action> triggers; otherwise they allow invalid data to be stored.
* This is a quick fix for SF Bug #444463 until a more robust one is devised
* using trigger's rdb$flags or another mechanism.
*
* 2001.10.10 Ann Harrison: Don't increment the format version unless the
* table is actually reformatted. At the same time, break out some of
* the parts of make_version making some new subroutines with the goal
* of making make_version readable.
*
* 2001.10.18 Ann Harrison: some cleanup of trigger & constraint handling.
* it now appears to work correctly on new Firebird databases with lots
* of system types and on InterBase databases, without checking for
* missing source.
*
* 23-Feb-2002 Dmitry Yemanov - Events wildcarding
*
* 2002-02-24 Sean Leyne - Code Cleanup of old Win 3.1 port (WINDOWS_ONLY)
*
* Adriano dos Santos Fernandes
*
* 2008-03-16 Alex Peshkoff - avoid most of data modifications in system transaction.
* Problems took place when same data was modified in user transaction, and later -
* in system transaction. System transaction always performs updates in place,
* but when between RPB setup and actual modification garbage was collected (this
* was noticed with GC thread active, but may happen due to any read of the record),
* BUGCHECK(291) took place. To avoid that issue, it was decided not to modify data
* in system transaction. An exception is RDB$FORMATS relation, which is always modified
* by transaction zero. Also an aspect of 'dirty' access from system transaction was
* taken into an account in add_file(), make_version() and create_index().
*
*/
#include "firebird.h"
#include "../common/classes/fb_string.h"
#include <stdio.h>
#include <string.h>
#include "../jrd/common.h"
#include <stdarg.h>
#include "../jrd/ibase.h"
#include "../jrd/jrd.h"
#include "../jrd/val.h"
#include "../jrd/irq.h"
#include "../jrd/tra.h"
#include "../jrd/os/pio.h"
#include "../jrd/ods.h"
#include "../jrd/btr.h"
#include "../jrd/req.h"
#include "../jrd/exe.h"
#include "../jrd/scl.h"
#include "../jrd/blb.h"
#include "../jrd/met.h"
#include "../jrd/lck.h"
#include "../jrd/sdw.h"
#include "../jrd/flags.h"
#include "../jrd/intl.h"
#include "../intl/charsets.h"
#include "../jrd/align.h"
#include "../jrd/gdsassert.h"
#include "../jrd/blb_proto.h"
#include "../jrd/btr_proto.h"
#include "../jrd/cch_proto.h"
#include "../jrd/cmp_proto.h"
#include "../jrd/dfw_proto.h"
#include "../jrd/dpm_proto.h"
#include "../jrd/dsc_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/exe_proto.h"
#include "../jrd/ext_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/grant_proto.h"
#include "../jrd/idx_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/isc_f_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/pag_proto.h"
#include "../jrd/pcmet_proto.h"
#include "../jrd/os/pio_proto.h"
#include "../jrd/rlck_proto.h"
#include "../jrd/scl_proto.h"
#include "../jrd/sdw_proto.h"
#include "../jrd/thread_proto.h"
#include "../jrd/event_proto.h"
#include "../jrd/nbak.h"
#include "../jrd/trig.h"
#include "../jrd/IntlManager.h"
#include "../jrd/UserManagement.h"
#include "../common/utils_proto.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "gen/iberror.h"
/* Pick up system relation ids */
#include "../jrd/ini.h"
/* Define range of user relation ids */
const int MIN_RELATION_ID = rel_MAX;
const int MAX_RELATION_ID = 32767;
const int COMPUTED_FLAG = 128;
const int WAIT_PERIOD = -1;
DATABASE DB = FILENAME "ODS.RDB";
using namespace Jrd;
/*==================================================================
*
* NOTE:
*
* The following functions required the same number of
* parameters to be passed.
*
*==================================================================
*/
static bool add_file(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool add_shadow(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_shadow(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool compute_security(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool modify_index(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool create_index(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_index(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool create_procedure(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_procedure(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool modify_procedure(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool create_relation(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_relation(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool scan_relation(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool create_trigger(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_trigger(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool modify_trigger(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool create_collation(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_collation(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_exception(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_generator(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool modify_generator(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_udf(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool create_field(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_field(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool modify_field(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_global(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_parameter(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_rfr(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool make_version(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool add_difference(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool delete_difference(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool begin_backup(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool end_backup(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
static bool user_management(thread_db*, SSHORT, DeferredWork*, jrd_tra*);
/* ---------------------------------------------------------------- */
static void check_dependencies(thread_db*, const TEXT*, const TEXT*, int, jrd_tra*);
static void check_filename(const Firebird::string&, bool);
static void check_system_generator(const TEXT*, const dfw_t);
static bool formatsAreEqual(const Format*, const Format*);
static bool find_depend_in_dfw(thread_db*, TEXT*, USHORT, USHORT, jrd_tra*);
static void get_array_desc(thread_db*, const TEXT*, Ods::InternalArrayDesc*);
static void get_procedure_dependencies(DeferredWork*, bool);
static void get_trigger_dependencies(DeferredWork*, bool);
static void load_trigs(thread_db*, jrd_rel*, trig_vec**);
static Format* make_format(thread_db*, jrd_rel*, USHORT *, TemporaryField*);
static DeferredWork* post_work(jrd_tra*, SLONG, DeferredWork**, enum dfw_t,
const dsc*, USHORT);
static void put_summary_blob(blb*, enum rsr_t, bid*, jrd_tra*);
static void put_summary_record(blb*, enum rsr_t, const UCHAR*, USHORT);
static void setup_array(thread_db*, blb*, const TEXT*, USHORT, TemporaryField*);
static blb* setup_triggers(thread_db*, jrd_rel*, bool, trig_vec**, blb*);
static void setup_trigger_details(thread_db*, jrd_rel*, blb*, trig_vec**, const TEXT*,
const TEXT*, bool);
static bool validate_text_type (thread_db*, const TemporaryField*);
static Lock* protect_relation(thread_db*, jrd_tra*, jrd_rel*, bool&);
static void release_protect_lock(thread_db*, jrd_tra*, Lock*);
static void check_partners(thread_db*, const USHORT);
static const UCHAR nonnull_validation_blr[] =
{
blr_version5,
blr_not,
blr_missing,
blr_fid, 0, 0, 0,
blr_eoc
};
typedef bool (*dfw_task_routine) (thread_db*, SSHORT, DeferredWork*, jrd_tra*);
struct deferred_task
{
enum dfw_t task_type;
dfw_task_routine task_routine;
};
static const deferred_task task_table[] =
{
{ dfw_add_file, add_file },
{ dfw_add_shadow, add_shadow },
{ dfw_delete_index, modify_index },
{ dfw_delete_expression_index, modify_index },
{ dfw_delete_rfr, delete_rfr },
{ dfw_delete_relation, delete_relation },
{ dfw_delete_shadow, delete_shadow },
{ dfw_create_field, create_field },
{ dfw_delete_field, delete_field },
{ dfw_modify_field, modify_field },
{ dfw_delete_global, delete_global },
{ dfw_create_relation, create_relation },
{ dfw_update_format, make_version },
{ dfw_scan_relation, scan_relation },
{ dfw_compute_security, compute_security },
{ dfw_create_index, modify_index },
{ dfw_create_expression_index, modify_index },
{ dfw_grant, GRANT_privileges },
{ dfw_create_trigger, create_trigger },
{ dfw_delete_trigger, delete_trigger },
{ dfw_modify_trigger, modify_trigger },
{ dfw_create_procedure, create_procedure },
{ dfw_delete_procedure, delete_procedure },
{ dfw_modify_procedure, modify_procedure },
{ dfw_delete_prm, delete_parameter },
{ dfw_create_collation, create_collation },
{ dfw_delete_collation, delete_collation },
{ dfw_delete_exception, delete_exception },
{ dfw_delete_generator, delete_generator },
{ dfw_modify_generator, modify_generator },
{ dfw_delete_udf, delete_udf },
{ dfw_add_difference, add_difference },
{ dfw_delete_difference, delete_difference },
{ dfw_begin_backup, begin_backup },
{ dfw_end_backup, end_backup },
{ dfw_user_management, user_management },
{ dfw_null, NULL }
};
USHORT DFW_assign_index_type(thread_db* tdbb,
DeferredWork* work, SSHORT field_type, SSHORT ttype)
{
/**************************************
*
* D F W _ a s s i g n _ i n d e x _ t y p e
*
**************************************
*
* Functional description
* Define the index segment type based
* on the field's type and subtype.
*
**************************************/
SET_TDBB(tdbb);
if (field_type == dtype_varying ||
field_type == dtype_cstring ||
field_type == dtype_text)
{
switch (ttype)
{
case ttype_none:
return idx_string;
case ttype_binary:
return idx_byte_array;
case ttype_metadata:
return idx_metadata;
case ttype_ascii:
return idx_string;
}
/* Dynamic text cannot occur here as this is for an on-disk
index, which must be bound to a text type. */
fb_assert(ttype != ttype_dynamic);
if (INTL_defined_type(tdbb, ttype))
return INTL_TEXT_TO_INDEX(ttype);
ERR_post_nothrow(isc_no_meta_update,
isc_arg_gds, isc_random, isc_arg_string,
ERR_cstring(work->dfw_name), 0);
INTL_texttype_lookup(tdbb, ttype); // should punt
ERR_punt(); // if INTL_texttype_lookup hasn't punt
}
switch (field_type)
{
case dtype_timestamp:
return idx_timestamp2;
case dtype_sql_date:
return idx_sql_date;
case dtype_sql_time:
return idx_sql_time;
// idx_numeric2 used for 64-bit Integer support
case dtype_int64:
return idx_numeric2;
default:
return idx_numeric;
}
}
void DFW_delete_deferred( jrd_tra* transaction, SLONG sav_number)
{
/**************************************
*
* D F W _ d e l e t e _ d e f e r r e d
*
**************************************
*
* Functional description
* Get rid of work deferred that was to be done at
* COMMIT time as the statement has been rolled back.
*
* if (sav_number == -1), then remove all entries.
*
**************************************/
/* If there is no deferred work, just return */
if (!transaction->tra_deferred_work) {
return;
}
/* Remove deferred work and events which are to be rolled back */
bool deferred_meta = false;
DeferredWork* work;
for (DeferredWork** ptr = &transaction->tra_deferred_work; (work = *ptr);)
{
if ((work->dfw_sav_number == sav_number) || (sav_number == -1))
{
*ptr = work->dfw_next;
delete work;
}
else
{
ptr = &(*ptr)->dfw_next;
if (work->dfw_type != dfw_post_event)
deferred_meta = true;
}
}
if (!deferred_meta) {
transaction->tra_flags &= ~TRA_deferred_meta;
}
}
DeferredWork::~DeferredWork()
{
if (dfw_args)
{
if (dfw_type == dfw_user_management)
{
UserManagement* sec = reinterpret_cast<UserManagement*>(dfw_args);
delete sec;
}
else
{
DeferredWork* tmp = dfw_args;
while (tmp)
{
DeferredWork* next = tmp->dfw_next;
delete tmp;
tmp = next;
}
}
}
if (dfw_lock)
{
LCK_release(JRD_get_thread_data(), dfw_lock);
delete dfw_lock;
}
}
void DFW_merge_work(jrd_tra* transaction,
SLONG old_sav_number,
SLONG new_sav_number)
{
/**************************************
*
* D F W _ m e r g e _ w o r k
*
**************************************
*
* Functional description
* Merge the deferred work with the previous level. This will
* be called only if there is a previous level.
*
**************************************/
/* If there is no deferred work, just return */
if (!transaction->tra_deferred_work)
return;
/* Decrement the save point number in the deferred block
* i.e. merge with the previous level.
*/
DeferredWork* work;
for (DeferredWork** ptr = &transaction->tra_deferred_work; (work = *ptr);)
{
if (work->dfw_sav_number == old_sav_number)
{
work->dfw_sav_number = new_sav_number;
/* merge this entry with other identical entries at
* same save point level. Start from the beginning and
* stop with the that is being merged.
*/
DeferredWork* work_m;
for (DeferredWork** ptr_m = &transaction->tra_deferred_work;
((work_m = *ptr_m) && (work_m != work));
ptr_m = &(*ptr_m)->dfw_next)
{
if (work_m->dfw_type == work->dfw_type &&
work_m->dfw_id == work->dfw_id &&
work_m->dfw_name == work->dfw_name &&
work_m->dfw_sav_number == work->dfw_sav_number)
{
/* Yes! There is a duplicate entry. Take out the
* entry for which the save point was decremented
*/
*ptr = work->dfw_next;
if (work_m->dfw_name.length()) {
work_m->dfw_count += work->dfw_count;
}
delete work;
work = NULL;
break;
}
}
}
if (work) {
ptr = &(*ptr)->dfw_next;
}
}
}
void DFW_perform_system_work(thread_db* tdbb)
{
/**************************************
*
* D F W _ p e r f o r m _ s y s t e m _ w o r k
*
**************************************
*
* Functional description
* Flush out the work left to be done in the
* system transaction.
*
**************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
DFW_perform_work(tdbb, dbb->dbb_sys_trans);
}
void DFW_perform_work(thread_db* tdbb, jrd_tra* transaction)
{
/**************************************
*
* D F W _ p e r f o r m _ w o r k
*
**************************************
*
* Functional description
* Do work deferred to COMMIT time 'cause that time has
* come.
*
**************************************/
/* If no deferred work or it's all deferred event posting
don't bother */
if (!transaction->tra_deferred_work ||
!(transaction->tra_flags & TRA_deferred_meta))
{
return;
}
SET_TDBB(tdbb);
Jrd::ContextPoolHolder context(tdbb, transaction->tra_pool);
/* Loop for as long as any of the deferred work routines says that it has
more to do. A deferred work routine should be able to deal with any
value of phase, either to say that it wants to be called again in the
next phase (by returning true) or that it has nothing more to do in this
or later phases (by returning false). By convention, phase 0 has been
designated as the cleanup phase. If any non-zero phase punts, then phase 0
is executed for all deferred work blocks to cleanup work-in-progress. */
bool dump_shadow = false;
SSHORT phase = 1;
bool more;
ISC_STATUS_ARRAY err_status = {0};
do
{
more = false;
try {
tdbb->tdbb_flags |= (TDBB_dont_post_dfw | TDBB_use_db_page_space);
for (const deferred_task* task = task_table;
task->task_type != dfw_null; ++task)
{
for (DeferredWork* work = transaction->tra_deferred_work;
work;
work = work->dfw_next)
{
if (work->dfw_type == task->task_type)
{
if (work->dfw_type == dfw_add_shadow)
{
dump_shadow = true;
}
if ((*task->task_routine)(tdbb, phase, work, transaction))
{
more = true;
}
}
}
}
if (!phase) {
Firebird::stuff_exception(tdbb->tdbb_status_vector,
Firebird::status_exception(err_status, true));
ERR_punt();
}
++phase;
tdbb->tdbb_flags &= ~(TDBB_dont_post_dfw | TDBB_use_db_page_space);
}
catch (const Firebird::Exception& ex) {
tdbb->tdbb_flags &= ~(TDBB_dont_post_dfw | TDBB_use_db_page_space);
/* Do any necessary cleanup */
if (!phase) {
Firebird::stuff_exception(tdbb->tdbb_status_vector, ex);
ERR_punt();
}
else {
Firebird::stuff_exception(err_status, ex);
}
phase = 0;
more = true;
}
} while (more);
/* Remove deferred work blocks so that system transaction and
commit retaining transactions don't re-execute them. Leave
events to be posted after commit */
DeferredWork* work;
for (DeferredWork** ptr = &transaction->tra_deferred_work; (work = *ptr);)
{
if ((work->dfw_type == dfw_post_event) ||
(work->dfw_type == dfw_delete_shadow))
{
ptr = &(*ptr)->dfw_next;
}
else
{
*ptr = work->dfw_next;
delete work;
}
}
transaction->tra_flags &= ~TRA_deferred_meta;
if (dump_shadow) {
SDW_dump_pages();
}
}
void DFW_perform_post_commit_work(jrd_tra* transaction)
{
/**************************************
*
* D F W _ p e r f o r m _ p o s t _ c o m m i t _ w o r k
*
**************************************
*
* Functional description
* Perform any post commit work
* 1. Post any pending events.
* 2. Unlink shadow files for dropped shadows
*
* Then, delete it from chain of pending work.
*
**************************************/
ISC_STATUS_ARRAY status;
if (!transaction->tra_deferred_work)
return;
bool pending_events = false;
Database* dbb = GET_DBB();
Lock* lock = dbb->dbb_lock;
DeferredWork* work;
for (DeferredWork** ptr = &transaction->tra_deferred_work; (work = *ptr);)
{
if (work->dfw_type == dfw_post_event)
{
EVENT_post( status,
lock->lck_length,
(const TEXT*) & lock->lck_key,
work->dfw_name.length(),
work->dfw_name.c_str(),
work->dfw_count);
*ptr = work->dfw_next;
delete work;
pending_events = true;
}
else if (work->dfw_type == dfw_delete_shadow)
{
unlink(work->dfw_name.c_str());
*ptr = work->dfw_next;
delete work;
}
else
{
ptr = &(*ptr)->dfw_next;
}
}
if (pending_events)
EVENT_deliver();
}
DeferredWork* DFW_post_work(jrd_tra* transaction, enum dfw_t type, const dsc* desc,
USHORT id)
{
/**************************************
*
* D F W _ p o s t _ w o r k
*
**************************************
*
* Functional description
* Post work to be deferred to commit time.
*
**************************************/
/* get the current save point number */
const SLONG sav_number = transaction->tra_save_point ?
transaction->tra_save_point->sav_number : 0;
/* post work */
return post_work(transaction,
sav_number,
&transaction->tra_deferred_work,
type,
desc,
id);
}
DeferredWork* DFW_post_work_arg( jrd_tra* transaction, DeferredWork* work, const dsc* desc,
USHORT id)
{
/**************************************
*
* D F W _ p o s t _ w o r k _ a r g
*
**************************************
*
* Functional description
* Post an argument for work to be deferred to commit time.
*
**************************************/
return post_work(transaction,
work->dfw_sav_number,
&work->dfw_args,
work->dfw_type,
desc,
id);
}
void DFW_update_index(const TEXT* name, USHORT id, const SelectivityList& selectivity, jrd_tra* transaction)
{
/**************************************
*
* D F W _ u p d a t e _ i n d e x
*
**************************************
*
* Functional description
* Update information in the index relation after creation
* of the index.
*
**************************************/
thread_db* tdbb = JRD_get_thread_data();
Database* dbb = tdbb->getDatabase();
if (dbb->dbb_ods_version >= ODS_VERSION11) {
jrd_req* request = CMP_find_request(tdbb, irq_m_index_seg, IRQ_REQUESTS);
FOR(REQUEST_HANDLE request TRANSACTION_HANDLE transaction)
SEG IN RDB$INDEX_SEGMENTS WITH SEG.RDB$INDEX_NAME EQ name
SORTED BY SEG.RDB$FIELD_POSITION
if (!REQUEST(irq_m_index_seg))
REQUEST(irq_m_index_seg) = request;
MODIFY SEG USING
SEG.RDB$STATISTICS = selectivity[SEG.RDB$FIELD_POSITION];
END_MODIFY;
END_FOR;
if (!REQUEST(irq_m_index_seg))
REQUEST(irq_m_index_seg) = request;
}
jrd_req* request = CMP_find_request(tdbb, irq_m_index, IRQ_REQUESTS);
FOR(REQUEST_HANDLE request TRANSACTION_HANDLE transaction)
IDX IN RDB$INDICES WITH IDX.RDB$INDEX_NAME EQ name
if (!REQUEST(irq_m_index))
REQUEST(irq_m_index) = request;
MODIFY IDX USING
IDX.RDB$INDEX_ID = id + 1;
IDX.RDB$STATISTICS = selectivity.back();
END_MODIFY;
END_FOR;
if (!REQUEST(irq_m_index))
REQUEST(irq_m_index) = request;
}
static bool add_file(thread_db* tdbb, SSHORT phase, DeferredWork* work,
jrd_tra* transaction)
{
/**************************************
*
* a d d _ f i l e
*
**************************************
*
* Functional description
* Add a file to a database.
* This file could be a regular database
* file or a shadow file. Either way we
* require exclusive access to the database.
*
**************************************/
USHORT section, shadow_number;
SLONG start, max;
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
switch (phase)
{
case 0:
CCH_release_exclusive(tdbb);
return false;
case 1:
case 2:
return true;
case 3:
if (CCH_exclusive(tdbb, LCK_EX, WAIT_PERIOD))
return true;
ERR_post(isc_no_meta_update,
isc_arg_gds, isc_lock_timeout,
isc_arg_gds, isc_obj_in_use,
isc_arg_string, ERR_cstring(dbb->dbb_filename.c_str()),
0);
return false;
case 4:
CCH_flush(tdbb, FLUSH_FINI, 0L);
max = PageSpace::maxAlloc(dbb) + 1;
jrd_req* handle = 0;
jrd_req* handle2 = 0;
// Check the file name for node name. This has already
// been done for shadows in add_shadow()
if (work->dfw_type != dfw_add_shadow) {
check_filename(work->dfw_name, true);
}
// User transaction may be safely used instead of system, cause
// we requested and got exclusive database access. AP-2008.
// get any files to extend into
FOR(REQUEST_HANDLE handle TRANSACTION_HANDLE transaction) X IN RDB$FILES
WITH X.RDB$FILE_NAME EQ work->dfw_name.c_str()
/* First expand the file name This has already been done
** for shadows in add_shadow ()) */
if (work->dfw_type != dfw_add_shadow)
{
MODIFY X USING
ISC_expand_filename(X.RDB$FILE_NAME, 0,
X.RDB$FILE_NAME, sizeof(X.RDB$FILE_NAME), false);
END_MODIFY;
}
/* If there is no starting position specified, or if it is
too low a value, make a stab at assigning one based on
the indicated preference for the previous file length. */
if ((start = X.RDB$FILE_START) < max)
{
FOR(REQUEST_HANDLE handle2 TRANSACTION_HANDLE transaction)
FIRST 1 Y IN RDB$FILES
WITH Y.RDB$SHADOW_NUMBER EQ X.RDB$SHADOW_NUMBER
AND Y.RDB$FILE_SEQUENCE NOT MISSING
SORTED BY DESCENDING Y.RDB$FILE_SEQUENCE
start = Y.RDB$FILE_START + Y.RDB$FILE_LENGTH;
END_FOR;
}
start = MAX(max, start);
shadow_number = X.RDB$SHADOW_NUMBER;
if ((shadow_number &&
(section = SDW_add_file(X.RDB$FILE_NAME,
start,
shadow_number))) ||
(section = PAG_add_file(X.RDB$FILE_NAME, start)))
{
MODIFY X USING
X.RDB$FILE_SEQUENCE = section;
X.RDB$FILE_START = start;
END_MODIFY;
}
END_FOR;
CMP_release(tdbb, handle);
if (handle2)
{
CMP_release(tdbb, handle2);
}
if (section)
{
handle = NULL;
section--;
FOR(REQUEST_HANDLE handle TRANSACTION_HANDLE transaction) X IN RDB$FILES
WITH X.RDB$FILE_SEQUENCE EQ section
AND X.RDB$SHADOW_NUMBER EQ shadow_number
MODIFY X USING
X.RDB$FILE_LENGTH = start - X.RDB$FILE_START;
END_MODIFY;
END_FOR;
CMP_release(tdbb, handle);
}
CCH_release_exclusive(tdbb);
break;
}
return false;
}
static bool add_shadow( thread_db* tdbb, SSHORT phase, DeferredWork* work,
jrd_tra* transaction)
{
/**************************************
*
* a d d _ s h a d o w
*
**************************************
*
* Functional description
* A file or files have been added for shadowing.
* Get all files for this particular shadow first
* in order of starting page, if specified, then
* in sequence order.
*
**************************************/
jrd_req* handle;
Shadow* shadow;
USHORT sequence, add_sequence;
bool finished;
ULONG min_page;
Firebird::PathName expanded_fname;
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
switch (phase)
{
case 0:
CCH_release_exclusive(tdbb);
return false;
case 1:
case 2:
case 3:
return true;
case 4:
check_filename(work->dfw_name, false);
/* could have two cases:
1) this shadow has already been written to, so add this file using
the standard routine to extend a database
2) this file is part of a newly added shadow which has already been
fetched in totem and prepared for writing to, so just ignore it
*/
finished = false;
handle = NULL;
FOR(REQUEST_HANDLE handle TRANSACTION_HANDLE transaction)
F IN RDB$FILES
WITH F.RDB$FILE_NAME EQ work->dfw_name.c_str()
expanded_fname = F.RDB$FILE_NAME;
ISC_expand_filename(expanded_fname, false);
MODIFY F USING
expanded_fname.copyTo(F.RDB$FILE_NAME, sizeof(F.RDB$FILE_NAME));
END_MODIFY;
for (shadow = dbb->dbb_shadow; shadow; shadow = shadow->sdw_next)
{
if ((F.RDB$SHADOW_NUMBER == shadow->sdw_number) &&
!(shadow->sdw_flags & SDW_IGNORE))
{
if (F.RDB$FILE_FLAGS & FILE_shadow)
{
/* This is the case of a bogus duplicate posted
* work when we added a multi-file shadow
*/
finished = true;
}
else if (shadow->sdw_flags & (SDW_dumped))
{
/* Case of adding a file to a currently active
* shadow set.
* Note: as of 1995-January-31 there is
* no SQL syntax that supports this, but there
* may be GDML
*/
add_file(tdbb, 3, work, transaction);
add_file(tdbb, 4, work, transaction);
finished = true;
}
else
{
/* We cannot add a file to a shadow that is still
* in the process of being created.
*/
ERR_post(isc_no_meta_update,
isc_arg_gds, isc_obj_in_use,
isc_arg_string,
ERR_cstring(dbb->dbb_filename.c_str()),