Guardian Programmer's Guide

Table Of Contents
Communicating With Disk Files
Guardian Programmer’s Guide 421922-014
5 - 87
Using Alternate Keys With a Relative File
Alternate keys suit this application because:
Using a key value such as the date is a convenient way of accessing data. (The
relative-file example shown earlier in this section expects the user to know the
record number.)
The user can make more than one entry per day in the log, because alternate keys
can be duplicated.
Log entries can be made in any order, because the user can read sequentially by
alternate-key value instead of by physical record sequence (as you would get
when reading by record number).
The sample program needs a new primary-key file because of the new record
structure. You also need an alternate-key file. You can create these files
programmatically using the FILE_CREATE[LIST]_ procedure as described under
Creating Alternate-Key Files earlier in this section, or you can use the FUP utility.
The following example uses FUP to create a primary-key file called ALTLOG and an
alternate-key file called ALTKEY:
1> FUP
-SET TYPE R
-SET BLOCK 2048
-SET REC 512
-SET ALTKEY ("DA",KEYOFF 0,KEYLEN 8)
-SET ALTFILE (0,ALTKEY)
-SHOW
TYPE R
EXT ( 1 PAGES, 1 PAGES )
REC 512
BLOCK 2048
ALTKEY ( "DA", FILE 0, KEYOFF 0, KEYLEN 8 )
ALTFILE ( 0, $ADMIN.OPERATOR.ALTKEY)
ALTCREATE
MAXEXTENTS 16
-CREATE $ADMIN.OPERATOR.ALTLOG
CREATED - $ADMIN.OPERATOR.ALTLOG
CREATED - $ADMIN.OPERATOR.ALTKEY
-EXIT
2>
The sample program shown in this subsection enhances the program given in the
subsection Using Relative Files to use alternate keys. The major changes are
summarized as follows:
The program declares a data structure RECORD to describe each record. Each
record contains a 8-character date (in the format yyyymmdd), a 502-character field
for the users comments, and an integer value representing the length of the
comments in bytes. The READ^RECORD, UPDATE^RECORD, and
INSERT^RECORD procedures all use this data structure when reading records
from the data file or writing records to the data file.