15-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community...

Post on 26-Dec-2015

223 views 2 download

Tags:

Transcript of 15-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community...

15-1

COBOL for the 21st Century

Nancy Stern Hofstra University

Robert A. Stern Nassau Community College

James P. Ley University of Wisconsin-Stout (Emeritus)

John Wiley & Sons, Inc.11th edition

15-2

Indexed and Relative File Processing

Chapter 15

15-3

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-4

Chapter Contents

• System Considerations for Organizing Disk Files

• Feature of Magnetic Disks and Disk Drives

• Processing Indexed Disk Files– Creating, updating, accessing for reporting

15-5

Chapter Contents

• Additional Options for Indexed File Processing– ALTERNATE RECORD KEYS– START Statement– Dynamic Access– FILE STATUS Clause– Exception Handling

15-6

Chapter Contents

• Using Indexed Disk File As External Table

• Processing Relative Disk Files

• Converting Key Field to RELATIVE KEY

15-7

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-8

Sequential File Organization

• Records stored in order they are written to file

• Must be accessed in sequence - to access 50th record in file, must read past first 49

• Typically sorted into sequence by a key field

15-9

Indexed File Organization

• Consists of two files– Data file - records in sequence– Index file - contains value of

• Each key field• Disk address of record with 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-10

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-11

Magnetic Disks, Disk Drives

• Storage media for data

• Can be accessed at very high speeds

• Have metal oxide coating that stores data as magnetized bits

• Disks may be floppy disk, hard disk or disk pack

15-12

Magnetic Disks, Disk Drives

• Data stored in concentric circles on disk called tracks

• Disk drives have access arm with read/write head for accessing data– Access arm can be positioned over any

track on disk– Makes random access possible

15-13

Addressing Disk Records

• Address individual records on disks by– Surface number - top or bottom side of disk– Track number– Sector (floppy disks) or cylinder (larger

units) number• Sector or cylinder is wedge-shaped section of

tracks

15-14

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

15-15

SELECT Statement

SELECT file-name-1

ASSIGN TO implementor-name-1

[ORGANIZATION IS] INDEXED

[ACCESS MODE IS SEQUENTIAL]

RECORD KEY IS data-name-1

• SELECT to create indexed file

Format

15-16

SELECT Statement

• ORGANIZATION INDEXED– Indicates index file to be created along with

data file– Index file must be established to be able to

randomly access file later

• ACCESS MODE SEQUENTIAL– Records written in sequence by key field– Optional since SEQUENTIAL is default

mode

15-17

SELECT Statement

• RECORD KEY clause– Names key field within disk record used to

form index– Must be in same physical location in each

record (usually first field)– Value must be unique for each record– Best to use numeric field as key

15-18

WRITE … INVALID KEY

• 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-19

WRITE … INVALID KEY

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

Format

15-20

Updating Index File Randomly

• Master records can be updated directly without creating a new file

• With index file, changes can be made in any sequence

15-21

Updating Index File Randomly

• 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-22

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-23

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-24

REWRITE … INVALID KEY

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

Format

15-25

Add New Record

• To add new record to indexed file– Move data to master record fields– Use WRITE … INVALID KEY to create

new record

15-26

Delete Existing Record

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

Format

15-27

Debugging Tips

• Must run program to create indexed file– Cannot be created using text editor

• To test an update program, always run index file creation program first

• May not be able to DISPLAY or print indexed records on your system directly– Move data to standard sequential record

first

15-28

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-29

Printing Indexed File Randomly

• May need to access only records about which inquiries have been made

• Specify ACCESS IS RANDOM

15-30

Printing Indexed File Randomly

• Read inquiry record

• Move inquiry key field value to RECORD KEY of master file

• Read master record into storage (READ … INVALID KEY)

• Move desired fields to output record

• WRITE output record to print file

15-31

Random Interactive Inquiries

• Inquiries often made interactively with output displayed on screen

• Assume indexed accounts receivable file with customer's Acct-No as key field

• Interactive inquiries made about balance due for a customer

• Code using random access for one inquiry follows

15-32

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

15-33

ALTERNATE RECORD KEY

• Clause to enable file to be accessed randomly using more than one key field– May want to access accounts receivable

records by account number or name

• Add to SELECT statement after RECORD KEY clause to establish multiple key fields for indexing

15-34

ALTERNATE RECORD KEY

[ALTERNATE RECORD KEY IS

data-name-2 [WITH DUPLICATES] ] …

• Multiple ALTERNATE keys allowed

• Need not be unique

• Access records by RECORD KEY or any ALTERNATE RECORD KEYs

Format

15-35

SELECT Example

Select Accts-Receivable

Assign To 'C:\AcctRec.ndx'

Organization Is Indexed

Access Is Sequential

Record Key Is Acct-No

Alternate Record Key

Is Cst-Last-Name

With Duplicates.

15-36

Random Access by ALTERNATE

• To access file by Cst-Last-NameDisplay '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-37

Random Access by ALTERNATE

• KEY clause used with READ to specify access by ALTERNATE RECORD KEY

• Since WITH DUPLICATES specified– May be more than one record with same

last name– Record retrieved is first one placed on disk

• If KEY clause omitted, uses RECORD KEY field (Acct-No) to find record

15-38

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-39

START Statement

START file-name-1 IS =

KEY IS > data-name-1IS NOT <IS >=

[INVALID KEY imperative-statement-1][NOT INVALID KEY

imperative-statement-2][END-START]

Format

15-40

START Statement• To begin processing with record whose

account number equals 025

Move 025 To Acct-NoStart Accts-Receivable

Invalid Key Display 'Acct-No 025 not found'

Not Invalid Key Perform 300-Proc-Rec

End-Start

15-41

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-42

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-43

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-44

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-45

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-46

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-47

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-48

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-49

FILE STATUS Clause

• Data-name must appear in WORKING-STORAGE 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-50

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-51

FILE STATUS Values

File Status

field value Meaning

00 No error occurred

21 Sequence error - keys not

in correct order

22 Attempt to write record

creating duplicate primary

record key

15-52

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-53

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

15-54

DECLARATIVES Format

DECLARATIVES.

section-name SECTION.

USE AFTER STANDARD

EXCEPTION PROCEDURE

ERROR

ON file-name-1 …

END DECLARATIVES.

15-55

DECLARATIVES ExampleProcedure Division.Declaratives.A000-Exception-Handling Section.

Use After Error ProcedureOn Indexed-Pay-File

A100-Check-It.If WS-Status = '21'

Display 'Key not in sequence'End-If …

End Declaratives.

15-56

DECLARATIVES Example

B000-Regular-Processing Section.

B100-Main-Paragraph.

Read Indexed-Pay-File

Write Indexed-Pay-File

15-57

DECLARATIVES Example• Once section header used, rest of

PROCEDURE DIVISION must be divided into sections

• Statements in paragraph A100-Check-It in DECLARATIVES test value of FILE STATUS field

• INVALID KEY not needed for READ or WRITE since errors handled in DECLARATIVES

15-58

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-59

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-60

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-61

FD for Relative Files

• RELATIVE KEY not part of record– 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-62

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-63

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-64

Processing Relative Files

• REWRITE … INVALID KEY to update

• DELETE … INVALID KEY to remove record from file

15-65

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-66

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• 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

15-67

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-68

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-69

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-70

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-71

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

15-72

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

• Data-name used as RELATIVE KEY defined in WORKING-STORAGE

15-73

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-74

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-75

Chapter Summary

• Updating a relative file– ACCESS IS RANDOM– OPEN for I-O– Use READ, WRITE, REWRITE, or

DELETE with INVALID KEY clauses

15-76

Copyright © 2003 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express written permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.