22
#include <drizzled/server_includes.h>
23
23
#include <drizzled/field/blob.h>
24
#include <drizzled/table.h>
25
#include <drizzled/session.h>
26
#include "plugin/myisam/myisam.h"
36
static uint32_t blob_pack_length_to_max_length(uint32_t arg)
38
return max(UINT32_MAX,
39
(uint32_t)((INT64_C(1) << min(arg, 4U) * 8) - INT64_C(1)));
26
blob_pack_length_to_max_length(uint32_t arg)
28
return (INT64_C(1) << cmin(arg, 4U) * 8) - INT64_C(1);
43
32
/****************************************************************************
45
34
** A blob is saved as a length and a pointer. The length is stored in the
46
** packlength slot and is sizeof(uint32_t) (4 bytes)
35
** packlength slot and may be from 1-4.
47
36
****************************************************************************/
49
Field_blob::Field_blob(unsigned char *ptr_arg,
50
unsigned char *null_ptr_arg,
51
unsigned char null_bit_arg,
52
const char *field_name_arg,
54
const CHARSET_INFO * const cs)
56
blob_pack_length_to_max_length(sizeof(uint32_t)),
38
Field_blob::Field_blob(unsigned char *ptr_arg, unsigned char *null_ptr_arg, unsigned char null_bit_arg,
39
enum utype unireg_check_arg, const char *field_name_arg,
40
TABLE_SHARE *share, uint32_t blob_pack_length,
41
const CHARSET_INFO * const cs)
42
:Field_longstr(ptr_arg, blob_pack_length_to_max_length(blob_pack_length),
43
null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
45
packlength(blob_pack_length)
63
48
share->blob_fields++;
64
/* TODO: why do not fill table->getShare()->blob_field array here? */
49
/* TODO: why do not fill table->s->blob_field array here? */
67
53
void Field_blob::store_length(unsigned char *i_ptr,
54
uint32_t i_packlength,
56
bool low_byte_first __attribute__((unused)))
71
#ifndef WORDS_BIGENDIAN
75
#ifdef WORDS_BIGENDIAN
78
int4store(i_ptr,i_number);
58
switch (i_packlength) {
60
i_ptr[0]= (unsigned char) i_number;
63
#ifdef WORDS_BIGENDIAN
66
int2store(i_ptr,(unsigned short) i_number);
70
shortstore(i_ptr,(unsigned short) i_number);
73
int3store(i_ptr,i_number);
76
#ifdef WORDS_BIGENDIAN
79
int4store(i_ptr,i_number);
83
longstore(i_ptr,i_number);
82
longstore(i_ptr,i_number);
86
void Field_blob::store_length(unsigned char *i_ptr, uint32_t i_number)
88
store_length(i_ptr, i_number, getTable()->getShare()->db_low_byte_first);
92
88
uint32_t Field_blob::get_length(const unsigned char *pos,
95
#ifndef WORDS_BIGENDIAN
99
#ifdef WORDS_BIGENDIAN
105
return (uint32_t) tmp;
109
uint32_t Field_blob::get_packed_size(const unsigned char *ptr_arg,
112
return sizeof(uint32_t) + get_length(ptr_arg, low_byte_first);
116
uint32_t Field_blob::get_length(uint32_t row_offset)
118
return get_length(ptr+row_offset,
119
getTable()->getShare()->db_low_byte_first);
123
uint32_t Field_blob::get_length(const unsigned char *ptr_arg)
125
return get_length(ptr_arg, getTable()->getShare()->db_low_byte_first);
89
uint32_t packlength_arg,
90
bool low_byte_first __attribute__((unused)))
92
switch (packlength_arg) {
94
return (uint32_t) pos[0];
98
#ifdef WORDS_BIGENDIAN
104
return (uint32_t) tmp;
107
return (uint32_t) uint3korr(pos);
111
#ifdef WORDS_BIGENDIAN
117
return (uint32_t) tmp;
120
return 0; // Impossible
130
125
Put a blob length field into a record buffer.
132
Blob length is always stored in sizeof(uint32_t) (4 bytes)
127
Depending on the maximum length of a blob, its length field is
128
put into 1 to 4 bytes. This is a property of the blob object,
129
described by 'packlength'.
134
131
@param pos Pointer into the record buffer.
135
132
@param length The length value to put.
172
180
from= tmpstr.ptr();
175
new_length= min(max_data_length(), field_charset->mbmaxlen * length);
183
new_length= cmin(max_data_length(), field_charset->mbmaxlen * length);
176
184
if (value.alloc(new_length))
188
if (f_is_hex_escape(flags))
190
copy_length= my_copy_with_hex_escaping(field_charset,
191
(char*) value.ptr(), new_length,
193
Field_blob::store_length(copy_length);
195
memcpy(ptr + packlength, &tmp, sizeof(char*));
180
199
"length" is OK as "nchars" argument to well_formed_copy_nchars as this
181
200
is never used to limit the length of the data. The cut of long data
203
222
/* Fatal OOM error */
204
223
memset(ptr, 0, Field_blob::pack_length());
209
228
int Field_blob::store(double nr)
211
230
const CHARSET_INFO * const cs=charset();
212
ASSERT_COLUMN_MARKED_FOR_WRITE;
213
231
value.set_real(nr, NOT_FIXED_DEC, cs);
214
return Field_blob::store(value.ptr(),(uint32_t) value.length(), cs);
232
return Field_blob::store(value.ptr(),(uint) value.length(), cs);
218
236
int Field_blob::store(int64_t nr, bool unsigned_val)
220
238
const CHARSET_INFO * const cs=charset();
221
ASSERT_COLUMN_MARKED_FOR_WRITE;
222
239
value.set_int(nr, unsigned_val, cs);
223
return Field_blob::store(value.ptr(), (uint32_t) value.length(), cs);
240
return Field_blob::store(value.ptr(), (uint) value.length(), cs);
250
ASSERT_COLUMN_MARKED_FOR_READ;
252
memcpy(&blob,ptr+sizeof(uint32_t),sizeof(char*));
264
memcpy(&blob,ptr+packlength,sizeof(char*));
255
267
uint32_t length=get_length(ptr);
256
268
return my_strntoll(charset(),blob,length,10,NULL,¬_used);
259
String *Field_blob::val_str(String *,
271
String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
264
ASSERT_COLUMN_MARKED_FOR_READ;
266
memcpy(&blob,ptr+sizeof(uint32_t),sizeof(char*));
275
memcpy(&blob,ptr+packlength,sizeof(char*));
268
277
val_ptr->set("",0,charset()); // A bit safer than ->length(0)
309
313
uint32_t max_length)
311
315
unsigned char *blob1,*blob2;
312
memcpy(&blob1,a_ptr+sizeof(uint32_t),sizeof(char*));
313
memcpy(&blob2,b_ptr+sizeof(uint32_t),sizeof(char*));
316
memcpy(&blob1,a_ptr+packlength,sizeof(char*));
317
memcpy(&blob2,b_ptr+packlength,sizeof(char*));
314
318
uint32_t a_len= get_length(a_ptr), b_len= get_length(b_ptr);
315
319
set_if_smaller(a_len, max_length);
316
320
set_if_smaller(b_len, max_length);
321
325
int Field_blob::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
326
330
uint32_t a_length,b_length;
327
memcpy(&a,a_ptr+sizeof(uint32_t),sizeof(char*));
328
memcpy(&b,b_ptr+sizeof(uint32_t),sizeof(char*));
330
a_length= get_length(a_ptr);
331
memcpy(&a,a_ptr+packlength,sizeof(char*));
332
memcpy(&b,b_ptr+packlength,sizeof(char*));
333
a_length=get_length(a_ptr);
332
334
if (a_length > max_length)
333
a_length= max_length;
335
b_length= get_length(b_ptr);
336
b_length=get_length(b_ptr);
337
337
if (b_length > max_length)
338
b_length= max_length;
340
diff= memcmp(a,b,min(a_length,b_length));
342
return diff ? diff : (unsigned int) (a_length - b_length);
339
diff=memcmp(a,b,cmin(a_length,b_length));
340
return diff ? diff : (int) (a_length - b_length);
345
344
/* The following is used only when comparing a key */
346
uint32_t Field_blob::get_key_image(unsigned char *buff, uint32_t length)
346
uint32_t Field_blob::get_key_image(unsigned char *buff,
348
imagetype type_arg __attribute__((unused)))
348
350
uint32_t blob_length= get_length(ptr);
349
351
unsigned char *blob;
372
uint32_t Field_blob::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
374
uint32_t blob_length= get_length(ptr);
378
uint32_t local_char_length= length / field_charset->mbmaxlen;
379
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
381
set_if_smaller(blob_length, local_char_length);
383
unsigned char len_buff[HA_KEY_BLOB_LENGTH];
384
int2store(len_buff,length);
385
buff.append(len_buff);
386
buff.append(blob, blob_length);
388
if (length > blob_length)
391
Must clear this as we do a memcmp in optimizer/range.cc to detect
395
buff.append(length-blob_length, '0');
397
return HA_KEY_BLOB_LENGTH+length;
400
374
void Field_blob::set_key_image(const unsigned char *buff,uint32_t length)
402
376
length= uint2korr(buff);
403
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length, field_charset);
377
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
406
382
int Field_blob::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
408
384
unsigned char *blob1;
409
385
uint32_t blob_length=get_length(ptr);
410
memcpy(&blob1,ptr+sizeof(uint32_t),sizeof(char*));
386
memcpy(&blob1,ptr+packlength,sizeof(char*));
411
387
const CHARSET_INFO * const cs= charset();
412
388
uint32_t local_char_length= max_key_length / cs->mbmaxlen;
413
389
local_char_length= my_charpos(cs, blob1, blob1+blob_length,
424
400
b+HA_KEY_BLOB_LENGTH, uint2korr(b));
405
Save the field metadata for blob fields.
407
Saves the pack length in the first byte of the field metadata array
408
at index of *metadata_ptr.
410
@param metadata_ptr First byte of field metadata
412
@returns number of bytes written to metadata_ptr
414
int Field_blob::do_save_field_metadata(unsigned char *metadata_ptr)
416
*metadata_ptr= pack_length_no_ptr();
427
421
uint32_t Field_blob::sort_length() const
429
return (uint32_t) (getTable()->getSession()->variables.max_sort_length +
430
(field_charset == &my_charset_bin ? 0 : sizeof(uint32_t)));
423
return (uint32_t) (current_session->variables.max_sort_length +
424
(field_charset == &my_charset_bin ? 0 : packlength));
433
428
void Field_blob::sort_string(unsigned char *to,uint32_t length)
435
430
unsigned char *blob;
447
442
Store length of blob last in blob to shorter blobs before longer blobs
449
length-= sizeof(uint32_t); // size of stored blob length
452
mi_int4store(pos, blob_length);
447
switch (packlength) {
449
*pos= (char) blob_length;
452
mi_int2store(pos, blob_length);
455
mi_int3store(pos, blob_length);
458
mi_int4store(pos, blob_length);
454
memcpy(&blob,ptr+sizeof(uint32_t),sizeof(char*));
462
memcpy(&blob,ptr+packlength,sizeof(char*));
456
464
blob_length=my_strnxfrm(field_charset,
457
465
to, length, blob, blob_length);
458
466
assert(blob_length == length);
462
uint32_t Field_blob::pack_length() const
464
return (uint32_t) (sizeof(uint32_t) + portable_sizeof_char_ptr);
467
471
void Field_blob::sql_type(String &res) const
475
479
unsigned char *Field_blob::pack(unsigned char *to, const unsigned char *from,
476
uint32_t max_length, bool low_byte_first)
480
uint32_t max_length, bool low_byte_first)
478
482
unsigned char *save= ptr;
479
483
ptr= (unsigned char*) from;
480
uint32_t length= get_length(); // Length of from string
484
uint32_t length=get_length(); // Length of from string
483
487
Store max length, which will occupy packlength bytes. If the max
484
488
length given is smaller than the actual length of the blob, we
485
489
just store the initial bytes of the blob.
487
store_length(to, min(length, max_length), low_byte_first);
491
store_length(to, packlength, cmin(length, max_length), low_byte_first);
490
494
Store the actual blob data, which will occupy 'length' bytes.
494
498
get_ptr((unsigned char**) &from);
495
memcpy(to+sizeof(uint32_t), from,length);
499
memcpy(to+packlength, from,length);
498
ptr= save; // Restore org row pointer
499
return(to+sizeof(uint32_t)+length);
501
ptr=save; // Restore org row pointer
502
return(to+packlength+length);
503
507
Unpack a blob field from row data.
505
This method is used to unpack a blob field from a master whose size of
509
This method is used to unpack a blob field from a master whose size of
506
510
the field is less than that of the slave. Note: This method is included
507
511
to satisfy inheritance rules, but is not needed for blob fields. It
508
512
simply is used as a pass-through to the original unpack() method for
517
521
@return New pointer into memory based on from + length of the data
519
const unsigned char *Field_blob::unpack(unsigned char *,
520
const unsigned char *from,
523
const unsigned char *Field_blob::unpack(unsigned char *to __attribute__((unused)),
524
const unsigned char *from,
524
uint32_t const length= get_length(from, low_byte_first);
525
getTable()->setWriteSet(position());
526
store(reinterpret_cast<const char*>(from) + sizeof(uint32_t),
528
uint32_t const master_packlength=
529
param_data > 0 ? param_data & 0xFF : packlength;
530
uint32_t const length= get_length(from, master_packlength, low_byte_first);
531
bitmap_set_bit(table->write_set, field_index);
532
store(reinterpret_cast<const char*>(from) + master_packlength,
527
533
length, field_charset);
528
return(from + sizeof(uint32_t) + length);
534
return(from + master_packlength + length);
537
/* Keys for blobs are like keys on varchars */
539
int Field_blob::pack_cmp(const unsigned char *a, const unsigned char *b, uint32_t key_length_arg,
540
bool insert_or_update)
542
uint32_t a_length, b_length;
543
if (key_length_arg > 255)
545
a_length=uint2korr(a); a+=2;
546
b_length=uint2korr(b); b+=2;
550
a_length= (uint) *a++;
551
b_length= (uint) *b++;
553
return field_charset->coll->strnncollsp(field_charset,
560
int Field_blob::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
561
bool insert_or_update)
564
uint32_t a_length, b_length;
565
memcpy(&a,ptr+packlength,sizeof(char*));
567
return key_length_arg > 0 ? -1 : 0;
569
a_length= get_length(ptr);
570
if (key_length_arg > 255)
572
b_length= uint2korr(b); b+=2;
575
b_length= (uint) *b++;
576
return field_charset->coll->strnncollsp(field_charset,
531
582
/** Create a packed key that will be used for storage from a MySQL row. */
534
585
Field_blob::pack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
586
bool low_byte_first __attribute__((unused)))
537
588
unsigned char *save= ptr;
538
589
ptr= (unsigned char*) from;
609
Unpack a blob key into a record buffer.
611
A blob key has a maximum size of 64K-1.
612
In its packed form, the length field is one or two bytes long,
613
depending on 'max_length'.
614
Depending on the maximum length of a blob, its length field is
615
put into 1 to 4 bytes. This is a property of the blob object,
616
described by 'packlength'.
617
Blobs are internally stored apart from the record buffer, which
618
contains a pointer to the blob buffer.
621
@param to Pointer into the record buffer.
622
@param from Pointer to the packed key.
623
@param max_length Key length limit from key description.
626
Pointer into 'from' past the last byte copied from packed key.
629
const unsigned char *
630
Field_blob::unpack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
631
bool low_byte_first __attribute__((unused)))
633
/* get length of the blob key */
634
uint32_t length= *from++;
635
if (max_length > 255)
636
length+= *from++ << 8;
638
/* put the length into the record buffer */
639
put_length(to, length);
641
/* put the address of the blob buffer or NULL */
643
memcpy(to + packlength, &from, sizeof(from));
645
memset(to + packlength, 0, sizeof(from));
647
/* point to first byte of next field in 'from' */
648
return from + length;
652
/** Create a packed key that will be used for storage from a MySQL key. */
655
Field_blob::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
656
bool low_byte_first __attribute__((unused)))
658
uint32_t length=uint2korr(from);
659
if (length > max_length)
661
*to++= (char) (length & 255);
662
if (max_length > 255)
663
*to++= (char) (length >> 8);
665
memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
670
uint32_t Field_blob::packed_col_length(const unsigned char *data_ptr, uint32_t length)
673
return uint2korr(data_ptr)+2;
674
return (uint) *data_ptr + 1;
678
uint32_t Field_blob::max_packed_col_length(uint32_t max_length)
680
return (max_length > 255 ? 2 : 1)+max_length;
684
uint32_t Field_blob::is_equal(Create_field *new_field)
686
if (compare_str_field_flags(new_field, flags))
689
return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
690
&& new_field->charset == field_charset &&
691
((Field_blob *)new_field->field)->max_data_length() ==
558
697
maximum possible display length for blob.