ug_ch5a.htm

来自「db.* (pronounced dee-be star) is an adva」· HTM 代码 · 共 1,565 行 · 第 1/4 页

HTM
1,565
字号
</td><td width="21%" valign="top"><p><font size="2"><b>d_dbfpath</b></font></p></td><td width="51%" valign="top"><p><font size="2">Set path to directory containing the databasefiles.</font></p></td></tr><tr><td width="28%" valign="top"><p><font size="2">DBLOG</font></p></td><td width="21%" valign="top"><p><font size="2"><b>d_dblog</b></font></p></td><td width="51%" valign="top"><p><font size="2">Set path or name of database log file.</font></p></td></tr><tr><td width="28%" valign="top"><p><font size="2">DBTAF</font></p></td><td width="21%" valign="top"><p><font size="2"><b>d_dbtaf</b></font></p></td><td width="51%" valign="top"><p><font size="2">Set path or name of database family transactionactivity file.</font></p></td></tr><tr><td width="28%" valign="top"><p><font size="2">LOCKMGR</font></p></td><td width="21%" valign="top"><p><font size="2"><b>d_lockmgr</b></font></p></td><td width="51%" valign="top"><p><font size="2">Set the name of the lock manager to be used bythis application.</font></p></td></tr></table><h4><a name="Substituting" id="Substituting"></a>SubstitutingDatabase Files</h4><p><font size="2">Alternate names for data and key files can bespecified at runtime, prior to opening a database, through use ofthe <b>d_renfile</b> function. In order to use this function,however, a file identifier must be provided in the DDL data and keyfile statements for those files that are to be renamed. The DDLfile statement syntax including the specification of a fileidentifier is as follows:</font></p><p><b>data file</b> [<i>fileid</i> =] <i>filename</i><b>contains</b> <i>recname</i> [,<i>recname</i>]... ;<br><b>key file</b> [<i>fileid</i> =] <i>filename</i> <b>contains</b><i>recname</i> [,<i>recname</i>]... ;</p><p>If provided, <i>fileid</i> must be an identifier. It will beincluded in <b>dbname.h</b> as a <b>#define</b> constant that isdefined to be the file table dictionary entry number for thespecified data or key file.</p><p>See the example below:</p><pre><font color="#0000FF">database tims {&lt;   data file timsdat = "tims.dat" contains ...;   key  file timskey = "tims.key" contains ...;   ...</font></pre><p><font size="2">File <b>tims.h</b> would include the followingconstants:</font></p><pre><font color="#0000FF">#define   TIMSDAT 0#define   TIMSKEY 1</font></pre><p><font size="2">Function <b>d_renfile</b> could then be used tosubstitute the files as follows:</font></p><pre><font color="#0000FF">d_renfile("tims", TIMSDAT, "ittia.dat", task);d_renfile("tims", TIMSKEY, "ittia.key", task);d_open("tims","x", task);</font></pre><p><font size="2">The calls to <b>d_renfile</b> must be issuedprior to the call to <b>d_open</b> of database <b>tims</b>. Thename of the database needs to be specified in the <b>d_renfile</b>call, to distinguish between files contained in different databasesif more than one is opened.</font></p><blockquote>Note<b><i>:</i></b> When <b>d_open</b> is locating thedatabase files, the database file specification provided in the<b>d_renfile</b> call overrides the environment variable (DBFPATH)and any path prefix to the database name. The dictionary andcontrol file specifications are unaffected.</blockquote><p><font size="2">The <b>d_renfile</b> function has a companionfunction, <b>d_renclean</b>. Function <b>d_renclean</b> can only becalled while all databases are closed, and will clean up all memoryand references used by <b>d_renfile</b>. This allows switching backand forth between databases as seen in the following example. Inthis example, there are two copies of the data files, one copy thatis shared by everyone on the network (drive <b>s:</b>) and theother one on the users hard drive (drive <b>c:</b>).</font></p><pre><font color="#0000FF">d_open("tims", "s", task);/* ... normal processing on network copy ... */d_close(task);d_renfile("tims", TIMSDAT, " /tims/tims.dat", task);d_renfile("tims", TIMSKEY, " /tims/tims.key", task);d_open("tims", "o", task);/* ... processing on the local copy ... */d_close(task);/* clean up all memory from previous d_renfile functions. */d_renclean(task);d_open("tims", "s", task);/* ... back to processing on the network copy ... */d_close(task);</font></pre><h3><a name="Dynamic" id="Dynamic"></a>5.2.3 Dynamic DatabaseInitialization</h3><p><font size="2">Dynamic database initialization functions allow aprogram to initialize an entire database, or one or more files, ina database at program execution time. These functions are usefulwhen a first-time initialization needs to be included as part of anapplication or when a temporary database is needed.</font></p><blockquote>Note: These functions will destroy existing data. Theyare not part of a normal database open sequence.</blockquote><p><font size="2">Function <b>d_initialize</b> will initialize alldatabase data and key files associated with the open database. Inmulti-user environments, it can only be called when the database isopened in exclusive access mode or exclusive locks have been placedon all the files. Function <b>d_destroy</b> is called to close thedatabase and to delete all of the database files comprising theopen database.</font></p><blockquote>Note: If <b>d_renfile</b> has been called,<b>d_destroy</b> will remove the new file names and leave theoriginal ones untouched.</blockquote><p><font size="2">Individual files that have a <i>fileid</i>specified in the DDL file statement can be initialized usingfunction <b>d_initfile</b>. For example, the following DDL definesfiles that keep track of a daily user login history:</font></p><pre><font color="#0000FF">data file day_data = "dayfile.dat" contains login_history;key file day_key = "dayfile.key" contains login_id;</font></pre><p><font size="2">These files are initialized by the applicationwhen the user logs in at the beginning of the day, asfollows:</font></p><pre><font color="#0000FF">#include "db.star.h"#include "login.h"#include "mis.h"DB_TASK *task;.../* Open Login and Mgt Info System databases */d_opentask(&amp;task);d_open("login;mis", "s", task);/*   Since each user has his own login file, an    exclusive lock on that file will preclude the use of   other locks and yield better performance. */d_reclock(LOGIN_HISTORY, "x", task, CURR_DB);/* Initialize daily login files */d_initfile(DAY_KEY, task, CURR_DB);d_initfile(DAY_DATA, task, CURR_DB);...</font></pre><h3><a name="Runtime" id="Runtime"></a>5.2.4 Runtime Control</h3><p><font size="2">Runtime control functions inform the<b><i>db.*</i></b> runtime of the maximum number of files that canbe opened at a time and the size of the database cache, and areused to turn on or off one or more of several runtime options.These functions provide the flexibility to tune runtime performanceto meet the requirements of individual applications.</font></p><h4><a name="Maximum" id="Maximum"></a>Maximum Open Files</h4><p><font size="2">Many operating systems impose a limitation on thenumber of files that an application program can have open at onetime. UNIX/Linux may have an upper limit of open files. Manydatabase management systems restrict the maximum number of files ina database to this operating system limit, but <b><i>db.*</i></b>allows up to 32,767 files to be accessed in a single database. Thisis done by dynamically opening and closing files as necessary tokeep the number of open files within the operating system'slimit.</font></p><p>Before the database is opened, the program must inform<b><i>db.*</i></b> of the maximum number of data and key filehandles it can have open at a time. This is accomplished withfunction <b>d_setfiles</b>, which is passed an integer valuespecifying this maximum. Function <b>d_setfiles</b> must be calledbefore the database is opened, and the value passed must be lessthan or equal to the operating system limit, minus the maximumnumber of additional files the application will have open at atime. The default is a maximum of eight open files at a time.</p><p>For example, normally a standard C program will have (at least)three files open upon initiation: <b>stdin</b>, <b>stdout</b>, and<b>stderr</b>. If the operating system limit is 100, then thefollowing call will ensure that <b><i>db.*</i></b> will not opentoo many files:</p><pre><font color="#0000FF">d_setfiles(90, task);        /* db.* can open 90 files */d_open("tims", "o", task); /* Open database "tims" */</font></pre><p><font size="2">Setting the number of open files too small mayresult in some performance degradation due to unnecessary fileclosings and openings.</font></p><p>If there are occasions when the application needs more filehandles than are available, it is possible to tell the runtime toclose all files it currently holds open. The <b>d_closeall</b>function will close all files under <b><i>db.*</i></b> control thatare currently open. The files will be re-opened as needed. If thisfunction is used frequently, overall performance will decrease dueto the extra opening and closing of files.</p><h4><a name="Cache" id="Cache"></a>Size of Runtime Cache</h4><p><b><i><font size="2">db.*</font></i> performs all input andoutput for the database files through a</b> <font size="2">cacheconsisting of a fixed number of database page buffers. Thistechnique yields large performance benefits by reducing the numberof actual disk accesses required to read or write information inthe database.</font></p><p>The programmer can specify the number of buffers to allocate forthe cache with function <b>d_setpages</b>. In general, the morepages specified the better the potential performance gains. Thefirst argument to this function specifies the number of pages inthe standard database cache. A second argument specifies the numberof pages in the transaction overflow-index cache (which should besmall because an overflow should occur infrequently).</p><p>If <b>d_setpages</b> is not called, <b><i>db.*</i></b> willallocate 100 pages in the database cache and five pages in thetransaction overflow cache. The cache lookups are performed using ahashing algorithm, as shown below:</p><pre><font color="#0000FF">d_setpages(200, 4, task);</font></pre><p><font size="2">The initial size of the pages allocated for thedatabase cache will be equal to the size of the largest page in thedatabase. Thus, if the largest page is 4096 bytes long, then thedatabase cache in the above example will occupy 524K bytes (128 *4096). The size of the pages in the overflow cache is fixed at 1024bytes. If there is not enough memory available to accommodate therequested number of pages, function <b>d_open</b> will returnstatus S_NOMEMORY.</font></p><h4><a name="Settings" id="Settings"></a>Option Settings</h4><p><font size="2">Various runtime option settings allow you to dothese activities plus others:</font></p><ul><li>Turn on or off the use of delete chain slots (for when newrecords are created)</li><li>Turn on or off transaction logging and recovery</li><li>Turn on or off archive logging</li><li>Change between case-insensitive and case-sensitive sorting</li><li>Dictate how often to close files</li></ul><p>Rather than supplying separate functions to control the settingof these runtime options, <b><i>db.*</i></b> provides twoparameter-based system option setting functions called<b>d_on_opt</b> and <b>d_off_opt</b>. These functions are passed abit status word, which has a bit associated with each option. Theseoptions have been assigned constants in <b>db.star.h</b>. See the<b><i>db.*</i></b> <i>Reference Manual</i> for a complete list.</p><p>Here are some examples. To turn on delete chain usage andarchive logging:</p><pre><font color="#0000FF">d_on_opt(DBCHAINUSE | ARCLOGGING, task);</font></pre><p><font size="2">To turn off transaction logging and archivelogging:</font></p><pre><font color="#0000FF">d_off_opt(TRLOGGING | ARCLOGGING, task);</font></pre><p><font size="2">Transaction and archive logging are discussed inChapter 6, "Transaction Processing."</font></p><p>The ability to turn on or off the use of deleted record slotsprovides some application control over the placement of relatedrecords in the database. If all member record occurrences of agiven set are entered together, and use of the delete chain isturned off, the records will all be physically placed in the orderin which they're entered at the end of the data file. This willimprove performance when, later, they are all accessedtogether.</p><p>To establish case-insensitive sorting with keys:</p><pre><font color="#0000FF">d_on_opt(IGNORECASE, task);</font></pre><p><font size="2">This option will re-define the collating sequenceof characters, and must be used for the lifetime of a database. Youcannot build a database with this option turned on, then later useit with it turned off.</font></p><p>The default settings have transaction logging and delete chainuse turned <i>on</i>, and archive logging and case-insensitivesorting turned <i>off</i>.</p><h2><a name="Currency" id="Currency"></a>5.3 Currency Tables</h2><p><font size="2">All of the data contained in a <b><i>db.*</i></b>database is accessed through use of the currency tables. Thus, athorough understanding of the use of these tables isnecessary.</font></p><p>The retrieval of <b><i>db.*</i></b> data is always a two-stepprocess. First the location of the data is established, and thenthe data is read. The located data is always in the form of aspecific record occurrence. The database address of a record is thelocation in the database where the record is stored. The currencytables contain database addresses as follows:</p><table cellspacing="0" border="0" cellpadding="7" width="542"><tr><td width="22%" valign="top"><p><i><font size="2">current record</font></i></p></td><td width="78%" valign="top"><p><font size="2">The database address of the most recentlyaccessed record occurrence.</font></p></td></tr><tr><td width="22%" valign="top"><p><i><font size="2">current owner</font></i></p></td><td width="78%" valign="top"><p><font size="2">The database address of an owner recordoccurrence for the set. There is a current owner entry for each setin the database.</font></p></td></tr><tr><td width="22%" valign="top"><p><i><font size="2">current member</font></i></p></td><td width="78%" valign="top"><p><font size="2">The database address of a member recordoccurrence for the set. There is a current member entry for eachset in the database.</font></p></td></tr></table><p><font size="2">A currency table value is established through therecord location functions (for example, <b>d_keyfind</b> or<b>d_findnm</b>), and through additional functions that directlymodify currency table entries (for example, <b>d_setor</b> copiesthe <i>current record</i> value to the <i>current owner</i> entryof the specified set). Once a record has been located, its databaseaddress is automatically stored in the currency table. Its contentscan then be read (for example, <b>d_recread</b> reads the contentsof the <i>current record</i>).</font></p><p>Locating record occurrences through set relationships directlyinvolves manipulation of the currency tables. This process iscalled set navigation. The currency tables are used to keep trackof the path through the network of set relationships that wasfollowed to arrive at a particular record occurrence.</p><p>A portion of the <b>tims</b> database schema is given in Figure5-1, with an example of a possible currency table state showingpointers to particular record occurrences in the database.</p><p align="center">&nbsp;</p><p align="center"><img alt="Example Currency Table" border="0"height="371" src="dbstar_5-1.gif" width="466"></p><p align="center">Fig. 5-1. Example Currency Table</p><p>Because of the importance of the currency tables, a rich set offunctions is provided to give the programmer complete control overcurrency table settings. These functions are listed in Table 5-3.The <b>d_setXY</b> functions have a naming convention in which<b>X</b> specifies the destination currency and <b>Y</b> specifiesthe source currency for the assignment operation. For example, in<b>d_setro</b>, the <b>r</b> indicates that the current record isto be assigned and the <b>o</b> indicates that it is to be assignedfrom the current owner of the specified set. Use of the currencytables will be explained in the sections which follow.</p><p align="center"><b>Table 5-3 Currency Control Functions</b></p><table cellspacing="0" border="0" cellpadding="7" width="542"><tr><td valign="top" colspan="2"><p><font size="2"><b>Currency Manipulation Functions</b></font></p></td></tr><tr><td width="40%" valign="top">

⌨️ 快捷键说明

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