天天看點

【TcaplusDB知識庫】插入資料示例(TDR表)

【TcaplusDB知識庫】插入資料示例(TDR表)

前提

成功建立:叢集,表格組,PLAYERONLINECNT表

PLAYERONLINECNT表描述檔案table_test.xml如下:

<?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="機關為分鐘" />
    <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線上人數" />
    <entry name="OnlineCntAndroid"  type="uint32"     defaultvalue="0" desc="android線上人數" />
    <entry name="BinaryLen"         type="smalluint" defaultvalue="1" desc="資料來源資料長度;長度為0時,忽略來源檢查"/>
    <entry name="binary"            type="tinyint"     desc="二進制" count= "1000" refer="BinaryLen" />
    <entry name="binary2"           type="tinyint"     desc="二進制2" count= "1000" refer="BinaryLen" />
    <entry name="strstr"            type="string"     size="64" desc="字元串"/>
    <index name="index_id"  column="TimeStamp"/>
</struct>
</metalib>      

定義配置參數

// 目标業務的tcapdir位址
static const char DIR_URL_ARRAY[][TCAPLUS_MAX_STRING_LENGTH] =
{
   "tcp://10.191.***.99:9999",
   "tcp://10.191.***.88:9999"
};
// 目标業務的tcapdir 位址個數
static const int32_t DIR_URL_COUNT = 2;
​
// 目标業務的叢集ID
static const int32_t APP_ID = 3;
// 目标業務的表格組ID
static const int32_t ZONE_ID = 1;
// 目标業務的業務密碼
static const char * SIGNATURE = "*******";
// 目标業務的表名 PLAYERONLINECNT
static const char * TABLE_NAME = "PLAYERONLINECNT";      

初始化TcaplusAPI日志句柄

日志配置檔案:tlogconf.xml

//TCaplus service 日志類
TcaplusService::TLogger* g_pstTlogger;
LPTLOGCATEGORYINST g_pstLogHandler;
LPTLOGCTX g_pstLogCtx;
int32_t InitLog()
{
    // 日志配置檔案的絕對路徑
    const char* sLogConfFile = "tlogconf.xml";
    // 日志類名
    const char* sCategoryName = "mytest";
    //從配置檔案初始化日志句柄
    g_pstLogCtx = tlog_init_from_file(sLogConfFile);
    if (NULL == g_pstLogCtx)
    {
        fprintf(stderr, "tlog_init_from_file failed.\n");
        return -1;
    }
    // 擷取日志類
    g_pstLogHandler = tlog_get_category(g_pstLogCtx, sCategoryName);
    if (NULL == g_pstLogHandler)
    {
        fprintf(stderr, "tlog_get_category(mytest) failed.\n");
        return -2;
    }
    // 初始化日志句柄
    g_pstTlogger = new TcaplusService::TLogger(g_pstLogHandler);
    if (NULL == g_pstTlogger)
    {
        fprintf(stderr, "TcaplusService::TLogger failed.\n");
        return -3;
    }
​
    return 0;
}      

初始化TcaplusAPI用戶端

//TCaplus service API的用戶端主類
TcaplusService::TcaplusServer g_stTcapSvr;
//表的meta資訊
extern unsigned char g_szMetalib_tcaplus_tb[];
LPTDRMETA g_szTableMeta = NULL;
int32_t InitServiceAPI()
{
    // 初始化
    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;
    }
​
    // 添加目錄伺服器
    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;
        }
    }  
​
    // 取得表的meta描述
    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;
    }
​
    // 注冊資料表(連接配接dir伺服器,認證,擷取表路由),10s逾時
    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;
    }
​
    // 連接配接表對應的所有tcaplus proxy伺服器
    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;
}      

通過API發送replace請求

也可以發送TCAPLUS_API_INSERT_REQ插入資料,資料存在會傳回失敗

TCAPLUS_API_REPLACE_REQ插入資料,資料存在會替換

int32_t SendReplaceRequest()
{
    // 請求對象類
    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;
    }
​
    //初始化請求對象
    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;
    }
​
    //為請求添加表記錄
    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));
​
    // 設定更新的key資訊
    stPLAYERONLINECNT.dwTimeStamp = 1;
    snprintf(stPLAYERONLINECNT.szGameSvrID, sizeof(stPLAYERONLINECNT.szGameSvrID), "%s", "mysvrid");
​
    // 設定更新的value資訊
    snprintf(stPLAYERONLINECNT.szGameAppID, sizeof(stPLAYERONLINECNT.szGameAppID), "%s", "myappid");
    stPLAYERONLINECNT.dwOnlineCntIOS = 1;
    stPLAYERONLINECNT.dwOnlineCntAndroid = 1;
​
    // 設定基于TDR描述設定record資料
    iRet = pstRecord->SetData(&stPLAYERONLINECNT, sizeof(stPLAYERONLINECNT));
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "pstRecord->SetData() failed, iRet: %d.", iRet);
        return iRet;
    }
​
    // 擷取結果異步事務ID
    uint64_t asyncid = response->GetAsynID();
​
    // 設定使用者緩存,應答将攜帶傳回
    iRet = pstRequest->SetUserBuff(&stPLAYERONLINECNT, sizeof(stPLAYERONLINECNT));
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "pstRequest->SetUserBuff() failed, iRet: %d.", iRet);
        return iRet;
    }
​
    // 發送請求消息包
    iRet= g_stTcapSvr.SendRequest(pstRequest);
    if(0 != iRet)
    {
        tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.SendRequest failed, iRet: %d.", iRet);
              

繼續閱讀