1350
Repair CSV table in the case, it is crashed.
1354
session The thread, performing repair
1355
check_opt The options for repair. We do not use it currently.
1358
If the file is empty, change # of rows in the file and complete recovery.
1359
Otherwise, scan the table looking for bad rows. If none were found,
1360
we mark file as a good one and return. If a bad row was encountered,
1361
we truncate the datafile up to the last good row.
1363
TODO: Make repair more clever - it should try to recover subsequent
1364
rows (after the first bad one) as well.
1367
int ha_tina::repair(Session* session, HA_CHECK_OPT *)
1369
char repaired_fname[FN_REFLEN];
1373
ha_rows rows_repaired= 0;
1374
off_t write_begin= 0, write_end;
1377
if (!share->saved_data_file_length)
1379
share->rows_recorded= 0;
1383
/* Don't assert in field::val() functions */
1384
table->use_all_columns();
1385
if (!(buf= (unsigned char*) malloc(table->s->reclength)))
1386
return(HA_ERR_OUT_OF_MEM);
1388
/* position buffer to the start of the file */
1389
if (init_data_file())
1390
return(HA_ERR_CRASHED_ON_REPAIR);
1393
Local_saved_data_file_length is initialized during the lock phase.
1394
Sometimes this is not getting executed before ::repair (e.g. for
1395
the log tables). We set it manually here.
1397
local_saved_data_file_length= share->saved_data_file_length;
1398
/* set current position to the beginning of the file */
1399
current_position= next_position= 0;
1401
init_alloc_root(&blobroot, BLOB_MEMROOT_ALLOC_SIZE, 0);
1403
/* Read the file row-by-row. If everything is ok, repair is not needed. */
1404
while (!(rc= find_current_row(buf)))
1406
session_inc_row_count(session);
1408
current_position= next_position;
1411
free_root(&blobroot, MYF(0));
1415
if (rc == HA_ERR_END_OF_FILE)
1418
All rows were read ok until end of file, the file does not need repair.
1419
If rows_recorded != rows_repaired, we should update rows_recorded value
1420
to the current amount of rows.
1422
share->rows_recorded= rows_repaired;
1427
Otherwise we've encountered a bad row => repair is needed.
1428
Let us create a temporary file.
1430
if ((repair_file= my_create(fn_format(repaired_fname, share->table_name,
1432
MY_REPLACE_EXT|MY_UNPACK_FILENAME),
1433
0, O_RDWR | O_TRUNC,MYF(MY_WME))) < 0)
1434
return(HA_ERR_CRASHED_ON_REPAIR);
1436
file_buff->init_buff(data_file);
1439
/* we just truncated the file up to the first bad row. update rows count. */
1440
share->rows_recorded= rows_repaired;
1442
/* write repaired file */
1445
write_end= std::min(file_buff->end(), current_position);
1447
off_t write_length= write_end - write_begin;
1448
if ((uint64_t)write_length > SIZE_MAX)
1452
if ((write_length) &&
1453
(my_write(repair_file, (unsigned char*)file_buff->ptr(),
1454
(size_t)write_length, MYF_RW)))
1457
write_begin= write_end;
1458
if (write_end== current_position)
1461
file_buff->read_next(); /* shift the buffer */
1465
Close the files and rename repaired file to the datafile.
1466
We have to close the files, as on Windows one cannot rename
1467
a file, which descriptor is still open. EACCES will be returned
1468
when trying to delete the "to"-file in my_rename().
1470
if (my_close(data_file,MYF(0)) || my_close(repair_file, MYF(0)) ||
1471
my_rename(repaired_fname, share->data_file_name, MYF(0)))
1474
/* Open the file again, it should now be repaired */
1475
if ((data_file= my_open(share->data_file_name, O_RDWR|O_APPEND,
1479
/* Set new file size. The file size will be updated by ::update_status() */
1480
local_saved_data_file_length= (size_t) current_position;
1483
share->crashed= false;
1484
return(HA_ADMIN_OK);
1488
1334
DELETE without WHERE calls this
1575
int ha_tina::check(Session* session, HA_CHECK_OPT *)
1579
const char *old_proc_info;
1580
ha_rows count= share->rows_recorded;
1582
old_proc_info= get_session_proc_info(session);
1583
set_session_proc_info(session, "Checking table");
1584
if (!(buf= (unsigned char*) malloc(table->s->reclength)))
1585
return(HA_ERR_OUT_OF_MEM);
1587
/* position buffer to the start of the file */
1588
if (init_data_file())
1589
return(HA_ERR_CRASHED);
1592
Local_saved_data_file_length is initialized during the lock phase.
1593
Check does not use store_lock in certain cases. So, we set it
1596
local_saved_data_file_length= share->saved_data_file_length;
1597
/* set current position to the beginning of the file */
1598
current_position= next_position= 0;
1600
init_alloc_root(&blobroot, BLOB_MEMROOT_ALLOC_SIZE, 0);
1602
/* Read the file row-by-row. If everything is ok, repair is not needed. */
1603
while (!(rc= find_current_row(buf)))
1605
session_inc_row_count(session);
1607
current_position= next_position;
1610
free_root(&blobroot, MYF(0));
1613
set_session_proc_info(session, old_proc_info);
1615
if ((rc != HA_ERR_END_OF_FILE) || count)
1617
share->crashed= true;
1618
return(HA_ADMIN_CORRUPT);
1621
return(HA_ADMIN_OK);
1625
1408
drizzle_declare_plugin