Fix page deletion logic

master
Yury Kurlykov 2019-10-29 15:52:41 +10:00
parent f838445bed
commit ebedf5619c
2 changed files with 11 additions and 5 deletions

View File

@ -102,7 +102,7 @@ YDB_Error ydb_next_page(YDB_Engine* instance);
* *
* @todo Possible error codes. * @todo Possible error codes.
*/ */
YDB_Error ydb_replace_page(YDB_Engine* instance, YDB_TablePage* page); YDB_Error ydb_replace_current_page(YDB_Engine* instance, YDB_TablePage* page);
/** /**
* @brief Appends a page to a table. * @brief Appends a page to a table.
@ -124,7 +124,7 @@ YDB_Error ydb_append_page(YDB_Engine* instance, YDB_TablePage* page);
YDB_TablePage* ydb_get_current_page(YDB_Engine* instance); YDB_TablePage* ydb_get_current_page(YDB_Engine* instance);
/** /**
* Delete current page. * Delete current page and seek to the previous one.
* @param instance A YeltsinDB instance. * @param instance A YeltsinDB instance.
* @return Operation status. * @return Operation status.
*/ */

View File

@ -46,7 +46,7 @@ void ydb_terminate_instance(YDB_Engine *instance) {
} }
// Internal usage only! // Internal usage only!
// Its only purpose to read page data and set next_page offset. // Its only purpose to read current page data and set next_page offset.
// You should write prev_page offset on your own. // You should write prev_page offset on your own.
inline YDB_Error __ydb_read_page(YDB_Engine *inst) { inline YDB_Error __ydb_read_page(YDB_Engine *inst) {
THROW_IF_NULL(inst, YDB_ERR_INSTANCE_NOT_INITIALIZED); THROW_IF_NULL(inst, YDB_ERR_INSTANCE_NOT_INITIALIZED);
@ -308,7 +308,7 @@ YDB_Error ydb_append_page(YDB_Engine* instance, YDB_TablePage* page) {
return YDB_ERR_SUCCESS; return YDB_ERR_SUCCESS;
} }
YDB_Error ydb_replace_page(YDB_Engine *instance, YDB_TablePage *page) { YDB_Error ydb_replace_current_page(YDB_Engine *instance, YDB_TablePage *page) {
THROW_IF_NULL(instance, YDB_ERR_INSTANCE_NOT_INITIALIZED); THROW_IF_NULL(instance, YDB_ERR_INSTANCE_NOT_INITIALIZED);
THROW_IF_NULL(instance->in_use, YDB_ERR_INSTANCE_NOT_IN_USE); THROW_IF_NULL(instance->in_use, YDB_ERR_INSTANCE_NOT_IN_USE);
THROW_IF_NULL(page, YDB_ERR_PAGE_NOT_INITIALIZED); THROW_IF_NULL(page, YDB_ERR_PAGE_NOT_INITIALIZED);
@ -341,6 +341,7 @@ YDB_Error ydb_replace_page(YDB_Engine *instance, YDB_TablePage *page) {
} }
YDB_Error ydb_delete_current_page(YDB_Engine *instance) { YDB_Error ydb_delete_current_page(YDB_Engine *instance) {
// TODO check if it's only page in table
THROW_IF_NULL(instance, YDB_ERR_INSTANCE_NOT_INITIALIZED); THROW_IF_NULL(instance, YDB_ERR_INSTANCE_NOT_INITIALIZED);
THROW_IF_NULL(instance->in_use, YDB_ERR_INSTANCE_NOT_IN_USE); THROW_IF_NULL(instance->in_use, YDB_ERR_INSTANCE_NOT_IN_USE);
@ -351,7 +352,6 @@ YDB_Error ydb_delete_current_page(YDB_Engine *instance) {
// Write last_free_page_offset as the next page for current one // Write last_free_page_offset as the next page for current one
fwrite(&(instance->last_free_page_offset), sizeof(YDB_Offset), 1, instance->fd); fwrite(&(instance->last_free_page_offset), sizeof(YDB_Offset), 1, instance->fd);
// Link the previous page with next one (could be null ptr) // Link the previous page with next one (could be null ptr)
fseek(instance->fd, instance->prev_page_offset + 1, SEEK_SET); // Skip flags fseek(instance->fd, instance->prev_page_offset + 1, SEEK_SET); // Skip flags
fwrite(&(instance->next_page_offset), sizeof(YDB_Offset), 1, instance->fd); fwrite(&(instance->next_page_offset), sizeof(YDB_Offset), 1, instance->fd);
@ -369,5 +369,11 @@ YDB_Error ydb_delete_current_page(YDB_Engine *instance) {
// Flush buffer // Flush buffer
fflush(instance->fd); fflush(instance->fd);
// Set previous page
int64_t ind = --(instance->current_page_index);
if (ydb_seek_page(instance, ind + 1)) {
return YDB_ERR_UNKNOWN;
}
return YDB_ERR_SUCCESS; return YDB_ERR_SUCCESS;
} }