-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ha_tina.cc
1838 lines (1556 loc) · 52.1 KB
/
ha_tina.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, 2011, 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 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-1335 USA */
/*
Make sure to look at ha_tina.h for more details.
First off, this is a play thing for me, there are a number of things
wrong with it:
*) It was designed for csv and therefore its performance is highly
questionable.
*) Indexes have not been implemented. This is because the files can
be traded in and out of the table directory without having to worry
about rebuilding anything.
*) NULLs and "" are treated equally (like a spreadsheet).
*) There was in the beginning no point to anyone seeing this other
then me, so there is a good chance that I haven't quite documented
it well.
*) Less design, more "make it work"
Now there are a few cool things with it:
*) Errors can result in corrupted data files.
*) Data files can be read by spreadsheets directly.
TODO:
*) Move to a block system for larger files
*) Error recovery, its all there, just need to finish it
*) Document how the chains work.
-Brian
*/
#include <my_global.h>
#include "sql_priv.h"
#include "sql_class.h" // SSV
#include <mysql/psi/mysql_file.h>
#include "ha_tina.h"
#include "probes_mysql.h"
/*
uchar + uchar + ulonglong + ulonglong + ulonglong + ulonglong + uchar
*/
#define META_BUFFER_SIZE sizeof(uchar) + sizeof(uchar) + sizeof(ulonglong) \
+ sizeof(ulonglong) + sizeof(ulonglong) + sizeof(ulonglong) + sizeof(uchar)
#define TINA_CHECK_HEADER 254 // The number we use to determine corruption
#define BLOB_MEMROOT_ALLOC_SIZE 8192
/* The file extension */
#define CSV_EXT ".CSV" // The data file
#define CSN_EXT ".CSN" // Files used during repair and update
#define CSM_EXT ".CSM" // Meta file
struct ha_table_option_struct
{
bool ietf_quotes;
};
ha_create_table_option csv_table_option_list[]=
{
HA_TOPTION_BOOL("IETF_QUOTES", ietf_quotes, 0),
HA_TOPTION_END
};
static TINA_SHARE *get_share(const char *table_name, TABLE *table);
static int free_share(TINA_SHARE *share);
static int read_meta_file(File meta_file, ha_rows *rows);
static int write_meta_file(File meta_file, ha_rows rows, bool dirty);
extern "C" void tina_get_status(void* param, int concurrent_insert);
extern "C" void tina_update_status(void* param);
extern "C" my_bool tina_check_status(void* param);
/* Stuff for shares */
mysql_mutex_t tina_mutex;
static HASH tina_open_tables;
static handler *tina_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root);
/*****************************************************************************
** TINA tables
*****************************************************************************/
/*
Used for sorting chains with qsort().
*/
int sort_set (tina_set *a, tina_set *b)
{
/*
We assume that intervals do not intersect. So, it is enought to compare
any two points. Here we take start of intervals for comparison.
*/
return ( a->begin > b->begin ? 1 : ( a->begin < b->begin ? -1 : 0 ) );
}
static uchar* tina_get_key(TINA_SHARE *share, size_t *length,
my_bool not_used __attribute__((unused)))
{
*length=share->table_name_length;
return (uchar*) share->table_name;
}
static PSI_memory_key csv_key_memory_tina_share;
static PSI_memory_key csv_key_memory_blobroot;
static PSI_memory_key csv_key_memory_tina_set;
static PSI_memory_key csv_key_memory_row;
#ifdef HAVE_PSI_INTERFACE
static PSI_mutex_key csv_key_mutex_tina, csv_key_mutex_TINA_SHARE_mutex;
static PSI_mutex_info all_tina_mutexes[]=
{
{ &csv_key_mutex_tina, "tina", PSI_FLAG_GLOBAL},
{ &csv_key_mutex_TINA_SHARE_mutex, "TINA_SHARE::mutex", 0}
};
static PSI_file_key csv_key_file_metadata, csv_key_file_data,
csv_key_file_update;
static PSI_file_info all_tina_files[]=
{
{ &csv_key_file_metadata, "metadata", 0},
{ &csv_key_file_data, "data", 0},
{ &csv_key_file_update, "update", 0}
};
static PSI_memory_info all_tina_memory[]=
{
{ &csv_key_memory_tina_share, "TINA_SHARE", PSI_FLAG_GLOBAL},
{ &csv_key_memory_blobroot, "blobroot", 0},
{ &csv_key_memory_tina_set, "tina_set", 0},
{ &csv_key_memory_row, "row", 0},
{ &csv_key_memory_Transparent_file, "Transparent_file", 0}
};
static void init_tina_psi_keys(void)
{
const char* category= "csv";
int count;
count= array_elements(all_tina_mutexes);
mysql_mutex_register(category, all_tina_mutexes, count);
count= array_elements(all_tina_files);
mysql_file_register(category, all_tina_files, count);
count= array_elements(all_tina_memory);
mysql_memory_register(category, all_tina_memory, count);
}
#endif /* HAVE_PSI_INTERFACE */
/*
If frm_error() is called in table.cc this is called to find out what file
extensions exist for this handler.
*/
static const char *ha_tina_exts[] = {
CSV_EXT,
CSM_EXT,
CSN_EXT,
NullS
};
static int tina_init_func(void *p)
{
handlerton *tina_hton;
#ifdef HAVE_PSI_INTERFACE
init_tina_psi_keys();
#endif
tina_hton= (handlerton *)p;
mysql_mutex_init(csv_key_mutex_tina, &tina_mutex, MY_MUTEX_INIT_FAST);
(void) my_hash_init(csv_key_memory_tina_share, &tina_open_tables,
Lex_ident_table::charset_info(),
32, 0, 0, (my_hash_get_key)
tina_get_key, 0, 0);
tina_hton->db_type= DB_TYPE_CSV_DB;
tina_hton->create= tina_create_handler;
tina_hton->flags= (HTON_CAN_RECREATE | HTON_SUPPORT_LOG_TABLES |
HTON_NO_PARTITION);
tina_hton->tablefile_extensions= ha_tina_exts;
tina_hton->table_options= csv_table_option_list;
return 0;
}
static int tina_done_func(void *p)
{
my_hash_free(&tina_open_tables);
mysql_mutex_destroy(&tina_mutex);
return 0;
}
/*
Simple lock controls.
*/
static TINA_SHARE *get_share(const char *table_name, TABLE *table)
{
TINA_SHARE *share;
char meta_file_name[FN_REFLEN];
MY_STAT file_stat; /* Stat information for the data file */
char *tmp_name;
uint length;
mysql_mutex_lock(&tina_mutex);
length=(uint) strlen(table_name);
/*
If share is not present in the hash, create a new share and
initialize its members.
*/
if (!(share=(TINA_SHARE*) my_hash_search(&tina_open_tables, (uchar*)
table_name, length)))
{
if (!my_multi_malloc(csv_key_memory_tina_share, MYF(MY_WME | MY_ZEROFILL),
&share, sizeof(*share), &tmp_name, length+1, NullS))
{
mysql_mutex_unlock(&tina_mutex);
return NULL;
}
share->use_count= 0;
share->is_log_table= FALSE;
share->table_name_length= length;
share->table_name= tmp_name;
share->crashed= FALSE;
share->rows_recorded= 0;
share->update_file_opened= FALSE;
share->tina_write_opened= FALSE;
share->data_file_version= 0;
strmov(share->table_name, table_name);
fn_format(share->data_file_name, table_name, "", CSV_EXT,
MY_REPLACE_EXT|MY_UNPACK_FILENAME);
fn_format(meta_file_name, table_name, "", CSM_EXT,
MY_REPLACE_EXT|MY_UNPACK_FILENAME);
if (mysql_file_stat(csv_key_file_data,
share->data_file_name, &file_stat, MYF(MY_WME)) == NULL)
goto error;
share->saved_data_file_length= file_stat.st_size;
if (my_hash_insert(&tina_open_tables, (uchar*) share))
goto error;
thr_lock_init(&share->lock);
mysql_mutex_init(csv_key_mutex_TINA_SHARE_mutex,
&share->mutex, MY_MUTEX_INIT_FAST);
/*
Open or create the meta file. In the latter case, we'll get
an error during read_meta_file and mark the table as crashed.
Usually this will result in auto-repair, and we will get a good
meta-file in the end.
*/
if (((share->meta_file= mysql_file_open(csv_key_file_metadata,
meta_file_name,
O_RDWR|O_CREAT,
MYF(MY_WME))) == -1) ||
read_meta_file(share->meta_file, &share->rows_recorded))
share->crashed= TRUE;
}
share->use_count++;
mysql_mutex_unlock(&tina_mutex);
return share;
error:
mysql_mutex_unlock(&tina_mutex);
my_free(share);
return NULL;
}
/*
Read CSV meta-file
SYNOPSIS
read_meta_file()
meta_file The meta-file filedes
ha_rows Pointer to the var we use to store rows count.
These are read from the meta-file.
DESCRIPTION
Read the meta-file info. For now we are only interested in
rows counf, crashed bit and magic number.
RETURN
0 - OK
non-zero - error occurred
*/
static int read_meta_file(File meta_file, ha_rows *rows)
{
uchar meta_buffer[META_BUFFER_SIZE];
uchar *ptr= meta_buffer;
DBUG_ENTER("ha_tina::read_meta_file");
mysql_file_seek(meta_file, 0, MY_SEEK_SET, MYF(0));
if (mysql_file_read(meta_file, (uchar*)meta_buffer, META_BUFFER_SIZE, 0)
!= META_BUFFER_SIZE)
DBUG_RETURN(my_errno= HA_ERR_CRASHED_ON_USAGE);
/*
Parse out the meta data, we ignore version at the moment
*/
ptr+= sizeof(uchar)*2; // Move past header
*rows= (ha_rows)uint8korr(ptr);
ptr+= sizeof(ulonglong); // Move past rows
/*
Move past check_point, auto_increment and forced_flushes fields.
They are present in the format, but we do not use them yet.
*/
ptr+= 3*sizeof(ulonglong);
/* check crashed bit and magic number */
if ((meta_buffer[0] != (uchar)TINA_CHECK_HEADER) ||
((bool)(*ptr)== TRUE))
DBUG_RETURN(HA_ERR_CRASHED_ON_USAGE);
mysql_file_sync(meta_file, MYF(MY_WME));
DBUG_RETURN(0);
}
/*
Write CSV meta-file
SYNOPSIS
write_meta_file()
meta_file The meta-file filedes
ha_rows The number of rows we have in the datafile.
dirty A flag, which marks whether we have a corrupt table
DESCRIPTION
Write meta-info the the file. Only rows count, crashed bit and
magic number matter now.
RETURN
0 - OK
non-zero - error occurred
*/
static int write_meta_file(File meta_file, ha_rows rows, bool dirty)
{
uchar meta_buffer[META_BUFFER_SIZE];
uchar *ptr= meta_buffer;
DBUG_ENTER("ha_tina::write_meta_file");
*ptr= (uchar)TINA_CHECK_HEADER;
ptr+= sizeof(uchar);
*ptr= (uchar)TINA_VERSION;
ptr+= sizeof(uchar);
int8store(ptr, (ulonglong)rows);
ptr+= sizeof(ulonglong);
memset(ptr, 0, 3*sizeof(ulonglong));
/*
Skip over checkpoint, autoincrement and forced_flushes fields.
We'll need them later.
*/
ptr+= 3*sizeof(ulonglong);
*ptr= (uchar)dirty;
mysql_file_seek(meta_file, 0, MY_SEEK_SET, MYF(0));
if (mysql_file_write(meta_file, (uchar *)meta_buffer, META_BUFFER_SIZE, 0)
!= META_BUFFER_SIZE)
DBUG_RETURN(-1);
mysql_file_sync(meta_file, MYF(MY_WME));
DBUG_RETURN(0);
}
bool ha_tina::check_and_repair(THD *thd)
{
HA_CHECK_OPT check_opt;
DBUG_ENTER("ha_tina::check_and_repair");
check_opt.init();
DBUG_RETURN(repair(thd, &check_opt));
}
int ha_tina::init_tina_writer()
{
DBUG_ENTER("ha_tina::init_tina_writer");
/*
Mark the file as crashed. We will set the flag back when we close
the file. In the case of the crash it will remain marked crashed,
which enforce recovery.
*/
(void)write_meta_file(share->meta_file, share->rows_recorded, TRUE);
if ((share->tina_write_filedes=
mysql_file_open(csv_key_file_data,
share->data_file_name, O_RDWR|O_APPEND,
MYF(MY_WME))) == -1)
{
DBUG_PRINT("info", ("Could not open tina file writes"));
share->crashed= TRUE;
DBUG_RETURN(my_errno ? my_errno : -1);
}
share->tina_write_opened= TRUE;
DBUG_RETURN(0);
}
bool ha_tina::is_crashed() const
{
DBUG_ENTER("ha_tina::is_crashed");
DBUG_RETURN(share->crashed);
}
/*
Free lock controls.
*/
static int free_share(TINA_SHARE *share)
{
DBUG_ENTER("ha_tina::free_share");
mysql_mutex_lock(&tina_mutex);
int result_code= 0;
if (!--share->use_count){
/* Write the meta file. Mark it as crashed if needed. */
if (share->meta_file != -1)
{
(void)write_meta_file(share->meta_file, share->rows_recorded,
share->crashed ? TRUE :FALSE);
if (mysql_file_close(share->meta_file, MYF(0)))
result_code= 1;
}
if (share->tina_write_opened)
{
if (mysql_file_close(share->tina_write_filedes, MYF(0)))
result_code= 1;
share->tina_write_opened= FALSE;
}
my_hash_delete(&tina_open_tables, (uchar*) share);
thr_lock_delete(&share->lock);
mysql_mutex_destroy(&share->mutex);
my_free(share);
}
mysql_mutex_unlock(&tina_mutex);
DBUG_RETURN(result_code);
}
/*
This function finds the end of a line and returns the length
of the line ending.
We support three kinds of line endings:
'\r' -- Old Mac OS line ending
'\n' -- Traditional Unix and Mac OS X line ending
'\r''\n' -- DOS\Windows line ending
*/
my_off_t find_eoln_buff(Transparent_file *data_buff, my_off_t begin,
my_off_t end, int *eoln_len)
{
*eoln_len= 0;
for (my_off_t x= begin; x < end; x++)
{
/* Unix (includes Mac OS X) */
if (data_buff->get_value(x) == '\n')
*eoln_len= 1;
else
if (data_buff->get_value(x) == '\r') // Mac or Dos
{
/* old Mac line ending */
if (x + 1 == end || (data_buff->get_value(x + 1) != '\n'))
*eoln_len= 1;
else // DOS style ending
*eoln_len= 2;
}
if (*eoln_len) // end of line was found
return x;
}
return 0;
}
static handler *tina_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root)
{
return new (mem_root) ha_tina(hton, table);
}
ha_tina::ha_tina(handlerton *hton, TABLE_SHARE *table_arg)
:handler(hton, table_arg),
/*
These definitions are found in handler.h
They are not probably completely right.
*/
current_position(0), next_position(0), local_saved_data_file_length(0),
file_buff(0), chain_alloced(0), chain_size(DEFAULT_CHAIN_LENGTH),
local_data_file_version(0), records_is_known(0)
{
/* Set our original buffers from pre-allocated memory */
buffer.set((char*)byte_buffer, IO_SIZE, &my_charset_bin);
chain= chain_buffer;
file_buff= new Transparent_file();
init_alloc_root(csv_key_memory_blobroot, &blobroot, BLOB_MEMROOT_ALLOC_SIZE,
0, MYF(0));
}
/*
Encode a buffer into the quoted format.
*/
int ha_tina::encode_quote(const uchar *buf)
{
char attribute_buffer[1024];
String attribute(attribute_buffer, sizeof(attribute_buffer),
&my_charset_bin);
bool ietf_quotes= table_share->option_struct->ietf_quotes;
MY_BITMAP *org_bitmap= dbug_tmp_use_all_columns(table, &table->read_set);
buffer.length(0);
for (Field **field=table->field ; *field ; field++)
{
const char *ptr;
const char *end_ptr;
const bool was_null= (*field)->is_null();
/*
assistance for backwards compatibility in production builds.
note: this will not work for ENUM columns.
*/
if (was_null)
{
(*field)->set_default();
(*field)->set_notnull();
}
(*field)->val_str(&attribute,&attribute);
if (was_null)
(*field)->set_null();
if ((*field)->str_needs_quotes())
{
ptr= attribute.ptr();
end_ptr= attribute.length() + ptr;
/*
Ensure that buffer is big enough. This will also speed things up
as we don't have to do any new allocation in the loop below
*/
if (buffer.realloc(buffer.length() + attribute.length()*2+2))
return 0; // Failure
buffer.append('"');
for (; ptr < end_ptr; ptr++)
{
if (*ptr == '"')
{
buffer.append(ietf_quotes ? '"' : '\\');
buffer.append('"');
}
else if (*ptr == '\r')
{
buffer.append('\\');
buffer.append('r');
}
else if (*ptr == '\\')
{
buffer.append('\\');
buffer.append('\\');
}
else if (*ptr == '\n')
{
buffer.append('\\');
buffer.append('n');
}
else
buffer.append(*ptr);
}
buffer.append('"');
}
else
{
buffer.append(attribute);
}
buffer.append(',');
}
// Remove the comma, add a line feed
buffer.length(buffer.length() - 1);
buffer.append('\n');
//buffer.replace(buffer.length(), 0, "\n", 1);
dbug_tmp_restore_column_map(&table->read_set, org_bitmap);
return (buffer.length());
}
/*
chain_append() adds delete positions to the chain that we use to keep
track of space. Then the chain will be used to cleanup "holes", occurred
due to deletes and updates.
*/
int ha_tina::chain_append()
{
if ( chain_ptr != chain && (chain_ptr -1)->end == current_position)
(chain_ptr -1)->end= next_position;
else
{
/* We set up for the next position */
if ((off_t)(chain_ptr - chain) == (chain_size -1))
{
my_off_t location= chain_ptr - chain;
chain_size += DEFAULT_CHAIN_LENGTH;
if (chain_alloced)
{
if ((chain= (tina_set *) my_realloc(csv_key_memory_tina_set,
(uchar*)chain, chain_size,
MYF(MY_WME))) == NULL)
return -1;
}
else
{
tina_set *ptr= (tina_set *) my_malloc(csv_key_memory_tina_set,
chain_size * sizeof(tina_set),
MYF(MY_WME));
memcpy(ptr, chain, DEFAULT_CHAIN_LENGTH * sizeof(tina_set));
chain= ptr;
chain_alloced++;
}
chain_ptr= chain + location;
}
chain_ptr->begin= current_position;
chain_ptr->end= next_position;
chain_ptr++;
}
return 0;
}
/*
Scans for a row.
*/
int ha_tina::find_current_row(uchar *buf)
{
my_off_t end_offset, curr_offset= current_position;
int eoln_len;
int error;
bool read_all;
bool ietf_quotes= table_share->option_struct->ietf_quotes;
DBUG_ENTER("ha_tina::find_current_row");
free_root(&blobroot, MYF(0));
/*
We do not read further then local_saved_data_file_length in order
not to conflict with undergoing concurrent insert.
*/
if ((end_offset=
find_eoln_buff(file_buff, current_position,
local_saved_data_file_length, &eoln_len)) == 0)
DBUG_RETURN(HA_ERR_END_OF_FILE);
/* We must read all columns in case a table is opened for update */
read_all= !bitmap_is_clear_all(table->write_set);
/* Avoid asserts in ::store() for columns that are not going to be updated */
MY_BITMAP *org_bitmap= dbug_tmp_use_all_columns(table, &table->write_set);
error= HA_ERR_CRASHED_ON_USAGE;
memset(buf, 0, table->s->null_bytes);
/*
Parse the line obtained using the following algorithm
BEGIN
1) Store the EOL (end of line) for the current row
2) Until all the fields in the current query have not been
filled
2.1) If the current character is a quote
2.1.1) Until EOL has not been reached
a) If end of current field is reached, move
to next field and jump to step 2.3
b) If current character is a \\ handle
\\n, \\r, \\, and \\" if not in ietf_quotes mode
c) if in ietf_quotes mode and the current character is
a ", handle ""
d) else append the current character into the buffer
before checking that EOL has not been reached.
2.2) If the current character does not begin with a quote
2.2.1) Until EOL has not been reached
a) If the end of field has been reached move to the
next field and jump to step 2.3
b) If current character begins with \\ handle
\\n, \\r, \\, \\"
c) else append the current character into the buffer
before checking that EOL has not been reached.
2.3) Store the current field value and jump to 2)
TERMINATE
*/
for (Field **field=table->field ; *field ; field++)
{
char curr_char;
buffer.length(0);
if (curr_offset >= end_offset)
goto err;
curr_char= file_buff->get_value(curr_offset);
/* Handle the case where the first character is a quote */
if (curr_char == '"')
{
/* Increment past the first quote */
curr_offset++;
/* Loop through the row to extract the values for the current field */
for ( ; curr_offset < end_offset; curr_offset++)
{
curr_char= file_buff->get_value(curr_offset);
/* check for end of the current field */
if (curr_char == '"' &&
(curr_offset == end_offset - 1 ||
file_buff->get_value(curr_offset + 1) == ','))
{
/* Move past the , and the " */
curr_offset+= 2;
break;
}
if (ietf_quotes && curr_char == '"'
&& file_buff->get_value(curr_offset + 1) == '"')
{
/* Embedded IETF quote */
curr_offset++;
buffer.append('"');
}
else if (curr_char == '\\' && curr_offset != (end_offset - 1))
{
/* A quote followed by something else than a comma, end of line, or
(in IETF mode) another quote will be handled as a regular
character. */
curr_offset++;
curr_char= file_buff->get_value(curr_offset);
if (curr_char == 'r')
buffer.append('\r');
else if (curr_char == 'n' )
buffer.append('\n');
else if (curr_char == '\\' || (!ietf_quotes && curr_char == '"'))
buffer.append(curr_char);
else /* This could only happed with an externally created file */
{
buffer.append('\\');
buffer.append(curr_char);
}
}
else // ordinary symbol
{
/*
If we are at final symbol and no last quote was found =>
we are working with a damaged file.
*/
if (curr_offset == end_offset - 1)
goto err;
buffer.append(curr_char);
}
}
}
else
{
for ( ; curr_offset < end_offset; curr_offset++)
{
curr_char= file_buff->get_value(curr_offset);
/* Move past the ,*/
if (curr_char == ',')
{
curr_offset++;
break;
}
if (curr_char == '\\' && curr_offset != (end_offset - 1))
{
curr_offset++;
curr_char= file_buff->get_value(curr_offset);
if (curr_char == 'r')
buffer.append('\r');
else if (curr_char == 'n' )
buffer.append('\n');
else if (curr_char == '\\' || curr_char == '"')
buffer.append(curr_char);
else /* This could only happed with an externally created file */
{
buffer.append('\\');
buffer.append(curr_char);
}
}
else
{
/*
We are at the final symbol and a quote was found for the
unquoted field => We are working with a damaged field.
*/
if (curr_offset == end_offset - 1 && curr_char == '"')
goto err;
buffer.append(curr_char);
}
}
}
if (read_all || bitmap_is_set(table->read_set, (*field)->field_index))
{
bool is_enum= ((*field)->real_type() == MYSQL_TYPE_ENUM);
/*
Here CHECK_FIELD_WARN checks that all values in the csv file are valid
which is normally the case, if they were written by
INSERT -> ha_tina::write_row. '0' values on ENUM fields are considered
invalid by Field_enum::store() but it can store them on INSERT anyway.
Thus, for enums we silence the warning, as it doesn't really mean
an invalid value.
*/
if ((*field)->store_text(buffer.ptr(), buffer.length(), buffer.charset(),
is_enum ? CHECK_FIELD_IGNORE : CHECK_FIELD_WARN))
{
if (!is_enum)
goto err;
}
if ((*field)->flags & BLOB_FLAG)
{
Field_blob *blob= *(Field_blob**) field;
uchar *src, *tgt;
uint length, packlength;
packlength= blob->pack_length_no_ptr();
length= blob->get_length(blob->ptr);
memcpy(&src, blob->ptr + packlength, sizeof(char*));
if (src)
{
tgt= (uchar*) alloc_root(&blobroot, length);
bmove(tgt, src, length);
memcpy(blob->ptr + packlength, &tgt, sizeof(char*));
}
}
}
}
next_position= end_offset + eoln_len;
error= 0;
err:
dbug_tmp_restore_column_map(&table->write_set, org_bitmap);
DBUG_RETURN(error);
}
/*
Three functions below are needed to enable concurrent insert functionality
for CSV engine. For more details see mysys/thr_lock.c
*/
my_bool tina_get_status(void* param, my_bool concurrent_insert)
{
ha_tina *tina= (ha_tina*) param;
tina->get_status();
return 0;
}
void tina_update_status(void* param)
{
ha_tina *tina= (ha_tina*) param;
tina->update_status();
}
/* this should exist and return 0 for concurrent insert to work */
my_bool tina_check_status(void* param)
{
return 0;
}
/*
Save the state of the table
SYNOPSIS
get_status()
DESCRIPTION
This function is used to retrieve the file length. During the lock
phase of concurrent insert. For more details see comment to
ha_tina::update_status below.
*/
void ha_tina::get_status()
{
if (share->is_log_table)
{
/*
We have to use mutex to follow pthreads memory visibility
rules for share->saved_data_file_length
*/
mysql_mutex_lock(&share->mutex);
local_saved_data_file_length= share->saved_data_file_length;
mysql_mutex_unlock(&share->mutex);
return;
}
local_saved_data_file_length= share->saved_data_file_length;
}
/*
Correct the state of the table. Called by unlock routines
before the write lock is released.
SYNOPSIS
update_status()
DESCRIPTION
When we employ concurrent insert lock, we save current length of the file
during the lock phase. We do not read further saved value, as we don't
want to interfere with undergoing concurrent insert. Writers update file
length info during unlock with update_status().
NOTE
For log tables concurrent insert works different. The reason is that
log tables are always opened and locked. And as they do not unlock
tables, the file length after writes should be updated in a different
way. For this purpose we need is_log_table flag. When this flag is set
we call update_status() explicitly after each row write.
*/
void ha_tina::update_status()
{
/* correct local_saved_data_file_length for writers */
share->saved_data_file_length= local_saved_data_file_length;
}
/*
Open a database file. Keep in mind that tables are caches, so
this will not be called for every request. Any sort of positions
that need to be reset should be kept in the ::extra() call.
*/
int ha_tina::open(const char *name, int mode, uint open_options)
{
DBUG_ENTER("ha_tina::open");
if (!(share= get_share(name, table)))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
if (share->crashed && !(open_options & HA_OPEN_FOR_REPAIR))
{
free_share(share);
DBUG_RETURN(my_errno ? my_errno : HA_ERR_CRASHED_ON_USAGE);
}
local_data_file_version= share->data_file_version;
if ((data_file= mysql_file_open(csv_key_file_data,
share->data_file_name,
O_RDONLY, MYF(MY_WME))) == -1)
{
free_share(share);
DBUG_RETURN(my_errno ? my_errno : -1);
}
/*
Init locking. Pass handler object to the locking routines,
so that they could save/update local_saved_data_file_length value
during locking. This is needed to enable concurrent inserts.
*/
thr_lock_data_init(&share->lock, &lock, (void*) this);
ref_length= sizeof(my_off_t);
init_alloc_root(csv_key_memory_blobroot, &blobroot, BLOB_MEMROOT_ALLOC_SIZE,
0, MYF(0));
share->lock.get_status= tina_get_status;
share->lock.update_status= tina_update_status;