当前位置:网站首页>[tcapulusdb knowledge base] insert data example (TDR table)

[tcapulusdb knowledge base] insert data example (TDR table)

2022-06-23 22:22:00 Tcapulus Jun

【TcaplusDB The knowledge base 】 Insert data sample (TDR surface )

Premise

Successfully created : colony , Table group ,PLAYERONLINECNT surface

PLAYERONLINECNT Table description file table_test.xml as follows :

<?xml version="1.0" encoding="GBK" standalone="yes" ?>
<metalib name="tcaplus_tb" tagsetversion="1" version="1">
<struct name="PLAYERONLINECNT" version="1" primarykey="TimeStamp,GameSvrID" splittablekey="TimeStamp">
    <entry name="TimeStamp"         type="uint32"     desc=" In minutes " />
    <entry name="GameSvrID"         type="string"     size="64" />
    <entry name="GameAppID"         type="string"     size="64" desc="gameapp id" />
    <entry name="OnlineCntIOS"      type="uint32"     defaultvalue="0" desc="ios Number of people online " />
    <entry name="OnlineCntAndroid"  type="uint32"     defaultvalue="0" desc="android Number of people online " />
    <entry name="BinaryLen"         type="smalluint" defaultvalue="1" desc=" Data source data length ; The length is 0 when , Ignore source check "/>
    <entry name="binary"            type="tinyint"     desc=" Binary system " count= "1000" refer="BinaryLen" />
    <entry name="binary2"           type="tinyint"     desc=" Binary system 2" count= "1000" refer="BinaryLen" />
    <entry name="strstr"            type="string"     size="64" desc=" character string "/>
    <index name="index_id"  column="TimeStamp"/>
</struct>
</metalib>

Define configuration parameters

//  Target business tcapdir Address 
static const char DIR_URL_ARRAY[][TCAPLUS_MAX_STRING_LENGTH] =
{
   "tcp://10.191.***.99:9999",
   "tcp://10.191.***.88:9999"
};
//  Target business tcapdir  Number of addresses 
static const int32_t DIR_URL_COUNT = 2;
​
//  Cluster of target business ID
static const int32_t APP_ID = 3;
//  Table group of target business ID
static const int32_t ZONE_ID = 1;
//  The business password of the target business 
static const char * SIGNATURE = "*******";
//  Table name of the target business  PLAYERONLINECNT
static const char * TABLE_NAME = "PLAYERONLINECNT";

initialization TcaplusAPI Log handle

Log profile :tlogconf.xml

//TCaplus service  Log class 
TcaplusService::TLogger* g_pstTlogger;
LPTLOGCATEGORYINST g_pstLogHandler;
LPTLOGCTX g_pstLogCtx;
int32_t InitLog()
{
    //  The absolute path to the log configuration file 
    const char* sLogConfFile = "tlogconf.xml";
    //  Log class name 
    const char* sCategoryName = "mytest";
    // Initialize log handle from configuration file 
    g_pstLogCtx = tlog_init_from_file(sLogConfFile);
    if (NULL == g_pstLogCtx)
    {
        fprintf(stderr, "tlog_init_from_file failed.\n");
        return -1;
    }
    //  Get log class 
    g_pstLogHandler = tlog_get_category(g_pstLogCtx, sCategoryName);
    if (NULL == g_pstLogHandler)
    {
        fprintf(stderr, "tlog_get_category(mytest) failed.\n");
        return -2;
    }
    //  Initialize log handle 
    g_pstTlogger = new TcaplusService::TLogger(g_pstLogHandler);
    if (NULL == g_pstTlogger)
    {
        fprintf(stderr, "TcaplusService::TLogger failed.\n");
        return -3;
    }
​
    return 0;
}

initialization TcaplusAPI client

//TCaplus service API Client main class 
TcaplusService::TcaplusServer g_stTcapSvr;
// Tabular meta Information 
extern unsigned char g_szMetalib_tcaplus_tb[];
LPTDRMETA g_szTableMeta = NULL;
int32_t InitServiceAPI()
{
    //  initialization 
    int32_t iRet = g_stTcapSvr.Init(g_pstTlogger, /*module_id*/0, /*app id*/APP_ID, /*zone id*/ZONE_ID, /*signature*/SIGNATURE);
    if (0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.Init failed, iRet: %d.", iRet);
        return iRet;
    }
​
    //  Add directory server 
    for (int32_t i = 0; i< DIR_URL_COUNT; i++)
    {
        iRet = g_stTcapSvr.AddDirServerAddress(DIR_URL_ARRAY[i]);
        if (0 != iRet)
        {
            tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.AddDirServerAddress(%s) failed, iRet: %d.", DIR_URL_ARRAY[i], iRet);
            return iRet;
        }
    }  
​
    //  Get the of the table meta describe 
    g_szTableMeta = tdr_get_meta_by_name((LPTDRMETALIB)g_szMetalib_tcaplus_tb, TABLE_NAME);
    if(NULL == g_szTableMeta)
    {
        tlog_error(g_pstLogHandler, 0, 0,"tdr_get_meta_by_name(%s) failed.", TABLE_NAME);
        return -1;
    }
​
    //  Registration data table ( Connect dir The server , authentication , Get table route ),10s Overtime 
    iRet = g_stTcapSvr.RegistTable(TABLE_NAME, g_szTableMeta, /*timeout_ms*/10000);
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.RegistTable(%s) failed, iRet: %d.", TABLE_NAME, iRet);
        return iRet;
    }
​
    //  Connect all the data corresponding to the table tcaplus proxy The server 
    iRet = g_stTcapSvr.ConnectAll(/*timeout_ms*/10000, 0);
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.ConnectAll failed, iRet: %d.", iRet);
        return iRet;
    }
​
    return 0;
}

adopt API send out replace request

It can also be sent TCAPLUS_API_INSERT_REQ insert data , If the data exists, it will return failure

TCAPLUS_API_REPLACE_REQ insert data , If the data exists, it will replace

int32_t SendReplaceRequest()
{
    //  Request object class 
    TcaplusService::TcaplusServiceRequest* pstRequest = g_stTcapSvr.GetRequest(TABLE_NAME);
    if (NULL == pstRequest)
    {
        tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.GetRequest(%s) failed.", TABLE_NAME);
        return -1;
    }
​
    // Initialize the request object 
    int iRet = pstRequest->Init(TCAPLUS_API_REPLACE_REQ, NULL, 0, 0, 0, 0);
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "pstRequest->Init(TCAPLUS_API_REPLACE_REQ) failed, iRet: %d.", iRet);
        return iRet;
    }
​
    // Add table record for request 
    TcaplusService::TcaplusServiceRecord* pstRecord = pstRequest->AddRecord();
    if (NULL == pstRecord)
    {
        tlog_error(g_pstLogHandler, 0, 0, "pstRequest->AddRecord() failed.");
        return -1;
    }
​
    PLAYERONLINECNT stPLAYERONLINECNT;
    memset(&stPLAYERONLINECNT, 0, sizeof(stPLAYERONLINECNT));
​
    //  Set up updated key Information 
    stPLAYERONLINECNT.dwTimeStamp = 1;
    snprintf(stPLAYERONLINECNT.szGameSvrID, sizeof(stPLAYERONLINECNT.szGameSvrID), "%s", "mysvrid");
​
    //  Set up updated value Information 
    snprintf(stPLAYERONLINECNT.szGameAppID, sizeof(stPLAYERONLINECNT.szGameAppID), "%s", "myappid");
    stPLAYERONLINECNT.dwOnlineCntIOS = 1;
    stPLAYERONLINECNT.dwOnlineCntAndroid = 1;
​
    //  The settings are based on TDR Describe the settings record data 
    iRet = pstRecord->SetData(&stPLAYERONLINECNT, sizeof(stPLAYERONLINECNT));
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "pstRecord->SetData() failed, iRet: %d.", iRet);
        return iRet;
    }
​
    //  Get the result of an asynchronous transaction ID
    uint64_t asyncid = response->GetAsynID();
​
    //  Set user cache , The reply will carry back 
    iRet = pstRequest->SetUserBuff(&stPLAYERONLINECNT, sizeof(stPLAYERONLINECNT));
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "pstRequest->SetUserBuff() failed, iRet: %d.", iRet);
        return iRet;
    }
​
    //  Send request message packet 
    iRet= g_stTcapSvr.SendRequest(pstRequest);
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.SendRequest failed, iRet: %d.", iRet);
        return iRet;
    }
    return 0;
}

adopt API Receiving response

int RecvResp(TcaplusServiceResponse*& response)
{
    // Packet receiving is blocked here , Every time there's a blockage 1ms, closed 5000 Time 
    unsigned int sleep_us = 1000;
    unsigned int sleep_count = 5000;
    do
    {
        usleep(sleep_us);
        response = NULL;
        int ret = g_stTcapSvr.RecvResponse(response);
        if (ret < 0)
        {
            tlog_error(g_pstLogHandler, 0, 0, "tcaplus_server.RecvResponse failed. ret:%d", ret);
            return ret;
        }
        //  Get the result of an asynchronous transaction ID
        uint64_t asyncid = response->GetAsynID();
        //  Get the user cache information in the request 
        size_t buf_len = 0;
        const char* userbuf = response->GetUserBuff(&buf_len);
​
        // Received a response packet 
        if (1 == ret)
        {
             break;
        }
    } while ((--sleep_count) > 0);
​
    //5s Overtime 
    if (0 == sleep_count)
    {
        tlog_error(g_pstLogHandler, 0, 0, "tcaplus_server.RecvResponse wait timeout.");
        return -1;
    }
    return 0;
}

Example

main.cpp

int main(void) {
    // Initialize log 
    int ret = InitLog();
    if ( ret != 0)
    {
        printf("init log failed\n");
        return -1;
    }
​
    // initialization API client 
    ret = InitServiceAPI();
    if ( ret != 0)
    {
        printf("init InitServiceAPI failed\n");
        return -1;
    }
​
    // Send a request 
    ret = SendReplaceRequest();
    if (0 != ret)
    {
        printf("SendReplaceRequest failed\n");
        return -1;
    }
​
    // Receiving response 
    TcaplusServiceResponse* response = NULL;
    ret = RecvResp(response);
    if (0 != ret)
    {
        printf("RecvResp failed\n");
        return -1;
    }
​
    // Get the result of the operation ,0 It means success 
    int32_t result = response->GetResult();
    if (0 != result)
    {
        printf("the result is %d\n", result);
        return -1;
    }
    return 0;
}

TcaplusDB It's a distributed product of Tencent NoSQL database , The code for storage and scheduling is completely self-developed . With cache + Landing fusion architecture 、PB Levels of storage 、 Millisecond delay 、 Lossless horizontal expansion and complex data structure . At the same time, it has rich ecological environment 、 Easy migration 、 Extremely low operation and maintenance costs and five nine high availability features . Customer coverage game 、 Internet 、 government affairs 、 Finance 、 Manufacturing and the Internet of things .

原网站

版权声明
本文为[Tcapulus Jun]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112151541452417.html

随机推荐