-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathha_archive.cc
1612 lines (1323 loc) · 47.1 KB
/
ha_archive.cc
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, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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 "storage/archive/ha_archive.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <mysql/plugin.h>
#include "lex_string.h"
#include "m_string.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_dir.h"
#include "my_psi_config.h"
#include "myisam.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_file.h"
#include "mysql/psi/mysql_memory.h"
#include "nulls.h"
#include "sql/derror.h"
#include "sql/field.h"
#include "sql/sql_class.h"
#include "sql/sql_table.h"
#include "sql/system_variables.h"
#include "sql/table.h"
#include "template_utils.h"
/*
First, if you want to understand storage engines you should look at
ha_example.cc and ha_example.h.
This example was written as a test case for a customer who needed
a storage engine without indexes that could compress data very well.
So, welcome to a completely compressed storage engine. This storage
engine only does inserts. No replace, deletes, or updates. All reads are
complete table scans. Compression is done through a combination of packing
and making use of the zlib library
We keep a file pointer open for each instance of ha_archive for each read
but for writes we keep one open file handle just for that. We flush it
only if we have a read occur. azip handles compressing lots of records
at once much better then doing lots of little records between writes.
It is possible to not lock on writes but this would then mean we couldn't
handle bulk inserts as well (that is if someone was trying to read at
the same time since we would want to flush).
A "meta" file is kept alongside the data file. This file serves two purpose.
The first purpose is to track the number of rows in the table. The second
purpose is to determine if the table was closed properly or not. When the
meta file is first opened it is marked as dirty. It is opened when the table
itself is opened for writing. When the table is closed the new count for rows
is written to the meta file and the file is marked as clean. If the meta file
is opened and it is marked as dirty, it is assumed that a crash occurred. At
this point an error occurs and the user is told to rebuild the file.
A rebuild scans the rows and rewrites the meta file. If corruption is found
in the data file then the meta file is not repaired.
At some point a recovery method for such a drastic case needs to be divised.
Locks are row level, and you will get a consistent read.
For performance as far as table scans go it is quite fast. I don't have
good numbers but locally it has out performed both Innodb and MyISAM. For
Innodb the question will be if the table can be fit into the buffer
pool. For MyISAM its a question of how much the file system caches the
MyISAM file. With enough free memory MyISAM is faster. Its only when the OS
doesn't have enough memory to cache entire table that archive turns out
to be any faster.
Examples between MyISAM (packed) and Archive.
Table with 76695844 identical rows:
29680807 a_archive.ARZ
920350317 a.MYD
Table with 8991478 rows (all of Slashdot's comments):
1922964506 comment_archive.ARZ
2944970297 comment_text.MYD
TODO:
Allow users to set compression level.
Allow adjustable block size.
Implement versioning, should be easy.
Allow for errors, find a way to mark bad rows.
Add optional feature so that rows can be flushed at interval (which will
cause less compression but may speed up ordered searches). Checkpoint the meta
file to allow for faster rebuilds. Option to allow for dirty reads, this would
lower the sync calls, which would make inserts a lot faster, but would mean
highly arbitrary reads.
-Brian
Archive file format versions:
<5.1.5 - v.1
5.1.5-5.1.15 - v.2
>5.1.15 - v.3
*/
/* The file extension */
#define ARZ ".ARZ" // The data file
#define ARN ".ARN" // Files used during an optimize call
#define ARM ".ARM" // Meta file (deprecated)
/* 5.0 compatibility */
#define META_V1_OFFSET_CHECK_HEADER 0
#define META_V1_OFFSET_VERSION 1
#define META_V1_OFFSET_ROWS_RECORDED 2
#define META_V1_OFFSET_CHECK_POINT 10
#define META_V1_OFFSET_CRASHED 18
#define META_V1_LENGTH 19
/*
uchar + uchar
*/
#define DATA_BUFFER_SIZE 2 // Size of the data used in the data file
#define ARCHIVE_CHECK_HEADER 254 // The number we use to determine corruption
extern "C" PSI_file_key arch_key_file_data;
/* Static declarations for handerton */
static handler *archive_create_handler(handlerton *hton, TABLE_SHARE *table,
bool partitioned, MEM_ROOT *mem_root);
/*
Number of rows that will force a bulk insert.
*/
#define ARCHIVE_MIN_ROWS_TO_USE_BULK_INSERT 2
/*
Size of header used for row
*/
#define ARCHIVE_ROW_HEADER_SIZE 4
static handler *archive_create_handler(handlerton *hton, TABLE_SHARE *table,
bool, MEM_ROOT *mem_root) {
return new (mem_root) ha_archive(hton, table);
}
PSI_memory_key az_key_memory_frm;
PSI_memory_key az_key_memory_record_buffer;
PSI_mutex_key az_key_mutex_Archive_share_mutex;
#ifdef HAVE_PSI_MUTEX_INTERFACE
static PSI_mutex_info all_archive_mutexes[] = {
{&az_key_mutex_Archive_share_mutex, "Archive_share::mutex", 0, 0,
PSI_DOCUMENT_ME}};
#endif /* HAVE_PSI_MUTEX_INTERFACE */
PSI_file_key arch_key_file_data;
#ifdef HAVE_PSI_FILE_INTERFACE
PSI_file_key arch_key_file_metadata, arch_key_file_frm;
static PSI_file_info all_archive_files[] = {
{&arch_key_file_metadata, "metadata", 0, 0, PSI_DOCUMENT_ME},
{&arch_key_file_data, "data", 0, 0, PSI_DOCUMENT_ME},
{&arch_key_file_frm, "FRM", 0, 0, PSI_DOCUMENT_ME}};
#endif /* HAVE_PSI_FILE_INTERFACE */
#ifdef HAVE_PSI_MEMORY_INTERFACE
static PSI_memory_info all_archive_memory[] = {
{&az_key_memory_frm, "FRM", 0, 0, PSI_DOCUMENT_ME},
{&az_key_memory_record_buffer, "record_buffer", 0, 0, PSI_DOCUMENT_ME},
};
#endif /* HAVE_PSI_MEMORY_INTERFACE */
static void init_archive_psi_keys(void) {
const char *category [[maybe_unused]] = "archive";
int count [[maybe_unused]];
#ifdef HAVE_PSI_MUTEX_INTERFACE
count = static_cast<int>(array_elements(all_archive_mutexes));
mysql_mutex_register(category, all_archive_mutexes, count);
#endif /* HAVE_PSI_MUTEX_INTERFACE */
#ifdef HAVE_PSI_FILE_INTERFACE
count = static_cast<int>(array_elements(all_archive_files));
mysql_file_register(category, all_archive_files, count);
#endif /* HAVE_PSI_FILE_INTERFACE */
#ifdef HAVE_PSI_MEMORY_INTERFACE
count = static_cast<int>(array_elements(all_archive_memory));
mysql_memory_register(category, all_archive_memory, count);
#endif /* HAVE_PSI_MEMORY_INTERFACE */
}
/*
We just implement one additional file extension.
*/
static const char *ha_archive_exts[] = {ARZ, NullS};
/*
Initialize the archive handler.
SYNOPSIS
archive_db_init()
void *
RETURN
false OK
true Error
*/
static int archive_db_init(void *p) {
DBUG_TRACE;
handlerton *archive_hton;
#ifdef HAVE_PSI_INTERFACE
init_archive_psi_keys();
#endif
archive_hton = (handlerton *)p;
archive_hton->state = SHOW_OPTION_YES;
archive_hton->db_type = DB_TYPE_ARCHIVE_DB;
archive_hton->create = archive_create_handler;
archive_hton->flags = HTON_NO_FLAGS;
archive_hton->file_extensions = ha_archive_exts;
archive_hton->rm_tmp_tables = default_rm_tmp_tables;
return 0;
}
Archive_share::Archive_share() {
crashed = false;
in_optimize = false;
archive_write_open = false;
dirty = false;
DBUG_PRINT("ha_archive", ("Archive_share: %p", this));
thr_lock_init(&lock);
/*
We will use this lock for rows.
*/
mysql_mutex_init(az_key_mutex_Archive_share_mutex, &mutex,
MY_MUTEX_INIT_FAST);
}
ha_archive::ha_archive(handlerton *hton, TABLE_SHARE *table_arg)
: handler(hton, table_arg), share(nullptr), bulk_insert(false) {
/* Set our original buffer from pre-allocated memory */
buffer.set((char *)byte_buffer, IO_SIZE, system_charset_info);
/* The size of the offset value we will use for position() */
ref_length = sizeof(my_off_t);
archive_reader_open = false;
}
static void save_auto_increment(TABLE *table, ulonglong *value) {
Field *field = table->found_next_number_field;
ulonglong auto_value = (ulonglong)field->val_int(
table->record[0] + field->offset(table->record[0]));
if (*value <= auto_value) *value = auto_value + 1;
}
/**
@brief Read version 1 meta file (5.0 compatibility routine).
@return Completion status
@retval 0 Success
@retval !0 Failure
*/
int Archive_share::read_v1_metafile() {
char file_name[FN_REFLEN];
uchar buf[META_V1_LENGTH];
File fd;
DBUG_TRACE;
fn_format(file_name, data_file_name, "", ARM, MY_REPLACE_EXT);
if ((fd = mysql_file_open(arch_key_file_metadata, file_name, O_RDONLY,
MYF(0))) == -1)
return -1;
if (mysql_file_read(fd, buf, sizeof(buf), MYF(0)) != sizeof(buf)) {
mysql_file_close(fd, MYF(0));
return -1;
}
rows_recorded = uint8korr(buf + META_V1_OFFSET_ROWS_RECORDED);
crashed = buf[META_V1_OFFSET_CRASHED];
mysql_file_close(fd, MYF(0));
return 0;
}
/**
@brief Write version 1 meta file (5.0 compatibility routine).
@return Completion status
@retval 0 Success
@retval !0 Failure
*/
int Archive_share::write_v1_metafile() {
char file_name[FN_REFLEN];
uchar buf[META_V1_LENGTH];
File fd;
DBUG_TRACE;
buf[META_V1_OFFSET_CHECK_HEADER] = ARCHIVE_CHECK_HEADER;
buf[META_V1_OFFSET_VERSION] = 1;
int8store(buf + META_V1_OFFSET_ROWS_RECORDED, rows_recorded);
int8store(buf + META_V1_OFFSET_CHECK_POINT, (ulonglong)0);
buf[META_V1_OFFSET_CRASHED] = crashed;
fn_format(file_name, data_file_name, "", ARM, MY_REPLACE_EXT);
if ((fd = mysql_file_open(arch_key_file_metadata, file_name, O_WRONLY,
MYF(0))) == -1)
return -1;
if (mysql_file_write(fd, buf, sizeof(buf), MYF(0)) != sizeof(buf)) {
mysql_file_close(fd, MYF(0));
return -1;
}
mysql_file_close(fd, MYF(0));
return 0;
}
/**
@brief Pack version 1 row (5.0 compatibility routine).
@param[in] record the record to pack
@return Length of packed row
*/
unsigned int ha_archive::pack_row_v1(uchar *record) {
uint *blob, *end;
uchar *pos;
DBUG_TRACE;
memcpy(record_buffer->buffer, record, table->s->reclength);
pos = record_buffer->buffer + table->s->reclength;
for (blob = table->s->blob_field, end = blob + table->s->blob_fields;
blob != end; blob++) {
Field_blob *field = down_cast<Field_blob *>(table->field[*blob]);
const uint32 length = field->get_length();
if (length) {
memcpy(pos, field->get_blob_data(), length);
pos += length;
}
}
return pos - record_buffer->buffer;
}
/*
This method reads the header of a datafile and returns whether or not it was
successful.
*/
int ha_archive::read_data_header(azio_stream *file_to_read) {
int error;
size_t ret;
uchar data_buffer[DATA_BUFFER_SIZE];
DBUG_TRACE;
if (azrewind(file_to_read) == -1) return HA_ERR_CRASHED_ON_USAGE;
if (file_to_read->version >= 3) return 0;
/* Everything below this is just legacy to version 2< */
DBUG_PRINT("ha_archive", ("Reading legacy data header"));
ret = azread(file_to_read, data_buffer, DATA_BUFFER_SIZE, &error);
if (ret != DATA_BUFFER_SIZE) {
DBUG_PRINT("ha_archive",
("Reading, expected %d got %zu", DATA_BUFFER_SIZE, ret));
return 1;
}
if (error) {
DBUG_PRINT("ha_archive", ("Compression error (%d)", error));
return 1;
}
DBUG_PRINT("ha_archive", ("Check %u", data_buffer[0]));
DBUG_PRINT("ha_archive", ("Version %u", data_buffer[1]));
if ((data_buffer[0] != (uchar)ARCHIVE_CHECK_HEADER) &&
(data_buffer[1] != (uchar)ARCHIVE_VERSION))
return HA_ERR_CRASHED_ON_USAGE;
return 0;
}
/*
We create the shared memory space that we will use for the open table.
No matter what we try to get or create a share. This is so that a repair
table operation can occur.
See ha_example.cc for a longer description.
*/
Archive_share *ha_archive::get_share(const char *table_name, int *rc) {
Archive_share *tmp_share;
DBUG_TRACE;
lock_shared_ha_data();
if (!(tmp_share = static_cast<Archive_share *>(get_ha_share_ptr()))) {
azio_stream archive_tmp;
tmp_share = new Archive_share;
if (!tmp_share) {
*rc = HA_ERR_OUT_OF_MEM;
goto err;
}
DBUG_PRINT("ha_archive", ("new Archive_share: %p", tmp_share));
fn_format(tmp_share->data_file_name, table_name, "", ARZ,
MY_REPLACE_EXT | MY_UNPACK_FILENAME);
my_stpcpy(tmp_share->table_name, table_name);
DBUG_PRINT("ha_archive", ("Data File %s", tmp_share->data_file_name));
/*
We read the meta file, but do not mark it dirty. Since we are not
doing a write we won't mark it dirty (and we won't open it for
anything but reading... open it for write and we will generate null
compression writes).
*/
if (!(azopen(&archive_tmp, tmp_share->data_file_name, O_RDONLY))) {
delete tmp_share;
*rc = my_errno() ? my_errno() : HA_ERR_CRASHED;
tmp_share = nullptr;
goto err;
}
stats.auto_increment_value = archive_tmp.auto_increment + 1;
tmp_share->rows_recorded = (ha_rows)archive_tmp.rows;
tmp_share->crashed = archive_tmp.dirty;
share = tmp_share;
if (archive_tmp.version == 1) share->read_v1_metafile();
azclose(&archive_tmp);
set_ha_share_ptr(static_cast<Handler_share *>(tmp_share));
}
if (tmp_share->crashed) *rc = HA_ERR_CRASHED_ON_USAGE;
err:
unlock_shared_ha_data();
assert(tmp_share || *rc);
return tmp_share;
}
int Archive_share::init_archive_writer() {
DBUG_TRACE;
/*
It is expensive to open and close the data files and since you can't have
a gzip file that can be both read and written we keep a writer open
that is shared among all open tables.
*/
if (!(azopen(&archive_write, data_file_name, O_RDWR))) {
DBUG_PRINT("ha_archive", ("Could not open archive write file"));
crashed = true;
return 1;
}
archive_write_open = true;
return 0;
}
void Archive_share::close_archive_writer() {
mysql_mutex_assert_owner(&mutex);
if (archive_write_open) {
if (archive_write.version == 1) (void)write_v1_metafile();
azclose(&archive_write);
archive_write_open = false;
dirty = false;
}
}
/*
No locks are required because it is associated with just one handler instance
*/
int ha_archive::init_archive_reader() {
DBUG_TRACE;
/*
It is expensive to open and close the data files and since you can't have
a gzip file that can be both read and written we keep a writer open
that is shared among all open tables, but have one reader open for
each handler instance.
*/
if (!archive_reader_open) {
if (!(azopen(&archive, share->data_file_name, O_RDONLY))) {
DBUG_PRINT("ha_archive", ("Could not open archive read file"));
share->crashed = true;
return 1;
}
archive_reader_open = true;
}
return 0;
}
/*
When opening a file we:
Create/get our shared structure.
Init out lock.
We open the file we will read from.
*/
int ha_archive::open(const char *name, int, uint open_options,
const dd::Table *) {
int rc = 0;
DBUG_TRACE;
DBUG_PRINT("ha_archive",
("archive table was opened for crash: %s",
(open_options & HA_OPEN_FOR_REPAIR) ? "yes" : "no"));
share = get_share(name, &rc);
if (!share) return rc;
/* Allow open on crashed table in repair mode only. */
switch (rc) {
case 0:
break;
case HA_ERR_CRASHED_ON_USAGE:
if (open_options & HA_OPEN_FOR_REPAIR) break;
[[fallthrough]];
default:
return rc;
}
record_buffer =
create_record_buffer(table->s->reclength + ARCHIVE_ROW_HEADER_SIZE);
if (!record_buffer) return HA_ERR_OUT_OF_MEM;
thr_lock_data_init(&share->lock, &lock, nullptr);
DBUG_PRINT("ha_archive", ("archive table was crashed %s",
rc == HA_ERR_CRASHED_ON_USAGE ? "yes" : "no"));
if (rc == HA_ERR_CRASHED_ON_USAGE && open_options & HA_OPEN_FOR_REPAIR) {
return 0;
}
return rc;
}
/*
Closes the file.
SYNOPSIS
close();
IMPLEMENTATION:
We first close this storage engines file handle to the archive and
then remove our reference count to the table (and possibly free it
as well).
RETURN
0 ok
1 Error
*/
int ha_archive::close(void) {
int rc = 0;
DBUG_TRACE;
destroy_record_buffer(record_buffer);
if (archive_reader_open) {
if (azclose(&archive)) rc = 1;
}
return rc;
}
/*
We create our data file here. The format is pretty simple.
You can read about the format of the data file above.
Unlike other storage engines we do not "pack" our data. Since we
are about to do a general compression, packing would just be a waste of
CPU time. If the table has blobs they are written after the row in the order
of creation.
*/
int ha_archive::create(const char *name, TABLE *table_arg,
HA_CREATE_INFO *create_info, dd::Table *table_def) {
char name_buff[FN_REFLEN];
char linkname[FN_REFLEN];
int error;
azio_stream create_stream; /* Archive file we are working with */
MY_STAT file_stat; // Stat information for the data file
DBUG_TRACE;
stats.auto_increment_value = create_info->auto_increment_value;
for (uint key = 0; key < table_arg->s->keys; key++) {
KEY *pos = table_arg->key_info + key;
KEY_PART_INFO *key_part = pos->key_part;
KEY_PART_INFO *key_part_end = key_part + pos->user_defined_key_parts;
for (; key_part != key_part_end; key_part++) {
Field *field = key_part->field;
if (!field->is_flag_set(AUTO_INCREMENT_FLAG)) {
error = -1;
DBUG_PRINT("ha_archive", ("Index error in creating archive table"));
goto error;
}
}
}
/*
We reuse name_buff since it is available.
*/
#ifndef _WIN32
if (my_enable_symlinks && create_info->data_file_name &&
create_info->data_file_name[0] != '#') {
DBUG_PRINT("ha_archive", ("archive will create stream file %s",
create_info->data_file_name));
fn_format(name_buff, create_info->data_file_name, "", ARZ,
MY_REPLACE_EXT | MY_UNPACK_FILENAME);
fn_format(linkname, name, "", ARZ, MY_REPLACE_EXT | MY_UNPACK_FILENAME);
} else
#endif /* !_WIN32 */
{
if (create_info->data_file_name) {
push_warning_printf(table_arg->in_use, Sql_condition::SL_WARNING,
WARN_OPTION_IGNORED, ER_DEFAULT(WARN_OPTION_IGNORED),
"DATA DIRECTORY");
}
fn_format(name_buff, name, "", ARZ, MY_REPLACE_EXT | MY_UNPACK_FILENAME);
linkname[0] = 0;
}
/* Archive engine never uses INDEX DIRECTORY. */
if (create_info->index_file_name) {
push_warning_printf(table_arg->in_use, Sql_condition::SL_WARNING,
WARN_OPTION_IGNORED, ER_DEFAULT(WARN_OPTION_IGNORED),
"INDEX DIRECTORY");
}
/*
There is a chance that the file was "discovered". In this case
just use whatever file is there.
*/
if (!(mysql_file_stat(arch_key_file_data, name_buff, &file_stat, MYF(0)))) {
set_my_errno(0);
if (!(azopen(&create_stream, name_buff, O_CREAT | O_RDWR))) {
error = errno;
goto error2;
}
#ifndef _WIN32
if (linkname[0]) my_symlink(name_buff, linkname, MYF(0));
#endif
// TODO: Write SDI here?
if (create_info->comment.str)
azwrite_comment(&create_stream, create_info->comment.str,
create_info->comment.length);
/*
Yes you need to do this, because the starting value
for the autoincrement may not be zero.
*/
create_stream.auto_increment =
stats.auto_increment_value ? stats.auto_increment_value - 1 : 0;
if (azclose(&create_stream)) {
error = errno;
goto error2;
}
} else
set_my_errno(0);
DBUG_PRINT("ha_archive", ("Creating File %s", name_buff));
DBUG_PRINT("ha_archive", ("Creating Link %s", linkname));
return 0;
error2:
delete_table(name, table_def);
error:
/* Return error number, if we got one */
return error ? error : -1;
}
/*
This is where the actual row is written out.
*/
int ha_archive::real_write_row(uchar *buf, azio_stream *writer) {
my_off_t written;
unsigned int r_pack_length;
DBUG_TRACE;
/* We pack the row for writing */
r_pack_length = pack_row(buf, writer);
written = azwrite(writer, record_buffer->buffer, r_pack_length);
if (written != r_pack_length) {
DBUG_PRINT("ha_archive", ("Wrote %d bytes expected %d", (uint32)written,
(uint32)r_pack_length));
return -1;
}
if (!bulk_insert) share->dirty = true;
return 0;
}
/*
Calculate max length needed for row. This includes
the bytes required for the length in the header.
*/
uint32 ha_archive::max_row_length(const uchar *) {
uint32 length = (uint32)(table->s->reclength + table->s->fields * 2);
length += ARCHIVE_ROW_HEADER_SIZE;
uint *ptr, *end;
for (ptr = table->s->blob_field, end = ptr + table->s->blob_fields;
ptr != end; ptr++) {
if (!table->field[*ptr]->is_null())
length += 2 + ((Field_blob *)table->field[*ptr])->get_length();
}
return length;
}
unsigned int ha_archive::pack_row(uchar *record, azio_stream *writer) {
uchar *ptr;
DBUG_TRACE;
if (fix_rec_buff(max_row_length(record)))
return HA_ERR_OUT_OF_MEM; /* purecov: inspected */
if (writer->version == 1) return pack_row_v1(record);
/* Copy null bits */
memcpy(record_buffer->buffer + ARCHIVE_ROW_HEADER_SIZE, record,
table->s->null_bytes);
ptr = record_buffer->buffer + table->s->null_bytes + ARCHIVE_ROW_HEADER_SIZE;
for (Field **field = table->field; *field; field++) {
if (!((*field)->is_null())) ptr = (*field)->pack(ptr);
}
int4store(record_buffer->buffer,
(int)(ptr - record_buffer->buffer - ARCHIVE_ROW_HEADER_SIZE));
DBUG_PRINT("ha_archive",
("Pack row length %u", (unsigned int)(ptr - record_buffer->buffer -
ARCHIVE_ROW_HEADER_SIZE)));
return (unsigned int)(ptr - record_buffer->buffer);
}
/*
Look at ha_archive::open() for an explanation of the row format.
Here we just write out the row.
Wondering about start_bulk_insert()? We don't implement it for
archive since it optimizes for lots of writes. The only save
for implementing start_bulk_insert() is that we could skip
setting dirty to true each time.
*/
int ha_archive::write_row(uchar *buf) {
int rc;
uchar *read_buf = nullptr;
ulonglong temp_auto;
uchar *record = table->record[0];
DBUG_TRACE;
if (share->crashed) return HA_ERR_CRASHED_ON_USAGE;
ha_statistic_increment(&System_status_var::ha_write_count);
mysql_mutex_lock(&share->mutex);
if (!share->archive_write_open && share->init_archive_writer()) {
rc = HA_ERR_CRASHED_ON_USAGE;
goto error;
}
if (table->next_number_field && record == table->record[0]) {
KEY *mkey = &table->s->key_info[0]; // We only support one key right now
update_auto_increment();
temp_auto = (table->next_number_field->is_unsigned() ||
table->next_number_field->val_int() > 0
? table->next_number_field->val_int()
: 0);
/*
We don't support decremening auto_increment. They make the performance
just cry.
*/
if (temp_auto <= share->archive_write.auto_increment &&
mkey->flags & HA_NOSAME) {
rc = HA_ERR_FOUND_DUPP_KEY;
goto error;
} else {
if (temp_auto > share->archive_write.auto_increment)
stats.auto_increment_value =
(share->archive_write.auto_increment = temp_auto) + 1;
}
}
/*
Notice that the global auto_increment has been increased.
In case of a failed row write, we will never try to reuse the value.
*/
share->rows_recorded++;
rc = real_write_row(buf, &(share->archive_write));
error:
mysql_mutex_unlock(&share->mutex);
if (read_buf) my_free(read_buf);
return rc;
}
void ha_archive::get_auto_increment(ulonglong, ulonglong, ulonglong,
ulonglong *first_value,
ulonglong *nb_reserved_values) {
*nb_reserved_values = ULLONG_MAX;
*first_value = share->archive_write.auto_increment + 1;
}
/* Initialized at each key walk (called multiple times unlike rnd_init()) */
int ha_archive::index_init(uint keynr, bool) {
DBUG_TRACE;
active_index = keynr;
return 0;
}
/*
No indexes, so if we get a request for an index search since we tell
the optimizer that we have unique indexes, we scan
*/
int ha_archive::index_read(uchar *buf, const uchar *key, uint key_len,
enum ha_rkey_function find_flag) {
int rc;
DBUG_TRACE;
rc = index_read_idx(buf, active_index, key, key_len, find_flag);
return rc;
}
int ha_archive::index_read_idx(uchar *buf, uint index, const uchar *key,
uint key_len, enum ha_rkey_function) {
int rc;
bool found = false;
KEY *mkey = &table->s->key_info[index];
current_k_offset = mkey->key_part->offset;
current_key = key;
current_key_len = key_len;
DBUG_TRACE;
rc = rnd_init(true);
if (rc) goto error;
while (!(get_row(&archive, buf))) {
if (!memcmp(current_key, buf + current_k_offset, current_key_len)) {
found = true;
break;
}
}
if (found) {
/* notify handler that a record has been found */
return 0;
}
error:
return rc ? rc : HA_ERR_END_OF_FILE;
}
int ha_archive::index_next(uchar *buf) {
bool found = false;
int rc;
DBUG_TRACE;
while (!(get_row(&archive, buf))) {
if (!memcmp(current_key, buf + current_k_offset, current_key_len)) {
found = true;
break;
}
}
rc = found ? 0 : HA_ERR_END_OF_FILE;
return rc;
}
/*
All calls that need to scan the table start with this method. If we are told
that it is a table scan we rewind the file to the beginning, otherwise
we assume the position will be set.
*/
int ha_archive::rnd_init(bool scan) {
DBUG_TRACE;
if (share->crashed) return HA_ERR_CRASHED_ON_USAGE;
init_archive_reader();
/* We rewind the file so that we can read from the beginning if scan */
if (scan) {
scan_rows = stats.records;
DBUG_PRINT("info", ("archive will retrieve %llu rows",
(unsigned long long)scan_rows));
if (read_data_header(&archive)) return HA_ERR_CRASHED_ON_USAGE;
}
return 0;
}
/*
This is the method that is used to read a row. It assumes that the row is
positioned where you want it.
*/
int ha_archive::get_row(azio_stream *file_to_read, uchar *buf) {
int rc;
DBUG_TRACE;
DBUG_PRINT("ha_archive", ("Picking version for get_row() %d -> %d",
(uchar)file_to_read->version, ARCHIVE_VERSION));
if (file_to_read->version == ARCHIVE_VERSION)
rc = get_row_version3(file_to_read, buf);
else
rc = get_row_version2(file_to_read, buf);
DBUG_PRINT("ha_archive", ("Return %d\n", rc));
return rc;
}
/* Reallocate buffer if needed */
bool ha_archive::fix_rec_buff(unsigned int length) {
DBUG_TRACE;
DBUG_PRINT("ha_archive", ("Fixing %u for %u", length, record_buffer->length));
assert(record_buffer->buffer);
if (length > record_buffer->length) {
uchar *newptr;
if (!(newptr = (uchar *)my_realloc(az_key_memory_record_buffer,
(uchar *)record_buffer->buffer, length,
MYF(MY_ALLOW_ZERO_PTR))))
return true;
record_buffer->buffer = newptr;
record_buffer->length = length;
}
assert(length <= record_buffer->length);
return false;
}
int ha_archive::unpack_row(azio_stream *file_to_read, uchar *record) {
DBUG_TRACE;
size_t read;
int error;
uchar size_buffer[ARCHIVE_ROW_HEADER_SIZE], *size_buffer_p = size_buffer;
unsigned int row_len;
/* First we grab the length stored */
read = azread(file_to_read, size_buffer, ARCHIVE_ROW_HEADER_SIZE, &error);
if (error == Z_STREAM_ERROR || (read && read < ARCHIVE_ROW_HEADER_SIZE))
return HA_ERR_CRASHED_ON_USAGE;
/* If we read nothing we are at the end of the file */
if (read == 0 || read != ARCHIVE_ROW_HEADER_SIZE) return HA_ERR_END_OF_FILE;
row_len = uint4korr(size_buffer_p);
DBUG_PRINT("ha_archive", ("Unpack row length %u -> %u", row_len,
(unsigned int)table->s->reclength));
if (fix_rec_buff(row_len)) {
return HA_ERR_OUT_OF_MEM;
}