-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
blb.cpp
2857 lines (2393 loc) · 75.8 KB
/
blb.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: blb.cpp
* DESCRIPTION: Blob 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.23 Claudio Valderrama: move_from_string to accept assignments
* from string to blob field. First use was to allow inserting a literal string
* in a blob field without requiring an UDF.
*
* 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, as the engine now fully supports
* readonly databases.
*
* 2002.10.28 Sean Leyne - Code cleanup, removed obsolete "MPEXL" port
* 2002.10.28 Sean Leyne - Code cleanup, removed obsolete "DecOSF" port
*
* Adriano dos Santos Fernandes
*
*/
#include "firebird.h"
#include "memory_routines.h"
#include <string.h>
#include "../jrd/common.h"
#include "../jrd/ibase.h"
#include "../jrd/jrd.h"
#include "../jrd/tra.h"
#include "../jrd/val.h"
#include "../jrd/exe.h"
#include "../jrd/req.h"
#include "../jrd/blb.h"
#include "../jrd/ods.h"
#include "../jrd/lls.h"
#include "gen/iberror.h"
#include "../jrd/blob_filter.h"
#include "../jrd/sdl.h"
#include "../jrd/intl.h"
#include "../jrd/cch.h"
#include "../jrd/gdsassert.h"
#include "../jrd/blb_proto.h"
#include "../jrd/blf_proto.h"
#include "../jrd/cch_proto.h"
#include "../jrd/dpm_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/evl_proto.h"
#include "../jrd/filte_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/jrd_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/pag_proto.h"
#include "../jrd/sdl_proto.h"
#include "../jrd/dsc_proto.h"
#include "../common/classes/array.h"
#include "../common/classes/VaryStr.h"
using namespace Jrd;
using namespace Firebird;
using Firebird::UCharBuffer;
typedef Ods::blob_page blob_page;
inline bool SEGMENTED(const blb* blob)
{
return !(blob->blb_flags & BLB_stream);
}
static ArrayField* alloc_array(jrd_tra*, Ods::InternalArrayDesc*);
static blb* allocate_blob(thread_db*, jrd_tra*);
static ISC_STATUS blob_filter(USHORT, BlobControl*);
static blb* copy_blob(thread_db*, const bid*, bid*, USHORT, const UCHAR*, USHORT);
static void delete_blob(thread_db*, blb*, ULONG);
static void delete_blob_id(thread_db*, const bid*, SLONG, jrd_rel*);
static ArrayField* find_array(jrd_tra*, const bid*);
static BlobFilter* find_filter(thread_db*, SSHORT, SSHORT);
static blob_page* get_next_page(thread_db*, blb*, WIN *);
static void insert_page(thread_db*, blb*);
static void move_from_string(Jrd::thread_db*, const dsc*, dsc*, Jrd::jrd_nod*);
static void move_to_string(Jrd::thread_db*, dsc*, dsc*);
static void release_blob(blb*, const bool);
static void slice_callback(array_slice*, ULONG, dsc*);
static blb* store_array(thread_db*, jrd_tra*, bid*);
void BLB_cancel(thread_db* tdbb, blb* blob)
{
/**************************************
*
* B L B _ c a n c e l
*
**************************************
*
* Functional description
* Abort a blob operation. If the blob is a partially created
* temporary blob, free up any allocated pages. In any case,
* get rid of the blob block.
*
**************************************/
SET_TDBB(tdbb);
// Release filter control resources
if (blob->blb_flags & BLB_temporary)
delete_blob(tdbb, blob, 0);
release_blob(blob, true);
}
void BLB_check_well_formed(Jrd::thread_db* tdbb, const dsc* desc, Jrd::blb* blob)
{
/**************************************
*
* B L B _ c h e c k _ w e l l _ f o r m e d
*
**************************************
*
* Functional description
* Check if a text BLOB is well formed.
*
**************************************/
SET_TDBB(tdbb);
// Need to verify this to work in database creation, as the charsets didn't exist yet.
if (desc->getCharSet() == CS_NONE || desc->getCharSet() == CS_BINARY)
return;
CharSet* charSet = INTL_charset_lookup(tdbb, desc->getCharSet());
if (!charSet->shouldCheckWellFormedness())
return;
HalfStaticArray<UCHAR, BUFFER_MEDIUM> buffer;
ULONG pos = 0;
while (!(blob->blb_flags & BLB_eof))
{
const ULONG len = BLB_get_data(tdbb, blob,
buffer.getBuffer(buffer.getCapacity()) + pos, buffer.getCapacity() - pos, false);
buffer.resize(pos + len);
if (charSet->wellFormed(pos + len, buffer.begin(), &pos))
pos = 0;
else
{
if (pos == 0)
status_exception::raise(Arg::Gds(isc_malformed_string));
else
{
buffer.removeCount(0, pos);
pos = buffer.getCount();
}
}
}
if (pos != 0)
status_exception::raise(Arg::Gds(isc_malformed_string));
}
void BLB_close(thread_db* tdbb, Jrd::blb* blob)
{
/**************************************
*
* B L B _ c l o s e
*
**************************************
*
* Functional description
* Close a blob. If the blob is open for retrieval, release the
* blob block. If it's a temporary blob, flush out the last page
* (if necessary) in preparation for materialization.
*
**************************************/
SET_TDBB(tdbb);
// Release filter control resources
if (blob->blb_filter)
BLF_close_blob(tdbb, &blob->blb_filter);
blob->blb_flags |= BLB_closed;
if (!(blob->blb_flags & BLB_temporary))
{
release_blob(blob, true);
return;
}
if (blob->blb_level == 0)
{
//Database* dbb = tdbb->getDatabase();
blob->blb_temp_size = blob->blb_clump_size - blob->blb_space_remaining;
if (blob->blb_temp_size > 0)
{
blob->blb_temp_size += BLP_SIZE;
jrd_tra* transaction = blob->blb_transaction;
TempSpace* const tempSpace = transaction->getBlobSpace();
blob->blb_temp_offset = tempSpace->allocateSpace(blob->blb_temp_size);
tempSpace->write(blob->blb_temp_offset, blob->getBuffer(), blob->blb_temp_size);
}
}
else if (blob->blb_level >= 1 &&
blob->blb_space_remaining < blob->blb_clump_size)
{
insert_page(tdbb, blob);
}
blob->freeBuffer();
}
blb* BLB_create(thread_db* tdbb, jrd_tra* transaction, bid* blob_id)
{
/**************************************
*
* B L B _ c r e a t e
*
**************************************
*
* Functional description
* Create a shiney, new, empty blob.
*
**************************************/
SET_TDBB(tdbb);
return BLB_create2(tdbb, transaction, blob_id, 0, NULL);
}
blb* BLB_create2(thread_db* tdbb,
jrd_tra* transaction, bid* blob_id,
USHORT bpb_length, const UCHAR* bpb)
{
/**************************************
*
* B L B _ c r e a t e 2
*
**************************************
*
* Functional description
* Create a shiney, new, empty blob.
* Basically BLB_create() with BPB structure.
*
**************************************/
transaction = transaction->getOuter();
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
// Create a blob large enough to hold a single data page
SSHORT from, to;
SSHORT from_charset, to_charset;
const SSHORT type = gds__parse_bpb2(bpb_length,
bpb,
&from,
&to,
reinterpret_cast<USHORT*>(&from_charset), // safe - alignment not changed
reinterpret_cast<USHORT*>(&to_charset), // safe - alignment not changed
NULL, NULL, NULL, NULL);
blb* blob = allocate_blob(tdbb, transaction);
if (type & isc_bpb_type_stream)
blob->blb_flags |= BLB_stream;
if ((type & isc_bpb_storage_temp) || (dbb->dbb_flags & DBB_read_only)) {
blob->blb_pg_space_id = dbb->dbb_page_manager.getTempPageSpaceID(tdbb);
}
else {
blob->blb_pg_space_id = DB_PAGE_SPACE;
}
//blob->blb_source_interp = from_charset;
//blob->blb_target_interp = to_charset;
blob->blb_sub_type = to;
bool filter_required = false;
BlobFilter* filter = NULL;
if (to && from != to)
{
// ASF: filter_text is not supported for write operations
if (!(from == 0 && to == 1))
{
filter = find_filter(tdbb, from, to);
filter_required = true;
}
}
else if (to == isc_blob_text && (from_charset != to_charset))
{
if (from_charset == CS_dynamic)
from_charset = tdbb->getAttachment()->att_charset;
if (to_charset == CS_dynamic)
to_charset = tdbb->getAttachment()->att_charset;
if ((to_charset != CS_NONE) && (from_charset != CS_NONE) &&
(to_charset != CS_BINARY) && (from_charset != CS_BINARY) &&
(from_charset != to_charset))
{
filter = find_filter(tdbb, from, to);
filter_required = true;
}
}
blob->blb_space_remaining = blob->blb_clump_size;
if (filter_required)
{
BLF_create_blob(tdbb,
transaction,
&blob->blb_filter,
blob_id,
bpb_length,
bpb,
blob_filter,
filter);
blob->blb_flags |= BLB_temporary;
return blob;
}
blob->blb_flags |= BLB_temporary;
// Set up for a "small" blob -- a blob that fits on an ordinary blob page
blob_page* page = (blob_page*) blob->getBuffer();
memset(page, 0, BLP_SIZE); // initialize page header with NULLs
page->blp_header.pag_type = pag_blob;
blob->blb_segment = (UCHAR *) page->blp_page;
// Format blob id and return blob handle
blob_id->set_temporary(blob->blb_temp_id);
return blob;
}
//
// This function makes linear stacks lookup. Therefore
// in case of big stacks garbage collection speed may become
// real problem. Stacks should be sorted before run?
// 2006.03.17 hvlad: it is sorted now
void BLB_garbage_collect(thread_db* tdbb,
RecordStack& going,
RecordStack& staying,
SLONG prior_page, jrd_rel* relation)
{
/**************************************
*
* B L B _ g a r b a g e _ c o l l e c t
*
**************************************
*
* Functional description
* Garbage collect indices and blobs. Garbage_collect is passed a
* stack of record blocks of records that are being deleted and a stack
* of records blocks of records that are staying. Garbage_collect
* can be called from four operations:
*
* VIO_backout -- removing top record
* expunge -- removing all versions of a record
* purge -- removing all but top version of a record
* update_in_place -- replace the top level record.
*
* hvlad: note that same blob_id can be reused by the staying record version
* in the different field than it was in going record. This is happening
* with 3 blob fields and update_in_place
*
**************************************/
SET_TDBB(tdbb);
RecordBitmap bmGoing;
ULONG cntGoing = 0;
// Loop thru records on the way out looking for blobs to garbage collect
for (RecordStack::iterator stack1(going); stack1.hasData(); ++stack1)
{
Record* rec = stack1.object();
if (!rec)
continue;
// Look for active blob records
const Format* format = rec->rec_format;
for (USHORT id = 0; id < format->fmt_count; id++)
{
DSC desc;
if (DTYPE_IS_BLOB(format->fmt_desc[id].dsc_dtype) && EVL_field(0, rec, id, &desc))
{
const bid* blob = (bid*) desc.dsc_address;
if (!blob->isEmpty())
{
if (blob->bid_internal.bid_relation_id == relation->rel_id)
{
const RecordNumber number = blob->get_permanent_number();
bmGoing.set(number.getValue());
cntGoing++;
}
else
{
// hvlad: blob_id in descriptor is not from our relation. Yes, it is
// garbage in user data but we can handle it without bugcheck - just
// ignore it. To be reconsider latter based on real user reports.
// The same about staying blob few lines below
gds__log("going blob (%ld:%ld) is not owned by relation (id = %d), ignored",
blob->bid_quad.bid_quad_high, blob->bid_quad.bid_quad_low, relation->rel_id);
}
}
}
}
}
if (!cntGoing)
return;
// Make sure the blob doesn't stay in any record remaining
for (RecordStack::iterator stack2(staying); stack2.hasData(); ++stack2)
{
Record* rec = stack2.object();
if (!rec)
continue;
const Format* format = rec->rec_format;
for (USHORT id = 0; id < format->fmt_count; id++)
{
DSC desc;
if (DTYPE_IS_BLOB(format->fmt_desc[id].dsc_dtype) && EVL_field(0, rec, id, &desc))
{
const bid* blob = (bid*) desc.dsc_address;
if (!blob->isEmpty())
{
if (blob->bid_internal.bid_relation_id == relation->rel_id)
{
const RecordNumber number = blob->get_permanent_number();
if (bmGoing.test(number.getValue()))
{
bmGoing.clear(number.getValue());
if (!--cntGoing)
return;
}
}
else
{
gds__log("staying blob (%ld:%ld) is not owned by relation (id = %d), ignored",
blob->bid_quad.bid_quad_high, blob->bid_quad.bid_quad_low, relation->rel_id);
}
}
}
}
}
// Get rid of blob
if (bmGoing.getFirst())
{
do {
const FB_UINT64 id = bmGoing.current();
bid blob;
blob.set_permanent(relation->rel_id, RecordNumber(id));
delete_blob_id(tdbb, &blob, prior_page, relation);
} while (bmGoing.getNext());
}
}
void BLB_gen_bpb(SSHORT source, SSHORT target, UCHAR sourceCharset, UCHAR targetCharset, UCharBuffer& bpb)
{
bpb.resize(15);
UCHAR* p = bpb.begin();
*p++ = isc_bpb_version1;
*p++ = isc_bpb_source_type;
*p++ = 2;
put_vax_short(p, source);
p += 2;
if (source == isc_blob_text)
{
*p++ = isc_bpb_source_interp;
*p++ = 1;
*p++ = sourceCharset;
}
*p++ = isc_bpb_target_type;
*p++ = 2;
put_vax_short(p, target);
p += 2;
if (target == isc_blob_text)
{
*p++ = isc_bpb_target_interp;
*p++ = 1;
*p++ = targetCharset;
}
fb_assert(static_cast<size_t>(p - bpb.begin()) <= bpb.getCount());
// set the array count to the number of bytes we used
bpb.shrink(p - bpb.begin());
}
void BLB_gen_bpb_from_descs(const dsc* fromDesc, const dsc* toDesc, UCharBuffer& bpb)
{
BLB_gen_bpb(fromDesc->getBlobSubType(), toDesc->getBlobSubType(),
fromDesc->getCharSet(), toDesc->getCharSet(), bpb);
}
blb* BLB_get_array(thread_db* tdbb, jrd_tra* transaction, const bid* blob_id,
Ods::InternalArrayDesc* desc)
{
/**************************************
*
* B L B _ g e t _ a r r a y
*
**************************************
*
* Functional description
* Get array blob and array descriptor.
*
**************************************/
transaction = transaction->getOuter();
SET_TDBB(tdbb);
blb* blob = BLB_open2(tdbb, transaction, blob_id, 0, 0);
if (blob->blb_length < sizeof(Ods::InternalArrayDesc))
{
BLB_close(tdbb, blob);
IBERROR(193); // msg 193 null or invalid array
}
BLB_get_segment(tdbb, blob, reinterpret_cast<UCHAR*>(desc), sizeof(Ods::InternalArrayDesc));
const USHORT n = desc->iad_length - sizeof(Ods::InternalArrayDesc);
if (n) {
BLB_get_segment(tdbb, blob, reinterpret_cast<UCHAR*>(desc) + sizeof(Ods::InternalArrayDesc), n);
}
return blob;
}
ULONG BLB_get_data(thread_db* tdbb, blb* blob, UCHAR* buffer, SLONG length, bool close)
{
/**************************************
*
* B L B _ g e t _ d a t a
*
**************************************
*
* Functional description
* Get a large hunk of data from a blob, which can then be
* closed (if close == true). Return total number of bytes read.
* Don't worry about segment boundaries.
*
**************************************/
SET_TDBB(tdbb);
BLOB_PTR* p = buffer;
while (length > 0)
{
// I have no idea why this limit is 32768 instead of 32767
// 1994-August-12 David Schnepper
USHORT n = (USHORT) MIN(length, (SLONG) 32768);
n = BLB_get_segment(tdbb, blob, p, n);
p += n;
length -= n;
if (blob->blb_flags & BLB_eof)
break;
}
if (close)
BLB_close(tdbb, blob);
return (ULONG)(p - buffer);
}
USHORT BLB_get_segment(thread_db* tdbb, blb* blob, UCHAR* segment, USHORT buffer_length)
{
/**************************************
*
* B L B _ g e t _ s e g m e n t
*
**************************************
*
* Functional description
* Get next segment or fragment from a blob. Return the number
* of bytes returned.
*
**************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
if (--tdbb->tdbb_quantum < 0)
JRD_reschedule(tdbb, 0, true);
// If we reached end of file, we're still there
if (blob->blb_flags & BLB_eof)
return 0;
if (blob->blb_filter)
{
blob->blb_fragment_size = 0;
USHORT tmp_len = 0;
const ISC_STATUS status =
BLF_get_segment(tdbb, &blob->blb_filter, &tmp_len, buffer_length, segment);
switch (status)
{
case isc_segstr_eof:
blob->blb_flags |= BLB_eof;
break;
case isc_segment:
blob->blb_fragment_size = 1;
break;
default:
fb_assert(status == 0);
}
return tmp_len;
}
// If there is a seek pending, handle it here
USHORT seek = 0;
if (blob->blb_flags & BLB_seek)
{
if (blob->blb_seek >= blob->blb_length)
{
blob->blb_flags |= BLB_eof;
return 0;
}
const USHORT l = dbb->dbb_page_size - BLP_SIZE;
blob->blb_sequence = blob->blb_seek / l;
seek = (USHORT)(blob->blb_seek % l); // safe cast
blob->blb_flags &= ~BLB_seek;
blob->blb_fragment_size = 0;
if (blob->blb_level)
{
blob->blb_space_remaining = 0;
blob->blb_segment = NULL;
}
else
{
blob->blb_space_remaining = blob->blb_length - seek;
blob->blb_segment = blob->getBuffer() + seek;
}
}
if (!blob->blb_space_remaining && blob->blb_segment)
{
blob->blb_flags |= BLB_eof;
return 0;
}
// Figure out how much data is to be moved, move it, and if necessary,
// advance to the next page. The length is a function of segment
// size (or fragment size), buffer size, and amount of data left in the blob.
BLOB_PTR* to = segment;
const BLOB_PTR* from = blob->blb_segment;
USHORT length = blob->blb_space_remaining;
bool active_page = false;
fb_assert(blob->blb_pg_space_id);
WIN window(blob->blb_pg_space_id, -1); // there was no initialization of win_page here.
if (blob->blb_flags & BLB_large_scan)
{
window.win_flags = WIN_large_scan;
window.win_scans = 1;
}
while (true)
{
// If the blob is segmented, and this isn't a fragment, pick up
// the length of the next segment.
if (SEGMENTED(blob) && !blob->blb_fragment_size)
{
while (length < 2)
{
if (active_page)
{
if (window.win_flags & WIN_large_scan)
CCH_RELEASE_TAIL(tdbb, &window);
else
CCH_RELEASE(tdbb, &window);
}
const blob_page* page = get_next_page(tdbb, blob, &window);
if (!page)
{
blob->blb_flags |= BLB_eof;
return 0;
}
from = (const UCHAR*) page->blp_page;
length = page->blp_length;
active_page = true;
}
UCHAR* p = (UCHAR *) & blob->blb_fragment_size;
*p++ = *from++;
*p++ = *from++;
length -= 2;
}
// Figure out how much data can be moved. Then account for the space, and move the data
USHORT l = MIN(buffer_length, length);
if (SEGMENTED(blob))
{
l = MIN(l, blob->blb_fragment_size);
blob->blb_fragment_size -= l;
}
length -= l;
buffer_length -= l;
memcpy(to, from, l);
to += l;
from += l;
// If we ran out of space in the data clump, and there is a next clump, get it.
if (!length)
{
if (active_page)
{
if (window.win_flags & WIN_large_scan)
CCH_RELEASE_TAIL(tdbb, &window);
else
CCH_RELEASE(tdbb, &window);
}
const blob_page* page = get_next_page(tdbb, blob, &window);
if (!page)
{
active_page = false;
break;
}
from = reinterpret_cast<const UCHAR*>(page->blp_page) + seek;
length = page->blp_length - seek;
seek = 0;
active_page = true;
}
// If either the buffer or the fragment is exhausted, we're done.
if (!buffer_length || (SEGMENTED(blob) && !blob->blb_fragment_size))
break;
}
if (active_page)
{
UCHAR* buffer = blob->getBuffer();
memcpy(buffer, from, length);
from = buffer;
if (window.win_flags & WIN_large_scan)
CCH_RELEASE_TAIL(tdbb, &window);
else
CCH_RELEASE(tdbb, &window);
}
blob->blb_segment = const_cast<BLOB_PTR*>(from); // safe cast
blob->blb_space_remaining = length;
length = to - segment;
blob->blb_seek += length;
// If this is a stream blob, fake fragment unless we're at the end
if (!SEGMENTED(blob)) { // stream blob
blob->blb_fragment_size = (blob->blb_seek == blob->blb_length) ? 0 : 1;
}
return length;
}
SLONG BLB_get_slice(thread_db* tdbb,
jrd_tra* transaction,
const bid* blob_id,
const UCHAR* sdl,
USHORT param_length,
const UCHAR* param, SLONG slice_length, UCHAR* slice_addr)
{
/**************************************
*
* B L B _ g e t _ s l i c e
*
**************************************
*
* Functional description
* Fetch a slice of an array.
*
**************************************/
transaction = transaction->getOuter();
SET_TDBB(tdbb);
//Database* database = GET_DBB();
Jrd::ContextPoolHolder context(tdbb, transaction->tra_pool);
// Checkout slice description language
SLONG variables[64];
sdl_info info;
memcpy(variables, param, MIN(sizeof(variables), param_length));
if (SDL_info(tdbb->tdbb_status_vector, sdl, &info, variables)) {
ERR_punt();
}
SLONG stuff[IAD_LEN(16) / 4];
Ods::InternalArrayDesc* desc = (Ods::InternalArrayDesc*) stuff;
blb* blob = BLB_get_array(tdbb, transaction, blob_id, desc);
SLONG length = desc->iad_total_length;
// Get someplace to put data
UCharBuffer data_buffer;
UCHAR* const data = data_buffer.getBuffer(desc->iad_total_length);
// zero out memory, so that it does not have to be done for each element
memset(data, 0, desc->iad_total_length);
SLONG offset = 0;
array_slice arg;
// If we know something about the subscript bounds, prepare
// to fetch only stuff we really care about
if (info.sdl_info_dimensions)
{
const SLONG from = SDL_compute_subscript(tdbb->tdbb_status_vector, desc,
info.sdl_info_dimensions, info.sdl_info_lower);
const SLONG to = SDL_compute_subscript(tdbb->tdbb_status_vector, desc,
info.sdl_info_dimensions, info.sdl_info_upper);
if (from != -1 && to != -1)
{
if (from)
{
offset = from * desc->iad_element_length;
BLB_lseek(blob, 0, offset + (SLONG) desc->iad_length);
}
length = (to - from + 1) * desc->iad_element_length;
}
}
length = BLB_get_data(tdbb, blob, data + offset, length) + offset;
// Walk array
arg.slice_desc = info.sdl_info_element;
arg.slice_desc.dsc_address = slice_addr;
arg.slice_end = slice_addr + slice_length;
arg.slice_count = 0;
arg.slice_element_length = info.sdl_info_element.dsc_length;
arg.slice_direction = array_slice::slc_reading_array; // fetching from array
arg.slice_high_water = data + length;
arg.slice_base = data + offset;
ISC_STATUS status = SDL_walk(tdbb->tdbb_status_vector, sdl,
data, desc, variables, slice_callback, &arg);
if (status) {
ERR_punt();
}
return (SLONG) (arg.slice_count * arg.slice_element_length);
}
SLONG BLB_lseek(blb* blob, USHORT mode, SLONG offset)
{
/**************************************
*
* B L B _ l s e e k
*
**************************************
*
* Functional description
* Position a blob for direct access. Seek is only defined for stream
* type blobs.
*
**************************************/
if (!(blob->blb_flags & BLB_stream))
ERR_post(Arg::Gds(isc_bad_segstr_type));
if (mode == 1)
offset += blob->blb_seek;
else if (mode == 2)
offset = blob->blb_length + offset;
if (offset < 0)
offset = 0;
if (offset > (SLONG) blob->blb_length)
offset = blob->blb_length;
blob->blb_seek = offset;
blob->blb_flags |= BLB_seek;
blob->blb_flags &= ~BLB_eof;
return offset;
}
// This function can't take from_desc as const because it may call store_array,
// which in turn calls BLB_create2 that writes in the blob id. Although the
// compiler allows to modify from_desc->dsc_address' contents when from_desc is
// constant, this is misleading so I didn't make the source descriptor constant.
void BLB_move(thread_db* tdbb, dsc* from_desc, dsc* to_desc, jrd_nod* field)
{
/**************************************
*
* B L B _ m o v e
*
**************************************
*
* Functional description
* Perform an assignment to a blob field or a blob descriptor.
* When assigning to a field, unless the blob is null,
* this requires that either a temporary blob be materialized or that
* a permanent blob be copied. Note: it is illegal to have a blob
* field in a message.
*
**************************************/
SET_TDBB(tdbb);
if (to_desc->dsc_dtype == dtype_array)
{
// only array->array conversions are allowed
if (from_desc->dsc_dtype != dtype_array && from_desc->dsc_dtype != dtype_quad)
{
ERR_post(Arg::Gds(isc_array_convert_error));
}
}
else if (DTYPE_IS_BLOB_OR_QUAD(to_desc->dsc_dtype))
{
if (!DTYPE_IS_BLOB_OR_QUAD(from_desc->dsc_dtype))
{
// anything that can be copied into a string can be copied into a blob
move_from_string(tdbb, from_desc, to_desc, field);
return;
}
}
else if (DTYPE_IS_BLOB_OR_QUAD(from_desc->dsc_dtype))
{
move_to_string(tdbb, from_desc, to_desc);
return;
}
else
fb_assert(false);
bool simpleMove = true;
// If the target node is a field, we need more work to do.
if (field)
{
switch (field->nod_type)
{
case nod_field:
// We should not materialize the blob if the destination field
// stream (nod_union, for example) doesn't have a relation.
simpleMove =
tdbb->getRequest()->req_rpb[(IPTR)field->nod_arg[e_fld_stream]].rpb_relation == NULL;
break;
case nod_argument:
case nod_variable:
break;