T_KRIndex* kr_index_create(T_KRDB *ptDB, int iIndexId, char *psIndexName, E_KRType eIndexFieldType) { T_KRIndex *ptIndex = (T_KRIndex *)kr_calloc(sizeof(T_KRIndex)); if (ptIndex == NULL) { fprintf(stderr, "kr_calloc ptIndex failed!\n"); return NULL; } ptIndex->ptDB = ptDB; ptIndex->iIndexId = iIndexId; strncpy(ptIndex->caIndexName, psIndexName, sizeof(ptIndex->caIndexName)); ptIndex->eIndexFieldType = eIndexFieldType; KRHashFunc hash_func = (KRHashFunc )kr_get_hash_func(eIndexFieldType); KREqualFunc equal_func = (KREqualFunc )kr_get_equal_func(eIndexFieldType); ptIndex->pHashTable = kr_hashtable_new(hash_func, equal_func); ptIndex->pIndexTableList = kr_list_new(); kr_list_set_match(ptIndex->pIndexTableList, (KRCompareFunc )kr_table_indexid_match); kr_list_add_tail(ptDB->pIndexList, ptIndex); return ptIndex; }
T_KRIndex* kr_create_index(T_KRDB *krdb, T_KRTable *krtable, int index_id, char *index_name, E_KRIndexType index_type, int index_field_id, int sort_field_id) { T_KRIndex *ptIndex = (T_KRIndex *)kr_calloc(sizeof(T_KRIndex)); if (ptIndex == NULL) { fprintf(stderr, "kr_calloc ptIndex failed!\n"); return NULL; } ptIndex->iIndexId = index_id; strncpy(ptIndex->caIndexName, index_name, sizeof(ptIndex->caIndexName)); ptIndex->eIndexType = index_type; ptIndex->iIndexFieldId = index_field_id; ptIndex->iSortFieldId = sort_field_id; E_KRType index_field_type = krtable->ptFieldDef[index_field_id].type; KRHashFunc hash_func = (KRHashFunc )kr_get_hash_func(index_field_type); KREqualFunc equal_func = (KREqualFunc )kr_get_equal_func(index_field_type); switch(ptIndex->eIndexType) { /* table-index build with two steps: * 1.create new hashtable; * 2.add index to the table-index list */ case KR_INDEXTYPE_TABLE: { ptIndex->pHashTable = kr_hashtable_new(hash_func, equal_func); kr_list_add_tail(krtable->pIndexList, ptIndex); break; } /* db-index build will be a little bit complicated... * Check whether db-index with same index_id exists? * exists: link hashtable to the exist one, add to table-index list; * not: create an new hashtable, add to table-index and db-index list; */ case KR_INDEXTYPE_DB: { T_KRIndex *ptDBIndex = kr_get_db_index(krdb, index_id); if (ptDBIndex != NULL) { ptIndex->pHashTable = ptDBIndex->pHashTable; kr_list_add_tail(krtable->pIndexList, ptIndex); } else { ptIndex->pHashTable = kr_hashtable_new(hash_func, equal_func); kr_list_add_tail(krtable->pIndexList, ptIndex); kr_list_add_tail(krdb->pIndexList, ptIndex); } break; } default: { fprintf(stderr, "Unknown index type[%c]\n", ptIndex->eIndexType); return NULL; } } return ptIndex; }
/*create a new set*/ T_KRHashSet *kr_hashset_create(char *name, E_KRType key_type) { T_KRHashSet *krset = (T_KRHashSet *)kr_calloc(sizeof(T_KRHashSet)); krset->name = (char *)kr_strdup(name); krset->type = key_type; KRHashFunc hash_func = kr_get_hash_func(key_type); KREqualFunc equal_func = kr_get_equal_func(key_type); krset->set = kr_hashtable_new_full(hash_func, equal_func, kr_free, NULL); return krset; }