forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNdbBlob.cpp
More file actions
3439 lines (3222 loc) · 98.2 KB
/
NdbBlob.cpp
File metadata and controls
3439 lines (3222 loc) · 98.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
This program 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; version 2 of the License.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "API.hpp"
#include <signaldata/TcKeyReq.hpp>
#include <NdbEnv.h>
#include <ndb_version.h>
/*
* Reading index table directly (as a table) is faster but there are
* bugs or limitations. Keep the code and make possible to choose.
*/
static const bool g_ndb_blob_ok_to_read_index_table = false;
// get state
NdbBlob::State
NdbBlob::getState()
{
return theState;
}
void
NdbBlob::getVersion(int& version)
{
version = theEventBlobVersion;
}
// set state (inline)
inline void
NdbBlob::setState(State newState)
{
DBUG_ENTER("NdbBlob::setState");
DBUG_PRINT("info", ("this=%p newState=%u", this, newState));
theState = newState;
DBUG_VOID_RETURN;
}
// define blob table
int
NdbBlob::getBlobTableName(char* btname, Ndb* anNdb, const char* tableName, const char* columnName)
{
DBUG_ENTER("NdbBlob::getBlobTableName");
NdbTableImpl* t = anNdb->theDictionary->m_impl.getTable(tableName);
if (t == NULL)
DBUG_RETURN(-1);
NdbColumnImpl* c = t->getColumn(columnName);
if (c == NULL)
DBUG_RETURN(-1);
getBlobTableName(btname, t, c);
DBUG_RETURN(0);
}
void
NdbBlob::getBlobTableName(char* btname, const NdbTableImpl* t, const NdbColumnImpl* c)
{
DBUG_ENTER("NdbBlob::getBlobTableName");
assert(t != 0 && c != 0 && c->getBlobType() && c->getPartSize() != 0);
memset(btname, 0, NdbBlobImpl::BlobTableNameSize);
sprintf(btname, "NDB$BLOB_%d_%d", (int)t->m_id, (int)c->m_column_no);
DBUG_PRINT("info", ("blob table name: %s", btname));
DBUG_VOID_RETURN;
}
int
NdbBlob::getBlobTable(NdbTableImpl& bt, const NdbTableImpl* t, const NdbColumnImpl* c, NdbError& error)
{
DBUG_ENTER("NdbBlob::getBlobTable");
const int blobVersion = c->getBlobVersion();
assert(blobVersion == NDB_BLOB_V1 || blobVersion == NDB_BLOB_V2);
char btname[NdbBlobImpl::BlobTableNameSize];
getBlobTableName(btname, t, c);
bt.setName(btname);
bt.setLogging(t->getLogging());
/*
BLOB tables use the same fragmentation as the original table
It also uses the same tablespaces and it never uses any range or
list arrays.
*/
bt.m_primaryTableId = t->m_id;
bt.m_fd.clear();
bt.m_range.clear();
bt.setFragmentCount(t->getFragmentCount());
bt.m_tablespace_id = t->m_tablespace_id;
bt.m_tablespace_version = t->m_tablespace_version;
bt.setFragmentType(t->getFragmentType());
DBUG_PRINT("info", ("Define BLOB table V%d with"
" primary table = %u and Fragment Type = %u",
blobVersion,
bt.m_primaryTableId, (uint)bt.getFragmentType()));
if (unlikely(blobVersion == NDB_BLOB_V1)) {
/*
* Stripe size 0 in V1 does not work as intended.
* No point to add support for it now.
*/
if (c->getStripeSize() == 0) {
error.code = NdbBlobImpl::ErrTable;
DBUG_RETURN(-1);
}
{ NdbDictionary::Column bc("PK");
bc.setType(NdbDictionary::Column::Unsigned);
assert(t->m_keyLenInWords != 0);
bc.setLength(t->m_keyLenInWords);
bc.setPrimaryKey(true);
bc.setDistributionKey(true);
bt.addColumn(bc);
}
{ NdbDictionary::Column bc("DIST");
bc.setType(NdbDictionary::Column::Unsigned);
bc.setPrimaryKey(true);
bc.setDistributionKey(true);
bt.addColumn(bc);
}
{ NdbDictionary::Column bc("PART");
bc.setType(NdbDictionary::Column::Unsigned);
bc.setPrimaryKey(true);
bc.setDistributionKey(false);
bt.addColumn(bc);
}
{ NdbDictionary::Column bc("DATA");
switch (c->m_type) {
case NdbDictionary::Column::Blob:
bc.setType(NdbDictionary::Column::Binary);
break;
case NdbDictionary::Column::Text:
bc.setType(NdbDictionary::Column::Char);
break;
default:
assert(false);
break;
}
bc.setLength(c->getPartSize());
bc.setStorageType(c->getStorageType());
bt.addColumn(bc);
}
} else {
{
// table PK attributes
const uint columns = t->m_columns.size();
const uint noOfKeys = t->m_noOfKeys;
uint n = 0;
uint i;
for (i = 0; n < noOfKeys; i++) {
assert(i < columns);
const NdbColumnImpl* c = t->getColumn(i);
assert(c != NULL);
if (c->m_pk) {
bt.addColumn(*c);
// addColumn might usefully return the column added..
NdbColumnImpl* bc = bt.getColumn(n);
assert(bc != NULL);
if (c->getDistributionKey()) {
bc->setDistributionKey(true);
}
// confuses restore and wrong anyway
bc->setAutoIncrement(false);
bc->setDefaultValue("");
n++;
}
}
}
// in V2 add NDB$ to avoid conflict with table PK
if (c->getStripeSize() != 0)
{ NdbDictionary::Column bc("NDB$DIST");
bc.setType(NdbDictionary::Column::Unsigned);
bc.setPrimaryKey(true);
bc.setDistributionKey(true);
bt.addColumn(bc);
}
{ NdbDictionary::Column bc("NDB$PART");
bc.setType(NdbDictionary::Column::Unsigned);
bc.setPrimaryKey(true);
bc.setDistributionKey(false);
bt.addColumn(bc);
}
// in V2 add id sequence for use in blob event code
{ NdbDictionary::Column bc("NDB$PKID");
bc.setType(NdbDictionary::Column::Unsigned);
bc.setPrimaryKey(false);
bc.setDistributionKey(false);
bt.addColumn(bc);
}
// in V2 changes to Longvar* regardless of size
{ NdbDictionary::Column bc("NDB$DATA");
const Uint32 storageType = (Uint32)c->getStorageType();
switch (c->m_type) {
case NdbDictionary::Column::Blob:
if (storageType == NDB_STORAGETYPE_MEMORY)
bc.setType(NdbDictionary::Column::Longvarbinary);
else
bc.setType(NdbDictionary::Column::Binary);
break;
case NdbDictionary::Column::Text:
if (storageType == NDB_STORAGETYPE_MEMORY)
bc.setType(NdbDictionary::Column::Longvarchar);
else
bc.setType(NdbDictionary::Column::Char);
break;
default:
assert(false);
break;
}
// the 2 length bytes are not part of part size
bc.setLength(c->getPartSize());
bc.setStorageType(c->getStorageType());
bt.addColumn(bc);
}
}
DBUG_RETURN(0);
}
int
NdbBlob::getBlobEventName(char* bename, Ndb* anNdb, const char* eventName, const char* columnName)
{
NdbEventImpl* e = anNdb->theDictionary->m_impl.getEvent(eventName);
if (e == NULL)
return -1;
NdbColumnImpl* c = e->m_tableImpl->getColumn(columnName);
if (c == NULL)
return -1;
getBlobEventName(bename, e, c);
delete e; // it is from new NdbEventImpl
return 0;
}
void
NdbBlob::getBlobEventName(char* bename, const NdbEventImpl* e, const NdbColumnImpl* c)
{
// XXX events should have object id
BaseString::snprintf(bename, MAX_TAB_NAME_SIZE, "NDB$BLOBEVENT_%s_%d", e->m_name.c_str(), (int)c->m_column_no);
}
void
NdbBlob::getBlobEvent(NdbEventImpl& be, const NdbEventImpl* e, const NdbColumnImpl* c)
{
DBUG_ENTER("NdbBlob::getBlobEvent");
// blob table
assert(c->m_blobTable != NULL);
const NdbTableImpl& bt = *c->m_blobTable;
// blob event name
char bename[MAX_TAB_NAME_SIZE+1];
getBlobEventName(bename, e, c);
bename[sizeof(bename)-1]= 0;
be.setName(bename);
be.setTable(bt);
// simple assigments
be.mi_type = e->mi_type;
be.m_dur = e->m_dur;
be.m_mergeEvents = e->m_mergeEvents;
// report unchanged data
// not really needed now since UPD is DEL o INS and we subscribe to all
be.setReport(NdbDictionary::Event::ER_ALL);
// columns PK - DIST - PART - DATA
{ const NdbColumnImpl* bc = bt.getColumn((Uint32)0);
be.addColumn(*bc);
}
{ const NdbColumnImpl* bc = bt.getColumn((Uint32)1);
be.addColumn(*bc);
}
{ const NdbColumnImpl* bc = bt.getColumn((Uint32)2);
be.addColumn(*bc);
}
{ const NdbColumnImpl* bc = bt.getColumn((Uint32)3);
be.addColumn(*bc);
}
DBUG_VOID_RETURN;
}
// initialization
NdbBlob::NdbBlob(Ndb*)
{
init();
}
void
NdbBlob::init()
{
theBlobVersion = 0;
theFixedDataFlag = false;
theHeadSize = 0;
theVarsizeBytes = 0;
theState = Idle;
theEventBlobVersion = -1;
theBtColumnNo[0] = -1;
theBtColumnNo[1] = -1;
theBtColumnNo[2] = -1;
theBtColumnNo[3] = -1;
theBtColumnNo[4] = -1;
theNdb = NULL;
theNdbCon = NULL;
theNdbOp = NULL;
theEventOp = NULL;
theBlobEventOp = NULL;
theBlobEventPkRecAttr = NULL;
theBlobEventDistRecAttr = NULL;
theBlobEventPartRecAttr = NULL;
theBlobEventPkidRecAttr = NULL;
theBlobEventDataRecAttr = NULL;
theTable = NULL;
theAccessTable = NULL;
theBlobTable = NULL;
theColumn = NULL;
theFillChar = 0xFF;
theInlineSize = 0;
thePartSize = 0;
theStripeSize = 0;
theGetFlag = false;
theGetBuf = NULL;
theSetFlag = false;
theSetValueInPreExecFlag = false;
theSetBuf = NULL;
theGetSetBytes = 0;
thePendingBlobOps = 0;
theActiveHook = NULL;
theActiveHookArg = NULL;
thePartLen = 0;
theInlineData = NULL;
theHeadInlineRecAttr = NULL;
theHeadInlineReadOp = NULL;
theHeadInlineUpdateFlag = false;
userDefinedPartitioning = false;
thePartitionId = noPartitionId();
thePartitionIdRecAttr = NULL;
theNullFlag = -1;
theLength = 0;
thePos = 0;
theNext = NULL;
}
void
NdbBlob::release()
{
theKeyBuf.release();
theAccessKeyBuf.release();
thePackKeyBuf.release();
theHeadInlineBuf.release();
theHeadInlineCopyBuf.release();
thePartBuf.release();
theBlobEventDataBuf.release();
setState(Idle);
}
// buffers
NdbBlob::Buf::Buf() :
data(NULL),
size(0),
maxsize(0)
{
}
NdbBlob::Buf::~Buf()
{
delete [] data;
}
void
NdbBlob::Buf::alloc(unsigned n)
{
size = n;
if (maxsize < n) {
delete [] data;
// align to Uint64
if (n % 8 != 0)
n += 8 - n % 8;
data = new char [n];
maxsize = n;
}
#ifdef VM_TRACE
memset(data, 'X', maxsize);
#endif
}
void
NdbBlob::Buf::release()
{
if (data)
delete [] data;
data = NULL;
size = 0;
maxsize = 0;
}
void
NdbBlob::Buf::zerorest()
{
assert(size <= maxsize);
memset(data + size, 0, maxsize - size);
}
void
NdbBlob::Buf::copyfrom(const NdbBlob::Buf& src)
{
size = src.size;
memcpy(data, src.data, size);
}
// classify operations (inline)
inline bool
NdbBlob::isTableOp()
{
return theTable == theAccessTable;
}
inline bool
NdbBlob::isIndexOp()
{
return theTable != theAccessTable;
}
inline bool
NdbBlob::isKeyOp()
{
return
theNdbOp->theOperationType == NdbOperation::InsertRequest ||
theNdbOp->theOperationType == NdbOperation::UpdateRequest ||
theNdbOp->theOperationType == NdbOperation::WriteRequest ||
theNdbOp->theOperationType == NdbOperation::ReadRequest ||
theNdbOp->theOperationType == NdbOperation::ReadExclusive ||
theNdbOp->theOperationType == NdbOperation::DeleteRequest;
}
inline bool
NdbBlob::isReadOp()
{
return
theNdbOp->theOperationType == NdbOperation::ReadRequest ||
theNdbOp->theOperationType == NdbOperation::ReadExclusive;
}
inline bool
NdbBlob::isInsertOp()
{
return
theNdbOp->theOperationType == NdbOperation::InsertRequest;
}
inline bool
NdbBlob::isUpdateOp()
{
return
theNdbOp->theOperationType == NdbOperation::UpdateRequest;
}
inline bool
NdbBlob::isWriteOp()
{
return
theNdbOp->theOperationType == NdbOperation::WriteRequest;
}
inline bool
NdbBlob::isDeleteOp()
{
return
theNdbOp->theOperationType == NdbOperation::DeleteRequest;
}
inline bool
NdbBlob::isScanOp()
{
return
theNdbOp->theOperationType == NdbOperation::OpenScanRequest ||
theNdbOp->theOperationType == NdbOperation::OpenRangeScanRequest;
}
inline bool
NdbBlob::isReadOnlyOp()
{
return ! (
theNdbOp->theOperationType == NdbOperation::InsertRequest ||
theNdbOp->theOperationType == NdbOperation::UpdateRequest ||
theNdbOp->theOperationType == NdbOperation::WriteRequest
);
}
inline bool
NdbBlob::isTakeOverOp()
{
return
TcKeyReq::getTakeOverScanFlag(theNdbOp->theScanInfo);
}
// computations (inline)
inline Uint32
NdbBlob::getPartNumber(Uint64 pos)
{
assert(thePartSize != 0 && pos >= theInlineSize);
Uint64 partNo = (pos - theInlineSize) / thePartSize;
assert(partNo < (Uint64(1) << 32));
return Uint32(partNo);
}
inline Uint32
NdbBlob::getPartOffset(Uint64 pos)
{
assert(thePartSize != 0 && pos >= theInlineSize);
return (pos - theInlineSize) % thePartSize;
}
inline Uint32
NdbBlob::getPartCount()
{
if (theLength <= theInlineSize)
return 0;
return 1 + getPartNumber(theLength - 1);
}
inline Uint32
NdbBlob::getDistKey(Uint32 part)
{
assert(theStripeSize != 0);
Uint32 dist = 0;
if (unlikely(theBlobVersion == NDB_BLOB_V1))
dist = (part / theStripeSize) % theStripeSize;
else {
// correct the mistake
dist = (part / theStripeSize);
}
return dist;
}
inline void
NdbBlob::setHeadPartitionId(NdbOperation* anOp)
{
/* For UserDefined partitioned tables,
* we must set the head row's partition id
* manually when reading/modifying it with
* primary key or unique key.
* For scans we do not have to.
*/
if (userDefinedPartitioning &&
(thePartitionId != noPartitionId())) {
anOp->setPartitionId(thePartitionId);
}
}
inline void
NdbBlob::setPartPartitionId(NdbOperation* anOp)
{
/* For UserDefined partitioned tables
* we must set the part row's partition
* id manually when performing operations.
* This means that stripe size is ignored
* for UserDefined partitioned tables.
* All part row operations use primary keys
*/
if (userDefinedPartitioning) {
assert(thePartitionId != noPartitionId());
anOp->setPartitionId(thePartitionId);
}
}
// pack/unpack table/index key XXX support routines, shortcuts
int
NdbBlob::packKeyValue(const NdbTableImpl* aTable, const Buf& srcBuf)
{
DBUG_ENTER("NdbBlob::packKeyValue");
const Uint32* data = (const Uint32*)srcBuf.data;
unsigned pos = 0;
Uint32* pack_data = (Uint32*)thePackKeyBuf.data;
unsigned pack_pos = 0;
for (unsigned i = 0; i < aTable->m_columns.size(); i++) {
NdbColumnImpl* c = aTable->m_columns[i];
assert(c != NULL);
if (c->m_pk) {
unsigned len = c->m_attrSize * c->m_arraySize;
Uint32 pack_len;
bool ok = c->get_var_length(&data[pos], pack_len);
if (! ok) {
setErrorCode(NdbBlobImpl::ErrCorruptPK);
DBUG_RETURN(-1);
}
memcpy(&pack_data[pack_pos], &data[pos], pack_len);
while (pack_len % 4 != 0) {
char* p = (char*)&pack_data[pack_pos] + pack_len++;
*p = 0;
}
pos += (len + 3) / 4;
pack_pos += pack_len / 4;
}
}
assert(4 * pos == srcBuf.size);
assert(4 * pack_pos <= thePackKeyBuf.maxsize);
thePackKeyBuf.size = 4 * pack_pos;
thePackKeyBuf.zerorest();
DBUG_RETURN(0);
}
int
NdbBlob::unpackKeyValue(const NdbTableImpl* aTable, Buf& dstBuf)
{
DBUG_ENTER("NdbBlob::unpackKeyValue");
Uint32* data = (Uint32*)dstBuf.data;
unsigned pos = 0;
const Uint32* pack_data = (const Uint32*)thePackKeyBuf.data;
unsigned pack_pos = 0;
for (unsigned i = 0; i < aTable->m_columns.size(); i++) {
NdbColumnImpl* c = aTable->m_columns[i];
assert(c != NULL);
if (c->m_pk) {
unsigned len = c->m_attrSize * c->m_arraySize;
Uint32 pack_len;
bool ok = c->get_var_length(&pack_data[pack_pos], pack_len);
if (! ok) {
setErrorCode(NdbBlobImpl::ErrCorruptPK);
DBUG_RETURN(-1);
}
memcpy(&data[pos], &pack_data[pack_pos], pack_len);
while (pack_len % 4 != 0) {
char* p = (char*)&data[pos] + pack_len++;
*p = 0;
}
pos += (len + 3) / 4;
pack_pos += pack_len / 4;
}
}
assert(4 * pos == dstBuf.size);
assert(4 * pack_pos == thePackKeyBuf.size);
DBUG_RETURN(0);
}
/* Set both packed and unpacked KeyBuf from NdbRecord and row. */
int
NdbBlob::copyKeyFromRow(const NdbRecord *record, const char *row,
Buf& packedBuf, Buf& unpackedBuf)
{
char buf[NdbRecord::Attr::SHRINK_VARCHAR_BUFFSIZE];
DBUG_ENTER("NdbBlob::copyKeyFromRow");
assert(record->flags & NdbRecord::RecHasAllKeys);
char *packed= packedBuf.data;
char *unpacked= unpackedBuf.data;
for (Uint32 i= 0; i < record->key_index_length; i++)
{
const NdbRecord::Attr *col= &record->columns[record->key_indexes[i]];
Uint32 len= ~0;
bool len_ok;
const char *src;
if (col->flags & NdbRecord::IsMysqldShrinkVarchar)
{
/* Used to support special varchar format for mysqld keys. */
len_ok= col->shrink_varchar(row, len, buf);
src= buf;
}
else
{
len_ok= col->get_var_length(row, len);
src= &row[col->offset];
}
if (!len_ok)
{
setErrorCode(NdbBlobImpl::ErrCorruptPK);
DBUG_RETURN(-1);
}
/* Copy the key. */
memcpy(packed, src, len);
memcpy(unpacked, src, len);
/* Zero-pad if needed. */
Uint32 packed_len= (len + 3) & ~3;
Uint32 unpacked_len= (col->maxSize + 3) & ~3;
Uint32 packed_pad= packed_len - len;
Uint32 unpacked_pad= unpacked_len - len;
if (packed_pad > 0)
bzero(packed + len, packed_pad);
if (unpacked_pad > 0)
bzero(unpacked + len, unpacked_pad);
packed+= packed_len;
unpacked+= unpacked_len;
}
packedBuf.size= packed - packedBuf.data;
packedBuf.zerorest();
assert(unpacked == unpackedBuf.data + unpackedBuf.size);
DBUG_RETURN(0);
}
/*
* This method is used to get data ptr and length values for the
* header for an 'empty' Blob. This is a blob with length zero,
* or a NULL BLOB.
* This header is used to build signals for an insert or write
* operation before the correct blob header information is known.
* Once the blob header information is known, another operation will
* set the header information correctly.
*/
void
NdbBlob::getNullOrEmptyBlobHeadDataPtr(const char * & data,
Uint32 & byteSize)
{
/* Only for use when preparing signals before a blob value has been set
* e.g. NdbRecord
*/
assert(theState==Prepared);
assert(theLength==0);
assert(theSetBuf==NULL);
assert(theGetSetBytes==0);
assert(thePos==0);
assert(theHeadInlineBuf.data!=NULL);
DBUG_PRINT("info", ("getNullOrEmptyBlobHeadDataPtr. Nullable : %d",
theColumn->m_nullable));
if (theColumn->m_nullable)
{
/* Null Blob */
data = NULL;
byteSize = 0;
return;
}
/* Set up the buffer ptr to appear to be pointing to some data */
theSetBuf=(char*) 1; // Extremely nasty way of being non-null
// If it's ever de-reffed, should show up
/* Pack header etc. */
prepareSetHeadInlineValue();
data=theHeadInlineBuf.data;
/* Calculate size */
if (unlikely(theBlobVersion == NDB_BLOB_V1))
byteSize = theHeadInlineBuf.size;
else
byteSize = theHead.varsize + 2;
/* Reset affected members */
theSetBuf=NULL;
memset(&theHead, 0, sizeof(theHead));
/* This column is not null anymore - record the fact so that
* a setNull() call will modify state
*/
theNullFlag=false;
}
// getters and setters
void
NdbBlob::packBlobHead(const Head& head, char* buf, int blobVersion)
{
DBUG_ENTER("NdbBlob::packBlobHead");
DBUG_PRINT("info", ("version=%d", blobVersion));
if (unlikely(blobVersion == NDB_BLOB_V1)) {
// native
memcpy(buf, &head.length, sizeof(head.length));
} else {
unsigned char* p = (unsigned char*)buf;
// all little-endian
uint i, n;
for (i = 0, n = 0; i < 2; i++, n += 8)
*p++ = (head.varsize >> n) & 0xff;
for (i = 0, n = 0; i < 2; i++, n += 8)
*p++ = (head.reserved >> n) & 0xff;
for (i = 0, n = 0; i < 4; i++, n += 8)
*p++ = (head.pkid >> n) & 0xff;
for (i = 0, n = 0; i < 8; i++, n += 8)
*p++ = (head.length >> n) & 0xff;
assert(p - (uchar*)buf == 16);
assert(head.reserved == 0);
DBUG_DUMP("info", (uchar*)buf, 16);
}
DBUG_PRINT("info", ("pack: varsize=%u length=%u pkid=%u",
(uint)head.varsize, (uint)head.length, (uint)head.pkid));
DBUG_VOID_RETURN;
}
void
NdbBlob::unpackBlobHead(Head& head, const char* buf, int blobVersion)
{
DBUG_ENTER("NdbBlob::unpackBlobHead");
DBUG_PRINT("info", ("version=%d", blobVersion));
head.varsize = 0;
head.reserved = 0;
head.pkid = 0;
head.length = 0;
if (unlikely(blobVersion == NDB_BLOB_V1)) {
// native
memcpy(&head.length, buf, sizeof(head.length));
head.headsize = (NDB_BLOB_V1_HEAD_SIZE << 2);
} else {
const unsigned char* p = (const unsigned char*)buf;
// all little-endian
uint i, n;
for (i = 0, n = 0; i < 2; i++, n += 8)
head.varsize |= ((Uint16)*p++ << n);
for (i = 0, n = 0; i < 2; i++, n += 8)
head.reserved |= ((Uint32)*p++ << n);
for (i = 0, n = 0; i < 4; i++, n += 8)
head.pkid |= ((Uint32)*p++ << n);
for (i = 0, n = 0; i < 8; i++, n += 8)
head.length |= ((Uint64)*p++ << n);
assert(p - (uchar*)buf == 16);
assert(head.reserved == 0);
head.headsize = (NDB_BLOB_V2_HEAD_SIZE << 2);
DBUG_DUMP("info", (uchar*)buf, 16);
}
DBUG_PRINT("info", ("unpack: varsize=%u length=%u pkid=%u",
(uint)head.varsize, (uint)head.length, (uint)head.pkid));
DBUG_VOID_RETURN;
}
inline void
NdbBlob::packBlobHead()
{
packBlobHead(theHead, theHeadInlineBuf.data, theBlobVersion);
}
inline void
NdbBlob::unpackBlobHead()
{
unpackBlobHead(theHead, theHeadInlineBuf.data, theBlobVersion);
}
int
NdbBlob::getTableKeyValue(NdbOperation* anOp)
{
DBUG_ENTER("NdbBlob::getTableKeyValue");
Uint32* data = (Uint32*)theKeyBuf.data;
unsigned pos = 0;
for (unsigned i = 0; i < theTable->m_columns.size(); i++) {
NdbColumnImpl* c = theTable->m_columns[i];
assert(c != NULL);
if (c->m_pk) {
unsigned len = c->m_attrSize * c->m_arraySize;
if (anOp->getValue_impl(c, (char*)&data[pos]) == NULL) {
setErrorCode(anOp);
DBUG_RETURN(-1);
}
// odd bytes receive no data and must be zeroed
while (len % 4 != 0) {
char* p = (char*)&data[pos] + len++;
*p = 0;
}
pos += len / 4;
}
}
assert(pos == theKeyBuf.size / 4);
DBUG_RETURN(0);
}
// in V2 operation can also be on blob part
int
NdbBlob::setTableKeyValue(NdbOperation* anOp)
{
DBUG_ENTER("NdbBlob::setTableKeyValue");
DBUG_DUMP("info", (uchar*) theKeyBuf.data, 4 * theTable->m_keyLenInWords);
const bool isBlobPartOp = (anOp->m_currentTable == theBlobTable);
const Uint32* data = (const Uint32*)theKeyBuf.data;
const unsigned columns = theTable->m_columns.size();
uint n = 0;
const uint noOfKeys = theTable->m_noOfKeys;
unsigned pos = 0;
for (unsigned i = 0; n < noOfKeys; i++) {
assert(i < columns);
const NdbColumnImpl* c = theTable->getColumn(i);
assert(c != NULL);
if (c->m_pk) {
unsigned len = c->m_attrSize * c->m_arraySize;
if (isBlobPartOp) {
c = theBlobTable->getColumn(n);
assert(c != NULL);
}
if (anOp->equal_impl(c, (const char*)&data[pos]) == -1) {
setErrorCode(anOp);
DBUG_RETURN(-1);
}
pos += (len + 3) / 4;
n++;
}
}
assert(pos == theKeyBuf.size / 4);
DBUG_RETURN(0);
}
int
NdbBlob::setAccessKeyValue(NdbOperation* anOp)
{
DBUG_ENTER("NdbBlob::setAccessKeyValue");
DBUG_DUMP("info", (uchar*) theAccessKeyBuf.data,
4 * theAccessTable->m_keyLenInWords);
const Uint32* data = (const Uint32*)theAccessKeyBuf.data;
const unsigned columns = theAccessTable->m_columns.size();
unsigned pos = 0;
for (unsigned i = 0; i < columns; i++) {
NdbColumnImpl* c = theAccessTable->m_columns[i];
assert(c != NULL);
if (c->m_pk) {
unsigned len = c->m_attrSize * c->m_arraySize;
if (anOp->equal_impl(c, (const char*)&data[pos]) == -1) {
setErrorCode(anOp);
DBUG_RETURN(-1);
}
pos += (len + 3) / 4;
}
}
assert(pos == theAccessKeyBuf.size / 4);
DBUG_RETURN(0);
}
int
NdbBlob::setDistKeyValue(NdbOperation* anOp, Uint32 part)
{
DBUG_ENTER("NdbBlob::setDistKeyValue");
if (theStripeSize != 0) {
Uint32 dist = getDistKey(part);
DBUG_PRINT("info", ("dist=%u", dist));
if (anOp->equal(theBtColumnNo[BtColumnDist], dist) == -1)
DBUG_RETURN(-1);
}
DBUG_RETURN(0);
}
int
NdbBlob::setPartKeyValue(NdbOperation* anOp, Uint32 part)
{
DBUG_ENTER("NdbBlob::setPartKeyValue");
DBUG_PRINT("info", ("part=%u packkey=", part));
DBUG_DUMP("info", (uchar*) thePackKeyBuf.data, thePackKeyBuf.size);
// TODO use attr ids after compatibility with 4.1.7 not needed
if (unlikely(theBlobVersion == NDB_BLOB_V1)) {
// keep using names
if (anOp->equal("PK", thePackKeyBuf.data) == -1 ||
anOp->equal("DIST", getDistKey(part)) == -1 ||
anOp->equal("PART", part) == -1) {
setErrorCode(anOp);
DBUG_RETURN(-1);
}
} else {
if (setTableKeyValue(anOp) == -1 ||
setDistKeyValue(anOp, part) == -1 ||
anOp->equal(theBtColumnNo[BtColumnPart], part) == -1) {
setErrorCode(anOp);
DBUG_RETURN(-1);
}
}
setPartPartitionId(anOp);
DBUG_RETURN(0);
}
int
NdbBlob::setPartPkidValue(NdbOperation* anOp, Uint32 pkid)
{
DBUG_ENTER("NdbBlob::setPartPkidValue");
DBUG_PRINT("info", ("pkid=%u", pkid));
if (unlikely(theBlobVersion == NDB_BLOB_V1))
;
else {
if (anOp->setValue(theBtColumnNo[BtColumnPkid], pkid) == -1) {
setErrorCode(anOp);
DBUG_RETURN(-1);
}
}
DBUG_RETURN(0);
}
int
NdbBlob::getPartDataValue(NdbOperation* anOp, char* buf, Uint16* aLenLoc)
{
DBUG_ENTER("NdbBlob::getPartDataValue");
assert(aLenLoc != NULL);
Uint32 bcNo = theBtColumnNo[BtColumnData];
if (theFixedDataFlag) {
if (anOp->getValue(bcNo, buf) == NULL) {
setErrorCode(anOp);
DBUG_RETURN(-1);
}
// length is full size and is not returned via NDB API
*aLenLoc = thePartSize;
} else {
const NdbColumnImpl* bc = theBlobTable->getColumn(bcNo);
assert(bc != NULL);