T_KRDB* kr_db_create(char *psDBName, T_DbsEnv *ptDbsEnv, T_KRModule *ptModule) { T_KRDB *ptDB = (T_KRDB *)kr_calloc(sizeof(T_KRDB)); if (ptDB == NULL) { fprintf(stderr, "kr_calloc ptDB failed!\n"); return NULL; } strncpy(ptDB->caDBName, psDBName, sizeof(ptDB->caDBName)); ptDB->dbsenv = ptDbsEnv; ptDB->pTableList = kr_list_new(); kr_list_set_match(ptDB->pTableList, (KRCompareFunc )kr_tableid_match); kr_list_set_free(ptDB->pTableList, (KRFreeFunc )kr_table_drop); ptDB->pIndexList = kr_list_new(); kr_list_set_match(ptDB->pIndexList, (KRCompareFunc )kr_indexid_match); kr_list_set_free(ptDB->pIndexList, (KRFreeFunc )kr_index_drop); ptDB->pIndexTableList = kr_list_new(); kr_list_set_match(ptDB->pIndexTableList, (KRCompareFunc )kr_index_table_match); kr_list_set_free(ptDB->pIndexTableList, (KRFreeFunc )kr_index_table_drop); return ptDB; }
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; }
int main(int argc,char *argv[]) { int i = 0; T_KRList *ptList = kr_list_new(); kr_list_set_compare(ptList, (KRCompareDataFunc )record_compare); srandom((unsigned int)time(NULL)); for (i=0; i<10; i++) { gstRecord[i].i=random()%9; gstRecord[i].d = i; snprintf(gstRecord[i].s, 20, "record_%d", i); printf("kr_list_add_sorted[%d] [%d]\n", i, gstRecord[i].i); //kr_list_add_tail(ptList, &gstRecord[i]); kr_list_add_sorted(ptList, &gstRecord[i], NULL); } kr_list_foreach(ptList, print_record, NULL); T_KRListNode *ptNode = kr_list_search(ptList, &gstRecord[7]); if (ptNode != NULL) { print_record(kr_list_value(ptNode), NULL); } kr_list_destroy(ptList); return 0; }
T_KROutput* kr_output_construct(T_KRParam *ptParam, char *psOutputModule, KRGetTypeFunc pfGetType, KRGetValueFunc pfGetValue) { T_KROutput *ptOutput = kr_calloc(sizeof(*ptOutput)); if (ptOutput == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptOutput failed!"); return NULL; } /* load output module */ ptOutput->ptOutputModule = kr_module_open(psOutputModule, RTLD_LAZY); if (ptOutput->ptOutputModule == NULL) { KR_LOG(KR_LOGERROR, "kr_module_open %s failed!", psOutputModule); kr_output_destruct(ptOutput); return NULL; } ptOutput->pfGetType = pfGetType; ptOutput->pfGetValue = pfGetValue; /*alloc output define list*/ ptOutput->ptOutputDefineList = kr_list_new(); kr_list_set_match(ptOutput->ptOutputDefineList, (KRListMatchFunc )kr_output_define_match); kr_list_set_free(ptOutput->ptOutputDefineList, (KRListFreeFunc )kr_output_define_free); /*create output list*/ if (kr_param_object_foreach(ptParam, KR_PARAM_OUTPUT, _kr_output_define_new, ptOutput) != 0) { KR_LOG(KR_LOGERROR, "_kr_db_build_table Error!"); return NULL; } ptOutput->tConstructTime = kr_param_load_time(ptParam); return ptOutput; }
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; }
T_KRDB* kr_create_db(char *db_name, T_KRModule *krdbmodule) { T_KRDB *ptDB = (T_KRDB *)kr_calloc(sizeof(T_KRDB)); if (ptDB == NULL) { fprintf(stderr, "kr_calloc ptDB failed!\n"); return NULL; } strncpy(ptDB->caDBName, db_name, sizeof(ptDB->caDBName)); ptDB->ptModule = krdbmodule; ptDB->pTableList = kr_list_new(); kr_list_set_match(ptDB->pTableList, (KRCompareFunc )kr_tableid_match); ptDB->pIndexList = kr_list_new(); kr_list_set_match(ptDB->pIndexList, (KRCompareFunc )kr_indexid_match); return ptDB; }
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); }