Exemple #1
0
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;
}
int kr_coordi_handle_reply(T_KREventLoop *el, int fd, T_KRMessage *krmsg)
{
    KR_LOG(KR_LOGDEBUG, "Client [%s] Got Reply[%s] From Server [%s]!", \
        krmsg->clientid, krmsg->msgid, krmsg->serverid);
        
    /* Check this server whether allowed */
    T_KRActualNode *server = NULL;
    server = kr_conhash_lookup(krcoordi.servers, krmsg->serverid);
    if (server == NULL) {
        KR_LOG(KR_LOGERROR, "Server %s not allowed!", krmsg->serverid);
        return -1;
    }
    T_KRCoordiConnector *ptServer = (T_KRCoordiConnector *)server->priv;
    ++ptServer->rplnum;
        
    /* Check this client whether exists */
    T_KRListNode *node = kr_list_search(krcoordi.clients, krmsg->clientid);
    if (node == NULL) {
        KR_LOG(KR_LOGERROR, "Client %s not exists!", krmsg->clientid);
        return -1;
    }
    T_KRCoordiConnector *ptClient = (T_KRCoordiConnector *)node->value;
    
    /*send reply to client*/
    int writeLen = kr_message_write(ptClient->fd, krmsg);
    if (writeLen <= 0) {/* write message failure */        
        KR_LOG(KR_LOGERROR, "write message error[%s]!", krcoordi.neterr);
        strcpy(ptClient->neterr, krcoordi.neterr);
        ++ptClient->errnum;
        return -1;
    }
    
    ++ptClient->rplnum;
    return 0;
}
int kr_coordi_handle_clioff(T_KREventLoop *el, int fd, T_KRMessage *krmsg)
{
    KR_LOG(KR_LOGDEBUG, "Client [%s] Offline [%d]!", krmsg->clientid, fd);

    /* Check this client whether exists */
    T_KRListNode *node = kr_list_search(krcoordi.clients, krmsg->clientid);
    if (node == NULL) {
        KR_LOG(KR_LOGDEBUG, "Client %s not exists!", krmsg->clientid);
        return 0;
    }
   
    /*remove from list*/
    T_KRCoordiConnector *ptClient = (T_KRCoordiConnector *)node->value;
    kr_list_remove(krcoordi.clients, ptClient->id);

    /* remove from fdtable */
    kr_hashtable_remove(krcoordi.fdtable, &fd);
    
    /* Free this client */
    kr_event_file_delete(el, fd, KR_EVENT_READABLE);
    close(fd);
    
    kr_free(ptClient);
    
    return 0;
}
Exemple #4
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;
}
Exemple #5
0
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;
}
Exemple #6
0
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;
}
Exemple #7
0
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;
}
Exemple #8
0
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;
}
Exemple #9
0
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);
}
int kr_coordi_handle_apply(T_KREventLoop *el, int fd, T_KRMessage *krmsg)
{
    KR_LOG(KR_LOGDEBUG, "Client [%s] Send Apply[%s] To Server [%s]!", \
        krmsg->clientid, krmsg->msgid, krmsg->serverid);
    
    /* Check this client whether allowed */
    T_KRListNode *node = kr_list_search(krcoordi.clients, krmsg->clientid);
    if (node == NULL) {
        KR_LOG(KR_LOGERROR, "Client %s not allowed!", krmsg->clientid);
        return -1;
    }
    T_KRCoordiConnector *ptClient = (T_KRCoordiConnector *)node->value;
    ++ptClient->aplnum;

    T_KRActualNode *server = NULL;
    /*if apply message specified the destination server*/
    if (krmsg->serverid[0] != '\0') {
        /* Check this server whether exists */
        server = kr_conhash_lookup(krcoordi.servers, krmsg->serverid);
        if (server == NULL) {
            KR_LOG(KR_LOGDEBUG, "Specified Server [%s] doesn't exists!", \
                krmsg->serverid);
        }
    }
    
    /*choose server according to the key*/
    if (server == NULL) {
        server = kr_conhash_locate(krcoordi.servers, krmsg->msgid);
        if (server == NULL) {
            KR_LOG(KR_LOGERROR, "kr_conhash_locate %s failed!", \
               krmsg->msgid);
            return -1;   
        }
    }
    
    /* padding serverid field */
    T_KRCoordiConnector *ptServer = (T_KRCoordiConnector *)server->priv;
    strcpy(krmsg->serverid, ptServer->id);
    ++ptServer->aplnum;
    
    KR_LOG(KR_LOGDEBUG, "Located Server [%s] with Consistent hashing [%s]!", \
                krmsg->serverid, krmsg->msgid);
    
    /* send to the one who's the specified or need replication */
    kr_conhash_foreach_node(krcoordi.servers, \
        (KRForEachFunc )kr_coordi_send_to_server, krmsg);

    return 0;
}
Exemple #11
0
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_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;
}