⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ug_ch14.htm

📁 db.* (pronounced dee-be star) is an advanced, high performance, small footprint embedded database fo
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"><meta http-equiv="content-type" content="text/html; charset=us-ascii"><meta name="Generator" content="Microsoft Word 97"><title>db.* User's Guide Chapter 14</title></head><body><h1><a name="Formats" id="Formats"></a>Chapter 14<br>File Formats and Dictionary Tables</h1><h2><a name="Introduction" id="Introduction"></a>14.1Introduction</h2><p><font size="2">This chapter describes in detail the physicalorganization of <b><i>db.*</i></b> data and key files, thestructure of the runtime dictionary and currency tables, and somesource code implementation details. Although an understanding ofthis information is not necessary to make effective use of<b><i>db.*</i></b>, it can be helpful in making better use of thesystem.</font></p><p>You may want to write utilities or functions that use thisinformation. For example, the file structure information couldallow you to write utilities that can read through a database andupdate all occurrences of a certain field type. Please note,however, that while the <b><i>db.*</i></b> runtime is in operation,it is not safe for an application to directly change any databasefile other than through the use of <b><i>db.*</i></b> functions.Any updates to <b><i>db.*</i></b> files or global tables duringruntime execution are made at your own risk.</p><p>The information in this chapter can also be useful when makingdecisions about the design of a database, since the trade-offs willbe better understood. This information can also be used to betterunderstand the source code.</p><h2><a name="Organization" id="Organization"></a>14.2 Database FileOrganization</h2><p><font size="2">This section describes the structure of<b><i>db.*</i></b> files. Certain properties of all<b><i>db.*</i></b> files are the same; these are discussed insection 14.2.1, followed by individual descriptions of<b><i>db.*</i></b> data and key files.</font></p><h3><a name="Structure" id="Structure"></a>14.2.1 General FileStructure</h3><p><font size="2">Figure 14-1 shows the units of storage used by<b><i>db.*</i></b>. A database is composed of one or more files. Afile contains one or more pages. Each page is divided into equalnumbers of slots, which contain records (in a data file) or keys(in a key file).</font></p><p align="center"><b><img alt="dbstar_14-1.gif - 3207 Bytes"border="0" height="190" src="dbstar_14-1.gif" width="337"></b></p><p align="center">Fig. 14-1. Units of an <i>db.*</i> Database</p><p>All <b><i>db.*</i></b> files are viewed as an array offixed-length pages. The length of a page may vary from file tofile, but within each file, all pages are the same length. All<b><i>db.*</i></b> database input and output reads and writes fullpages of data, even though the user level functions operate onrecords and keys.</p><p align="center"><b>Table 14-1 Page Zero Contents</b></p><table cellspacing="0" border="0" cellpadding="7" width="542"><tr><td width="10%" valign="top"><p><b><font size="2">Offset</font></b></p></td><td width="11%" valign="top"><p><b><font size="2">Length</font></b></p></td><td width="15%" valign="top"><p><b><font size="2">Name</font></b></p></td><td width="64%" valign="top"><p><b><font size="2">Contents</font></b></p></td></tr><tr><td width="10%" valign="top"><p><font size="2">0</font></p></td><td width="11%" valign="top"><p><font size="2">4</font></p></td><td width="15%" valign="top"><p><b><font size="2">dchain</font></b></p></td><td width="64%" valign="top"><p><font size="2">Head of the delete chain</font></p></td></tr><tr><td width="10%" valign="top"><p><font size="2">4</font></p></td><td width="11%" valign="top"><p><font size="2">4</font></p></td><td width="15%" valign="top"><p><b><font size="2">next</font></b></p></td><td width="64%" valign="top"><p><font size="2">Next available space in the file</font></p></td></tr><tr><td width="10%" valign="top"><p><font size="2">8</font></p></td><td width="11%" valign="top"><p><font size="2">4</font></p></td><td width="15%" valign="top"><p><b><font size="2">timestamp</font></b></p></td><td width="64%" valign="top"><p><font size="2">Next timestamp value</font></p></td></tr><tr><td width="10%" valign="top"><p><font size="2">12</font></p></td><td width="11%" valign="top"><p><font size="2">4</font></p></td><td width="15%" valign="top"><p><b><font size="2">cdate</font></b></p></td><td width="64%" valign="top"><p><font size="2">Database creation date and time</font></p></td></tr><tr><td width="10%" valign="top"><p><font size="2">16</font></p></td><td width="11%" valign="top"><p><font size="2">4</font></p></td><td width="15%" valign="top"><p><b><font size="2">bdate</font></b></p></td><td width="64%" valign="top"><p><font size="2">Last database backup date and time</font></p></td></tr><tr><td width="10%" valign="top"><p><font size="2">20</font></p></td><td width="11%" valign="top"><p><font size="2">21</font></p></td><td width="15%" valign="top"><p><b><font size="2">version</font></b></p></td><td width="64%" valign="top"><p><font size="2">Version of <b><i>db.*</i></b> software (in ASCII)that created the file</font></p></td></tr></table><p><font size="2">Pages are numbered, starting with page zero. Pagezero is the file header, which only stores file status information.No user data is stored in page zero of any file. Because of this,the contents of page zero after the <b>bdate</b> field are subjectto change without notice. Table 14-1 identifies the contents ofpage zero.</font></p><p align="left"><b><img alt="Page Zero" border="0" height="163"src="dbstar_14-2.gif" width="576"></b></p><p align="center">Fig. 14-2. Page Zero</p><p>The first four bytes of all pages numbered one and above containa long integer that serves as a date/time stamp of the last timethe page was updated and written to the database. The remainder ofeach page is divided into equal-length slots. The size of the slotsis based on the maximum record or key size that must be stored inthe file. Figure 14-3 shows the general layout of one<b><i>db.*</i></b> file.</p><p align="center"><b><img alt="dbstar_14-3.gif - 2469 Bytes"border="0" height="131" src="dbstar_14-3.gif" width="379"></b></p><p align="center">Fig. 14-3. <i>db.*</i> File Structure</p><p>The fixed-length scheme of structuring pages and slots maycreate some wasted space in a file (see section 4.4.2, "PhysicalDesign Considerations"), but its benefits in performance andsimplicity outweigh this concern. Calculations to locate the exactpage and slot number of a record are quick, and management of thespace left by record deletions is simple. Because the disk spacenever becomes fragmented, there is rarely a need to reorganize adatabase for the purpose of compacting records. New pages areeither allocated from the end of each file (<b>next</b>) or from achain of deleted (<b>dchain</b>) data file record slots or key filepages.</p><h3><a name="DataFile" id="DataFile"></a>14.2.2 Data FileOrganization</h3><p><font size="2">This section discusses the characteristics of a<b><i>db.*</i></b> file that is designated for storage of datarecords. All characteristics of the general file structure (seesection 14.2.1, "General File Structure") apply in addition tothose discussed here.</font></p><p>In a data file, the first two page zero entries contain recordslot addresses. Individual record slots are allocated from thedelete chain or from the end of the file. The <b>dchain</b> entrypoints to the first record slot on the delete chain and the<b>next</b> entry contains the number of the next available slot atthe end of the file.</p><h4><a name="DatabaseAddresses" id="DatabaseAddresses"></a>14.2.2.1Database Addresses</h4><p><font size="2">Every record slot within a database is uniquelynamed and addressed by a four-byte structure called a databaseaddress. A database address is composed of a file number and a slotnumber. Figure 14-4 shows the structure of a databaseaddress.</font></p><p align="center"><b><img alt="dbstar_14-4.gif - 1506 Bytes"border="0" height="66" src="dbstar_14-4.gif" width="309"></b></p><p align="center">Fig. 14-4. Database Address</p><p>The file number is an unsigned value between zero and 255 andidentifies the file where the record is stored. It is used as anindex into the file table, discussed in section 14.3.2, "FileTable." The slot number is an unsigned value between one and16,777,215 (there is no slot zero). This establishes the maximumnumber of records that can be stored in one file (with a three-byteslot number). Note that the number of slots can be increased byreassigning the bits used for the file number and slot number. Thiswould decrease the maximum number of files that could bereferenced.</p><p>Slot number zero does not exist and hence is used as a nulldatabase address value. By convention, both the file and recordnumbers are set to zero (0L) to indicate that a database addressvariable does not point to any valid record.</p><p>Whenever a record is created, it is assigned to a slot, andgiven the slot's database address for its lifetime. The databaseaddress of a record occurrence will not change.</p><p>Given a database address, the physical location on the file canbe calculated directly. The file number portion of the databaseaddress is used to obtain the number of slots per page<b>(slots_per_page)</b>, the slot size <b>(slsize)</b>, and thepage size <b>(pgsize)</b>. The page is calculated as:</p><div style="margin-left: 4em"><p><i>page = ( (slot number - 1) / slots_per_page ) + 1</i></p></div><p>and the offset within a page is:</p><div style="margin-left: 4em"><p><i>offset = slsize * ( ( slot number - 1 ) % slots_per_page ) +4</i></p></div><p>where the addition of four is required because of the four-byteupdate date/time at the beginning of the page.</p><p>The physical address then becomes:</p><div style="margin-left: 4em"><p><i>address = page * pgsize + offset</i></p></div><p>In the remainder of this chapter, we will use the followingnotation for database addresses: <b>[file number:slot number]</b>.For example, <b>[3:35]</b> will refer to slot 35 in file number 3.A null database address, then, is <b>[0:0]</b>. In figures, thebrackets will be left off.</p><h4><a name="SetPointers" id="SetPointers"></a>14.2.2.2 Set andMember Pointers</h4><p><font size="2">The implementation of variable-length sets usingfixed-length records is accomplished by creating linked lists ofdatabase addresses. Record types that are owners of sets willcontain within each record a 12-byte area (or 16-byte iftimestamped), which uses database addresses to point to the firstand last members of the set, and which contains a count of themembers. These 12- or 16- byte segments are referred to as setpointers. For each set type owned by the record type, one setpointer will exist in the record header. Table 14-2 shows thestructure of set pointers.</font></p><p align="center"><b>Table 14-2. Set Pointer Contents</b></p><table cellspacing="0" border="0" cellpadding="7" width="542"><tr><td width="16%" valign="top"><p><b><font size="2">Offset</font></b></p></td><td width="18%" valign="top"><p><b><font size="2">Length</font></b></p></td><td width="66%" valign="top"><p><b><font size="2">Contents</font></b></p></td></tr><tr><td width="16%" valign="top"><p><font size="2">0</font></p></td><td width="18%" valign="top"><p><font size="2">4</font></p></td><td width="66%" valign="top"><p><font size="2">Count of members in set</font></p></td></tr><tr><td width="16%" valign="top"><p><font size="2">4</font></p></td><td width="18%" valign="top"><p><font size="2">4</font></p></td><td width="66%" valign="top"><p><font size="2">Database address of first member</font></p></td></tr><tr><td width="16%" valign="top"><p><font size="2">8</font></p></td><td width="18%" valign="top"><p><font size="2">4</font></p></td><td width="66%" valign="top"><p><font size="2">Database address of last member</font></p></td></tr><tr><td width="16%" valign="top"><p><font size="2">12</font></p></td><td width="18%" valign="top"><p><font size="2">4</font></p></td><td width="66%" valign="top"><p><font size="2">Optional last update timestamp</font></p></td></tr></table><p align="center"><font size="2"><img alt="dbstar_14-5.gif - 2144 Bytes" border="0" height="51" src="dbstar_14-5.gif" width="397"></font></p><p align="center"><b>Fig. 14-5. Set Pointer</b></p><p>If the <b>count</b> field in the set pointer is zero, then theset instance owned by the record is empty (it has no members).</p><p>When a record type is the member of one or more sets, it willcontain a 12-byte member pointer for each set of which it is amember. A member pointer contains the database addresses of theowner record, previous member record, and next member record. Table14-3 shows the structure of a member pointer.</p><p align="center"><b>Table 14-3. Member Pointer Contents</b></p><table cellspacing="0" border="0" cellpadding="7" width="542"><tr><td width="15%" valign="top"><p><b><font size="2">Offset</font></b></p></td><td width="16%" valign="top"><p><b><font size="2">Length</font></b></p></td><td width="69%" valign="top"><p><b><font size="2">Contents</font></b></p></td></tr><tr><td width="15%" valign="top"><p><font size="2">0</font></p></td><td width="16%" valign="top"><p><font size="2">4</font></p></td><td width="69%" valign="top"><p><font size="2">Database address of owner record</font></p></td></tr><tr><td width="15%" valign="top"><p><font size="2">4</font></p></td><td width="16%" valign="top"><p><font size="2">4</font></p></td><td width="69%" valign="top"><p><font size="2">Database address of previous member</font></p></td></tr><tr><td width="15%" valign="top"><p><font size="2">8</font></p></td><td width="16%" valign="top"><p><font size="2">4</font></p></td><td width="69%" valign="top"><p><font size="2">Database address of next member</font></p></td></tr></table>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -