[PB Table][C++ SDK] Get the Total Number of Table Records
1. Interface Description
Count the total number of table records (example path: examples/tcaplus/C++_pb2_asyncmode_simpletable/SingleOperation/get_count)
2. Version Requirements
This interface is provided in all versions without special requirements.
3. Preparations
Refer to Preparation document to complete the preparation before using this interface, create the following PB Generic table, and use the PB tool to convert the table into C++ code.
syntax = "proto2";
package myTcaplusTable;
import "tcaplusservice.optionv1.proto";
message tb_online {
option(tcaplusservice.tcaplus_primary_key) = "openid,tconndid,timekey";
required int32 openid = 1; //QQ Uin
required int32 tconndid = 2;
required string timekey = 3;
required string gamesvrid = 4;
optional int32 logintime = 5 [default = 1];
repeated int64 lockid = 6 [packed = true]; //The repeated type field is decorated with the packed keyword
optional pay_info pay = 7;
message pay_info {
optional uint64 total_money = 1;
optional uint64 pay_times = 2;
}
}
Get the following information after the preparation. These details will be used by the SDK:
- Directory server address list
- App ID
- App access password
- Game zone ID
- Table name
4. Example Code
4.1 Example Code for Asynchronous Call
Basic execution process of example code:
- Define table configuration parameters;
- Create a client;
- Define a callback function to process the response;
- Send a request;
- example framework;
Steps 1, 2 and 5 are common codes for all examples, focusing on sending requests and the callback function for response processing in steps 3 and 4
4.1.1 Define table configuration parameters (code path: examples/tcaplus/C++_common_for_pb/common.h)
It mainly sets the configuration parameters of the table, which are located in the header of the code file
// Tcapdir address of the target app
static const char DIR_URL_ARRAY[][TCAPLUS_MAX_STRING_LENGTH] =
{
"tcp://10.191.***.99:9999"
};
// Number of tcapdir addresses of the target app
static const int32_t DIR_URL_COUNT = 1;
// Set ID of the target app
static const int32_t APP_ID = 3;
// Table group ID of the target app
static const int32_t ZONE_ID = 1;
// App password of the target app
static const char * SIGNATURE = "*******";
// Table name of the target app: tb_online
static const char * TABLE_NAME = "tb_online";
4.1.2 Create a SDK Client (code path: examples/tcaplus/C++_common_for_pb/common.h)
Create a log handle through the log configuration file tlogconf.xml for SDK log printing; Create a Tcaplus client through this code. This client is only allowed to be used by a single thread. The multi-threading model can initialize a client instance on each thread
//Tcaplus PB API client
TcaplusAsyncPbApi g_stAsyncApi;
int32_t InitAsyncPbApi()
{
//PB API configuration
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 table accessed
cfg.tables.push_back(TABLE_NAME);
//Log configuration
strncpy(cfg.log_cfg, "tlogconf.xml", sizeof(cfg.log_cfg));
//Initialization connection timeout 5s
cfg.timeout = 5000;
//Initialization connection
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;
}
4.1.3 Define a callback function to process the response (code path: examples/tcaplus/C++_pb2_asyncmode_simpletable/SingleOperation/get_count/main.cpp)
class CommonCallback : public TcaplusPbCallback
{
public:
CommonCallback()
{
cout << "Init CommonCallback." << endl;
}
~CommonCallback()
{
cout << "Fini ~CommonCallback." << endl;
}
int OnRecv(int64_t &count)
{
g_dwTotalRevNum++;
printf("count: %d\n",(int)count);
return 0;
}
int OnError(int64_t &count)
{
g_dwTotalRevNum++;
cout<<"error"<<endl;
return 0;
}
int OnTimeout(const std::vector< ::google::protobuf::Message *> &msgs)
{
cout<<"timeout"<<endl;
return 0;
}
int OnFinish(const NS_TCAPLUS_PROTOBUF_API::MsgParam ¶m)
{
cout << "OnFinish: " << param.m_nOperation << " req: " << param.m_vecMsgs.size() << endl;
return 0;
}
};
4.1.4 Send a request (code path: examples/tcaplus/C++_pb2_asyncmode_simpletable/SingleOperation/get_count/main.cpp)
Send requests using the client
void SendRequest(struct schedule * S, void* arg)
{
static CommonCallback cb;
int32_t iRet = g_stAsyncApi.GetCount("tb_online", &cb);
if (iRet != TcapErrCode::GEN_ERR_SUC)
{
cout << "ERROR, Error iRet = " << iRet << endl;
}
}
4.1.5 Example asynchronous framework (code path: examples/tcaplus/C++_common_for_pb/common.h)
The proc main framework calls the corresponding functions to send requests, receive responses, and handle timeouts by implementing the three function pointers of the callback function. All examples of this framework are common
int32_t main(int32_t argc, char* argv[])
{
int32_t iRet = 0;
//Initialization
iRet = InitAsync("tlogconf.xml");
if (0 != iRet)
{
cout << "ERROR: Init failed, iRet: " << iRet << ", please check the tcaplus_pb.log for detail." << endl;
return iRet;
}
//Initialization
g_stPbCallbackFunctions.pfnSendRequest = SendRequest;
iRet = ProcAsync();
if (0 != iRet)
{
cout << "ERROR: ProcAsync failed, iRet: " << iRet << ", please check the tcaplus_pb.log for detail." << endl;
return iRet;
}
FinishAsync();
return 0;
}
// Initialize pb api
int32_t InitAsync(const char* log_conf)
{
int32_t iRet = 0;
iRet = InitAsyncPbApi(log_conf);
if (0 != iRet)
{
cout << "ERROR: InitCoroutinePbApi failed, iRet: " << iRet << "." << endl;
return iRet;
}
return iRet;
}
//************************************************************************
// Method: FinishAsync
// Returns:
// Qualifier: Processing logic on function exit
//*************************************************************************/
void FinishAsync()
{
cout << "g_stAsyncApi finish!" << endl;
g_stAsyncApi.Fini();
google::protobuf::ShutdownProtobufLibrary();
}
//************************************
// Method: ProcAsync
// Returns: int32_t
// Qualifier: Processing function
//************************************
int32_t ProcAsync()
{
int32_t iRet = 0;
// The variable iSendRequestCount used in this sample is to control the number of packages
int32_t iSendRequestCount = 0;
do
{
if (iSendRequestCount < TOTAL_SEND_RECV_NUM)
{
if(NULL == g_stPbCallbackFunctions.pfnSendRequest)
{
cout << "ERROR: g_stPbCallbackFunctions.pfnSendRequest is NULL, so will finish example." << endl;
break;
}
int32_t iUin = iSendRequestCount;
g_stPbCallbackFunctions.pfnSendRequest(NULL, (void *)&iUin);
iSendRequestCount++;
}
// Update and receive the response package
g_stAsyncApi.UpdateNetwork();
usleep(TCAPLUS_SLEEEP_US);
} while (g_dwTotalRevNum != TOTAL_SEND_RECV_NUM);
cout << "end mytest, please check the mytest.log for detail." << endl;
return 0;
}
5. FAQ
For details, see Meaning and Handling of Error Codes.
6. Other Reference Documents
[PB Table] [Go SDK] Interface Description for Counting the Total Number of Table Records
[PB Table][RESTFul API] Interface Description for Counting the Total Number of Table Records