Guardian Programmer's Guide

Table Of Contents
Communicating With Disk Files
Guardian Programmer’s Guide 421922-014
5 - 5
Structured Files
Key-Sequenced Files
In key-sequenced files, each record is identified by a unique key that is stored in the
record itself. With key-sequenced files, you can read records, insert new records,
delete records, and update records. When updating records, you also have the
possibility of changing the length of the record.
Tree-structured index records provide random access to the data records. Data
records can also be accessed sequentially in key sequence.
Key-sequenced files are “tree structured.” The trunk of the tree is an index block
containing records that each point to a second level of index block. The second-level
index blocks contain pointers to a third level, and so on. Finally, the lowest level of
index block contains pointers to the leaves of the tree that contain the data records.
Figure 5-3 shows the structure of a key-sequenced file. This example shows two
levels of index blocks. The second level of index blocks points directly to the data
blocks.
You access data records randomly by specifying the key value of a record. The search
starts by comparing the supplied key with the record keys in the highest-level index
block. The system software finds the highest key in the block that is less than or equal
to the supplied key value. The corresponding record contains a pointer to a second-
level index block where the key comparison is repeated. By traversing index blocks in
this way, the Enscribe software finally arrives at the data block that contains the
desired record. A search of this block locates the record.
When adding a record to a key-sequenced file, the data record is added to the file and
the index records are updated accordingly. Key-sequenced files are initially set up with
empty records in the data blocks to enable records to be added efficiently. When a
data block is full, the Enscribe software creates another and sets up the index pointers
accordingly.
Key-sequenced files also support sequential access of data records by key sequence.
Key-sequenced files are suitable for any application where random access by key
value is required. An inventory file where each record describes a part could be set up
as a key-sequenced file, using the part number as the unique key. A banking system
organized by account number is another typical example.
Note. All index blocks and data blocks of a key-sequenced file reside in the same file.