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

📄 ug_ch4b.htm

📁 db.* (pronounced dee-be star) is an advanced, high performance, small footprint embedded database fo
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<p>Sets, however, only implement one-to-many relationships. On thesurface, it might appear that the following incorrectimplementation using two sets would work.</p><pre><font color="#0000FF">record class {   unique key char class_id[6];   ... other fields}record student {   key char name[36];   ... other fields}set my_students {   order last;   owner class;   member student;}set my_classes {   order last;   owner student;   member class;}</font></pre><p><font size="2">The schema diagram in Figure 4-1 illustrates therelationships.</font></p><p align="center">&nbsp;</p><p align="center"><img alt="dbstar_4-1.gif - 2336 Bytes" border="0"height="173" src="dbstar_4-1.gif" width="233"></p><p align="center">Fig. 4-1. Incorrect Many-to-ManyImplementation</p><p>Consider, however, the following instance of the<b>my_students</b> set.</p><pre><font color="#0000FF">Class:   Computer Science 101 (CS101)Students:   Smith      Jones      Kelly      Carlson</font></pre><p><font size="2">A problem occurs when the <b>my_classes</b> setinstances for these students is examined. Each student in CS101must have that same class as a member of his <b>my_classes</b> setinstance. This is not possible, however, since CS101 can only beconnected to a single owner in the <b>my_classes</b>set.</font></p><p>The correct technique for implementing many-to-manyrelationships does indeed utilize two sets but through the use ofan intersection record type, as follows:</p><pre><font color="#0000FF">record class {   unique key char class_id[6];   ... other fields}record student {   key char name[36];   ... other fields}record intersect {} set my_students {   order last;   owner class;   member intersect;}set my_classes {   order last;   owner student;   member intersect;}</font></pre><p><font size="2">Figure 4-2 shows the schema diagram thatcorresponds to the above DDL.</font></p><p align="center">&nbsp;</p><p align="center"><img alt="dbstar_4-2.gif - 2237 Bytes" border="0"height="125" src="dbstar_4-2.gif" width="250"></p><p align="center">Fig. 4-2. Correct Many-to-Many Implementation</p><p>Each <b>student</b> record occurrence will have its own set of<b>intersect</b> record occurrences through the <b>my_classes</b>set. Each of these intersect records is also owned by a specificclass record through the <b>my_students</b> set. Similarly, each<b>class</b> record will have its own set of <b>intersect</b>record occurrences through the <b>my_students</b> set, each ofwhich is owned by an individual student record through the<b>my_classes</b> set.</p><p>In this case the intersection record has no user-defined data init. However, it could be an ideal place to store the student'sgrade for the class if that was needed. In that case you wouldprobably want to change the name of the record type from<b>intersect</b> to <b>grade</b> or some other more meaningfulname.</p><p>A description of the actual database manipulation involved inmaintaining many-to-many relationships, based on the databasedesign example presented in section 4.5, is provided in Chapter5.</p><h5>Variable-length Records</h5><p><font size="2">Only fixed-length records are allowed in<b><i>db.*</i></b>. In systems where variable-length records aresupported, they are often implemented by storing thevariable-length information in a linked list of fixed-lengthsegments. This technique can be implemented by the<b><i>db.*</i></b> application developer through sets.</font></p><p>In the simplest example, a note of arbitrary length isassociated with a record such as a customer record. If the note isstored as a field in the customer record, then an arbitrary limiton the length of the note text would need to be imposed. Much ofthe space taken up by the field would never be used. If, however, aseparate record type were defined to store a single line of text, aset relationship between the customer and note text line wouldallow any number of text lines to be connected to a particularcustomer. The schema diagram in Figure 4-3 shows thisrelationship.</p><p align="center">&nbsp;</p><p align="center"><img alt="dbstar_4-3.gif - 1699 Bytes" border="0"height="139" src="dbstar_4-3.gif" width="135"></p><p align="center">Fig. 4-3. Example Variable-Length TextImplementation</p><p>The corresponding DDL statements are shown below.</p><pre><font color="#0000FF">record customer {   unique key char cust_id[7];   char company[21];   ... other fields}record text {   char line[80]; }set cust_notes {   order last;   owner customer;   member text;}</font></pre><p><font size="2">Use of multiple member sets can save even morespace. Consider the alternative schema diagram in Figure4-4.</font></p><p align="center"><b><img alt="Better Variable-Length Text Implementation" border="0" height="145" src="dbstar_4-4.gif" width="298"></b></p><p align="center">Fig. 4-4. Better Variable-Length TextImplementation</p><p>The DDL corresponding to this implementation follows:</p><pre><font color="#0000FF">record customer {   unique key char cust_id[7];   char company[21];   ... other fields}record text30 {   char t30_line[30]; }record text55 {   char t55_line[55];}record text80 {   char t80_line[80];}set cust_notes {   order last;   owner customer;   member text30;   member text55;   member text80;}</font></pre><p><font size="2">Multiple member sets allow any occurrences of therecord types that are defined as valid members to exist in the sameset instance. Thus, in this example, a customer record may have<b>text30</b>, <b>text55</b>, and <b>text80</b> record occurrencesconnected through the <b>cust_notes</b> set. This allows theprogram to choose the record type that best fits the amount of textactually entered for a given line of text. Note, however, that eachof the text record types should be contained in a separate datafile, or no real space savings will occur (see section 4.4.2,"Physical Design Considerations").</font></p><p>The C code that manages the <b>cust_notes</b> set connections isdescribed in Chapter 5, "Database Manipulation."</p><h4><a name="Multiple" id="Multiple"></a>Use of MultipleDatabases</h4><p><font size="2">Application programs can open and access morethan one database at a time. The use of multiple databases in anapplication can yield certain advantages and therefore should be aconsideration in the design of the database. In this sense, anapplication's database may actually consist of several<b><i>db.*</i></b> databases.</font></p><p>There are at least two situations in which multiple databasedesign is useful:</p><ul><li>A temporary control or working database is desired</li><li>An application is to consist of optional, modularcomponents</li></ul><p>Sometimes temporary application control information needs to bemanipulated in sufficient quantities to benefit from the use ofdatabase operations on that data. This information can be stored ina temporary working database, which is initialized when the programbegins and deleted when the program terminates.</p><p>An accounting package is a good example of a modular databaseapplication. Most such packages provide separate modules forvarious accounting functions. Companies only purchase the modulesthey need. A typical package would include these modules :</p><div style="margin-left: 4em"><p>General Ledger</p><p>Accounts Receivable</p><p>Accounts Payable</p><p>Payroll</p><p>Inventory</p><p>Sales Orders</p></div><p>Almost all the modules require the general ledger module. Theamount of shared information between the other modules depends onthe application.</p><p>When designing an application that will use multiple databases,keep in mind that inter-database relationships can exist, but canonly be implemented relationally through keys or through the use of<b>DB_ADDR</b> fields containing the database addresses of recordsin another database. It is not possible to have a set with an ownerdefined in one database and a member defined in another. However,an account number stored in the accounts receivable database can beused to find the associated account record in the generalledger.</p><h3><a name="Physical" id="Physical"></a>4.4.2 Physical DesignConsiderations</h3><p><font size="2">This section describes those aspects of thephysical characteristics of a <b><i>db.*</i></b> database that canimpact the disk space usage and access performance of anapplication program. An understanding of these physicalimplementation issues will help you design the most efficientdatabases possible under <b><i>db.*</i></b>.</font></p><h4><a name="FileStructure" id="FileStructure"></a>The <i>db.*</i>File Structure</h4><p><font size="2">Data and key files consist of pages, whichcontain a specific number of fixed-length record and key slots.Control information, as well as the normal record and key contents,are stored in these fixed-length slots. The size of the record andkey slots in a file is based on the size of the largest record orkey contained in that file. Smaller records or keys will ofnecessity contain unused slot space.</font></p><p>The amount of control information in a key is always 10 bytes.However, the amount of space required for the control informationin a record varies, and can therefore have database designimplications.</p><p>The control information maintained by <b><i>db.*</i></b> in arecord is as follows:</p><ul><li>Record number (two bytes), required</li><li>Database address (four bytes), required</li><li>Optional key bit maps (one byte for every eight optional keysdefined in the record)</li><li>If record type is timestamped, creation timestamp (fourbytes)</li><li>If record type is timestamped, update timestamp (fourbytes)</li><li>One set pointer (12 or 16 bytes) for each set for which recordtype is defined as an owner. A set pointer for a timestamped setuses 16 bytes, otherwise only 12 bytes are used</li><li>One member pointer (12 bytes) for each set for which recordtype is defined as a member</li></ul><p>The information to be considered in database design is the spacerequired for set and member pointers, as this is determined fromthe DDL.</p><p>Chapter 14, "File Formats and Dictionary Tables," describes indetail the structure of <b><i>db.*</i></b> files, records, andkeys.</p><h4><a name="Placement" id="Placement"></a>Record and KeyPlacement</h4><p><font size="2">The factors that dictate the optimal placement ofrecords and keys into files are not always easy to determine, andare sometimes even mutually exclusive. Here are some "rules ofthumb" to assist you. Some of these involve conflictingrequirements. The best determination for your particularenvironment will be based on your own intuition supported by sometesting, but ultimately through experience gained from actualuse.</font></p><ul><li>Place dissimilar sized records and keys in separate files.</li><li>Minimizing the slot space that would be unused in the smallerrecords is the motivation for this guideline.</li><li>Place records and keys in separate files for better multi-userconcurrency.</li></ul><div style="margin-left: 4em"><p>The chances of multiple users locking the same file is increasedwhen several record or key types are contained in the same file.Placing each key and record type in its own file will minimizeaccess conflicts.</p></div><ul><li>Place owner and member record types in the same file forimproved set access performance.</li></ul><div style="margin-left: 4em"><p>If new owner and member record occurrences are stored andconnected in the same transaction and if they are located on thesame file the likelihood of their being placed on the same databasepage is increased. This could yield better set accessperformance.</p></div><ul><li>Minimize the number of separate key files to improve cacheperformance.</li></ul><div style="margin-left: 4em"><p>Caching is a technique in which frequently accessed databasepages are kept in memory, thus reducing the amount of actual diskinput and output required. Using fewer key files increases theprobability that needed index pages will be in the cache.</p></div><ul><li>Experiment with the size of the pages to change the number ofslots per page.</li></ul><div style="margin-left: 4em"><p>Once the application is built, there will be only a fixed amountof memory left over that can be used for the <b><i>db.*</i></b>cache. Some applications get better performance from a smallernumber of larger-sized pages that contain more slots per page.Other applications get better performance from a larger number ofsmaller-sized pages that contain fewer slots per page.</p></div><h4><a name="Report" id="Report"></a>ddlp File StructureReport</h4><p><font size="2">The File Structure Report is produced by usingthe <b>-r</b> option on the <b>ddlp</b> command line. This reportsummarizes the physical characteristics for each file and recordtype defined in the DDL. The report produced for the checkingaccount database is given below.</font></p><pre><font color="#0000FF">db.* 1.0.0 Summary of Database: ckngacctFri Dec 17 15:05:41 1999FILE: chkg.dat   Id  : 0

⌨️ 快捷键说明

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