Guardian Programmer's Guide

Table Of Contents
Communicating With Disk Files
Guardian Programmer’s Guide 421922-014
5 - 52
Key-Sequenced File Programming Example
-EXIT
2>
The program is similar to the relative-file example given earlier in this section in that it
enables the user to read records, add records, and update records. Because access
to the file is by key value, however, the mechanism is different.
The following procedures provide the major functions of reading, updating, and
inserting records:
The READ^RECORD procedure allows the user to read one record followed
optionally by subsequent sequential reads as it did in the relative-file program. But
here, the key to the random record is the part number—a field of data in the record
itself, not the physical record number. Also, because of the way Enscribe
manages key-sequenced files, sequential reading returns records in key sequence
(by part number), not physical sequence.
This procedure uses an approximate key position to enable the user to start
reading from a particular key value without concern as to whether the key actually
exists. This feature enables the user to start browsing the file from any key value.
The UPDATE^RECORD procedure displays the record for update before
prompting the user for the updated information. First it prompts the user for the
key to the record to be updated (the part number). Then it uses the
READUPDATEX procedure to get the current information from the record. After
displaying the current contents of the record on the user's terminal and receiving
the new contents from the user, this procedure reads the record from the disk file
again, this time using the READUPDATELOCKX procedure; in addition to reading
the record to check whether the record has been modified by some other user
since the previous READUPDATEX call, this procedure also locks the record to
ensure exclusive access while updating. Finally, UPDATE^RECORD issues a call
to WRITEUPDATEUNLOCKX to write the new record contents to disk and unlock
the record.
Locking and unlocking the record protects the record against other processes while
your process is updating the record. Multiple copies of this program can therefore
exist without corrupting each other’s view of data.
The INSERT^RECORD procedure replaces the APPEND^RECORD procedure of
the log-file program. INSERT^RECORD allows the user to insert new records into
the file. Here, the procedure prompts for the contents of the new record (including
the part number) and then writes the record in the appropriate position in the file.
The insertion is rejected if a record with the same key already exists.
The following procedures support the above major procedures:
The DISPLAY^RECORD procedure displays the contents of a part record.
The ENTER^RECORD procedure prompts for information from the user to create a
record. When creating a new record, this procedure prompts for every field in the
new record. When updating an existing record, this procedure prompts for all but