Exemple #1
0
/*
 * Get key/value
 */
int ConfigGet(tConfigDB* db,const void* pKey,int KeySize,void* pValue,int *pValueSize)
{
    tCluster * cluster = (tCluster*)db->cluster;
    int ret = -1;
    tKey key;
    key.str = (char*)pKey;
    key.len = KeySize;
    tValue value;
    value.str = (char*)pValue;
    value.len = *pValueSize;
    unsigned int hash = gethash(key.str,key.len);
    hash = hash%MAX_NODE_NUM;/* distribute strategy */
    tNode* pNode = (tNode*)GetNode(cluster,hash);
    /* hit local */
    if(pNode->fd == 0)
    {
        ret = DBGetKeyValue(db->db,key,&value);
    }
    else if(pNode->fd > 0)
    {
        ret = RemoteDBGetKeyValue(pNode->fd,key,&value);
    }
    else
    {
        pNode->fd = RemoteDBCreate(db->filename,pNode->addr,pNode->port);
        if(pNode->fd == -1)
        {
            return -1;
        }
        ret = RemoteDBGetKeyValue(pNode->fd,key,&value);
    }
    /* fail to get,get from master */
    if(ret == -1)
    {
        if(pNode == (tNode*)GetNode(cluster,MAX_NODE_NUM))
        {
            return -1;
        }
        tNode* pNode = (tNode*)GetNode(cluster,MAX_NODE_NUM);
        if(pNode->fd == 0)
        {
            ret = DBGetKeyValue(db->db,key,&value);
        }
        else if(pNode->fd > 0)
        {
            ret = RemoteDBGetKeyValue(pNode->fd,key,&value);
        }
        else
        {
            pNode->fd = RemoteDBCreate(db->filename,pNode->addr,pNode->port);
            if(pNode->fd == -1)
            {
                return -1;
            }
            ret = RemoteDBGetKeyValue(pNode->fd,key,&value);
        }  
    }
    *pValueSize = value.len;
    return SUCCESS;
}
Exemple #2
0
int ExecCmd(char * cmdbuf)
{
    char temp[MAX_STR_LEN] = "\0";
    if(CheckCmd(cmdbuf,"help") == 0 )
    {
        printf("open filename - EX:open nezha.hdb\n");
        printf("set key value - EX:set 100 helloworld\n");
        printf("get key       - EX:get 100\n");
        printf("delete key    - EX:delete 100\n");
        printf("close         - leave nezha.hdb\n");
        printf("help          - list cmds info\n");
    }
    else if(CheckCmd(cmdbuf,"open (.*)\\.hdb") == 0)
    {
        if(strlen(dbname) > 0)
        {
            printf("Please close %s first.\n",dbname);
        }
        else
        {
            sscanf(cmdbuf,"%s%s",temp,dbname);
            db = DBCreate(dbname);
        }
    }
    else if(db == NULL && CheckCmd(cmdbuf,"set|get|delete") == 0)
    {
        printf("Please open a database file first.\n");
    }
    else if(CheckCmd(cmdbuf,"close") == 0)
    {
        if(db != NULL)
        {
            DBDelete(db);
            db = NULL;
        }
        dbname[0] = '\0';
    }
    else if(CheckCmd(cmdbuf,"exit") == 0)
    {
        if(db != NULL)
        {
            DBDelete(db);
        }
        exit(0);
    }
    else if(CheckCmd(cmdbuf,"set ([0-9]+) (.*)") == 0)
    {
        tKey key;
        tValue value;
        char str[MAX_STR_LEN] = "\0";
        sscanf(cmdbuf,"%s%d%s",temp,&key,str);
        value.str = strstr(cmdbuf,str);
        value.len = cmdbuf + MAX_STR_LEN - value.str;
        if(DBSetKeyValue(db,key,value) != 0)
        {
            printf("ERROR:set %d %s\n",(int)key,value.str);
        }
        //printf("set %d %s\n",(int)key,value.str);
    }
    else if(CheckCmd(cmdbuf,"get ([0-9]+)") == 0)
    {
        tKey key = -1;
        tValue value;
        char str[MAX_STR_LEN] = "\0";
        value.str = str;
        value.len = MAX_STR_LEN;
        sscanf(cmdbuf,"%s%d",temp,&key);
        if(DBGetKeyValue(db,key,&value) == 0)
        {
            printf("%d -> %s\n",key,value.str);
        }
        else
        {
            printf("ERROR:get %d Not found!\n",(int)key);
        }
    }
    else if(CheckCmd(cmdbuf,"delete ([0-9]+)") == 0)
    {
        tKey key = -1;
        tValue value;
        char str[MAX_STR_LEN] = "\0";
        value.str = str;
        value.len = MAX_STR_LEN;
        sscanf(cmdbuf,"%s%d",temp,&key);
        if(DBDelKeyValue(db,key) != 0)
        {
            printf("ERROR:delete %d\n",(int)key);
        }
    }
    else
    {
        printf("Unknow Command!\n");
    }
    return 0;
}