![]() |
Introduction Unit aims, objectives and prerequisites. | |
![]() |
A look at some programs that use Indexed files We begin with three example programs. The first creates an Indexed file from a Sequential file, the second reads the Indexed file sequentially or either the primary or the alternate key, and the third reads the file directly on either key. | |
![]() |
Indexed
file organization Explains by means of an animated diagram how the index of the primary key is organized and shows how it is used to read a record directly. Explains how the alternate key index is organized and shows how it is used to read a record directly. | |
![]() |
Indexed
file - declarations Examines the SELECT and ASSIGN clause declarations required for an Indexed file. | |
![]() |
Indexed
file - file processing verbs Examines the Procedure Division verbs used to process Indexed files - OPEN, CLOSE, READ, WRITE, REWRITE, DELETE, START | |
![]() |
A comprehensive example
|
|
A look at some programs that use Indexed files | ||||
Example program - Creating an Indexed file |
| |||
Example program - Reading an Indexed file sequentially |
| |||
Example program - Reading an Indexed file directly |
|
|
|
Indexed file - Declarations | |
Introduction |
As we have seen in the example programs, when Indexed files are used a number of new entries for the SELECT and ASSIGN clause are required. |
Select and Assign clause syntax |
|
The Record Key phrase |
The RECORD KEY phrase defines the Indexed file's primary key. Every Indexed file must have a primary key. The key must be a field in the record description that contains a unique value for each record. Contrast this with Relative Files where the key must not be part of the file's record description. The key field must be a numeric or alphanumeric data item. |
The Alternate Record Key phrase |
In addition to the primary key, up to 254 alternate keys may be defined for the file. Just as with the primary key, these alternate keys must be fields in the file's record description. Each alternate key may be unique or may have duplicate values (for this the WITH DUPLICATES clause is required).
|
|
Indexed file - file processing verbs | |
Introduction |
The file processing verb used with Indexed files are the same as those used with Relative files. Indexed files use the same file processing verbs as Relative files (OPEN, CLOSE, READ, WRITE, REWRITE, DELETE and START) but there are some syntactic and semantic differences. In this section we will only examine those verbs which differ in syntax or semantics from those used with Relative files. |
The READ verb |
When an Indexed file has an ACCESS MODE of SEQUENTIAL, the format of the READ is the same as for Sequential files, but when the ACCESS MODE is DYNAMIC Sequential processing of the file is complicated by the presence of a number of indexes. The order in which the data records will be read, will depend on which index is being processed sequentially. |
Key Of Reference |
When an Indexed file has an ACCESS MODE of SEQUENTIAL, the file is always processed in ascending primary key order. But, if an Indexed file has an ACCESS MODE of DYNAMIC and is processed sequentially, the file system must be able to tell which of the keys to use as the basis for processing the file. Since the format of the sequential READ does not have a key phrase, the file system refers to a special item called the Key Of Reference to discover which key to use for processing the file. Before reading a file that has an ACCESS MODE of DYNAMIC sequentially, the programmer must establish one of the file's keys as the Key Of Reference. A Key Of Reference is established by using the key in a START or a direct READ. When the file is opened the primary key is, by default, the Key Of Reference. |
Reading Sequentially |
Notes This format will read the file in ascending sequence on the key that has been established as the key of reference. The READ NEXT will read the logical record pointed to by the next record pointer (This will be the current record if positioned by the START and the next record if positioned by a direct READ). Operation
|
Reading using a key |
The syntax of the direct READ is the same as that for Relative files but the semantics are somewhat different Operation
When the READ is executed the record with the key value equal to the present value of KeyName will be read.
Notes If the KEY IS clause is omitted, the key used will be the primary key. When the READ is executed, the key mentioned in the KEY IS phrase will be established as the Key Of Reference. If there is no KEY IS phrase, the primary key will be established as the Key Of Reference. If duplicates are allowed, only the first record in a group with duplicates can be read directly. The rest of the duplicates must be read sequentially using the READ NEXT format. The KEY IS phrase can only be used with Indexed files and it is used because Indexed files may have more than one key. After the record has been read, the next record pointer points to the next logical record in the file. If the Key Of Reference is the primary key then this record will be an actual data record but if the Key Of Reference is one of the alternate keys then the pointer will point to the next alternate index base record. The file must have an ACCESS MODE of DYNAMIC or RANDOM. The file must be opened for I-O or INPUT. |
The WRITE, REWRITE and DELETE verbs. |
The syntax and semantics of the WRITE, REWRITE and DELETE verbs is the same as that for Relative files with the following exceptions;
|
The START verb |
In Indexed files, the the START verb is used to control the position of the next record pointer and to establish a key as the Key Of Reference. Where the START verb appears in a program it is usually followed by a sequential READ or WRITE. Operation
To establish a key as the Key of Reference and position the Next Record Pointer at the first record in that key sequence
Notes Before the START is executed some value must be moved to the KeyName. Using the START with some key establishes that key as the Key Of Reference. The file must be opened for INPUT or I-O when the START is executed. Execution of the START statement does not change the contents of the record area (i.e. the START does not actually read the record it merely positions the Next Record Pointer). When the START is executed the Next Record Pointer is set to the first logical record in the file whose key satisfies the condition. If no record satisfies the condition then the INVALID key clause is activated. |
|
An Comprehensive Example Program | |||||||||||||||||||||||||||||||||||||||||||||||||||
Program Specification |
ROYALTY PAYMENT REPORT PROGRAM. Introduction
The report must be printed in agent name sequence. The sequence of authors within each agent and of books within each author does not matter. The print specification for the report is on the next page. All the data required to produce the ROYALTY PAYMENT REPORT is contained in two indexed files. The indexed file BOOKS.DAT has the following record description;-
The indexed file AUTHOR.DAT has the following record description;-
Program Procedure
In addition to producing the report the program must perform a small update on the QuarterBorrowings field of the BOOKS file. When all the calculations involving the QuarterBorrowings field have been done, it must be set to zero so that the borrowings for the new quarter may be accumulated. | ||||||||||||||||||||||||||||||||||||||||||||||||||
Example program | |||||||||||||||||||||||||||||||||||||||||||||||||||
$ SET SOURCEFORMAT"FREE" IDENTIFICATION DIVISION. PROGRAM-ID. WirthMemLib. AUTHOR. Michael Coughlan. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT BookFile ASSIGN TO "BOOKS.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS BookNumber ALTERNATE RECORD KEY IS AuthorNumber WITH DUPLICATES FILE STATUS IS BookErrorStatus. SELECT AuthorFile ASSIGN TO "AUTHOR.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS AuthorNum ALTERNATE RECORD KEY IS AgentName WITH DUPLICATES FILE STATUS IS AuthorErrorStatus. SELECT PrintFile ASSIGN TO "REPORT.EXM". DATA DIVISION. FILE SECTION. FD BookFile. 01 BookRec. 88 EndOfBookFile VALUE HIGH-VALUES. 88 NotEndOfBookFile VALUE LOW-VALUES. 02 BookNumber PIC X(7). 02 BookName PIC X(25). 02 AuthorNumber PIC 9(7). 02 RoyaltyRate PIC V999. 02 QtrBorrowings PIC 999. FD AuthorFile. 01 AuthorRec. 88 EndOfAuthorFile VALUE HIGH-VALUES 02 AuthorNum PIC X(7). 02 AuthorName PIC X(25). 02 AgentName PIC X(25). FD PrintFile. 01 PrintLine PIC X(130). WORKING-STORAGE SECTION. 01 ErrorStates. 02 BookErrorStatus PIC X(2). 88 RecordAlreadyExists VALUE "22". 88 RecordDoesNotExist VALUE "23". 02 AuthorErrorStatus PIC X(2). 88 RecordAlreadyExists VALUE "22". 88 RecordDoesNotExist VALUE "23". 01 IntermediateVariables. 02 BookRoyalty PIC 9(3)V99. 02 QtrAuthorBorrows PIC 9(4). 02 AuthorRoyalties PIC 9(4)V99. 02 AgentPayment PIC 9(6)V99. 02 PrevAuthor PIC 9(7). 02 PrevAgent PIC X(25). 01 ReportLines. 02 ReportHeader. 03 FILLER PIC X(37) VALUE SPACES. 03 FILLER PIC X(24) VALUE "ROYALTY PAYMENT REPORT". 02 Underline. 03 FILLER PIC X(36) VALUE SPACES. 03 FILLER PIC X(25) VALUE ALL "-". 02 FieldHeaders. 03 FILLER PIC X(9) VALUE SPACES. 03 FILLER PIC X(5) VALUE "AGENT". 03 FILLER PIC X(21) VALUE SPACES. 03 FILLER PIC X(6) VALUE "AUTHOR". 03 FILLER PIC X(20) VALUE SPACES. 03 FILLER PIC X(4) VALUE "BOOK". 03 FILLER PIC X(16) VALUE SPACES. 03 FILLER PIC X(7) VALUE "QTR.BRW". 03 FILLER PIC X(9) VALUE " ROYALTY". 02 BookLine. 03 AgentNamePrn PIC X(25). 03 AuthorNamePrn PIC BBX(25). 03 BookNamePrn PIC BBX(25). 03 BookQtrBorrowsPrn PIC BBBBZZ9. 03 BookRoyaltyPrn PIC BBBB$$$9.99. 02 AuthorLines. 03 QtrBorrowsLine. 04 FILLER PIC X(54) VALUE SPACES. 04 FILLER PIC X(36) VALUE "QUARTER BORROWINGS FOR THIS AUTHOR =". 04 QtrBorrowsPrn PIC BBBBBZ,ZZ9. 03 QtrRoyaltiesLine. 04 FILLER PIC X(54) VALUE SPACES. 04 FILLER PIC X(36) VALUE "ROYALTIES OWED TO THIS AUTHOR =". 04 QtrRoyaltiesPrn PIC B$$,$$9.99. 02 AgentLine. 03 FILLER PIC X(55) VALUE SPACES. 03 FILLER PIC X(33) VALUE "AMOUNT TO BE PAID TO THIS AGENT =". 03 AgentRoyaltiesPrn PIC B$$$$,$$9.99. PROCEDURE DIVISION. Begin. OPEN I-O BookFile. OPEN I-O AuthorFile. OPEN OUTPUT PrintFile. MOVE SPACES TO PrintLine. WRITE PrintLine AFTER ADVANCING PAGE. WRITE PrintLine FROM ReportHeader AFTER ADVANCING 1 LINE. WRITE PrintLine FROM Underline AFTER ADVANCING 1 LINE. WRITE PrintLine FROM FieldHeaders AFTER ADVANCING 3 LINES. MOVE SPACES TO PrintLine. WRITE PrintLine AFTER ADVANCING 1 LINE. MOVE SPACES TO AgentName. START AuthorFile KEY IS GREATER THAN AgentName INVALID KEY DISPLAY "OH DEAR SOMETHING WRONG IN BEGIN PARA" END-START. READ AuthorFile NEXT RECORD AT END SET EndOfAuthorFile TO TRUE END-READ. PERFORM ProcessAgents UNTIL EndOfAuthorFile. CLOSE BookFile. CLOSE AuthorFile. CLOSE PrintFile. STOP RUN. ProcessAgents. MOVE AgentName TO AgentNamePrn, PrevAgent. MOVE ZEROS TO AgentPayment. PERFORM ProcessAuthors UNTIL EndOfAuthorFile OR AgentName NOT EQUAL TO PrevAgent. MOVE AgentPayment TO AgentRoyaltiesPrn. WRITE PrintLine FROM AgentLine AFTER ADVANCING 1 LINE. MOVE SPACES TO PrintLine. WRITE PrintLine AFTER ADVANCING 2 LINES. ProcessAuthors. MOVE ZEROS TO QtrAuthorBorrows, AuthorRoyalties. MOVE AuthorNum TO AuthorNumber, PrevAuthor. MOVE AuthorName TO AuthorNamePrn. READ BookFile KEY IS AuthorNumber INVALID KEY DISPLAY "ERROR IN ProcessAgents = " BookErrorStatus END-READ. PERFORM ProcessBooks UNTIL EndOfBookFile OR AuthorNumber NOT EQUAL TO PrevAuthor. SET NotEndOfBookFile TO TRUE. MOVE QtrAuthorBorrows TO QtrBorrowsPrn. MOVE AuthorRoyalties TO QtrRoyaltiesPrn. WRITE PrintLine FROM QtrBorrowsLine AFTER ADVANCING 2 LINES. WRITE PrintLine FROM QtrRoyaltiesLine AFTER ADVANCING 1 LINE. MOVE SPACES TO PrintLine. WRITE PrintLine AFTER ADVANCING 2 LINES. READ AuthorFile NEXT RECORD AT END SET EndOfAuthorFile TO TRUE END-READ. ProcessBooks. PERFORM ProcessOneBook. READ BookFile NEXT RECORD AT END SET EndOfBookFile TO TRUE END-READ. MOVE SPACES TO AuthorNamePrn, AgentNamePrn. ProcessOneBook. MULTIPLY QtrBorrowings BY RoyaltyRate GIVING BookRoyalty ROUNDED. ADD QtrBorrowings TO QtrAuthorBorrows. ADD BookRoyalty TO AuthorRoyalties, AgentPayment. MOVE BookName TO BookNamePrn. MOVE QtrBorrowings TO BookQtrBorrowsPrn. MOVE BookRoyalty TO BookRoyaltyPrn. WRITE PrintLine FROM BookLine AFTER ADVANCING 1 LINE. MOVE ZEROS TO QtrBorrowings. REWRITE BookRec INVALID KEY DISPLAY "REWRITE ProcessOneBook " BookErrorStatus END-REWRITE. | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
Copyright NoticeThese COBOL course materials are the copyright property of Michael Coughlan. All rights reserved. No part of these course materials may be reproduced in any form or by any means - graphic, electronic, mechanical, photocopying, printing, recording, taping or stored in an information storage and retrieval system - without the written permission of the author. (c) Michael Coughlan Last updated : April 1998 |