T_KRIndexTable* kr_index_table_create(T_KRDB *ptDB, int iIndexId, int iTableId, int iIndexFieldId, int iSortFieldId) { T_KRIndex *ptIndex = kr_index_get(ptDB, iIndexId); if (ptIndex == NULL) { fprintf(stderr, "kr_index_get [%d] failed!\n", iIndexId); return NULL; } T_KRTable *ptTable = kr_table_get(ptDB, iTableId); if (ptTable == NULL) { fprintf(stderr, "kr_table_get [%d] failed!\n", iTableId); return NULL; } T_KRIndexTable *ptIndexTable = \ (T_KRIndexTable *)kr_calloc(sizeof(T_KRIndexTable)); if (ptIndexTable == NULL) { fprintf(stderr, "kr_calloc ptIndexTable failed!\n"); return NULL; } ptIndexTable->ptIndex = ptIndex; ptIndexTable->ptTable = ptTable; ptIndexTable->iIndexFieldId = iIndexFieldId; ptIndexTable->iSortFieldId = iSortFieldId; kr_list_add_tail(ptIndex->pIndexTableList, ptIndexTable); kr_list_add_tail(ptTable->pIndexTableList, ptIndexTable); kr_list_add_tail(ptDB->pIndexTableList, ptIndexTable); return ptIndexTable; }
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; }
T_KRTable* kr_create_table(T_KRDB *krdb, int table_id, char *table_name, char *mmap_file, E_KRSizeKeepMode keep_mode, long keep_value, int field_cnt, KRLoadDefFunc load_field_def_func, KRMapFuncPre map_func_pre, KRMapFunc map_func, KRMapFuncPost map_func_post, void *param) { T_KRTable *ptTable = kr_calloc(sizeof(T_KRTable)); if (ptTable == NULL) { fprintf(stderr, "kr_calloc ptTable failed!\n"); return NULL; } ptTable->iTableId = table_id; strncpy(ptTable->caTableName, table_name, sizeof(ptTable->caTableName)); strncpy(ptTable->caMMapFile, mmap_file, sizeof(ptTable->caMMapFile)); ptTable->eSizeKeepMode = keep_mode; ptTable->lSizeKeepValue = keep_value; ptTable->iFieldCnt = field_cnt; ptTable->ptFieldDef = kr_calloc(ptTable->iFieldCnt * sizeof(T_KRFieldDef)); if (ptTable == NULL) { fprintf(stderr, "kr_calloc ptTable->ptFieldDef failed!\n"); return NULL; } load_field_def_func(param, ptTable->ptFieldDef, &ptTable->iTableId); ptTable->iRecordSize = sizeof(T_KRRecord); int i = 0; for (i = 0; i<ptTable->iFieldCnt; i++) { ptTable->iRecordSize += ptTable->ptFieldDef[i].length; } ptTable->iRecordSize = KR_MEMALIGN(ptTable->iRecordSize); ptTable->pMapFuncPre = map_func_pre; ptTable->pMapFunc = map_func; ptTable->pMapFuncPost = map_func_post; ptTable->uiRecordNum = 0; ptTable->uiRecordLoc = 0; if (ptTable->caMMapFile[0] == '\0') { ptTable->pRecordBuff = \ (char *)kr_calloc(ptTable->iRecordSize * ptTable->lSizeKeepValue); if (ptTable->pRecordBuff == NULL) { fprintf(stderr, "kr_calloc ptTable->pRecordBuff failed!\n"); return NULL; } } else { if (kr_db_mmap_table(ptTable) != 0) { fprintf(stderr, "kr_create_mmap_file failed!\n"); return NULL; } } ptTable->pIndexList = kr_list_new(); kr_list_set_match(ptTable->pIndexList, (KRCompareFunc )kr_indexid_match); kr_list_add_tail(krdb->pTableList, ptTable); return ptTable; }
static void kr_rebuild_index_ins(T_KRIndexTable *ptIndextable, T_KRRecord *ptRecord) { void *key = kr_field_get_value(ptRecord, ptIndextable->iIndexFieldId); T_KRHashTable *pHashTable = ptIndextable->ptIndex->pHashTable; T_KRIndexSolt *ptIndexSlot = kr_hashtable_lookup(pHashTable, key); if (ptIndexSlot == NULL) { /*create slot if not found*/ ptIndexSlot = kr_calloc(sizeof(*ptIndexSlot)); ptIndexSlot->eKeyType = kr_field_get_type(ptRecord, ptIndextable->iIndexFieldId); KRDupFunc pfKeyDup = kr_get_dup_func(ptIndexSlot->eKeyType); ptIndexSlot->pKeyValue = pfKeyDup(key); ptIndexSlot->tLocMinProcTime = kr_get_proctime(ptRecord); ptIndexSlot->tLocMinTransTime = kr_get_transtime(ptRecord); ptIndexSlot->tExtMaxProcTime = kr_get_proctime(ptRecord); ptIndexSlot->tExtMaxTransTime = kr_get_transtime(ptRecord); ptIndexSlot->pRecList = kr_list_new(); kr_hashtable_insert(pHashTable, ptIndexSlot->pKeyValue, ptIndexSlot); } /*modify statistical fields of local*/ if (kr_get_proctime(ptRecord) < ptIndexSlot->tLocMinProcTime) { ptIndexSlot->tLocMinProcTime = kr_get_proctime(ptRecord); } if (kr_get_transtime(ptRecord) < ptIndexSlot->tLocMinTransTime) { ptIndexSlot->tLocMinTransTime = kr_get_transtime(ptRecord); } /*add record to list*/ kr_list_add_tail(ptIndexSlot->pRecList, ptRecord); }
T_KRTable* kr_table_create(T_KRDB *ptDB, int iTableId, char *psTableName, E_KRSizeKeepMode eKeepMode, long lKeepValue) { T_KRTable *ptTable = kr_calloc(sizeof(T_KRTable)); if (ptTable == NULL) { fprintf(stderr, "kr_calloc ptTable failed!\n"); return NULL; } pthread_mutex_init(&ptTable->tLock, NULL); ptTable->ptDB = ptDB; ptTable->iTableId = iTableId; strncpy(ptTable->caTableName, psTableName, sizeof(ptTable->caTableName)); ptTable->eKeepMode = eKeepMode; ptTable->lKeepValue = lKeepValue; ptTable->uiRecordNum = 0; ptTable->uiRecordLoc = 0; ptTable->pIndexTableList = kr_list_new(); kr_list_set_match(ptTable->pIndexTableList, (KRCompareFunc )kr_index_tableid_match); kr_list_add_tail(ptDB->pTableList, ptTable); return ptTable; }
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; }
static void kr_rebuild_index_ins(T_KRIndex *krindex, T_KRRecord *krrecord) { void *key = kr_get_field_value(krrecord, krindex->iIndexFieldId); T_KRList *pRecList = kr_hashtable_lookup(krindex->pHashTable, key); if (pRecList == NULL) { pRecList = kr_list_new(); kr_list_set_compare(pRecList, (KRCompareDataFunc)kr_db_field_compare); } if (krindex->iSortFieldId >= 0) { kr_list_add_sorted(pRecList, krrecord, &krindex->iSortFieldId); } else { kr_list_add_tail(pRecList, krrecord); } kr_hashtable_replace(krindex->pHashTable, key, pRecList); }
int kr_coordi_handle_clion(T_KREventLoop *el, int fd, T_KRMessage *krmsg) { KR_LOG(KR_LOGDEBUG, "Client [%s] Online [%d]!", krmsg->clientid, fd); char ip[32]; int port; kr_net_peer_to_string(fd, ip, &port); /* Check this client whether exists */ T_KRListNode *node = kr_list_search(krcoordi.clients, krmsg->clientid); if (node != NULL) { KR_LOG(KR_LOGDEBUG, "Client %s already exists!", krmsg->clientid); return 0; } /* allocate connector for this server */ T_KRCoordiConnector *ptClient = kr_calloc(sizeof(T_KRCoordiConnector)); if (ptClient == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptClient error!"); return -1; } ptClient->fd = fd; ptClient->role = KR_COORDI_CLIENT; strcpy(ptClient->id, krmsg->clientid); strcpy(ptClient->ip, ip); ptClient->aplnum = 0; ptClient->rplnum = 0; ptClient->errnum = 0; /* Manage this client with list */ kr_list_add_tail(krcoordi.clients, ptClient); /*add this client fd--id mapping to fdtable*/ kr_hashtable_replace(krcoordi.fdtable, &ptClient->fd, ptClient); return 0; }