[PB Table][Go SDK] Count the Total Number of Table Records

1. Interface Description

Get the Total Number of Table Records

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 and create the following PB Generic table. game_players table

syntax = "proto3";                      // Specify the version of the protocol buffers language

package tcaplusservice;

import "tcaplusservice.optionv1.proto"; // Use the public definitions of TcaplusDB by importing them.

message game_players {  // Define a TcaplusDB table with message

// Specify the primary keys with the option tcaplusservice.tcaplus_primary_key
// The primary key of a TcaplusDB table has a limit of 4 fields
option(tcaplusservice.tcaplus_primary_key) = "player_id, player_name, player_email";

// Specify the primary key indexes with the option tcaplusservice.tcaplus_index
option(tcaplusservice.tcaplus_index) = "index_1(player_id, player_name)";
option(tcaplusservice.tcaplus_index) = "index_2(player_id, player_email)";


// Value Types supported by TcaplusDB
// int32, int64, uint32, uint64, sint32, sint64, bool, fixed64, sfixed64, double, fixed32, sfixed32, float, string, bytes
// Nested Types Message

// primary key fields
int64 player_id = 1;
string player_name = 2;
string player_email = 3;


// Ordinary fields
int32 game_server_id = 4;
repeated string login_timestamp = 5;
repeated string logout_timestamp = 6;
bool is_online = 7;

payment pay = 8;
}


message payment {

int64 pay_id = 1;
uint64 amount = 2;
int64 method = 3;

}

Get the following information after the preparation. These details will be used by the SDK:

  1. Directory server address list
  2. App ID
  3. App access password
  4. Game zone ID
  5. Table name

4. Example Code

Basic execution process of example code:

  1. Define table configuration parameters
  2. Set log configuration;
  3. Create a client;
  4. Send a request and process the response;
  5. Destroy the client.

Example of client initialization:

package main

import (
    "fmt"
    "github.com/tencentyun/tcaplusdb-go-sdk/pb"
)

// Define table configuration parameters
const (
    AppId                = uint64(2)
    ZoneId               = uint32(3)
    DirUrl               = "tcp://x.x.x.x:xxxx"
    Signature            = "xxxxxxxxxxxxx"
    TableName            = "game_players"
)

var client *tcaplus.PBClient

func main() {
    // Create a client
    client = tcaplus.NewPBClient()
    // Set log configuration and the log level in the logconf.xml file
    if err := client.SetLogCfg("./logconf.xml"); err != nil {
      fmt.Println(err.Error())
      return
    }

    //Construct a Map object to store all tables under the corresponding table group
    zoneList := []uint32{ZoneId}
    zoneTable := make(map[uint32][]string)
    zoneTable[ZoneId] = []string{TableName}

    // Connect to the TcaplusDB backend
    err := client.Dial(AppId, zoneList, DirUrl, Signature, 30, zoneTable)
    if err != nil {
        fmt.Printf("init failed %v\n", err.Error())
        return
    }
    // Set the zone used by default
    client.SetDefaultZoneId(ZoneId)
    fmt.Printf("Dial finish\n")
    getCountExample()

    // Call Close to destroy the client when the program exits
    client.Close()
}

The synchronous call mode of this example is similar to that of the C++ interface, and the request needs to be created and the response needs to be parsed. Example Directory

package main

import (
    "fmt"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/example/PB/tools"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/logger"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/protocol/cmd"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/terror"
    "time"
)

func getCountExample() {
    // Generate a get request
    req, err := client.NewRequest(tools.Zone, "game_players", cmd.TcaplusApiGetTableRecordCountReq)
    if err != nil {
        logger.ERR("NewRequest error:%s", err)
        return
    }

    // Send a request and receive the response;
    resp, err := client.Do(req, 5*time.Second)
    if err != nil {
        logger.ERR("SendRequest error:%s", err)
        return
    }

    // Get the response result
    errCode := resp.GetResult()
    if errCode != terror.GEN_ERR_SUC {
        logger.ERR("insert error:%s", terror.GetErrMsg(errCode))
        return
    }
    fmt.Println("table rec count", resp.GetTableRecordCount())
    logger.INFO("get success")
    fmt.Println("get success")
}

Asynchronous sending can use fewer processes to achieve greater concurrency. The coding is relatively complex. Example Directory

package main

import (
    "fmt"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/example/PB/tools"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/logger"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/protocol/cmd"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/terror"
    "sync"
    "time"
)

func getCountExample() {
    wg := sync.WaitGroup{}
    wg.Add(1)
    // Process the response message in another coroutine
    go func() {
        defer wg.Done()
        for {
            // If resp and err are both nil, there is no response in the response pool
            resp, err := client.RecvResponse()
            if err != nil {
                logger.ERR("RecvResponse error:%s", err)
                continue
            } else if resp == nil {
                time.Sleep(time.Microsecond * 5)
                continue
            }

            // Get the response result
            errCode := resp.GetResult()
            if errCode != terror.GEN_ERR_SUC {
                logger.ERR("insert error:%s", terror.GetErrMsg(errCode))
                return
            }

            fmt.Println("table record count ", resp.GetRecordCount())
        }
    }()

    // Generate a get request
    req, err := client.NewRequest(tools.ZoneId, "game_players", cmd.TcaplusApiGetTableRecordCountReq)
    if err != nil {
        logger.ERR("NewRequest error:%s", err)
        return
    }

    // Send a request asynchronously
    if err := client.SendRequest(req); err != nil {
        fmt.Printf("SendRequest failed %v\n", err.Error())
        return
    }
    wg.Wait()
}

5. FAQ

For details, see Meaning and Handling of Error Codes.

6. Other Reference Documents

[PB Table] [C++ 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

results matching ""

    No results matching ""