22
#include <drizzled/server_includes.h>
23
23
#include <drizzled/field/blob.h>
24
24
#include <drizzled/table.h>
25
25
#include <drizzled/session.h>
26
#include "plugin/myisam/myisam.h"
31
29
using namespace std;
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)));
32
blob_pack_length_to_max_length(uint32_t arg)
34
return (uint32_t)cmax(UINT32_MAX,
35
(INT64_C(1) << cmin(arg, 4U) * 8) - INT64_C(1));
43
39
/****************************************************************************
45
41
** 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)
42
** packlength slot and may be from 1-4.
47
43
****************************************************************************/
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)),
45
Field_blob::Field_blob(unsigned char *ptr_arg, unsigned char *null_ptr_arg, unsigned char null_bit_arg,
46
enum utype unireg_check_arg, const char *field_name_arg,
47
TableShare *share, uint32_t blob_pack_length,
48
const CHARSET_INFO * const cs)
49
:Field_str(ptr_arg, blob_pack_length_to_max_length(blob_pack_length),
50
null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
52
packlength(blob_pack_length)
63
55
share->blob_fields++;
64
/* TODO: why do not fill table->getShare()->blob_field array here? */
56
/* TODO: why do not fill table->s->blob_field array here? */
67
60
void Field_blob::store_length(unsigned char *i_ptr,
61
uint32_t i_packlength,
69
63
bool low_byte_first)
71
65
#ifndef WORDS_BIGENDIAN
72
66
(void)low_byte_first;
75
#ifdef WORDS_BIGENDIAN
78
int4store(i_ptr,i_number);
68
switch (i_packlength) {
70
i_ptr[0]= (unsigned char) i_number;
73
#ifdef WORDS_BIGENDIAN
76
int2store(i_ptr,(unsigned short) i_number);
80
shortstore(i_ptr,(unsigned short) i_number);
83
int3store(i_ptr,i_number);
86
#ifdef WORDS_BIGENDIAN
89
int4store(i_ptr,i_number);
93
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)
98
void Field_blob::store_length(unsigned char *i_ptr, uint32_t i_packlength,
88
store_length(i_ptr, i_number, getTable()->getShare()->db_low_byte_first);
101
store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
92
105
uint32_t Field_blob::get_length(const unsigned char *pos,
106
uint32_t packlength_arg,
93
107
bool low_byte_first)
95
109
#ifndef WORDS_BIGENDIAN
96
110
(void)low_byte_first;
99
#ifdef WORDS_BIGENDIAN
105
return (uint32_t) tmp;
112
switch (packlength_arg) {
114
return (uint32_t) pos[0];
118
#ifdef WORDS_BIGENDIAN
124
return (uint32_t) tmp;
127
return (uint32_t) uint3korr(pos);
131
#ifdef WORDS_BIGENDIAN
137
return (uint32_t) tmp;
140
return 0; // Impossible
109
144
uint32_t Field_blob::get_packed_size(const unsigned char *ptr_arg,
110
145
bool low_byte_first)
112
return sizeof(uint32_t) + get_length(ptr_arg, low_byte_first);
147
return packlength + get_length(ptr_arg, packlength, low_byte_first);
116
151
uint32_t Field_blob::get_length(uint32_t row_offset)
118
return get_length(ptr+row_offset,
119
getTable()->getShare()->db_low_byte_first);
153
return get_length(ptr+row_offset, this->packlength,
154
table->s->db_low_byte_first);
123
158
uint32_t Field_blob::get_length(const unsigned char *ptr_arg)
125
return get_length(ptr_arg, getTable()->getShare()->db_low_byte_first);
160
return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first);
130
165
Put a blob length field into a record buffer.
132
Blob length is always stored in sizeof(uint32_t) (4 bytes)
167
Depending on the maximum length of a blob, its length field is
168
put into 1 to 4 bytes. This is a property of the blob object,
169
described by 'packlength'.
134
171
@param pos Pointer into the record buffer.
135
172
@param length The length value to put.
321
365
int Field_blob::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
326
370
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);
371
memcpy(&a,a_ptr+packlength,sizeof(char*));
372
memcpy(&b,b_ptr+packlength,sizeof(char*));
373
a_length=get_length(a_ptr);
332
374
if (a_length > max_length)
333
a_length= max_length;
335
b_length= get_length(b_ptr);
376
b_length=get_length(b_ptr);
337
377
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);
379
diff=memcmp(a,b,cmin(a_length,b_length));
380
return diff ? diff : (int) (a_length - b_length);
345
384
/* The following is used only when comparing a key */
346
uint32_t Field_blob::get_key_image(unsigned char *buff, uint32_t length)
386
uint32_t Field_blob::get_key_image(unsigned char *buff,
348
390
uint32_t blob_length= get_length(ptr);
349
391
unsigned char *blob;
397
441
return HA_KEY_BLOB_LENGTH+length;
400
445
void Field_blob::set_key_image(const unsigned char *buff,uint32_t length)
402
447
length= uint2korr(buff);
403
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length, field_charset);
448
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
406
453
int Field_blob::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
408
455
unsigned char *blob1;
409
456
uint32_t blob_length=get_length(ptr);
410
memcpy(&blob1,ptr+sizeof(uint32_t),sizeof(char*));
457
memcpy(&blob1,ptr+packlength,sizeof(char*));
411
458
const CHARSET_INFO * const cs= charset();
412
459
uint32_t local_char_length= max_key_length / cs->mbmaxlen;
413
460
local_char_length= my_charpos(cs, blob1, blob1+blob_length,
424
471
b+HA_KEY_BLOB_LENGTH, uint2korr(b));
476
Save the field metadata for blob fields.
478
Saves the pack length in the first byte of the field metadata array
479
at index of *metadata_ptr.
481
@param metadata_ptr First byte of field metadata
483
@returns number of bytes written to metadata_ptr
485
int Field_blob::do_save_field_metadata(unsigned char *metadata_ptr)
487
*metadata_ptr= pack_length_no_ptr();
427
492
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)));
494
return (uint32_t) (current_session->variables.max_sort_length +
495
(field_charset == &my_charset_bin ? 0 : packlength));
433
499
void Field_blob::sort_string(unsigned char *to,uint32_t length)
435
501
unsigned char *blob;
447
513
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);
518
switch (packlength) {
520
*pos= (char) blob_length;
523
mi_int2store(pos, blob_length);
526
mi_int3store(pos, blob_length);
529
mi_int4store(pos, blob_length);
454
memcpy(&blob,ptr+sizeof(uint32_t),sizeof(char*));
533
memcpy(&blob,ptr+packlength,sizeof(char*));
456
535
blob_length=my_strnxfrm(field_charset,
457
536
to, length, blob, blob_length);
475
556
unsigned char *Field_blob::pack(unsigned char *to, const unsigned char *from,
476
uint32_t max_length, bool low_byte_first)
557
uint32_t max_length, bool low_byte_first)
478
559
unsigned char *save= ptr;
479
560
ptr= (unsigned char*) from;
480
uint32_t length= get_length(); // Length of from string
561
uint32_t length=get_length(); // Length of from string
483
564
Store max length, which will occupy packlength bytes. If the max
484
565
length given is smaller than the actual length of the blob, we
485
566
just store the initial bytes of the blob.
487
store_length(to, min(length, max_length), low_byte_first);
568
store_length(to, packlength, cmin(length, max_length), low_byte_first);
490
571
Store the actual blob data, which will occupy 'length' bytes.
517
598
@return New pointer into memory based on from + length of the data
519
600
const unsigned char *Field_blob::unpack(unsigned char *,
520
const unsigned char *from,
601
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),
605
uint32_t const master_packlength=
606
param_data > 0 ? param_data & 0xFF : packlength;
607
uint32_t const length= get_length(from, master_packlength, low_byte_first);
608
table->setWriteSet(field_index);
609
store(reinterpret_cast<const char*>(from) + master_packlength,
527
610
length, field_charset);
528
return(from + sizeof(uint32_t) + length);
611
return(from + master_packlength + length);
531
614
/** Create a packed key that will be used for storage from a MySQL row. */