Exemplo n.º 1
0
static int _kr_data_item_hdi_load(char *psParamClassName, 
        char *psParamObjectKey, char *psParamObjectString, 
        void *ptParamObject, void *data)
{
    T_KRParamHdi *ptParamHdi = (T_KRParamHdi *)ptParamObject;
    T_KRData *ptData = (T_KRData *)data;

    T_KRDataItem *ptDataItem = kr_data_item_new(
            ptParamHdi,
            ptParamHdi->caHdiName[0], //FIXME: 
            ptParamHdi->lHdiId, 
            ptData->pfGetType,
            ptData->pfGetValue,
            kr_data_item_hdi_new,
            kr_data_item_hdi_aggr,
            kr_data_item_hdi_free);
    if (ptDataItem == NULL) {
        KR_LOG(KR_LOGERROR, "kr_data_item_create [%ld] failed!", \
                ptParamHdi->lHdiId);
        return -1; 
    }

    //insert into item table
    kr_hashtable_replace(ptData->ptItemTable, ptDataItem->caDataItemId, ptDataItem);

    return 0;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
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);
}
T_KRHdiCacheValue *kr_hdi_cache_get(T_KRCache *cache, void *key, long hid)
{
    T_KRHdiCacheValue *hdi_cache_val = NULL;
    T_KRHashTable *node = kr_cache_get(cache, key);
    if (node != NULL) {
        hdi_cache_val = kr_hashtable_lookup(node, &hid);
    } else {
        node = kr_hashtable_new_full(kr_long_hash, kr_long_equal, \
                            NULL, (KRDestroyNotify )kr_hdi_cache_value_free);
        kr_cache_set(cache, key, node);
    }
    
    if (hdi_cache_val == NULL) {
        hdi_cache_val = kr_hdi_cache_value_new(hid);
        kr_hashtable_replace(node, &hdi_cache_val->lHDIId, hdi_cache_val);
    }

    return hdi_cache_val;
}
Exemplo n.º 5
0
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);
    }
}
Exemplo n.º 6
0
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;
}