【TcaplusDB知識庫】PB表C++示例程式碼-刪除資料

zmgogo發表於2022-02-16

【TcaplusDB知識庫】PB表C++示例程式碼-刪除資料

前提

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

tb_online表描述檔案 table_test.proto 如下:( tcaplusservice.optionv1.proto 為依賴表)


syntax = 
"proto3";


package myTcaplusTable;
//import tcaplusdb extensions
import "tcaplusservice.optionv1.proto";

message tb_online {
    //define primary key
    option( tcaplusservice. tcaplus_primary_key) = "openid,tconndid,timekey";
    //primary key fields
    int32 openid = 1; //QQ Uin
    int32 tconndid = 2;
    string timekey = 3;

    //non-primary key fields
    string gamesvrid = 4;
    int32 logintime = 5 ;
    repeated int64 lockid = 6;
    pay_info pay = 7;

    message pay_info {
        uint64 total_money = 1;
        uint64 pay_times = 2;
   }

}

定義配置引數


// 目標業務的tcapdir地址

static const char DIR_URL_ARRAY[][ TCAPLUS_MAX_STRING_LENGTH] =
{
  "tcp://10.191.***.99:9999"
};
// 目標業務的tcapdir 地址個數
static const int32_t DIR_URL_COUNT = 1;

// 目標業務的叢集ID
static const int32_t APP_ID = 3;
// 目標業務的表格組ID
static const int32_t ZONE_ID = 1;
// 目標業務的業務密碼
static const char * SIGNATURE = "*******";
// 目標業務的表名 tb_online
static const char * TABLE_NAME = "tb_online";

初始化TcaplusPB客戶端


//Tcaplus PB API客戶端

TcaplusAsyncPbApi g_stAsyncApi;
int32_t InitAsyncPbApi()
{
    //PB API配置
    ClientOptions cfg;
    cfg. app_id = APP_ID;
    cfg. zones. push_back( ZONE_ID);
    strcpy( cfg. signature, SIGNATURE);
    for ( int32_t i = 0; i < DIR_URL_COUNT; i ++)
   {
        cfg. dirs. push_back( DIR_URL_ARRAY[ i]);
   }
    //訪問的PB表
    cfg. tables. push_back( TABLE_NAME);
    //日誌配置
    strncpy( cfg. log_cfg, "tlogconf.xml", sizeof( cfg. log_cfg));
    //初始化連線超時時間5s
    cfg. timeout = 5000;

    //初始化連線
    int32_t iRet = g_stAsyncApi. Init( cfg);
    if ( 0 != iRet)
   {
        cout << "ERROR: g_stAsyncApi.Init failed, log cfg: " << cfg. log_cfg << ", iRet: " << iRet << "." << endl;
        return iRet;
   }
    return iRet;
}

定義非同步回撥


//收到響應訊息計數

uint32_t g_dwTotalRevNum = 0;
class CommonCallback : public TcaplusPbCallback
{
public:
    CommonCallback()
   {
        cout << "Init CommonCallback." << endl;
   }

    ~CommonCallback()
   {
        cout << "Fini ~CommonCallback." << endl;
   }
    //收到響應訊息的回撥,msgs為獲取的記錄的陣列
    int OnRecv( const std::vector < :: google::protobuf::Message *> & msgs)
   {
        cout << "OnRecv[" << msgs. size() << endl;
        g_dwTotalRevNum ++;
        for ( size_t idx = 0; idx < msgs. size(); idx ++)
       {
            tb_online * t = dynamic_cast < tb_online *>( msgs[ idx]);
            if ( NULL == t)
           {
                cout << "ERROR: msgs[" << idx << "] not tb_online type." << endl;
                return - 1;
           }

            cout << "---------- receive a response----------:" << endl;
            cout << "openid=" << t -> openid() << endl;
            cout << "tconndid=" << t -> tconndid() << endl;
            cout << "timekey=" << t -> timekey() << endl;
            cout << "gamesvrid=" << t -> gamesvrid() << endl;
            cout << "logintime=" << t -> logintime() << endl;
            for ( int32_t i = 0; i < t -> lockid_size(); i ++)
           {
                cout << "lockid[" << i << "]=" << t -> lockid( i) << endl;
           }
            tb_online_pay_info pay = t -> pay();
            cout << "pay total_money=" << pay. total_money() << "; pay_times=" << pay. pay_times() << endl;

            //獲取記錄的版本號
            std::string version;
            int iRet = g_stAsyncApi. GetMessageOption( * t, NS_TCAPLUS_PROTOBUF_API::MESSAGE_OPTION_DATA_VERSION, & version);
            cout << "after GetMessageOption iRet= [" << iRet << "] version:" << version. c_str() << " msg:" << t << endl;
       }

        return 0;
   }

    //收到錯誤響應訊息的回撥
    int OnError( const std::vector < :: google::protobuf::Message *> & msgs, int errorcode)
   {
        cout << "OnError[" << msgs. size() << endl;
        g_dwTotalRevNum ++;
        for ( size_t idx = 0; idx < msgs. size(); idx ++)
       {
            tb_online * t = dynamic_cast < tb_online *>( msgs[ idx]);
            if ( NULL == t)
           {
                cout << "ERROR: msgs[" << idx << "] not tb_online type." << endl;
                return - 1;
           }

            if ( TcapErrCode::TXHDB_ERR_RECORD_NOT_EXIST == errorcode)
           {
                cout << "ERROR: openid= " << t -> openid() << ", tconndid= " << t -> tconndid() << ", timekey= " << t -> timekey() << ", record not exists" << endl;
           }
            cout << "ERROR: openid = [" << t -> openid() << "], tconndid = [" << t -> tconndid() << "], timekey =[" << t -> timekey() << "] failed:%d"   << errorcode << endl;
       }

        return 0;
   }

    //訊息超時的回撥
    int OnTimeout( const std::vector < :: google::protobuf::Message *> & msgs)
   {
        cout << "OnTimeout[" << msgs. size() << endl;
        for ( size_t idx = 0; idx < msgs. size(); idx ++)
       {
            tb_online * t = dynamic_cast < tb_online *>( msgs[ idx]);
            if ( NULL == t)
           {
                cout << "TIMEOUT: msgs[" << idx << "] not tb_online type!" << endl;
                return - 1;
           }

            cout << "TIMEOUT: openid = [" << t -> openid() << "], tconndid = [" << t -> tconndid() << "], timekey =[" << t -> timekey() << "] timeout" << endl;
       }

        return 0;
   }

    int OnFinish( const NS_TCAPLUS_PROTOBUF_API::MsgParam & param)
   {
        cout << "OnFinish: " << param. m_nOperation << " req: " << param. m_vecMsgs. size() << endl;
        return 0;
   }
};

傳送Delete請求


int 
SendDelRequest()

{
    static tb_online t;
    t. set_openid( 1);
    t. set_tconndid( 1);
    t. set_timekey( "test_tcaplus");

    static CommonCallback cb;
    std::string version = "-1";
    g_stApi. SetMessageOption( t, NS_TCAPLUS_PROTOBUF_API::MESSAGE_OPTION_DATA_VERSION, version);
    int32_t iRet = g_stAsyncApi. Del( & t, & cb);
    if ( iRet != TcapErrCode::GEN_ERR_SUC)
   {
        cout << "ERROR: openid= " << t. openid() << ", tconndid= " << t. tconndid() << ", timekey= " << t. timekey() << ", Delete Error iRet = " << iRet << endl;
        return - 1;
   }
    return 0;
}

示例

main.cpp


int 
main(
void) {


    //初始化API客戶端
    int ret = InitAsyncPbApi();
    if ( ret != 0)
   {
        printf( "InitAsyncPbApi failed\n");
        return - 1;
   }

    //傳送請求
    ret = SendDelRequest();
    if ( 0 != ret)
   {
        printf( "SendDelRequest failed\n");
        return - 1;
   }

    //接收響應
    do
   {
        // 更新,接收回包
        g_stAsyncApi. UpdateNetwork();
        usleep( 1000 * 10);
   } while ( g_dwTotalRevNum != 1);
    return 0;
}


img

TcaplusDB是騰訊出品的分散式NoSQL資料庫,儲存和排程的程式碼完全自研。具備快取+落地融合架構、PB級儲存、毫秒級時延、無損水平擴充套件和複雜資料結構等特性。同時具備豐富的生態、便捷的遷移、極低的運維成本和五個九高可用等特點。客戶覆蓋遊戲、網際網路、政務、金融、製造和物聯網等領域。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69982264/viewspace-2855916/,如需轉載,請註明出處,否則將追究法律責任。

相關文章