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_KRIndex* kr_get_db_index(T_KRDB *krdb, int id) { T_KRListNode *ptListNode = kr_list_search(krdb->pIndexList, &id); if (ptListNode != NULL) { return (T_KRIndex *)kr_list_value(ptListNode); } return NULL; }
T_KRIndex* kr_get_table_index(T_KRTable *krtable, int id) { T_KRListNode *ptListNode = kr_list_search(krtable->pIndexList, &id); if (ptListNode != NULL) { return (T_KRIndex *)kr_list_value(ptListNode); } return NULL; }
T_KRTable* kr_get_table(T_KRDB *krdb, int id) { T_KRListNode *ptListNode = kr_list_search(krdb->pTableList, &id); if (ptListNode != NULL) { return (T_KRTable *)kr_list_value(ptListNode); } return NULL; }
T_KRTable* kr_table_get(T_KRDB *ptDB, int iTableId) { T_KRListNode *ptListNode = kr_list_search(ptDB->pTableList, &iTableId); if (ptListNode != NULL) { return (T_KRTable *)kr_list_value(ptListNode); } return NULL; }
T_KRIndex* kr_index_get(T_KRDB *ptDB, int iIndexId) { T_KRListNode *ptListNode = kr_list_search(ptDB->pIndexList, &iIndexId); if (ptListNode != NULL) { return (T_KRIndex *)kr_list_value(ptListNode); } return NULL; }
T_KROutputDefine* kr_output_get_define(T_KROutput *ptOutput, int iOutputId) { T_KRListNode *ptListNode = kr_list_search(ptOutput->ptOutputDefineList, &iOutputId); if (ptListNode == NULL) { return NULL; } return (T_KROutputDefine *)kr_list_value(ptListNode); }
static void kr_rebuild_index_del(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) kr_list_remove(pRecList, krrecord); if (kr_list_length(pRecList) != 0 ) { T_KRRecord *ptRecord = kr_list_value(kr_list_last(pRecList)); T_KRIndex *newIndex = krindex; /* the dropped record and the last record of current list * belongs to different tables */ if (krrecord->ptTable != ptRecord->ptTable) { newIndex = kr_get_table_index(ptRecord->ptTable, krindex->iIndexId); } key = kr_get_field_value(ptRecord, newIndex->iIndexFieldId); kr_hashtable_replace(newIndex->pHashTable, key, pRecList); } else { kr_list_destroy(pRecList); kr_hashtable_remove(krindex->pHashTable, key); } }
T_KRIndexTable* kr_index_table_get(T_KRDB *ptDB, int iIndexId, int iTableId) { 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 stIndexTable = {0}; stIndexTable.ptIndex = ptIndex; stIndexTable.ptTable = ptTable; T_KRListNode *ptListNode = kr_list_search(ptDB->pIndexTableList, &stIndexTable); if (ptListNode != NULL) { return (T_KRIndexTable *)kr_list_value(ptListNode); } return NULL; }
int kr_ddi_aggr_func(T_KRDDI *krddi, T_KRContext *krcontext) { int iResult = -1; int iAbsLoc = -1; int iRelLoc = -1; time_t tCurrTransTime = kr_get_transtime(krcontext->ptCurrRec); time_t tRecTransTime; T_KRListNode *node = krcontext->ptRecList->tail; while(node) { krcontext->ptRecord = (T_KRRecord *)kr_list_value(node); iAbsLoc++; /*绝对位置加一*/ /*当笔是否包含校验*/ if ((krddi->ptShmDDIDef->caStatisticsType[0] == \ KR_DDI_STATISTICS_EXCLUDE) && (krcontext->ptRecord == krcontext->ptCurrRec)) { node = node->prev; continue; } /*统计数据源校验*/ if (((T_KRTable *)krcontext->ptRecord->ptTable)->iTableId != \ krddi->ptShmDDIDef->lStatisticsDatasrc) { node = node->prev; continue; } /*时间窗口校验*/ tRecTransTime = kr_get_transtime(krcontext->ptRecord); if ((tCurrTransTime - tRecTransTime) > krddi->ptShmDDIDef->lStatisticsValue ) { node = node->prev; continue; } /*过滤器校验*/ iResult = kr_calc_eval(krddi->ptDDICalc, krcontext); if (iResult != 0) { KR_LOG(KR_LOGERROR, "kr_calc_eval[%ld] failed!", krddi->lDDIId); return -1; } else if (krddi->ptDDICalc->result_type != KR_TYPE_BOOL) { KR_LOG(KR_LOGERROR, "result_type of ddi_calc must be boolean!"); return -1; } else if (krddi->ptDDICalc->result_ind != KR_VALUE_SETED || !krddi->ptDDICalc->result_value.b) { node = node->prev; continue; } iRelLoc++; /*相对位置加一*/ /*获取数据项值*/ E_KRType type = kr_get_field_type(krcontext->ptRecord, krddi->ptShmDDIDef->lStatisticsField); void *val = kr_get_field_value(krcontext->ptRecord, krddi->ptShmDDIDef->lStatisticsField); U_KRValue stValue = {0}; switch(type) { case KR_TYPE_INT: stValue.i = *(int *)val; break; case KR_TYPE_LONG: stValue.l = *(long *)val; break; case KR_TYPE_DOUBLE: stValue.d = *(double *)val; break; case KR_TYPE_STRING: stValue.s = (char *)val; break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", type); return -1; } switch(krddi->ptShmDDIDef->caStatisticsMethod[0]) { case KR_DDI_METHOD_SUM: switch(krddi->eValueType) { case KR_TYPE_INT: krddi->uValue.i = krddi->uValue.i + stValue.i; break; case KR_TYPE_LONG: krddi->uValue.l = krddi->uValue.l + stValue.l; break; case KR_TYPE_DOUBLE: krddi->uValue.d = krddi->uValue.d + stValue.d; break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", krddi->eValueType); return -1; } break; case KR_DDI_METHOD_MIN: switch(krddi->eValueType) { case KR_TYPE_INT: krddi->uValue.i = MIN(krddi->uValue.i, stValue.i); break; case KR_TYPE_LONG: krddi->uValue.l = MIN(krddi->uValue.l, stValue.l); break; case KR_TYPE_DOUBLE: krddi->uValue.d = MIN(krddi->uValue.d, stValue.d); break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", krddi->eValueType); return -1; } break; case KR_DDI_METHOD_MAX: switch(krddi->eValueType) { case KR_TYPE_INT: krddi->uValue.i = MAX(krddi->uValue.i, stValue.i); break; case KR_TYPE_LONG: krddi->uValue.l = MAX(krddi->uValue.l, stValue.l); break; case KR_TYPE_DOUBLE: krddi->uValue.d = MAX(krddi->uValue.d, stValue.d); break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", krddi->eValueType); return -1; } break; case KR_DDI_METHOD_COUNT: switch(krddi->eValueType) { case KR_TYPE_INT: krddi->uValue.i = krddi->uValue.i + 1; break; case KR_TYPE_LONG: krddi->uValue.l = krddi->uValue.l + 1; break; case KR_TYPE_DOUBLE: krddi->uValue.d = krddi->uValue.d + 1; break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", krddi->eValueType); return -1; } break; case KR_DDI_METHOD_CON_INC: //TODO break; case KR_DDI_METHOD_CON_DEC: //TODO break; case KR_DDI_METHOD_CNT_DIS: //TODO break; default: KR_LOG(KR_LOGERROR, "Bad Method [%c]!", \ krddi->ptShmDDIDef->caStatisticsMethod[0]); return -1; } /*add this record to related*/ kr_hashtable_insert(krddi->ptRelated, \ krcontext->ptRecord, krcontext->ptRecord); /* This is what the difference between SDI and DDI: * SDI only set once, while DDI still need to traversal all the list */ krddi->eValueInd = KR_VALUE_SETED; node = node->prev; } return 0; }
int kr_sdi_aggr_func(T_KRSDI *krsdi, T_KRContext *krcontext) { int iResult = -1; int iAbsLoc = -1; int iRelLoc = -1; T_KRListNode *node = krcontext->ptRecList->tail; while(node) { krcontext->ptRecord = (T_KRRecord *)kr_list_value(node); iAbsLoc++; /*绝对位置加一*/ /*数据项绝对统计定位校验*/ if (krsdi->ptShmSDIDef->caLocationProperty[0] == KR_LOC_ABSOLUTE) { if (krsdi->ptShmSDIDef->lStatisticsLocation != iAbsLoc) { node = node->prev; continue; } } /*统计数据源校验*/ if (((T_KRTable *)krcontext->ptRecord->ptTable)->iTableId != \ krsdi->ptShmSDIDef->lStatisticsDatasrc) { node = node->prev; continue; } /*过滤器校验*/ iResult = kr_calc_eval(krsdi->ptSDICalc, krcontext); if (iResult != 0) { KR_LOG(KR_LOGERROR, "kr_calc_eval [%ld] failed!", krsdi->lSDIId); return -1; } else if (krsdi->ptSDICalc->result_type != KR_TYPE_BOOL) { KR_LOG(KR_LOGERROR, "result_type of sdi_calc must be boolean!"); return -1; } else if (krsdi->ptSDICalc->result_ind != KR_VALUE_SETED || !krsdi->ptSDICalc->result_value.b) { node = node->prev; continue; } iRelLoc++; /*相对位置加一*/ /*数据项相对统计定位校验*/ if (krsdi->ptShmSDIDef->caLocationProperty[0] == KR_LOC_RELATIVE) { if (krsdi->ptShmSDIDef->lStatisticsLocation != iRelLoc) { node = node->prev; continue; } } /*获取数据项值*/ void *val = kr_get_field_value(krcontext->ptRecord, \ krsdi->ptShmSDIDef->lStatisticsField); switch(krsdi->eValueType) { case KR_TYPE_INT: krsdi->uValue.i = *(int *)val; break; case KR_TYPE_LONG: krsdi->uValue.l = *(long *)val; break; case KR_TYPE_DOUBLE: krsdi->uValue.d = *(double *)val; break; case KR_TYPE_STRING: krsdi->uValue.s = kr_strdup(val); break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", krsdi->eValueType); return -1; } /*add this record to related*/ kr_hashtable_insert(krsdi->ptRelated, \ krcontext->ptRecord, krcontext->ptRecord); /* This is what the difference between SDI and DDI: * SDI only set once, while DDI still need to traversal all the list */ krsdi->eValueInd = KR_VALUE_SETED; break; node = node->prev; } return 0; }
int kr_data_item_ddi_aggr(T_KRDataItem *ptDataItem, T_KRContext *ptContext) { T_KRParamDdi *ptParamDdi = (T_KRParamDdi *)ptDataItem->ptDataItemDef; T_KRDdi *ptDdi = (T_KRDdi *)ptDataItem->ptPrivate; T_KRRecord *ptCurrRec = kr_context_get_data(ptContext, "curr_rec"); if (ptCurrRec == NULL) { KR_LOG(KR_LOGERROR, "no current record in context"); return -1; } T_KRDB *ptDB = kr_context_get_data(ptContext, "db"); if (ptDB == NULL) { KR_LOG(KR_LOGERROR, "no db in context"); return -1; } //get record list ptDdi->ptRecList = kr_db_select(ptDB, ptParamDdi->lStatisticsIndex, NULL, //FIXME:key ptParamDdi->lDdiId, //FIXME:tBeginTime ptParamDdi->lDdiId, //FIXME:tEndTime KR_FIELDID_TRANSTIME //FIXME:iSortFieldId ); int iResult = -1, iAbsLoc = -1, iRelLoc = -1; T_KRListNode *node = ptDdi->ptRecList->tail; while(node) { T_KRRecord *ptTravRec = (T_KRRecord *)kr_list_value(node); kr_context_add_data(ptContext, "trav_rec", ptTravRec); iAbsLoc++; if ((ptParamDdi->caStatisticsType[0] == \ KR_DDI_STATISTICS_EXCLUDE) && (ptTravRec == ptCurrRec)) { node = node->prev; continue; } if (ptParamDdi->lStatisticsDatasrc != \ kr_record_get_input_id(ptTravRec)) { node = node->prev; continue; } time_t tCurrTransTime = kr_record_get_transtime(ptCurrRec); time_t tRecTransTime = kr_record_get_transtime(ptTravRec); if ((tCurrTransTime - tRecTransTime) > ptParamDdi->lStatisticsValue ) { node = node->prev; continue; } iResult = kr_calc_eval(ptDdi->ptFilterCalc, ptContext); if (iResult != 0) { KR_LOG(KR_LOGERROR, "kr_calc_eval filter failed!"); return -1; } else if (kr_calc_type(ptDdi->ptFilterCalc) != KR_TYPE_BOOL) { KR_LOG(KR_LOGERROR, "filter calc type must be boolean!"); return -1; } else if (kr_calc_ind(ptDdi->ptFilterCalc) != KR_VALUE_SETED || !kr_calc_value(ptDdi->ptFilterCalc)->b) { node = node->prev; continue; } iRelLoc++; E_KRType type = kr_record_get_field_type(ptTravRec, ptParamDdi->lStatisticsField); void *val = kr_record_get_field_value(ptTravRec, ptParamDdi->lStatisticsField); U_KRValue stValue = {0}; switch(type) { case KR_TYPE_INT: stValue.i = *(int *)val; break; case KR_TYPE_LONG: stValue.l = *(long *)val; break; case KR_TYPE_DOUBLE: stValue.d = *(double *)val; break; case KR_TYPE_STRING: stValue.s = (char *)val; break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", type); return -1; } /* switch(ptParamDdi->caStatisticsMethod[0]) { case KR_DDI_METHOD_SUM: switch(ptDataItem->eValueType) { case KR_TYPE_INT: ptDdi->uValue.i = ptDdi->uValue.i + stValue.i; break; case KR_TYPE_LONG: ptDdi->uValue.l = ptDdi->uValue.l + stValue.l; break; case KR_TYPE_DOUBLE: ptDdi->uValue.d = ptDdi->uValue.d + stValue.d; break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", ptDdi->eValueType); return -1; } break; case KR_DDI_METHOD_MIN: switch(ptDataItem->eValueType) { case KR_TYPE_INT: ptDdi->uValue.i = MIN(ptDdi->uValue.i, stValue.i); break; case KR_TYPE_LONG: ptDdi->uValue.l = MIN(ptDdi->uValue.l, stValue.l); break; case KR_TYPE_DOUBLE: ptDdi->uValue.d = MIN(ptDdi->uValue.d, stValue.d); break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", ptDdi->eValueType); return -1; } break; case KR_DDI_METHOD_MAX: switch(ptDataItem->eValueType) { case KR_TYPE_INT: ptDdi->uValue.i = MAX(ptDdi->uValue.i, stValue.i); break; case KR_TYPE_LONG: ptDdi->uValue.l = MAX(ptDdi->uValue.l, stValue.l); break; case KR_TYPE_DOUBLE: ptDdi->uValue.d = MAX(ptDdi->uValue.d, stValue.d); break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", ptDdi->eValueType); return -1; } break; case KR_DDI_METHOD_COUNT: switch(ptDataItem->eValueType) { case KR_TYPE_INT: ptDdi->uValue.i = ptDdi->uValue.i + 1; break; case KR_TYPE_LONG: ptDdi->uValue.l = ptDdi->uValue.l + 1; break; case KR_TYPE_DOUBLE: ptDdi->uValue.d = ptDdi->uValue.d + 1; break; default: KR_LOG(KR_LOGERROR, "Bad FieldType [%c]!", ptDdi->eValueType); return -1; } break; case KR_DDI_METHOD_CON_INC: //TODO break; case KR_DDI_METHOD_CON_DEC: //TODO break; case KR_DDI_METHOD_CNT_DIS: //TODO break; default: KR_LOG(KR_LOGERROR, "Bad Method [%c]!", \ ptDdi->ptParamDDIDef->caStatisticsMethod[0]); return -1; } */ /*add this record to related*/ kr_hashtable_insert(ptDdi->ptRelated, ptTravRec, ptTravRec); node = node->prev; } /* This is what the difference between DDI and DDI: * DDI only set once, while DDI still need to traversal all the list */ ptDataItem->eValueInd = KR_VALUE_SETED; return 0; }