Removing all indexes of a relation when a relation is removed
authorShivam Garg <garg.shivam@samsung.com>
Mon, 24 Jul 2017 06:54:45 +0000 (15:54 +0900)
committerShivam Garg <garg.shivam@samsung.com>
Mon, 24 Jul 2017 06:54:45 +0000 (15:54 +0900)
framework/src/arastorage/aql_exec.c
framework/src/arastorage/index_bplustree.c
framework/src/arastorage/index_manager.c
framework/src/arastorage/relation.c
framework/src/arastorage/relation.h
framework/src/arastorage/storage_interface.c

index a1173bb..80aaf8f 100644 (file)
@@ -168,7 +168,7 @@ db_result_t db_exec(char *format)
        }
 
        optype = AQL_GET_EXEC_TYPE(AQL_GET_TYPE(&adt));
-       if (optype != AQL_TYPE_CREATE_RELATION && optype != AQL_TYPE_REMOVE_RELATION) {
+       if (optype != AQL_TYPE_CREATE_RELATION) {
                rel = aql_get_relation(&adt);
                if (rel == NULL) {
                        DB_LOG_E("DB : get relation Failed\n");
@@ -225,7 +225,7 @@ db_result_t db_exec(char *format)
                }
                break;
        case AQL_TYPE_REMOVE_RELATION:
-               res = relation_remove(adt.relations[0], 1);
+               res = relation_remove(rel, 1);
                break;
        default:
                break;
index 2356668..5753c30 100644 (file)
@@ -550,18 +550,21 @@ static db_result_t load(index_t *index)
        DB_LOG_D("load index : descriptor file name : %s\n", index->descriptor_file);
        fd = storage_open(index->descriptor_file, O_RDWR);
        if (fd < 0) {
-               DB_LOG_E("Failed opening index descriptor file\n");
-               goto storage_error;
+               DB_LOG_E("Failed opening index descriptor file :%s Error Code %d",index->descriptor_file);
+               free(tree);
+               return DB_STORAGE_ERROR;
        }
        if (DB_ERROR(storage_read_from(fd, bucket_file, sizeof(tree_t), sizeof(bucket_file)))) {
                DB_LOG_E("Failed reading bucket file\n");
                storage_close(fd);
-               goto storage_error;
+               free(tree);
+               return DB_STORAGE_ERROR;
        }
        if (DB_ERROR(storage_read_from(fd, tree, 0, sizeof(tree_t)))) {
                DB_LOG_E("Failed  reading tree structure from descriptor file\n");
                storage_close(fd);
-               goto storage_error;
+               free(tree);
+               return DB_STORAGE_ERROR;
        }
        storage_close(fd);
 
@@ -642,17 +645,6 @@ static db_result_t load(index_t *index)
 
        return DB_OK;
 
-storage_error:
-       DB_LOG_E("DB: Storage error while loading index\n");
-       free(tree->node_cache->in_cache.head);
-       free(tree->node_cache->in_cache.tail);
-       free(tree->buck_cache->in_cache.head);
-       free(tree->buck_cache->in_cache.tail);
-       free(tree->buck_cache);
-       free(tree->node_cache);
-       free(tree);
-       return DB_STORAGE_ERROR;
-
 }
 
 static db_result_t release(index_t *index)
index 0d427d2..72d9c57 100644 (file)
@@ -173,7 +173,7 @@ db_result_t index_create(index_type_t index_type, relation_t *rel, attribute_t *
        attr->index = index;
        list_push(indices, index);
 
-       if (index->descriptor_file[0] != '\0' && DB_ERROR(storage_put_index(index))) {
+       if (DB_ERROR(storage_put_index(index))) {
                index_destroy(index);
                DB_LOG_E("DB: Failed to store index data in file \"%s\"\n", index->descriptor_file);
                return DB_INDEX_ERROR;
@@ -223,10 +223,7 @@ db_result_t index_destroy(index_t *index)
        if (DB_ERROR(storage_remove_index(index->rel, index->attr))) {
                return DB_STORAGE_ERROR;
        }
-       index->rel = NULL;
        index->attr->index = NULL;
-       index->attr = NULL;
-       index = NULL;
        return DB_OK;
 }
 
index 2698590..274e2e4 100644 (file)
@@ -316,8 +316,9 @@ relation_t *relation_create(char *name, db_direction_t dir)
 
 db_result_t relation_rename(char *old_name, char *new_name)
 {
+       relation_t *rel = relation_load(new_name);
 
-       if (DB_ERROR(relation_remove(new_name, 1)) || DB_ERROR(storage_rename_relation(old_name, new_name))) {
+       if (DB_ERROR(relation_remove(rel, 1)) || DB_ERROR(storage_rename_relation(old_name, new_name))) {
                return DB_STORAGE_ERROR;
        }
 
@@ -417,12 +418,13 @@ db_result_t relation_set_primary_key(relation_t *rel, char *name)
        return DB_OK;
 }
 
-db_result_t relation_remove(char *name, int remove_tuples)
+db_result_t relation_remove(relation_t *rel, int remove_tuples)
 {
-       relation_t *rel;
        db_result_t result;
+       attribute_t *attr;
+       int len;
+       char *filename;
 
-       rel = relation_load(name);
        if (rel == NULL) {
                /*
                 * Attempt to remove an inexistent relation. To allow for this
@@ -441,6 +443,29 @@ db_result_t relation_remove(char *name, int remove_tuples)
                DB_LOG_D("DB : flush insert buffer!!\n");
        }
 #endif
+       attr = list_head(rel->attributes);
+
+       while (attr != NULL) {
+               if (DB_SUCCESS(index_load(rel, attr))) {
+                       result = index_destroy(attr->index);
+                       if (DB_ERROR(result)) {
+                               DB_LOG_E("DB: Index %s.%s destroy failed\n", rel->name, attr->name);
+                       }
+               }
+               attr = list_item_next(attr);
+       }
+
+       len = strlen(rel->name) + strlen(INDEX_NAME_SUFFIX) + 1;
+       filename = malloc(sizeof(char) * len);
+       if (filename == NULL) {
+               return DB_STORAGE_ERROR;
+       }
+       snprintf(filename, len, "%s%s\0", rel->name, INDEX_NAME_SUFFIX);
+       result = storage_remove(filename);
+       if (DB_ERROR(result)) {
+               DB_LOG_E("DB: Index file unlinking failed\n");
+       }
+
        result = storage_drop_relation(rel, remove_tuples);
        relation_free(rel);
        return result;
@@ -914,10 +939,10 @@ db_result_t relation_process_remove(db_handle_t **handle, db_cursor_t *cursor)
        unsigned attribute_count;
        unsigned char *from_ptr;
        attribute_t *from_attr;
-       index_t *index;
        storage_row_t row = NULL;
        tuple_t result_row;
        source_dest_map_t *attr_map_ptr, *attr_map_end;
+       char name[RELATION_NAME_LENGTH + 1];
        int i;
 
        if ((*handle)->tuple == NULL) {
@@ -987,24 +1012,17 @@ db_result_t relation_process_remove(db_handle_t **handle, db_cursor_t *cursor)
 
 end_removal:
        DB_LOG_E("DB: Finished removing tuples. Result relation has %d tuples\n", (*handle)->result_rel->cardinality);
-
-       relation_release((*handle)->rel);
+       memcpy(name, (*handle)->rel->name, sizeof(name));
+       relation_remove((*handle)->rel, 1);
 
        /* Rename the name of new relation to old relation */
-       result = relation_rename((*handle)->result_rel->name, (*handle)->rel->name);
+       result = relation_rename((*handle)->result_rel->name, name);
        if (DB_ERROR(result)) {
                DB_LOG_E("DB: Failed to rename newly created relation\n");
                goto errout;
        }
        memcpy((*handle)->result_rel->name, (*handle)->rel->name, sizeof((*handle)->result_rel->name));
 
-       /* Destory exsting index for old relation */
-       index = (*handle)->index_iterator.index;
-       if (index != NULL) {
-               index_destroy(index);
-               relation_index_clear((*handle)->result_rel);
-       }
-
        /* Process finished, we allocate cursor for result of remove */
        if (DB_ERROR(cursor_init(&cursor, (*handle)->result_rel)) || DB_ERROR(cursor_data_set(cursor, (*handle)->attr_map, (*handle)->result_rel->attribute_count))) {
                DB_LOG_E("DB: Failed to init cursor and set cursor data\n");
@@ -1090,6 +1108,7 @@ db_result_t relation_select(db_handle_t **handle, relation_t *rel, void *adt_ptr
        db_direction_t dir;
        char *attribute_name;
        attribute_t *attr, *attr_ptr;
+       relation_t * res_rel;
        int i;
        int normal_attributes = 0;
        adt = (aql_adt_t *)adt_ptr;
@@ -1107,7 +1126,8 @@ db_result_t relation_select(db_handle_t **handle, relation_t *rel, void *adt_ptr
                dir = DB_MEMORY;
        }
 
-       relation_remove(name, 1);
+       res_rel = relation_load(name);
+       relation_remove(res_rel, 1);
        relation_create(name, dir);
        (*handle)->result_rel = relation_load(name);
 
index bf11e2f..f8e0488 100644 (file)
@@ -189,7 +189,7 @@ attribute_t *relation_attribute_get(relation_t *, char *);
 db_result_t relation_get_value(relation_t *, attribute_t *, unsigned char *, attribute_value_t *);
 db_result_t relation_attribute_remove(relation_t *, char *);
 db_result_t relation_set_primary_key(relation_t *, char *);
-db_result_t relation_remove(char *, int);
+db_result_t relation_remove(relation_t *, int);
 db_result_t relation_insert(relation_t *, attribute_value_t *);
 db_result_t relation_select(db_handle_t **, relation_t *, void *);
 tuple_id_t relation_cardinality(relation_t *);
index 59aad94..41e83a9 100644 (file)
@@ -413,7 +413,7 @@ db_result_t storage_remove_index(relation_t *rel, attribute_t *attr)
        offset = storage_seek(fd, 0, SEEK_END);
        storage_close(fd);
 
-       if (sizeof(record) >= offset) {
+       if (sizeof(record) > offset) {
                res = storage_remove(filename);
                if (DB_ERROR(res)) {
                        free(filename);
@@ -441,7 +441,7 @@ db_result_t storage_remove_index(relation_t *rel, attribute_t *attr)
                        storage_close(fd);
                        return res;
                }
-               fd_tmp = storage_open(new_filename, O_RDWR);
+               fd_tmp = storage_open(new_filename, O_WROK | O_APPEND | O_CREAT);
                if (fd_tmp < 0) {
                        free(filename);
                        free(new_filename);
@@ -464,6 +464,7 @@ db_result_t storage_remove_index(relation_t *rel, attribute_t *attr)
                                        free(new_filename);
                                        return DB_STORAGE_ERROR;
                                }
+
                        }
                }
                storage_close(fd_tmp);