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; }
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_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_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_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; }
/*fixed format*/ void *fixed_define_pre_func(T_KRParamInput *ptParamInput) { T_KRIfaceFixedData *fixed_data = kr_calloc(sizeof(*fixed_data)); /* Open Define File */ fixed_data->datasrc_id = ptParamInput->lInputId; strcpy(fixed_data->datasrc_name, ptParamInput->caInputName); snprintf(fixed_data->filename, sizeof(fixed_data->filename), \ "%s.h", ptParamInput->caInputName); fixed_data->fp = fopen(fixed_data->filename, "w"); if (fixed_data->fp == NULL) { fprintf(stdout, "open output file: %s failed\n", fixed_data->filename); kr_free(fixed_data); return NULL; } /* Generate Define file head*/ FILE *fp = fixed_data->fp; fprintf(fp, "#ifndef __KR_INTERFACE_%d_H__ \n", fixed_data->datasrc_id); fprintf(fp, "#define __KR_INTERFACE_%d_H__ \n", fixed_data->datasrc_id); fprintf(fp, "#include <stdint.h> \n"); fprintf(fp, "\n\n"); fprintf(fp, "typedef struct _kr_interface_%d_t \n", fixed_data->datasrc_id); fprintf(fp, "{\n"); return fixed_data; }
/*static dataitem*/ T_KRDDI *kr_ddi_construct(T_KRParamDDIDef *ptParamDDIDef, T_KRModule *ptModule, KRGetTypeFunc pfGetType, KRGetValueFunc pfGetValue) { T_KRDDI *ptDDI = kr_calloc(sizeof(T_KRDDI)); if (ptDDI == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptDDI failed!"); return NULL; } ptDDI->ptParamDDIDef = ptParamDDIDef; ptDDI->lDDIId = ptParamDDIDef->lDdiId; ptDDI->ptDDICalc = kr_calc_construct(ptParamDDIDef->caDdiFilterFormat[0], \ ptParamDDIDef->caDdiFilterString, pfGetType, pfGetValue); ptDDI->eValueType = ptParamDDIDef->caDdiValueType[0]; /*get the retrieve data function from module*/ if (ptParamDDIDef->caDdiAggrFunc[0] != '\0') { ptDDI->pfDDIAggr = (KRDDIAggrFunc )kr_module_symbol(ptModule, ptParamDDIDef->caDdiAggrFunc); if (ptDDI->pfDDIAggr == NULL) { KR_LOG(KR_LOGERROR, "kr_module_symbol [%s] error!", \ ptParamDDIDef->caDdiAggrFunc); return NULL; } } ptDDI->eValueInd = KR_VALUE_UNSET; ptDDI->ptRelated = kr_hashtable_new(kr_pointer_hash, kr_pointer_equal); return ptDDI; }
T_KRHDITable *kr_hdi_table_construct(T_KRShmHDI *shm_hdi, T_KRModule *datamodule) { T_KRHDITable *krhditable = kr_calloc(sizeof(T_KRHDITable)); if (krhditable == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc krhditable failed!"); return NULL; } krhditable->ptShmHDIs = shm_hdi; krhditable->lHDICnt = shm_hdi->lHDIDefCnt; krhditable->ptHDITable = \ kr_hashtable_new_full(kr_long_hash, kr_long_equal, \ NULL, (KRDestroyNotify )kr_hdi_destruct); int i = 0; T_KRHDI *krhdi = NULL; for (i=0; i<shm_hdi->lHDIDefCnt; ++i) { krhdi = kr_hdi_construct(&shm_hdi->stShmHDIDef[i], datamodule); if (krhdi == NULL) { KR_LOG(KR_LOGERROR, "kr_hdi_construct [%d] failed!", i); kr_hashtable_destroy(krhditable->ptHDITable); kr_free(krhditable); return NULL; } kr_hashtable_insert(krhditable->ptHDITable, &krhdi->lHDIId, krhdi); } krhditable->tConstructTime = shm_hdi->tLastLoadTime; return krhditable; }
T_KRHDITable *kr_hdi_table_construct(T_KRParamHDI *ptParamHDI, T_KRModule *ptModule) { T_KRHDITable *ptHdiTable = kr_calloc(sizeof(T_KRHDITable)); if (ptHdiTable == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptHdiTable failed!"); return NULL; } ptHdiTable->ptParamHDI = ptParamHDI; ptHdiTable->lHDICnt = ptParamHDI->lHDIDefCnt; ptHdiTable->ptHDITable = \ kr_hashtable_new_full(kr_long_hash, kr_long_equal, \ NULL, (KRDestroyNotify )kr_hdi_destruct); int i = 0; T_KRHDI *ptHDI = NULL; for (i=0; i<ptParamHDI->lHDIDefCnt; ++i) { ptHDI = kr_hdi_construct(&ptParamHDI->stParamHDIDef[i], ptModule); if (ptHDI == NULL) { KR_LOG(KR_LOGERROR, "kr_hdi_construct [%d] failed!", i); kr_hashtable_destroy(ptHdiTable->ptHDITable); kr_free(ptHdiTable); return NULL; } kr_hashtable_insert(ptHdiTable->ptHDITable, &ptHDI->lHDIId, ptHDI); } ptHdiTable->tConstructTime = ptParamHDI->tLastLoadTime; return ptHdiTable; }
/* initialize rule detecting context */ T_KRContext *kr_context_init(T_KRContextEnv *ptEnv) { T_KRContext *ptContext = kr_calloc(sizeof(T_KRContext)); if (ptContext == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptContext failed!"); return NULL; } ptContext->ptEnv = ptEnv; /* reconnect database in thread */ T_DbsEnv *ptDbsEnv = ((T_KRContextEnv *)ptEnv)->ptDbsEnv; ptContext->ptDbsEnv = dbsConnect(ptDbsEnv->dsn, ptDbsEnv->user, ptDbsEnv->pass); if (ptContext->ptDbsEnv == NULL) { KR_LOG(KR_LOGERROR, "dbsConnect [%s] [%s] [%s] failed!", \ ptDbsEnv->dsn, ptDbsEnv->user, ptDbsEnv->pass); kr_free(ptContext); return NULL; } /* construct data */ ptContext->ptData = kr_data_construct(ptEnv->ptParam, ptEnv->dataModule, ptDbsEnv, ptEnv->ptHDICache, kr_data_get_type, kr_data_get_value); if (ptContext->ptData == NULL) { KR_LOG(KR_LOGERROR, "kr_data_construct failed!"); dbsDisconnect(ptContext->ptDbsEnv); kr_free(ptContext); return NULL; } /* construct flow */ return ptContext; }
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; }
int kr_coordi_handle_svron(T_KREventLoop *el, int fd, T_KRMessage *krmsg) { KR_LOG(KR_LOGDEBUG, "Server [%s] Online [%d]!", krmsg->serverid, fd); char ip[32]; int port; kr_net_peer_to_string(fd, ip, &port); cJSON *apply_json = cJSON_Parse(krmsg->msgbuf); if (apply_json == NULL) { KR_LOG(KR_LOGERROR, "parse apply json failed!"); return -1; } int weights = (int )cJSON_GetNumber(apply_json, "weights"); if (weights <= 0) { KR_LOG(KR_LOGERROR, "weights [%d] invalid!", weights); return -1; } int replica = (int )cJSON_GetNumber(apply_json, "replica"); if (replica <= 0) { KR_LOG(KR_LOGERROR, "replica [%d] invalid!", replica); return -1; } /* Check this server whether exists */ T_KRActualNode *node = \ kr_conhash_lookup(krcoordi.servers, krmsg->serverid); if (node != NULL) { KR_LOG(KR_LOGDEBUG, "Server %s already exists!", krmsg->serverid); return 0; } /* allocate connector for this server */ T_KRCoordiConnector *ptServer = kr_calloc(sizeof(T_KRCoordiConnector)); if (ptServer == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptServer error!"); return -1; } ptServer->fd = fd; ptServer->role = KR_COORDI_SERVER; strcpy(ptServer->id, krmsg->serverid); strcpy(ptServer->ip, ip); ptServer->aplnum = 0; ptServer->rplnum = 0; ptServer->errnum = 0; ptServer->weights = weights; ptServer->replica = replica; /* Manage this server with consistent hashing */ kr_conhash_add(krcoordi.servers, ptServer->id, ptServer->weights, ptServer); /*add this server fd--id mapping to fdtable*/ kr_hashtable_replace(krcoordi.fdtable, &ptServer->fd, ptServer); return 0; }
/*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; }
/* initialize rule detecting context */ T_KRContext *kr_context_init(T_KRContextEnv *ptEnv) { T_KRContext *ptContext = kr_calloc(sizeof(T_KRContext)); if (ptContext == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptContext failed!"); return NULL; } ptContext->ptEnv = ptEnv; /* reconnect database in thread */ T_DbsEnv *dbsenv = ((T_KRContextEnv *)ptEnv)->ptDbs; ptContext->ptDbs = dbsConnect(dbsenv->dsn, dbsenv->user, dbsenv->pass); if (ptContext->ptDbs == NULL) { KR_LOG(KR_LOGERROR, "dbsConnect [%s] [%s] [%s] failed!", \ dbsenv->dsn, dbsenv->user, dbsenv->pass); kr_free(ptContext); return NULL; } ptContext->ptArg = kr_calloc(sizeof(T_KRContextArg)); if (ptContext->ptArg == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptContext->ptArg failed!"); dbsDisconnect(ptContext->ptDbs); kr_free(ptContext); return NULL; } ptContext->ptDym = kr_dynamicmem_init(ptEnv->ptShm, \ ptEnv->dataModule, ptEnv->ruleModule); if (ptContext->ptDym == NULL) { KR_LOG(KR_LOGERROR, "kr_dynamicmem_init failed!"); dbsDisconnect(ptContext->ptDbs); kr_free(ptContext->ptArg); kr_free(ptContext); return NULL; } return ptContext; }
/*fixed format source*/ void *fixed_source_pre_func(T_KRParamInput *ptParamInput) { fixed_data = kr_calloc(sizeof(*fixed_data)); /* Open Source File */ fixed_data->datasrc_id = ptParamInput->lInputId; strcpy(fixed_data->datasrc_name, ptParamInput->caInputName); snprintf(fixed_data->filename, sizeof(fixed_data->filename), \ "%s_fixed.c", ptParamInput->caInputName); fixed_data->fp = fopen(fixed_data->filename, "w"); if (fixed_data->fp == NULL) { fprintf(stdout, "open output file: %s failed\n", fixed_data->filename); kr_free(fixed_data); return NULL; } /*generate pre&post function*/ FILE *fp = fixed_data->fp; fprintf(fp, "#include <stdio.h> \n"); fprintf(fp, "#include <stdlib.h> \n"); fprintf(fp, "#include <stdint.h> \n"); fprintf(fp, "#include <string.h> \n"); fprintf(fp, "#include <time.h> \n"); fprintf(fp, "\n"); /*generate fixed struct definition */ kr_traversal_fields(ptParamInput, pre_func, fixed_define_func, post_func); /* map_pre_func */ fprintf(fp, "void *fixed_map_pre_func_%d(void *msg)\n", fixed_data->datasrc_id); fprintf(fp, "{ \n"); fprintf(fp, " return msg;\n"); fprintf(fp, "} \n"); fprintf(fp, "\n\n"); /* map_func_post */ fprintf(fp, "void fixed_map_post_func_%d(void *data)\n", fixed_data->datasrc_id); fprintf(fp, "{ \n"); fprintf(fp, " return;\n"); fprintf(fp, "} \n"); fprintf(fp, "\n\n"); /* map_func */ fprintf(fp, "void fixed_map_func_%d(void *fldval, int fldno, int fldlen, void *data)\n", fixed_data->datasrc_id); fprintf(fp, "{ \n"); fprintf(fp, " T_KRInterface%d *root = (T_KRInterface%d *)data; \n", \ fixed_data->datasrc_id, fixed_data->datasrc_id); fprintf(fp, " memset(fldval, 0x00, fldlen); \n"); fprintf(fp, " switch(fldno) {\n"); return fixed_data; }
/* create a new calctree node */ T_KRCalcTree *kr_calctree_node_new(E_KRCalcKind kind) { int i; T_KRCalcTree *t = (T_KRCalcTree *)kr_calloc(sizeof(T_KRCalcTree)); if (t == NULL) { fprintf(stderr, "kr_calloc T_KRCalcTree failed!\n"); return NULL; } t->kind = kind; t->ind = KR_VALUE_UNSET; t->type = KR_TYPE_UNKNOWN; for (i=0; i<MAXCHILDREN; i++) { t->child[i] = NULL; } return t; }
T_KRCalc *kr_calc_construct(E_KRCalcFormat format, char *calcstr) { T_KRCalc *krcalc = (T_KRCalc *)kr_calloc(sizeof(T_KRCalc)); krcalc->calc_format = format; krcalc->calc_string = kr_strdup(calcstr); krcalc->get_type_cb = kr_data_get_type; krcalc->get_value_cb = kr_data_get_value; if (kr_calc_parse(krcalc) != 0) { fprintf(stderr, "kr_calc_parse failed[%s]!\n", krcalc->err_msg); return NULL; } kr_calc_dump(krcalc, stdout); return krcalc; }
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; }
void *kr_data_item_ddi_new(T_KRDataItem *ptDataItem) { T_KRParamDdi *ptParamDdi = (T_KRParamDdi *)ptDataItem->ptDataItemDef; T_KRDdi *ptDdi = kr_calloc(sizeof(*ptDdi)); if (ptDdi == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptDdi failed!"); return NULL; } ptDdi->ptFilterCalc = kr_calc_construct( ptParamDdi->caDdiFilterFormat[0], \ ptParamDdi->caDdiFilterString, \ ptDataItem->pfGetType, \ ptDataItem->pfGetValue); if (ptDdi->ptFilterCalc == NULL) { KR_LOG(KR_LOGERROR, "kr_calc_construct filter failed!"); return NULL; } /* ptDdi->ptResultCalc = kr_calc_construct( ptParamSdi->caSdiResultFormat[0], \ ptParamSdi->caSdiResultString, \ ptDataItem->pfGetType, \ ptDataItem->pfGetValue); if (ptDdi->ptResultCalc == NULL) { KR_LOG(KR_LOGERROR, "kr_calc_construct result failed!"); return NULL; } */ ptDdi->ptRelated = kr_hashtable_new(kr_pointer_hash, kr_pointer_equal); if (ptDdi->ptRelated == NULL) { KR_LOG(KR_LOGERROR, "kr_hashtable_new related failed!"); return NULL; } ptDataItem->ptPrivate = ptDdi; return ptDdi; }
T_KRSkipListNode * kr_skiplist_create_node(int level, unsigned int key, void *value) { T_KRSkipListNode *node = kr_malloc(sizeof(T_KRSkipListNode)); if (node == NULL) { fprintf(stderr, "kr_malloc node failed!\n"); return NULL; } node->key = key; node->value = value; node->forward = kr_calloc(level * sizeof(T_KRSkipListNode)); if (node->forward == NULL) { fprintf(stderr, "kr_malloc node->forward failed!\n"); kr_free(node); return NULL; } return node; }
/*data item*/ T_KRDataItem *kr_data_item_new(void *ptDataItemDef, char kind, int id, KRGetTypeFunc pfGetType, KRGetValueFunc pfGetValue, KRDataItemNewFunc pfDataItemNew, KRDataItemAggrFunc pfDataItemAggr, KRDataItemFreeFunc pfDataItemFree) { T_KRDataItem *ptDataItem = kr_calloc(sizeof(T_KRDataItem)); if (ptDataItem == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptDataItem failed!"); return NULL; } ptDataItem->ptDataItemDef = ptDataItemDef; ptDataItem->cDataItemKind = kind; ptDataItem->lDataItemId = id; ptDataItem->pfGetType = pfGetType; ptDataItem->pfGetValue = pfGetValue; ptDataItem->pfDataItemNew = pfDataItemNew; ptDataItem->pfDataItemAggr = pfDataItemAggr; ptDataItem->pfDataItemFree = pfDataItemFree; snprintf(ptDataItem->caDataItemId, sizeof(ptDataItem->caDataItemId), "%c_%d", kind, id); //initialize variable ptDataItem->ptCurrRec = NULL; ptDataItem->eValueInd == KR_VALUE_UNSET; //new private data if new function specified if (ptDataItem->pfDataItemNew != NULL) { ptDataItem->ptPrivate = ptDataItem->pfDataItemNew(ptDataItem); if (ptDataItem->ptPrivate == NULL) { KR_LOG(KR_LOGERROR, "call pfDataItemNew failed!"); kr_free(ptDataItem); return NULL; } } return ptDataItem; }
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; }
/*history dataitem*/ T_KRHDI *kr_hdi_construct(T_KRShmHDIDef *hdi_def, T_KRModule *datamodule) { T_KRHDI *krhdi = kr_calloc(sizeof(T_KRHDI)); if (krhdi == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc krhdi failed!"); return NULL; } krhdi->ptShmHDIDef = hdi_def; krhdi->lHDIId = hdi_def->lHdiId; krhdi->eValueType = hdi_def->caHdiValueType[0]; /*get the retrieve data function from module*/ if (hdi_def->caHdiAggrFunc[0] != '\0') { krhdi->HDIAggrFunc = (KRHDIAggrFunc )kr_module_symbol(datamodule, hdi_def->caHdiAggrFunc); if (krhdi->HDIAggrFunc == NULL) { KR_LOG(KR_LOGERROR, "kr_module_symbol [%s] error!", \ hdi_def->caHdiAggrFunc); return NULL; } } krhdi->ptCurrRec = NULL; return krhdi; }
/*history dataitem*/ T_KRHDI *kr_hdi_construct(T_KRParamHDIDef *ptParamHDIDef, T_KRModule *ptModule) { T_KRHDI *ptHDI = kr_calloc(sizeof(T_KRHDI)); if (ptHDI == NULL) { KR_LOG(KR_LOGERROR, "kr_calloc ptHDI failed!"); return NULL; } ptHDI->ptParamHDIDef = ptParamHDIDef; ptHDI->lHDIId = ptParamHDIDef->lHdiId; ptHDI->eValueType = ptParamHDIDef->caHdiValueType[0]; /*get the retrieve data function from module*/ if (ptParamHDIDef->caHdiAggrFunc[0] != '\0') { ptHDI->pfHDIAggr = (KRHDIAggrFunc )kr_module_symbol(ptModule, ptParamHDIDef->caHdiAggrFunc); if (ptHDI->pfHDIAggr == NULL) { KR_LOG(KR_LOGERROR, "kr_module_symbol [%s] error!", \ ptParamHDIDef->caHdiAggrFunc); return NULL; } } ptHDI->ptCurrRec = NULL; return ptHDI; }
/** * kr_ntree_new: * @data: the data of the new node * * Creates a new #T_KRNTree containing the given data. * Used to create the first node in a tree. * * Returns: a new #T_KRNTree */ T_KRNTree* kr_ntree_new(kr_pointer data) { T_KRNTree *node = kr_calloc(sizeof(T_KRNTree)); node->data = data; return node; }
T_KRMessage *kr_message_alloc(void) { /*need calloc here*/ T_KRMessage *krmsg = kr_calloc(sizeof(*krmsg)); return krmsg; }