Indexed Files

Section 2

Dynamic Access:

Dynamic access allows the user to both sequentially and randomly access the file in the same program. For example, the program might have a menu where the user makes a selection that leads to random access or sequential access or the program might want randomly locate a record and then sequentially process from that point until a condition is met. Dynamically accessing the file allows the user to switch back and forth as needed. The select for the file could look like this:

SELECT MASTER-FILE
    ASSIGN TO "C:\PCOBWIN\VSAM\VSAMALT.DAT"
    ORGANIZATION IS INDEXED
    ACCESS IS DYNAMIC
    RECORD KEY IS MID
    ALTERNATE RECORD KEY IS MITEM-NAME WITH DUPLICATES.

Reading a file with dynamic access:

The programmer will use the standard random read with the INVALID KEY clause when randomly reading from the file. If the read is using the primary key, then the READ does not have to specifiy the key. If the read is using the alternate key, then the READ must have a clause indicating the key that is being used. The sequential read assumes that it will be reading the next record along the path that has been established. (Note that there is also a previous clause that can be used instead of the next which will let you read the previous record on the path.)

Random read using primary key:

MOVE RETR-ID TO MID.
READ MASTER-FILE
    INVALID KEY
        PERFORM B-310-INVALID
    NOT INVALID KEY
        PERFORM B-300-PROCESS
END-READ.

Random read using alternate key:

MOVE RETR-NAME TO MITEM-NAME.
READ MASTER-FILE
    KEY IS MITEM-NAME
    INVALID KEY
        PERFORM B-410-INVALID
    NOT INVALID KEY
        PERFORM B-400-PROCESS.

Sequential read using primary key:

(Assumes starting point follows the random read above or that the start point has been established).

READ MASTER-FILE NEXT
    AT END
        MOVE "YES" TO EOF-IND.

(Note that obviously the AT END clause will vary depending on the program)

Sequential read using alternate key:

(Assumes starting point follows the random read above or that the start point has been established).

READ MASTER-FILE NEXT
    AT END
        MOVE "YES" TO EOF-IND.

(Note that obviously the AT END clause will vary depending on the program)

Random Updating an Indexed Sequential File:

A file can be updated if it has an index and has been opened with the ORGANIZATION IS INDEXED clause and ACCESS IS RANDOM or ACCESS IS DYNAMIC clause along with the RECORD KEY clause. Random updates have a different logic then sequential updates. To start out, the random update is TRANSACTION DRIVEN. This means the logic is based around reading a transaction and acting on it. When there are no more transactions, processing will end. Updating is done using the primary key since that is the basic organization of the file. If the transaction is an ADD, then the record will be written to the file using the WRITE statement as long as there is no record with the same primary key. If the transaction is a CHANGE, the record will be retrieved from the indexed file, the change will be made in memory and the record will be rewritten on the indexed file using the REWRITE statement. If the transaction is a DELETE, the record will be deleted providing that it exists. You are both reading from the indexed file and writing to the indexed file when you do a random update. Therefore, the file should be opened as an I-O file, so that it is both an input and an output file and can therefore be read from and written to.

The logic for a random update is:

First read the transaction record from the transaction file or take in transaction information from the screen. You will be using the field on the transaction record that contains the data that is located in the primary key of the indexed file to process the transaction. In other words if the primary key on the indexed file is the identification number, then you will be working with the identification number from the transaction file. If the primary key on the indexed file is the social security number, then you will be working with the social security number from the transaction file.

 

Record on the indexed file

Record not on the indexed file

ADD

Error condition

WRITE the record on the indexed file

CHANGE

Make changes in memory

REWRITE the record on the indexed file

Error condition

DELETE

DELETE the record from the indexed file

Error condition

 

The logic can be developed differently - the programmer should choose the style that they find easiest to understand and the style that they feel will work most efficiently giving the records that will be processed.

Example #1:

Note: The sample program included with this handout uses this logic.

Outside the processing loop:

Processing loop: (Done until there are no more transactions to process)

 

Example #2:

Outside the processing loop:

Processing loop: (Done until there are no more transactions to process)