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

Source: Internet
Author: User

This article provides some extensions based on the previous article.

Internal data structure definition
The internal data structure configuration file can be in the xml format. The file format is actually very simple. The example is as follows:
1
<DataDef>
2
<Field key = "cardNo" length = "19"/>
3
<Field key = "amount" length = "13"/>
4
<Field key = "tradeDate" length = "8"/>
5
<Field key = "tradeTime" length = "6"/>
6
</DataDef>
For example, this internal structure contains four fields, including the card number, amount, Transaction date, and transaction time of a transaction information. After this configuration is loaded, the data ing relationships in the memory are as follows:
Data key length offset
CardNo 19
Amount 13 20
TradeDate 8 36
TradeTime 6 48
Considering the computing efficiency, for example, in 32 systems, the actually allocated storage length is the smallest integer multiple of 4, which is greater than the length configured in the xml file. In this way, the offset is an integer multiple of 4.
Internal data storage and access methods
The internal storage mode of data can be in the string format, whether it is an integer, floating point number, the core of the data read and write interfaces is similar to this form:

1
Int getDataStr (const void * dataAddr, const char * szKey, const int iBufLen, char * szValue );
2
Int putDataStr (void * dataAddr, const char * szKey, const char * szValue );
DataAddr is the storage address of all data. It finds the offset of the corresponding data based on szKey (if multiple data domains can be sorted by key after the configuration is loaded, the binary search method is used for searching ), then, read or write data. For other types, convert the character to an integer or floating point type. In addition, the data contains a natural internal length, which can be checked during writing and cannot exceed the length of the xml file. In one place, the memory overflow BUG is limited.

1
Int getDataInt (const void * dataAddr, const char * szKey, int * iValue );
2
Int setDataInt (void * dataAddr, const char * szKey, const int iValue );
3
Int getDataDouble (const void * dataAddr, const char * szKey, double * dValue );
4
Int setDataDouble (void * dataAddr, const char * szKey, const double dValue );
The simplest design has been completed. The biggest difference between dynamic Map implementation and dynamic Map implementation is that keys of all values used must be pre-defined, however, the position of the value has been determined at the time of loading. dynamic memory application and release are not required during access, which ensures a certain efficiency. In Linux/Unix systems, the internal structure data can be stored in the shared memory, so that multiple processes can access the data (if necessary, the data access interface can be implemented to ensure thread security), without passing data between processes.

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.