Chapter 15 Indexed and Relative File Processing 15

  • Slides: 64
Download presentation
Chapter 15 Indexed and Relative File Processing 15 -1

Chapter 15 Indexed and Relative File Processing 15 -1

Chapter Objectives To familiarize you with ◦ Methods of disk file organization ◦ Random

Chapter Objectives To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access indexed disk files ◦ How to create, update, and access relative files ◦ Methods used for organizing relative files 15 -2

Chapter Contents Additional Options for Indexed File Processing ◦ ◦ ◦ ALTERNATE RECORD KEYS

Chapter Contents Additional Options for Indexed File Processing ◦ ◦ ◦ ALTERNATE RECORD KEYS START Statement Dynamic Access FILE STATUS Clause Exception Handling 15 -3

Chapter Contents Using Indexed Disk File As External Table Processing Relative Disk Files Converting

Chapter Contents Using Indexed Disk File As External Table Processing Relative Disk Files Converting Key Field to RELATIVE KEY 15 -4

Disk File Organization File is collection of records Three major ways records stored or

Disk File Organization File is collection of records Three major ways records stored or organized on disk - Sequential File Organization - Indexed File Organization - Relative File Organization 15 -5

Sequential File Organization Records stored in order they are written to file Must be

Sequential File Organization Records stored in order they are written to file Must be accessed in sequence - to access 50 th record in file, must read past first 49 Typically sorted into sequence by a key field Nothing new, studied this previously 15 -6

Indexed File Organization Consists of two files ◦ Data file - records in sequence

Indexed File Organization Consists of two files ◦ Data file - records in sequence ◦ Index file - contains value of Each key field Disk address of record of that corresponding key field For random access, look up key field in index file to find address Then access record in data file directly 15 -7

Relative File Organization When records created, key field used to compute a disk address

Relative File Organization When records created, key field used to compute a disk address where record is written To randomly access records ◦ User enters key field ◦ Disk address computed from key field ◦ Record then accessed directly No index needed 15 -8

Creating an Indexed File Records written in sequence by key field as for sequential

Creating an Indexed File Records written in sequence by key field as for sequential disk file Once index file created, records can be accessed randomly P. 650 15 10

 IDENTIFICATION DIVISION. PROGRAM-ID. CH 1501. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-MASTER ASSIGN

IDENTIFICATION DIVISION. PROGRAM-ID. CH 1501. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-MASTER ASSIGN TO "CH 1501. NDX" ORGANIZATION IS INDEXED ACCESS IS RANDOM RECORD KEY IS CUSTOMER-NO-M. SELECT CUSTOMER-TRANS ASSIGN TO "CH 1501. DAT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD RECORD CONTAINS 38 CHARACTERS. CUSTOMER-TRANS 01 TRANS-REC. 05 CUSTOMER-NO-IN PIC X(5). 05 CUSTOMER-NAME-IN PIC X(20). 05 DATE-LAST-IN PIC 9(8). 05 AMOUNT-OWED-IN PIC 9(3)V 99. FD RECORD CONTAINS 39 CHARACTERS. CUSTOMER-MASTER 01 MASTER-REC. 05 CUSTOMER-NO-M PIC X(5). 05 CUSTOMER-NAME-M PIC X(20). 05 DATE-LAST-M PIC 9(8). 05 AMOUNT-OWED-M PIC 9(4)V 99. 15 11

WRITE … INVALID KEY clause required when writing indexed records to handle I/O errors

WRITE … INVALID KEY clause required when writing indexed records to handle I/O errors ◦ Key field not in sequence ◦ Key field same as one already in file If error detected with WRITE ◦ Record not written ◦ Statement(s) following INVALID KEY executed 15 15

WRITE … INVALID KEY Format WRITE record-name-1 [FROM identifier-1] [INVALID KEY imperative-statement-1] [NOT INVALID

WRITE … INVALID KEY Format WRITE record-name-1 [FROM identifier-1] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-WRITE] Statement(s) following NOT INVALID KEY executed if WRITE is successful 15 16

Updating Index File Randomly Not really Randomly Master records can be updated directly without

Updating Index File Randomly Not really Randomly Master records can be updated directly without creating a new file With index file, changes can be made in any sequence Read transaction record (or get update data interactively) Move key field value to RECORD KEY of master file Read master record into storage (READ … INVALID KEY) Make needed changes to fields REWRITE record to master file 15 17

Updating Index File Randomly In SELECT statement specify ACCESS MODE IS RANDOM Open indexed

Updating Index File Randomly In SELECT statement specify ACCESS MODE IS RANDOM Open indexed file for I-O ◦ I (Input) to read in records ◦ O (Output) to rewrite or update records 15 18

READ … INVALID KEY To locate record with key field equal to value stored

READ … INVALID KEY To locate record with key field equal to value stored in record key Move Trans-No To Master-No Read Indexed-File Invalid Key Perform 600 -Err-Rtn Not Invalid Key Perform 500 -OK-Rtn End-Read 15 19

REWRITE … INVALID KEY Format REWRITE record-name-1 [FROM identifier-1] [INVALID KEY imperative-statement-1] [NOT INVALID

REWRITE … INVALID KEY Format REWRITE record-name-1 [FROM identifier-1] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-REWRITE] To update existing indexed record INVALID KEY occurs if programmer has changed key field of record 15 20

Add New Record To add new record to indexed file ◦ Move data to

Add New Record To add new record to indexed file ◦ Move data to master record fields ◦ Use WRITE … INVALID KEY to create new record Page 661, 662 for example 15 21

Delete Existing Record Format DELETE index-file-name-1 RECORD [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2]

Delete Existing Record Format DELETE index-file-name-1 RECORD [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-DELETE] To delete record with key field equal to value stored in record key 15 22

Printing Indexed File Sequentially Process file in same way as a sequential file Specify

Printing Indexed File Sequentially Process file in same way as a sequential file Specify ACCESS IS SEQUENTIAL SORT file before printing if report is to be in sequence by field other than key field 15 24

Random Interactive Inquiries Display 'Enter account number' Accept Acct-No Read Accts-Receivable Invalid Key Display

Random Interactive Inquiries Display 'Enter account number' Accept Acct-No Read Accts-Receivable Invalid Key Display 'Account not on file' Not Invalid Key Display 'Balance due = ', Bal-Due End-Read Note here that Acct-No is a field in the structure of the indexed file and is automatically searched for 15 28

ALTERNATE RECORD KEY Format [ALTERNATE RECORD KEY IS data-name-2 [WITH DUPLICATES] ] … Multiple

ALTERNATE RECORD KEY Format [ALTERNATE RECORD KEY IS data-name-2 [WITH DUPLICATES] ] … Multiple ALTERNATE keys allowed Need not be unique ◦ If Dups, 1 st one on disk is returned Access records by RECORD KEY or any ALTERNATE RECORD KEYs 15 30

SELECT Example Select Accts-Receivable Assign To 'C: Acct. Rec. ndx' Organization Is Indexed Access

SELECT Example Select Accts-Receivable Assign To 'C: Acct. Rec. ndx' Organization Is Indexed Access Is Sequential Record Key Is Acct-No Alternate Record Key Is Cst-Last-Name With Duplicates. 15 31

Random Access by ALTERNATE To access file by Cst-Last-Name Display 'Enter last name' Accept

Random Access by ALTERNATE To access file by Cst-Last-Name Display 'Enter last name' Accept Cst-Last-Name Read Accts-Receivable Key is Cst-Last-Name Invalid Key Display 'No record found' Not Invalid Key Display 'Balance due = ', Bal-Due End-Read 15 32

Other Stuff Start Dynamic Access Relative addressing ◦ Efficient ◦ Uses key to generate

Other Stuff Start Dynamic Access Relative addressing ◦ Efficient ◦ Uses key to generate address 15 34

15 35

15 35

START Statement To begin processing indexed file sequentially starting from any record location ◦

START Statement To begin processing indexed file sequentially starting from any record location ◦ Print file beginning with customer record with Acct. No = 025 ◦ Print all customers with Cst-Last-Name beginning with letter 'S' 15 36

START Statement Format START file-name-1 IS = KEY IS > data-name-1 IS NOT <

START Statement Format START file-name-1 IS = KEY IS > data-name-1 IS NOT < IS >= [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-START] 15 37

START Statement To begin processing with record whose account number equals 025 Move 025

START Statement To begin processing with record whose account number equals 025 Move 025 To Acct-No Start Accts-Receivable Invalid Key Display 'Acct-No 025 not found' Not Invalid Key Perform 300 -Proc-Rec End-Start Note that it just starts here it’s not filtering anything yet. 15 38

START Statement START locates record with Acct-No = 025 INVALID KEY clause executed only

START Statement START locates record with Acct-No = 025 INVALID KEY clause executed only if no such record found START locates record but does not READ it 300 -Proc-Rec must include READ … AT END to bring record into storage for processing 15 39

START Statement KEY clause can be omitted only if checking for value equal to

START Statement KEY clause can be omitted only if checking for value equal to RECORD KEY value To locate record with Acct-No > 100: Move 100 To Acct-No Start Accts-Receivable Key > Acct-No Invalid Key …. . . 15 40

ACCESS IS DYNAMIC Mode used to access indexed file both randomly and sequentially in

ACCESS IS DYNAMIC Mode used to access indexed file both randomly and sequentially in single program For example, update selected records, then print control listing of entire indexed file ◦ Random access used for updating ◦ Sequential access used for printing report 15 41

ACCESS IS DYNAMIC Mode required for reading records in sequence by ALTERNATE RECORD KEY

ACCESS IS DYNAMIC Mode required for reading records in sequence by ALTERNATE RECORD KEY Also required when records accessed by both RECORD KEY and ALTERNATE RECORD KEY 15 42

READ … NEXT RECORD To perform sequential read of indexed file when ACCESS MODE

READ … NEXT RECORD To perform sequential read of indexed file when ACCESS MODE IS DYNAMIC To sequentially read from file by its ALTERNATE RECORD KEY To begin reading sequentially from some point other than beginning of file 15 43

READ … NEXT RECORD Assume first record with Acct-No > 100 has been located

READ … NEXT RECORD Assume first record with Acct-No > 100 has been located using START Use READ … NEXT RECORD to read in records sequentially from this position Only word NEXT required 15 44

READ … NEXT RECORD Perform Until More-Records = 'NO' Read Accts-Receivable Next Record At

READ … NEXT RECORD Perform Until More-Records = 'NO' Read Accts-Receivable Next Record At End Move 'NO' To More-Records Not At End Perform 300 -Proc-Rec End-Read End-Perform 15 45

FILE STATUS Clause To determine exact type of input or output error that occurred

FILE STATUS Clause To determine exact type of input or output error that occurred when accessing a file Included in SELECT statement for a file as last clause SELECT … [FILE STATUS IS data-name] • Format 15 46

FILE STATUS Clause Data-name must appear in WORKINGSTORAGE as two-position alphanumeric field Select Indexed-Pay-File

FILE STATUS Clause Data-name must appear in WORKINGSTORAGE as two-position alphanumeric field Select Indexed-Pay-File … File Status Is WS-Status. … Working-Storage Section. 01 WS-Status Pic X(2). • Example 15 47

FILE STATUS Clause When input or output operation performed on Indexed-Pay-File ◦ Value placed

FILE STATUS Clause When input or output operation performed on Indexed-Pay-File ◦ Value placed in WS-Status ◦ Can be tested by programmer in PROCEDURE DIVISION Several FILE STATUS field values and their meaning follow 15 48

FILE STATUS Values File Status field value 00 21 22 Meaning No error occurred

FILE STATUS Values File Status field value 00 21 22 Meaning No error occurred Sequence error - keys not in correct order Attempt to write record creating duplicate primary record key Complete list on page 681 15 49

Checking FILE STATUS Write Indexed-Pay-Rec Invalid Key If WS-Status = '21' Display 'Key not

Checking FILE STATUS Write Indexed-Pay-Rec Invalid Key If WS-Status = '21' Display 'Key not in sequence' End-If … Not Invalid Key Perform 600 -OK-Rtn End-Write 15 50

Exception Handling Most comprehensive method for handling input/output errors is to establish separate section(s)

Exception Handling Most comprehensive method for handling input/output errors is to establish separate section(s) for this Place exception handling routines in DECLARATIVES segment ◦ Always appears first in PROCEDURE DIVISION ◦ Must begin with section-name ◦ You’ll need at least one other section name for the rest of your procedure division. 15 51

DECLARATIVES Format DECLARATIVES. section-name SECTION. USE AFTER STANDARD EXCEPTION ERROR ON file-name-1 … END

DECLARATIVES Format DECLARATIVES. section-name SECTION. USE AFTER STANDARD EXCEPTION ERROR ON file-name-1 … END DECLARATIVES. PROCEDURE 15 52

DECLARATIVES Example Procedure Division. Declaratives. A 000 -Exception-Handling Section. Use After Error Procedure On

DECLARATIVES Example Procedure Division. Declaratives. A 000 -Exception-Handling Section. Use After Error Procedure On Indexed-Pay-File A 100 -Check-It. If WS-Status = '21' Display 'Key not in sequence' End-If … End Declaratives. 15 53

DECLARATIVES Example B 000 -Regular-Processing Section. B 100 -Main-Paragraph. … Read Indexed-Pay-File … Write

DECLARATIVES Example B 000 -Regular-Processing Section. B 100 -Main-Paragraph. … Read Indexed-Pay-File … Write Indexed-Pay-File … 15 54

DECLARATIVES Example Once section header used, rest of PROCEDURE DIVISION must be divided into

DECLARATIVES Example Once section header used, rest of PROCEDURE DIVISION must be divided into sections Statements in paragraph A 100 -Check-It in DECLARATIVES test value of FILE STATUS field INVALID KEY not needed for READ or WRITE since errors handled in DECLARATIVES 15 55

Relative Files File organization that converts key field to actual disk address to find

Relative Files File organization that converts key field to actual disk address to find location of record ◦ No need to look up disk address in index ◦ Convert key to disk address and access record directly Records may be accessed both sequentially and randomly 15 56

SELECT for Relative Files SELECT file-name-1 ASSIGN to implementor-name-1 [ORGANIZATION IS] RELATIVE [ACCESS IS

SELECT for Relative Files SELECT file-name-1 ASSIGN to implementor-name-1 [ORGANIZATION IS] RELATIVE [ACCESS IS SEQUENTIAL [RELATIVE KEY IS data-name-1] RANDOM RELATIVE KEY IS DYNAMIC data-name-1 [FILE STATUS IS data-name-2]. 15 57

SELECT for Relative Files RELATIVE KEY clause ◦ Optional if ACCESS is SEQUENTIAL ◦

SELECT for Relative Files RELATIVE KEY clause ◦ Optional if ACCESS is SEQUENTIAL ◦ Otherwise, required ACCESS IS DYNAMIC allows both sequential and random access in same program FILE STATUS field used same way as with indexed files 15 58

FD for Relative Files RELATIVE KEY not part of record ◦ Is placed in

FD for Relative Files RELATIVE KEY not part of record ◦ Is placed in separate WORKING-STORAGE entry If key is a three digit field and SELECT clause is Relative Key is R-Key Entry in WORKING-STORAGE is 01 R-Key Pic 9(3). 15 59

Creating Relative Files When created sequentially, either computer or user can supply keys If

Creating Relative Files When created sequentially, either computer or user can supply keys If RELATIVE KEY clause omitted, computer supplies keys First record placed in relative record location 1 (RELATIVE KEY = 1) Second record in relative record location 2 (RELATIVE KEY = 2), etc. 15 60

Processing Relative Files WRITE … INVALID KEY to write record to relative file READ

Processing Relative Files WRITE … INVALID KEY to write record to relative file READ … AT END to read sequentially READ … INVALID KEY to read randomly ◦ Move key value of record to locate to RELATIVE KEY before executing READ 15 61

Processing Relative Files REWRITE … INVALID KEY to update DELETE … INVALID KEY to

Processing Relative Files REWRITE … INVALID KEY to update DELETE … INVALID KEY to remove record from file 15 62

Relative Keys Sometimes key field not feasible to use as relative key For example,

Relative Keys Sometimes key field not feasible to use as relative key For example, a five digit Trans-No with values from 00001 to 99999 with only 1000 actual records would be wasteful ◦ 99999 record locations would need to be allocated but only a small portion used 15 63

Converting to Relative Keys Methods called hashing used to convert key field into relative

Converting to Relative Keys Methods called hashing used to convert key field into relative record number Simple hashing method Divide Trans-No by 1009 Giving Num Remainder Rel-Key will be number from 0 to 1008 Add 1 to get relative record number from 1 to 1009, enough positions for 1000 -record file Is there a problem here? 15 64

Relative Files Hashing algorithm used when: Creating relative file - each record's key field

Relative Files Hashing algorithm used when: Creating relative file - each record's key field used to calculate RELATIVE KEY for positioning record in file Accessing file randomly - convert inquiry or transaction record's key to RELATIVE KEY before reading 15 65

15 66

15 66

Chapter Summary Indexed Files - SELECT clauses ◦ ORGANIZATION IS INDEXED ◦ ACCESS IS

Chapter Summary Indexed Files - SELECT clauses ◦ ORGANIZATION IS INDEXED ◦ ACCESS IS RANDOM For nonsequential updates, inquiries ◦ ACCESS IS SEQUENTIAL For creating, reporting, updating sequentially 15 67

Chapter Summary Indexed Files - SELECT clauses ◦ RECORD KEY - Key field for

Chapter Summary Indexed Files - SELECT clauses ◦ RECORD KEY - Key field for establishing index, accessing records ◦ FILE STATUS IS data-name for indicating success of input or output operation 15 68

Chapter Summary Indexed Files - PROCEDURE DIVISION ◦ Creating indexed file ACCESS IS SEQUENTIAL

Chapter Summary Indexed Files - PROCEDURE DIVISION ◦ Creating indexed file ACCESS IS SEQUENTIAL Use READ … AT END ◦ Reading from indexed file In sequence - READ … AT END Randomly - READ … INVALID KEY 15 69

Chapter Summary Relative Files ◦ No index, record's key field converted to relative record

Chapter Summary Relative Files ◦ No index, record's key field converted to relative record number or RELATIVE KEY ◦ Fast for random access but may be slow for sequential access ◦ Why is this faster? ◦ What is the disadvantage compared to indexed files? 15 70

Chapter Summary Relative Files - SELECT clauses ◦ ORGANIZATION IS RELATIVE ◦ RELATIVE KEY

Chapter Summary Relative Files - SELECT clauses ◦ ORGANIZATION IS RELATIVE ◦ RELATIVE KEY clause uses For randomly accessing file For sequential reads, writes if conversion necessary from record's key field to RELATIVE KEY If you know you’re only doing sequential read then what? Data-name used as RELATIVE KEY defined in WORKING -STORAGE 15 71

Chapter Summary Relative Files - SELECT clauses ◦ Any of three ACCESS MODEs can

Chapter Summary Relative Files - SELECT clauses ◦ Any of three ACCESS MODEs can be used Creating a relative file ◦ ACCESS IS SEQUENTIAL ◦ Move or convert input record's key field to RELATIVE KEY in WORKING-STORAGE ◦ Use WRITE … INVALID KEY 15 72

Chapter Summary Accessing a relative file ◦ ACCESS IS RANDOM ◦ Move or convert

Chapter Summary Accessing a relative file ◦ ACCESS IS RANDOM ◦ Move or convert transaction record's key field to RELATIVE KEY in WORKING-STORAGE ◦ Use READ … INVALID KEY 15 73

Chapter Summary Updating a relative file ◦ ACCESS IS RANDOM ◦ OPEN for I-O

Chapter Summary Updating a relative file ◦ ACCESS IS RANDOM ◦ OPEN for I-O ◦ Use READ, WRITE, REWRITE, or DELETE with INVALID KEY clauses 15 74