[TDR 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 TDR Generic table. service_info table service_info.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<metalib name="service_info" tagsetversion="1" version="1">
  <struct name="service_info" version="1" primarykey="gameid,envdata,name,expansion" splittablekey="gameid" >
    <entry name="gameid"               type="string"     size="128" desc="gameapp id"/>
    <entry name="envdata"            type="string"     size="1024" desc="environment information"/>
    <entry name="name"            type="string"     size="1024" desc="name"/>
    <entry name="expansion"            type="string"   size="1024"           desc="expansion field"/>
    <entry name="filterdata"            type="string"     size="1024" desc="filter condition"/>
    <entry name="updatetime"           type="uint64"                  desc="latest update time, unit: ms"/>
    <entry name="inst_max_num"           type="uint64"                  desc="maximum number of instances"/>
    <entry name="inst_min_num"           type="uint64"                  desc="minimum number of instances"/>
    <entry name="routeinfo_len"      type="uint"     defaultvalue="0" desc="route information length" />
    <entry name="routeinfo"      type="char"     count="1024" refer="routeinfo_len" desc="route information length" />
    <index name="index_gameid_envdata_name" column="gameid,envdata,name" />
    <index name="index_gameid_envdata" column="gameid,envdata" />
    <index name="index_gameid_name" column="gameid,name" />
    <index name="index_gameid" column="gameid" />
  </struct>
</metalib>

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"
  "git.woa.com/gcloud_storage_group/tcaplus-go-api"
)

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

var client *tcaplus.Client

func main() {
  // Create a client
  client = tcaplus.NewClient()
  // 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
  }

  // Connect to the TcaplusDB backend
  err := client.Dial(AppId, []uint32{ZoneId}, DirUrl, Signature, 60)
  if err != nil {
    fmt.Printf("init failed %v\n", err.Error())
    return
  }
  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/protocol/cmd"
  "git.woa.com/gcloud_storage_group/tcaplus-go-api/terror"
  "time"
)

func getCountExample() {
  // Create a request
  req, err := client.NewRequest(ZoneId, TableName, cmd.TcaplusApiGetTableRecordCountReq)
  if err != nil {
    fmt.Printf("NewRequest failed %v\n", err.Error())
    return
  }

  // Use the client to send the request and receive the response synchronously
  if resp, err := client.Do(req, time.Duration(2*time.Second)); err != nil {
    fmt.Printf("recv err %s\n", err.Error())
    return
  } else {
    // Get the error code of the response message
    tcapluserr := resp.GetResult()
    if tcapluserr != 0 {
      fmt.Printf("response ret errCode: %d, errMsg: %s", tcapluserr, terror.GetErrMsg(tcapluserr))
      return
    }

    // Get records from the response message
    fmt.Printf("response success record count %d\n", resp.GetTableRecordCount())
  }
}

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/logger"
  "git.woa.com/gcloud_storage_group/tcaplus-go-api/protocol/cmd"
  "git.woa.com/gcloud_storage_group/tcaplus-go-api/terror"
  "strconv"
  "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
      }

      //Bring back the asynchronous ID of the request
      fmt.Printf("resp success, AsyncId:%d\n", resp.GetAsyncId())
      tcapluserr := resp.GetResult()
      if tcapluserr != 0 {
        fmt.Printf("response ret %s\n",
          "errCode: "+strconv.Itoa(tcapluserr)+", errMsg: "+terror.ErrorCodes[tcapluserr])
        return
      }
      //The response contains the obtained record
      fmt.Printf("response success record count %d\n", resp.GetTableRecordCount())
      return
    }
  }()

  //Create a Get request
  req, err := client.NewRequest(ZoneId, TableName, cmd.TcaplusApiGetTableRecordCountReq)
  if err != nil {
    fmt.Printf("NewRequest failed %v\n", err.Error())
    return
  }

  //Set the asynchronous request ID, through which the response and request are mapped
  req.SetAsyncId(667)

  // 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

[TDR Table] [C++ SDK] Interface Description for Counting the Total Number of Table Records

results matching ""

    No results matching ""