Indexed files

An indexed file is the one organisation that finds a record by its key. The bytes are stored with an index beside them, the program names the key in its SELECT, and the file returns its records in key order whatever order they were written in. This lesson writes one, reads it back and shows the two access modes that matter.

ORGANIZATION INDEXED and RECORD KEY

Three clauses make the file: the organisation, the access mode and the record key. The key is a field of the FD record, and the file keeps a copy of it in its own index:

IDENTIFICATION DIVISION. PROGRAM-ID. W2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUS-F ASSIGN TO "cus2.idx" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS CUS-ID FILE STATUS IS WS-STATUS. DATA DIVISION. FILE SECTION. FD CUS-F. 01 CUS-REC. 05 CUS-ID PIC X(3). 05 CUS-NAME PIC X(8). WORKING-STORAGE SECTION. 01 WS-STATUS PIC XX. PROCEDURE DIVISION. OPEN OUTPUT CUS-F. DISPLAY "open " WS-STATUS. MOVE "C30" TO CUS-ID. WRITE CUS-REC. DISPLAY "write C30 " WS-STATUS. MOVE "A10" TO CUS-ID. WRITE CUS-REC. DISPLAY "write A10 " WS-STATUS. MOVE "B20" TO CUS-ID. WRITE CUS-REC. DISPLAY "write B20 " WS-STATUS. CLOSE CUS-F. OPEN INPUT CUS-F. PERFORM UNTIL WS-STATUS = "10" READ CUS-F NEXT AT END CONTINUE NOT AT END DISPLAY "read " CUS-ID END-READ END-PERFORM. CLOSE CUS-F. STOP RUN.

cobc -x -free -Wall -o w2 w2.cob
./w2

open 00
write C30 00
write A10 21
write B20 21
read C30

ACCESS MODE SEQUENTIAL and status 21

ACCESS MODE IS SEQUENTIAL means the program walks the file in key order, and it applies to the writes as well: a key lower than the last one written is refused with status 21, "sequence error", and the record is not written. The program wrote C30 and then A10, so the file holds one record and the two failures are visible only in the status.

That is the mode a program uses when it builds a file from data already in key order, because it costs the least index work. A program that reads a file in an arbitrary order, or updates random records, needs the other mode.

ACCESS MODE DYNAMIC

With ACCESS MODE IS DYNAMIC the same three writes are accepted in any order, and a READ ... NEXT afterwards returns the records in key order:

IDENTIFICATION DIVISION. PROGRAM-ID. W3. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUS-F ASSIGN TO "cus3.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUS-ID FILE STATUS IS WS-STATUS. DATA DIVISION. FILE SECTION. FD CUS-F. 01 CUS-REC. 05 CUS-ID PIC X(3). 05 CUS-NAME PIC X(8). WORKING-STORAGE SECTION. 01 WS-STATUS PIC XX. PROCEDURE DIVISION. OPEN OUTPUT CUS-F. DISPLAY "open " WS-STATUS. MOVE "C30" TO CUS-ID. WRITE CUS-REC. DISPLAY "write C30 " WS-STATUS. MOVE "A10" TO CUS-ID. WRITE CUS-REC. DISPLAY "write A10 " WS-STATUS. MOVE "B20" TO CUS-ID. WRITE CUS-REC. DISPLAY "write B20 " WS-STATUS. CLOSE CUS-F. OPEN INPUT CUS-F. PERFORM UNTIL WS-STATUS = "10" READ CUS-F NEXT AT END CONTINUE NOT AT END DISPLAY "read " CUS-ID END-READ END-PERFORM. CLOSE CUS-F. STOP RUN.

cobc -x -free -Wall -o w3 w3.cob
./w3

open 00
write C30 00
write A10 00
write B20 00
read A10
read B20
read C30

The file is not a list the program appends to; it is a set organised by key, and the order of the writes has nothing to do with the order of the reads. The price is the file size: the three small records took 8 KB, because the index and the block of the file are allocated whether the records fill them or not.

ALTERNATE KEY

A second key gives the same records a second order and a second way to find one. It is declared beside the record key, and a read names it:

IDENTIFICATION DIVISION. PROGRAM-ID. ALTKEY. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUS-F ASSIGN TO "cus4.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS CUS-ID ALTERNATE KEY IS CUS-NAME FILE STATUS IS WS-STATUS. DATA DIVISION. FILE SECTION. FD CUS-F. 01 CUS-REC. 05 CUS-ID PIC X(3). 05 CUS-NAME PIC X(8). WORKING-STORAGE SECTION. 01 WS-STATUS PIC XX. 01 SEEK-NAME PIC X(8) VALUE "Grace". PROCEDURE DIVISION. OPEN OUTPUT CUS-F. MOVE "A10" TO CUS-ID. MOVE "Ada" TO CUS-NAME. WRITE CUS-REC. MOVE "B20" TO CUS-ID. MOVE "Grace" TO CUS-NAME. WRITE CUS-REC. MOVE "C30" TO CUS-ID. MOVE "Bob" TO CUS-NAME. WRITE CUS-REC. CLOSE CUS-F. OPEN INPUT CUS-F. MOVE SEEK-NAME TO CUS-NAME. READ CUS-F KEY IS CUS-NAME INVALID KEY DISPLAY "no customer named " SEEK-NAME NOT INVALID KEY DISPLAY "found " CUS-ID " " CUS-NAME END-READ. CLOSE CUS-F. STOP RUN.

cobc -x -free -Wall -o altkey altkey.cob
./altkey

found B20 Grace

The read found the record by the name and returned the identifier with it. An alternate key may allow duplicates with DUPLICATES after it, and a program that reads by such a key with duplicates gets the first one and continues with READ ... NEXT in the alternate order. The status table of the previous lesson applies to every one of these reads: 00 for a record, 23 for a key that is not there.

The next lesson walks an indexed file from a starting key, which is how a program processes the records of one customer or one branch without reading the whole file.

Exercises

  1. Write the three keys in ascending order with ACCESS MODE SEQUENTIAL and see all three statuses become 00.
  2. Add a fourth record whose key duplicates A10 with ACCESS MODE DYNAMIC. Which status comes back?
  3. Read the alternate key "Zed" and print the status. Which code is it?
  4. Remove ALTERNATE KEY and keep the READ ... KEY IS CUS-NAME. What does the compiler say?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does RECORD KEY name?

    Answer

    A field of the record whose value identifies the record

    The key is part of the FD record and the file keeps an index of it.

  2. In which order does READ NEXT return the records of an indexed file?

    Answer

    Key order, whatever order they were written in

    The dynamic program wrote C30, A10, B20 and read A10, B20, C30.

  3. What does ACCESS MODE SEQUENTIAL do to a write with a key below the last one?

    Answer

    It refuses it with status 21 and does not write the record

    The out-of-order writes printed 21 and the file held only C30.

  4. What does ALTERNATE KEY add?

    Answer

    A second key with its own index, used by READ ... KEY IS

    The lookup by name found the record whose identifier is B20.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q5.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. 01 N-F PIC 9 VALUE 0. PROCEDURE DIVISION. OPEN OUTPUT F-I. MOVE "BB" TO K-F. WRITE R-I. MOVE "AA" TO K-F. WRITE R-I. CLOSE F-I. OPEN INPUT F-I. PERFORM UNTIL W-S = "10" READ F-I NEXT AT END CONTINUE NOT AT END ADD 1 TO N-F END-READ END-PERFORM. CLOSE F-I. DISPLAY N-F. STOP RUN.

    Answer

    2

    Both records were written and both are read back in key order.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q6.idx" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-I. MOVE "BB" TO K-F. WRITE R-I. MOVE "AA" TO K-F. WRITE R-I. DISPLAY W-S. CLOSE F-I. STOP RUN.

    Answer

    21

    The second key is below the first, which the sequential access mode refuses with 21.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 K-F PIC X(3) VALUE "A10". 01 L-F PIC X(3) VALUE "B20". PROCEDURE DIVISION. IF K-F IS LESS THAN L-F DISPLAY "A first" ELSE DISPLAY "B first" END-IF. STOP RUN.

    Answer

    A first

    The comparison is alphanumeric, and A sorts before B.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT F-I ASSIGN TO "q8.idx" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS K-F FILE STATUS IS W-S. DATA DIVISION. FILE SECTION. FD F-I. 01 R-I. 05 K-F PIC X(2). WORKING-STORAGE SECTION. 01 W-S PIC XX. PROCEDURE DIVISION. OPEN OUTPUT F-I. MOVE "AA" TO K-F. WRITE R-I. WRITE R-I. DISPLAY W-S. CLOSE F-I. STOP RUN.

    Answer

    22

    The second write repeats the key, and a duplicate record key returns 22.

  9. A program builds an indexed file from a file of transactions in random key order with ACCESS MODE SEQUENTIAL. What happens?

    Answer

    Every write with a lower key fails with 21 and the file is incomplete

    The page measured exactly that: one record written, two statuses of 21.

  10. Why is an indexed file of three small records 8 KB?

    Answer

    The file allocates index and data blocks whether they are full or not

    The listing showed 8192 bytes for the file, and the blocks are the unit of the index.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev