C language implementation of a simple internal data structure of the Application Server (III)

Source: Internet
Author: User

In the above section, the most basic key-> value internal interface is implemented, which can also meet general requirements. However, there is a limitation that a key corresponds to a value type. In common applications, it is very likely that a group of identical arrays will be saved, such as arrays or lists. For example, a query request may have this requirement. In this case, you need to make some extensions to the data structure above. You can add a type to indicate

01
<DataDef>
02
<Field key = "cardNo" type = "common" length = "19"/>
03
<Field key = "amount" type = "common" length = "13"/>
04
<Field key = "tradeDate" type = "common" length = "8"/>
05
<Field key = "tradeTime" type = "common" length = "6"/>
06
<Field key = "listData" type = "list" size = "3" ref = "myListData"/>
07
</DataDef>
08
<ListDataDef name = "myListData">
09
<Field key = "listData1" length = "19"/>
10
<Field key = "listData2" length = "13"/>
11
<Field key = "listData3" length = "8"/>
12
<Field key = "listData4" length = "6"/>
13
</ListDataDef>
When type is common, it corresponds to a common value. When type is list, it indicates a group of values, and each element of this set of data is similar to a small Map. Size indicates
How many elements are allocated to this set of data. As for the definition of this element, you can use ref to associate with name (myListData) in ListDataDef, so that you can calculate each

The total size of the Data whose key is listData is multiplied by the space occupied by the element. To implement this method, we need a method similar to the second-level ing method.

First, find the basic offset of this set of data in the first-level ing. When the type is common, find the ing relationship of the first-level ing. At that time, the list will be searched through the ref.

List defines the second-level ing, then you can calculate the offset of each field of each element, you can access it.

The interface is defined as follows:

1
Int getDataList (const void * pDataAddr, const char * szKey, struct DataList * pDataList );
2
 
3
Int getDataInList (const void * pDataAddr, const struct DataList * pDataList, int iIdx, const char * szKey, const int iBufLen, char * szValue );
4
Int petDataInList (void * pDataAddr, const struct DataList * pDataList, int iIdx, const char * szKey, const char * szValue)
DataList stores the context information of the necessary List, such as base offset and second-level ing addresses. iIdx indicates the elements in the array, szKey naturally represents the value of a certain element.

In this way, the data support for this internal interface is complete and should be able to adapt to most applications.

Author: "OneThin blog"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.