Introduction MTB Statements File System Compiler Applications Reference Guide Index

Overview Keyed Files Indexed Sequential Files Text Files File I/O Operations

Keyed Files

Keyed files provide direct access to the data records.

Comet stores data for keyed files in two DOS disk files. One DOS file stores the data records in their entered order, and a "key tree" file stores the record keys in ascending ASCII order.

An individual record may be retrieved using this identifier using the KEY= parameter of the READ (or associated input) statement.

A keyed file may also be read in key-sequential order; records are retrieved in ascending sorted key order.

The index for a keyed file is a string field from 1 to 64 characters long. When the file is created, the key length is specified. So is the record length; this value ranges from 1 to 1,024 bytes, and is fixed for each record in the file. Because of the ease of use and advantages of direct access, keyed files are the most common type of data files used in MTB applications.

When an MTB program opens a keyed file, the Comet operating system assigns a unique file pointer to the file. Each user opening the file is assigned a unique pointer, allowing multiple users to access data from the same file at the same time. To avoid data integrity problems when more than one user is accessing a file, MTB provides a record locking mechanism. The EXTRACT statement is used to read and lock individual data records.

As a user accesses records in a keyed file, the file pointer keeps track of the current location. When the file is opened, the pointer is located at the record associated with the first key. If the records are retrieved without an index, the pointer moves along in ascending ASCII order of the key values (i.e., in key-sequential order).

When a record is accessed directly using an index, the pointer is moved to the associated position. If no record is found matching the specified index, an exception occurs, but the pointer is located at the index following the requested index value. From this point, the program could retrieve records in sequence (without an index), or retrieve another record from the file using another index value.

Keyed files may also be accessed with a numeric index. In this case, the index value is used to specify the record number in the file in record number sequence (rather than in key sequence). Note: This access method may produce records that have been marked for deletion. These records will contain a hex "FE" in the seventh byte of the record.

Application notes:

  1. A keyed file provides the most convenient way to randomly access a file. Therefore, most MTB programs use keyed files to store data records.

  2. When a record is deleted from a keyed file, the record space is reused by the file to store subsequent records. This advantage is another reason why keyed files are predominant in MTB applications.

  3. A special type of keyed file can be used to create secondary indexes for any keyed file. A key-only file is a keyed file with a record size of zero bytes and a specified key size from 1 to 64 bytes. Typically, a key-only file would contain a composite key value made up of two or more fields from a related keyed file.

    For example, suppose a keyed file contains customer names and addresses. Typically, this file would be keyed by customer number. A secondary key could be created for this file and saved in a separate key-only file. The secondary key could be composed of customer name and customer number (concatenated into a single key value). Thus, with the appropriate programming, an application could access the customer file via either the primary key (customer number) or the secondary key (customer name).

    The contents of key-only files are maintained by application programs, not by Comet. Likewise, lookup routines must be provided by the application program. Nonetheless, key-only files are popular tools in MTB applications.