The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

TDR table definition

Last updated: 2024-10-15 17:14:20

TcaplusDB supports two table definition formats: Protobuf (Protocol Buffers, PB) tables and TDR (Tencent Data Representation) tables.
Protobuf is a descriptive language developed by Google for serializing structured data, emphasizing simplicity and performance.
TDR is a cross-platform data representation language developed by Tencent, combining the advantages of XML, binary, and ORM (Object-Relational Mapping), and is widely used in the serialization scenarios of Tencent's game data. Both formats are fundamentally similar in usage. Choose the table definition language based on your specific business usage habits.
This document mainly describes the definition format of TDR tables.

TDR Table

TDR supports generic tables and list tables. Different types of tables can be created in a single XML file.
A generic table is a table that represents element attributes in a tabular form, such as students, employers, and game players.
A list table is a series of records, such as game leaderboards and in-game emails (typically the most recent 100 emails).

Table Definition

The element metalib is the root element of the XML file.
The attribute tagsetversion should always be 1.
A struct element containing a primarykey attribute is a table; a struct element without a primarykey attribute is a regular structure.
Each time the table structure is changed, the version attribute needs to be incremented by 1. The starting value of version attribute is always 1.
The primarykey attribute specifies the primary key fields; for generic tables, you can specify up to 4 primary key fields, and for list tables, up to 3.
The splittablekey attribute is equivalent to shard keys. TcaplusDB tables are split and stored across multiple storage nodes. The splittablekey must be one of the primary key fields, and a good splittablekey should be highly decentralized, meaning the range of values is large. It is recommended to use a string type.
The desc attribute contains the description of the current element.
The entry element defines a field, and supported value types include int32, string, char, int64, double, short, etc.
The index element defines an index that must include a splittable key. Since the primary key can be used to query the table, the index should not be the same as the primary key attributes.

Sample Code for Table Description File in TDR

<?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>
Additionally, you can use union to create nested types.
The union element contains a collection of primitive types, such as integers and strings. The union can be referenced as a custom-defined type. The macro Tag is used for definition constants.
<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>