44
44
{ bits-=(bit+1); break; } \
47
/* Size in uint16_t of a Huffman tree for byte compression of 256 byte values. */
47
/* Size in uint16 of a Huffman tree for byte compression of 256 byte values. */
48
48
#define OFFSET_TABLE_SIZE 512
50
50
static uint read_huff_table(MI_BIT_BUFF *bit_buff,MI_DECODE_TREE *decode_tree,
51
uint16_t **decode_table,uchar **intervall_buff,
53
static void make_quick_table(uint16_t *to_table,uint16_t *decode_table,
51
uint16 **decode_table,uchar **intervall_buff,
53
static void make_quick_table(uint16 *to_table,uint16 *decode_table,
54
54
uint *next_free,uint value,uint bits,
56
static void fill_quick_table(uint16_t *table,uint bits, uint max_bits,
56
static void fill_quick_table(uint16 *table,uint bits, uint max_bits,
58
static uint copy_decode_table(uint16_t *to_pos,uint offset,
59
uint16_t *decode_table);
60
static uint find_longest_bitstream(uint16_t *table, uint16_t *end);
58
static uint copy_decode_table(uint16 *to_pos,uint offset,
59
uint16 *decode_table);
60
static uint find_longest_bitstream(uint16 *table, uint16 *end);
61
61
static void (*get_unpack_function(MI_COLUMNDEF *rec))(MI_COLUMNDEF *field,
136
136
uint i,trees,huff_tree_bits,rec_reflength,length;
137
uint16_t *decode_table,*tmp_buff;
137
uint16 *decode_table,*tmp_buff;
138
138
ulong elements,intervall_length;
139
139
uchar *disk_cache;
140
140
uchar *intervall_buff;
141
141
uchar header[HEAD_LENGTH];
142
142
MYISAM_SHARE *share=info->s;
143
143
MI_BIT_BUFF bit_buff;
144
DBUG_ENTER("_mi_read_pack_info");
145
146
if (myisam_quick_table_bits < 4)
146
147
myisam_quick_table_bits=4;
176
177
share->base.min_block_length=share->min_pack_length+1;
177
178
if (share->min_pack_length > 254)
178
179
share->base.min_block_length+=2;
180
DBUG_PRINT("info", ("fixed header length: %u", HEAD_LENGTH));
181
DBUG_PRINT("info", ("total header length: %lu", share->pack.header_length));
182
DBUG_PRINT("info", ("pack file version: %u", share->pack.version));
183
DBUG_PRINT("info", ("min pack length: %lu", share->min_pack_length));
184
DBUG_PRINT("info", ("max pack length: %lu", share->max_pack_length));
185
DBUG_PRINT("info", ("elements of all trees: %lu", elements));
186
DBUG_PRINT("info", ("distinct values bytes: %lu", intervall_length));
187
DBUG_PRINT("info", ("number of code trees: %u", trees));
188
DBUG_PRINT("info", ("bytes for record lgt: %u", share->pack.ref_length));
189
DBUG_PRINT("info", ("record pointer length: %u", rec_reflength));
181
192
Memory segment #1:
198
209
This segment will be reallocated after construction of the tables.
200
211
length=(uint) (elements*2+trees*(1 << myisam_quick_table_bits));
201
if (!(share->decode_tables=(uint16_t*)
202
my_malloc((length + OFFSET_TABLE_SIZE) * sizeof(uint16_t) +
212
if (!(share->decode_tables=(uint16*)
213
my_malloc((length + OFFSET_TABLE_SIZE) * sizeof(uint16) +
203
214
(uint) (share->pack.header_length - sizeof(header)),
204
215
MYF(MY_WME | MY_ZEROFILL))))
223
234
share->rec[i].huff_tree=share->decode_trees+(uint) get_bits(&bit_buff,
225
236
share->rec[i].unpack=get_unpack_function(share->rec+i);
237
DBUG_PRINT("info", ("col: %2u type: %2u pack: %u slbits: %2u",
238
i, share->rec[i].base_type, share->rec[i].pack_type,
239
share->rec[i].space_length_bits));
227
241
skip_to_next_byte(&bit_buff);
235
249
&intervall_buff,tmp_buff))
237
251
/* Reallocate the decoding tables to the used size. */
238
decode_table=(uint16_t*)
252
decode_table=(uint16*)
239
253
my_realloc((uchar*) share->decode_tables,
240
254
(uint) ((uchar*) decode_table - (uchar*) share->decode_tables),
241
255
MYF(MY_HOLD_ON_ERROR));
245
259
share->decode_tables=decode_table;
246
260
for (i=0 ; i < trees ; i++)
247
261
share->decode_trees[i].table=ADD_TO_PTR(share->decode_trees[i].table,
251
265
/* Fix record-ref-length for keys */
254
268
for (i=0 ; i < share->base.keys ; i++)
256
270
MI_KEYDEF *keyinfo= &share->keyinfo[i];
257
keyinfo->keylength+= (uint16_t) diff_length;
258
keyinfo->minlength+= (uint16_t) diff_length;
259
keyinfo->maxlength+= (uint16_t) diff_length;
260
keyinfo->seg[keyinfo->keysegs].length= (uint16_t) rec_reflength;
271
keyinfo->keylength+= (uint16) diff_length;
272
keyinfo->minlength+= (uint16) diff_length;
273
keyinfo->maxlength+= (uint16) diff_length;
274
keyinfo->seg[keyinfo->keysegs].length= (uint16) rec_reflength;
262
276
if (share->ft2_keyinfo.seg)
264
278
MI_KEYDEF *ft2_keyinfo= &share->ft2_keyinfo;
265
ft2_keyinfo->keylength+= (uint16_t) diff_length;
266
ft2_keyinfo->minlength+= (uint16_t) diff_length;
267
ft2_keyinfo->maxlength+= (uint16_t) diff_length;
279
ft2_keyinfo->keylength+= (uint16) diff_length;
280
ft2_keyinfo->minlength+= (uint16) diff_length;
281
ft2_keyinfo->maxlength+= (uint16) diff_length;
271
285
if (bit_buff.error || bit_buff.pos < bit_buff.end)
277
291
my_errno=HA_ERR_WRONG_IN_RECORD;
305
319
static uint read_huff_table(MI_BIT_BUFF *bit_buff, MI_DECODE_TREE *decode_tree,
306
uint16_t **decode_table, uchar **intervall_buff,
320
uint16 **decode_table, uchar **intervall_buff,
309
323
uint min_chr,elements,char_bits,offset_bits,size,intervall_length,table_bits,
310
324
next_free_offset;
326
DBUG_ENTER("read_huff_table");
313
328
if (!get_bits(bit_buff,1))
319
334
offset_bits=get_bits(bit_buff,5);
320
335
intervall_length=0;
337
DBUG_PRINT("info", ("byte value compression"));
338
DBUG_PRINT("info", ("minimum byte value: %u", min_chr));
339
DBUG_PRINT("info", ("number of tree nodes: %u", elements));
340
DBUG_PRINT("info", ("bits for values: %u", char_bits));
341
DBUG_PRINT("info", ("bits for tree offsets: %u", offset_bits));
322
342
if (elements > 256)
344
DBUG_PRINT("error", ("ERROR: illegal number of tree elements: %u",
334
356
offset_bits=get_bits(bit_buff,5);
335
357
decode_tree->quick_table_bits=0;
336
358
ptr= *decode_table;
359
DBUG_PRINT("info", ("distinct column value compression"));
360
DBUG_PRINT("info", ("number of tree nodes: %u", elements));
361
DBUG_PRINT("info", ("value buffer length: %u", intervall_length));
362
DBUG_PRINT("info", ("bits for value index: %u", char_bits));
363
DBUG_PRINT("info", ("bits for tree offsets: %u", offset_bits));
338
365
size=elements*2-2;
366
DBUG_PRINT("info", ("tree size in uint16: %u", size));
367
DBUG_PRINT("info", ("tree size in bytes: %u",
368
size * (uint) sizeof(uint16)));
340
370
for (end=ptr+size ; ptr < end ; ptr++)
342
372
if (get_bit(bit_buff))
344
*ptr= (uint16_t) get_bits(bit_buff,offset_bits);
374
*ptr= (uint16) get_bits(bit_buff,offset_bits);
345
375
if ((ptr + *ptr >= end) || !*ptr)
377
DBUG_PRINT("error", ("ERROR: illegal pointer in decode tree"));
351
*ptr= (uint16_t) (IS_CHAR + (get_bits(bit_buff,char_bits) + min_chr));
382
*ptr= (uint16) (IS_CHAR + (get_bits(bit_buff,char_bits) + min_chr));
353
384
skip_to_next_byte(bit_buff);
360
391
/* Find longest Huffman code from begin to end of tree in bits. */
361
392
table_bits= find_longest_bitstream(tmp_buff, ptr);
362
393
if (table_bits >= OFFSET_TABLE_SIZE)
364
395
if (table_bits > myisam_quick_table_bits)
365
396
table_bits=myisam_quick_table_bits;
397
DBUG_PRINT("info", ("table bits: %u", table_bits));
367
399
next_free_offset= (1 << table_bits);
368
400
make_quick_table(*decode_table,tmp_buff,&next_free_offset,0,table_bits,
431
static void make_quick_table(uint16_t *to_table, uint16_t *decode_table,
463
static void make_quick_table(uint16 *to_table, uint16 *decode_table,
432
464
uint *next_free_offset, uint value, uint bits,
467
DBUG_ENTER("make_quick_table");
436
470
When down the table to the requested maximum, copy the rest of the
442
476
Remaining left Huffman tree segment starts behind quick table.
443
477
Remaining right Huffman tree segment starts behind left segment.
445
to_table[value]= (uint16_t) *next_free_offset;
479
to_table[value]= (uint16) *next_free_offset;
447
481
Re-construct the remaining Huffman tree segment at
448
482
next_free_offset in to_table.
450
484
*next_free_offset= copy_decode_table(to_table, *next_free_offset,
455
489
/* Descent on the left side. Left side bits are clear (0). */
519
static void fill_quick_table(uint16_t *table, uint bits, uint max_bits,
553
static void fill_quick_table(uint16 *table, uint bits, uint max_bits,
557
DBUG_ENTER("fill_quick_table");
525
560
Bits 1..8 of value represent the decoded byte value.
552
587
next free offset from to_pos.
555
static uint copy_decode_table(uint16_t *to_pos, uint offset,
556
uint16_t *decode_table)
590
static uint copy_decode_table(uint16 *to_pos, uint offset,
591
uint16 *decode_table)
558
593
uint prev_offset= offset;
594
DBUG_ENTER("copy_decode_table");
560
596
/* Descent on the left side. */
561
597
if (!(*decode_table & IS_CHAR))
578
614
if (!(*decode_table & IS_CHAR))
580
616
/* Set a pointer to the next free target node. */
581
to_pos[prev_offset+1]=(uint16_t) (offset-prev_offset-1);
617
to_pos[prev_offset+1]=(uint16) (offset-prev_offset-1);
582
618
/* Copy the right hand subtree to the entry of that node. */
583
619
offset=copy_decode_table(to_pos,offset,decode_table+ *decode_table);
615
651
>= OFFSET_TABLE_SIZE Error, broken tree. It does not end before 'end'.
618
static uint find_longest_bitstream(uint16_t *table, uint16_t *end)
654
static uint find_longest_bitstream(uint16 *table, uint16 *end)
623
659
if (!(*table & IS_CHAR))
625
uint16_t *next= table + *table;
661
uint16 *next= table + *table;
626
662
if (next > end || next == table)
664
DBUG_PRINT("error", ("ERROR: illegal pointer in decode tree"));
628
665
return OFFSET_TABLE_SIZE;
630
667
length= find_longest_bitstream(next, end) + 1;
633
670
if (!(*table & IS_CHAR))
635
uint16_t *next= table + *table;
672
uint16 *next= table + *table;
636
673
if (next > end || next == table)
675
DBUG_PRINT("error", ("ERROR: illegal pointer in decode tree"));
638
676
return OFFSET_TABLE_SIZE;
640
678
length2= find_longest_bitstream(next, end) + 1;
663
701
MI_BLOCK_INFO block_info;
703
DBUG_ENTER("mi_read_pack_record");
666
705
if (filepos == HA_OFFSET_ERROR)
667
return(-1); /* _search() didn't find record */
706
DBUG_RETURN(-1); /* _search() didn't find record */
669
708
file=info->dfile;
670
709
if (_mi_pack_get_block_info(info, &info->bit_buff, &block_info,
674
713
block_info.rec_len - block_info.offset, MYF(MY_NABP)))
676
715
info->update|= HA_STATE_AKTIV;
677
return(_mi_pack_rec_unpack(info, &info->bit_buff, buf,
716
DBUG_RETURN(_mi_pack_rec_unpack(info, &info->bit_buff, buf,
678
717
info->rec_buff, block_info.rec_len));
680
719
my_errno=HA_ERR_WRONG_IN_RECORD;
691
730
register MI_COLUMNDEF *end;
692
731
MI_COLUMNDEF *current_field;
693
732
MYISAM_SHARE *share=info->s;
733
DBUG_ENTER("_mi_pack_rec_unpack");
695
735
init_bit_buffer(bit_buff, (uchar*) from, reclength);
705
745
if (!bit_buff->error &&
706
746
bit_buff->pos - bit_buff->bits / 8 == bit_buff->end)
708
748
info->update&= ~HA_STATE_AKTIV;
709
return(my_errno=HA_ERR_WRONG_IN_RECORD);
749
DBUG_RETURN(my_errno=HA_ERR_WRONG_IN_RECORD);
710
750
} /* _mi_pack_rec_unpack */
771
811
uchar *to, uchar *end)
773
813
if (get_bit(bit_buff))
774
memset((char*) to, 0, (uint) (end-to));
814
bzero((char*) to,(uint) (end-to));
777
817
end-=rec->space_length_bits;
778
818
decode_bytes(rec,bit_buff,to,end);
779
memset((char*) end, 0, rec->space_length_bits);
819
bzero((char*) end,rec->space_length_bits);
786
826
if (get_bit(bit_buff))
787
memset((char*) to, 0, (uint) (end-to));
827
bzero((char*) to,(uint) (end-to));
789
829
decode_bytes(rec,bit_buff,to,end);
795
835
if (get_bit(bit_buff))
796
memset((uchar*) to, ' ', (end-to));
836
bfill((uchar*) to,(end-to),' ');
798
838
decode_bytes(rec,bit_buff,to,end);
816
856
if (to+spaces != end)
817
857
decode_bytes(rec,bit_buff,to,end-spaces);
818
memset((uchar*) end-spaces, ' ', spaces);
858
bfill((uchar*) end-spaces,spaces,' ');
821
861
decode_bytes(rec,bit_buff,to,end);
836
876
if (to+spaces != end)
837
877
decode_bytes(rec,bit_buff,to,end-spaces);
838
memset((uchar*) end-spaces, ' ', spaces);
878
bfill((uchar*) end-spaces,spaces,' ');
841
881
decode_bytes(rec,bit_buff,to,end);
848
888
if (get_bit(bit_buff))
849
memset((uchar*) to, ' ', (end-to));
889
bfill((uchar*) to,(end-to),' ');
852
892
if ((spaces=get_bits(bit_buff,rec->space_length_bits))+to > end)
857
897
if (to+spaces != end)
858
898
decode_bytes(rec,bit_buff,to,end-spaces);
859
memset((uchar*) end-spaces, ' ', spaces);
899
bfill((uchar*) end-spaces,spaces,' ');
872
912
if (to+spaces != end)
873
913
decode_bytes(rec,bit_buff,to,end-spaces);
874
memset((uchar*) end-spaces, ' ', spaces);
914
bfill((uchar*) end-spaces,spaces,' ');
877
917
static void uf_space_prespace_selected(MI_COLUMNDEF *rec, MI_BIT_BUFF *bit_buff,
889
929
bit_buff->error=1;
892
memset((uchar*) to, ' ', spaces);
932
bfill((uchar*) to,spaces,' ');
893
933
if (to+spaces != end)
894
934
decode_bytes(rec,bit_buff,to+spaces,end);
910
950
bit_buff->error=1;
913
memset((uchar*) to, ' ', spaces);
953
bfill((uchar*) to,spaces,' ');
914
954
if (to+spaces != end)
915
955
decode_bytes(rec,bit_buff,to+spaces,end);
926
966
if (get_bit(bit_buff))
927
memset((uchar*) to, ' ', (end-to));
967
bfill((uchar*) to,(end-to),' ');
930
970
if ((spaces=get_bits(bit_buff,rec->space_length_bits))+to > end)
932
972
bit_buff->error=1;
935
memset((uchar*) to, ' ', spaces);
975
bfill((uchar*) to,spaces,' ');
936
976
if (to+spaces != end)
937
977
decode_bytes(rec,bit_buff,to+spaces,end);
947
987
bit_buff->error=1;
950
memset((uchar*) to, ' ', spaces);
990
bfill((uchar*) to,spaces,' ');
951
991
if (to+spaces != end)
952
992
decode_bytes(rec,bit_buff,to+spaces,end);
958
998
end-=rec->space_length_bits;
959
999
decode_bytes(rec,bit_buff,(uchar*) to,end);
960
memset((char*) end, 0, rec->space_length_bits);
1000
bzero((char*) end,rec->space_length_bits);
963
1003
static void uf_constant(MI_COLUMNDEF *rec,
983
1023
MI_BIT_BUFF *bit_buff __attribute__((unused)),
984
1024
uchar *to, uchar *end)
986
memset((char*) to, 0, (uint) (end-to));
1026
bzero((char*) to,(uint) (end-to));
989
1029
static void uf_blob(MI_COLUMNDEF *rec, MI_BIT_BUFF *bit_buff,
990
1030
uchar *to, uchar *end)
992
1032
if (get_bit(bit_buff))
993
memset((uchar*) to, 0, (end-to));
1033
bzero((uchar*) to,(end-to));
996
1036
ulong length=get_bits(bit_buff,rec->space_length_bits);
998
1038
if (bit_buff->blob_pos+length > bit_buff->blob_end)
1000
1040
bit_buff->error=1;
1001
memset((uchar*) to, 0, (end-to));
1041
bzero((uchar*) to,(end-to));
1004
1044
decode_bytes(rec,bit_buff,bit_buff->blob_pos,bit_buff->blob_pos+length);
1005
1045
_my_store_blob_length((uchar*) to,pack_length,length);
1006
memcpy((char*) to+pack_length,(char*) &bit_buff->blob_pos,
1046
memcpy_fixed((char*) to+pack_length,(char*) &bit_buff->blob_pos,
1008
1048
bit_buff->blob_pos+=length;
1273
1314
&info->rec_buff, info->dfile, filepos);
1275
1316
goto err; /* Error code is already set */
1318
if (block_info.rec_len > share->max_pack_length)
1320
my_errno=HA_ERR_WRONG_IN_RECORD;
1277
1325
if (info->opt_flag & READ_CACHE_USED)
1293
1341
info->nextpos=block_info.filepos+block_info.rec_len;
1294
1342
info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
1296
return (_mi_pack_rec_unpack(info, &info->bit_buff, buf,
1344
DBUG_RETURN (_mi_pack_rec_unpack(info, &info->bit_buff, buf,
1297
1345
info->rec_buff, block_info.rec_len));
1347
DBUG_RETURN(my_errno);
1319
1367
VOID(my_seek(file,filepos,MY_SEEK_SET,MYF(0)));
1320
1368
if (my_read(file, header,ref_length,MYF(MY_NABP)))
1321
1369
return BLOCK_FATAL_ERROR;
1370
DBUG_DUMP("header",(uchar*) header,ref_length);
1323
1372
head_length= read_pack_length((uint) myisam->s->pack.version, header,
1324
1373
&info->rec_len);
1433
1482
my_bool _mi_memmap_file(MI_INFO *info)
1435
1484
MYISAM_SHARE *share=info->s;
1485
DBUG_ENTER("mi_memmap_file");
1437
1487
if (!info->s->file_map)
1439
1489
if (my_seek(info->dfile,0L,MY_SEEK_END,MYF(0)) <
1440
1490
share->state.state.data_file_length+MEMMAP_EXTRA_MARGIN)
1492
DBUG_PRINT("warning",("File isn't extended for memmap"));
1444
1495
if (mi_dynmap_file(info, share->state.state.data_file_length))
1447
1498
info->opt_flag|= MEMMAP_USED;
1448
1499
info->read_record= share->read_record= _mi_read_mempack_record;
1449
1500
share->read_rnd= _mi_read_rnd_mempack_record;
1484
1535
MI_BLOCK_INFO block_info;
1485
1536
MYISAM_SHARE *share=info->s;
1538
DBUG_ENTER("mi_read_mempack_record");
1488
1540
if (filepos == HA_OFFSET_ERROR)
1489
return(-1); /* _search() didn't find record */
1541
DBUG_RETURN(-1); /* _search() didn't find record */
1491
1543
if (!(pos= (uchar*) _mi_mempack_get_block_info(info, &info->bit_buff,
1492
1544
&block_info, &info->rec_buff,
1493
1545
(uchar*) share->file_map+
1496
return(_mi_pack_rec_unpack(info, &info->bit_buff, buf,
1548
DBUG_RETURN(_mi_pack_rec_unpack(info, &info->bit_buff, buf,
1497
1549
pos, block_info.rec_len));
1519
1572
(start=share->file_map+
1576
if (block_info.rec_len > info->s->max_pack_length)
1578
my_errno=HA_ERR_WRONG_IN_RECORD;
1522
1582
info->packed_length=block_info.rec_len;
1523
1583
info->lastpos=filepos;
1524
1584
info->nextpos=filepos+(uint) (pos-start)+block_info.rec_len;
1525
1585
info->update|= HA_STATE_AKTIV | HA_STATE_KEY_CHANGED;
1527
return (_mi_pack_rec_unpack(info, &info->bit_buff, buf,
1587
DBUG_RETURN (_mi_pack_rec_unpack(info, &info->bit_buff, buf,
1528
1588
pos, block_info.rec_len));
1590
DBUG_RETURN(my_errno);
1533
1593
#endif /* HAVE_MMAP */
1550
1610
*(uchar*) block_buff=255;
1551
1611
if (version == 1) /* old format */
1553
assert(length <= 0xFFFFFF);
1613
DBUG_ASSERT(length <= 0xFFFFFF);
1554
1614
int3store(block_buff + 1, (ulong) length);