关于TcaplusDB:TcaplusDB小知识之TcaplusDB表定义

25次阅读

共计 3589 个字符,预计需要花费 9 分钟才能阅读完成。

数据库以表为载体存储数据,不同的表往往示意不同的实体。作为国产自研键值型 nosql 数据库,TcaplusDB 反对 2 种类型的表:protobuf(Protocol Buffers)表和 TDR(Tencent Data Representation)表。

Protobuf 是 Google 开发的一种描述性语言,对结构化数据进行序列化,同时强调简略性和性能;TDR 是由腾讯开发的跨平台数据表示语言,联合了 XML,二进制和 ORM(对象关系映射)的劣势,在腾讯游戏数据的序列化场景中宽泛应用。本文将简略介绍如何定义这两种表。

Protobuf 表

以下是 protobuf 表 game_players.proto 的示例,您能够将文件上传到腾讯云控制台并创立该表。

syntax = "proto3";         // 指定 protobuf 语言版本,proto3.

// 导入 TcaplusDB 公共定义服务  
import "tcaplusservice.optionv1.proto"; 

message game_players {  // 定义 TcaplusDB 表,蕴含 message 类型

// 基于选择项 tcaplusservice.tcaplus_primary_key 创立主键字段
// TcaplusDB 单个表最多能指定 4 个主键字段
    option(tcaplusservice.tcaplus_primary_key) = "player_id, player_name, player_email";

    // 基于选择项 tcaplusservice.tcaplus_index 创立主键索引
    option(tcaplusservice.tcaplus_index) = "index_1(player_id, player_name)";
    option(tcaplusservice.tcaplus_index) = "index_2(player_id, player_email)";

    // TcaplusDB 反对的数值类型:
    // int32, int64, uint32, uint64, sint32, sint64, bool, fixed64, sfixed64, double, fixed32, sfixed32, float, string, bytes
    // 嵌套类型: message

    // 主键字段
    int64 player_id = 1;  
    string player_name = 2;
    string player_email = 3;
    
    // 一般(非主键) 字段
    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;

}

TDR 表

TDR 反对通用 (generic) 表和列表 (list) 表。generic 表是以表的模式示意元素属性的表,例如学生,雇主,游戏玩家表。list 表是一系列记录,例如游戏排行榜,游戏中的邮件(通常是最近的 100 封邮件)。

举荐在一个 XML 文件中创立两种不同类型的表。

  • 元素 metalib 是 xml 文件的根元素。另外,您能够应用 union 创立嵌套类型:
  • 蕴含属性 primarykey 的 struct 元素定义一个表;否则,它只是一个一般的构造体。
  • 每次批改表构造时,版本属性值须要相应地加 1,初始版本始终为 1。
  • primarykey 属性指定主键字段;对于 generic 表,您最多能够指定 8 个主键字段,对于 list 表,则能够指定 7 个。
  • splittablekey 属性等效于分片键(shard key),TcaplusDB 表被拆分存储到多个存储节点。splittablekey 必须是主键字段之一,一个好的 splittablekey 应该具备高度分散性,这意味着值的范畴很广,倡议选用字符串类型。
  • desc 属性蕴含以后元素的形容。
  • entry 元素定义一个字段。反对的值类型包含 int32,string,char,int64,double,short 等。
  • index 元素定义一个索引,该索引必须蕴含 splittablekey。因为能够应用主键查问表,因而索引不应与主键属性雷同。样例:users_mails.xml
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>

<metalib name="tcaplus_tb" tagsetversion="1" version="1">

  <!-- generic_table `users`, store the user' information -->
  <!-- an user may has many roles -->
  <struct name="users" version="1" primarykey="user_id,username,role_id" splittablekey="user_id" desc="user table">
    <entry name="user_id" type="uint64" desc="user id"/>
    <entry name="username" type="string" size="64" desc="login username"/>
    <entry name="role_id" type="int32" desc="a user can have multiple roles"/>

    <entry name="level" type="int32" defaultvalue="1" desc="role's level"/>
    <entry name="role_name" type="string" size="1024" desc="role's name"/>
    <entry name="last_login_time" type="string" size="64" defaultvalue=""desc="user login timestamp"/>
    <entry name="last_logout_time" type="string" size="64" defaultvalue=""desc="user logout timestamp"/>

    <index name="index1" column="user_id"/>
  </struct>

  <!-- list_table `mails`, store the role's mails -->
  <struct name="mails" version="1" primarykey="user_id,role_id" desc="mail table">
    <entry name="user_id" type="uint64" desc="user id"/>
    <entry name="role_id" type="int32" desc="a user may has many roles"/>

    <entry name="text" type="string" size="2048" desc="mail text"/>
    <entry name="send_time" type="string" size="64" defaultvalue=""desc="timestamp of the mail sent"/>
    <entry name="read_time" type="string" size="64" defaultvalue=""desc="timestamp of the mall read"/>
  </struct>

</metalib>
  • union 元素蕴含原始类型的汇合,例如整数和字符串,能够将 Union 也能够作为自定义类型来援用;
  • Macro 标签用于定义常量。
<macro name="DB_MAX_USER_MSG_LEN" value="301" desc="Max length of the message that user can define"/>
 <union name="DBPlayerMsg" version="1" desc="DB Player message">
   <entry name="SysMsgID"     type="uint8"         desc="Message ID" />
   <entry name="UsrMsg"       type="string"        size="DB_MAX_USER_MSG_LEN"   desc="player created message" />
 </union>

TcaplusDB 是腾讯出品的分布式 NoSQL 数据库,存储和调度的代码齐全自研。具备缓存 + 落地交融架构、PB 级存储、毫秒级时延、无损程度扩大和简单数据结构等个性。同时具备丰盛的生态、便捷的迁徙、极低的运维老本和五个九高可用等特点。客户笼罩游戏、互联网、政务、金融、制作和物联网等畛域。

正文完
 0