15- 1 chapter 15. to familiarize you with ◦ methods of disk file organization ◦ random...

64
15 -1 Chapter 15

Upload: kathryn-emma-mcbride

Post on 13-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

15-1

Chapter 15

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

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

15-3

Using Indexed Disk File As External Table Processing Relative Disk Files Converting Key Field to RELATIVE KEY

15-4

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

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 Nothing new, studied this previously

15-6

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

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

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. CH1501. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-MASTER ASSIGN TO "CH1501.NDX" ORGANIZATION IS INDEXED ACCESS IS RANDOM RECORD KEY IS CUSTOMER-NO-M. SELECT CUSTOMER-TRANS ASSIGN TO "CH1501.DAT" ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. FD CUSTOMER-TRANS RECORD CONTAINS 38 CHARACTERS. 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)V99. FD CUSTOMER-MASTER RECORD CONTAINS 39 CHARACTERS. 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)V99.

15-11

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

Format

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

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

To locate record with key field equal to value stored in record key

Move Trans-No To Master-NoRead Indexed-File

Invalid Key Perform 600-Err-RtnNot Invalid Key Perform 500-OK-

RtnEnd-Read

15-19

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

Format

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

Format

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

Display 'Enter account number'Accept Acct-NoRead Accts-ReceivableInvalid Key

Display 'Account not on file'Not Invalid Key

Display 'Balance due = ', Bal-DueEnd-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 IS data-name-2 [WITH DUPLICATES] ] …

Multiple ALTERNATE keys allowed Need not be unique

◦ If Dups, 1st one on disk is returned Access records by RECORD KEY or any

ALTERNATE RECORD KEYs

15-30

Format

Select Accts-ReceivableAssign To 'C:\AcctRec.ndx'Organization Is IndexedAccess Is SequentialRecord Key Is Acct-NoAlternate Record Key

Is Cst-Last-Name With Duplicates.

15-31

To access file by Cst-Last-NameDisplay 'Enter last name'Accept Cst-Last-NameRead Accts-Receivable

Key is Cst-Last-NameInvalid Key Display 'No record found'Not Invalid Key

Display 'Balance due = ', Bal-DueEnd-Read

15-32

Start Dynamic Access Relative addressing

◦ Efficient◦ Uses key to generate address

15-34

15-35

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

15-37

Format

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]

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

Note that it just starts here it’s not filtering anything yet.

15-38

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

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-NoInvalid Key …...

15-40

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

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

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

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

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-ReadEnd-Perform

15-45

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]

15-46

• Format

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).

15-47

• Example

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

field value Meaning00 No error occurred21 Sequence error - keys not

in correct order22 Attempt to write record

creating duplicate primary record key

Complete list on page 681

15-49

Write Indexed-Pay-RecInvalid Key

If WS-Status = '21'Display 'Key not in

sequence'End-If…

Not Invalid KeyPerform 600-OK-Rtn

End-Write

15-50

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.section-name SECTION.

USE AFTER STANDARDEXCEPTION PROCEDUREERROR

ON file-name-1 …END DECLARATIVES.

15-52

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

B000-Regular-Processing Section.B100-Main-Paragraph.

…Read Indexed-Pay-File…Write Indexed-Pay-File…

15-54

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

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 file-name-1ASSIGN to implementor-name-1[ORGANIZATION IS] RELATIVE[ACCESS IS

SEQUENTIAL [RELATIVE KEY IS data-name-1]

RANDOM RELATIVE KEY ISDYNAMIC data-name-1

[FILE STATUS IS data-name-2].

15-57

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

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

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

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

REWRITE … INVALID KEY to update DELETE … INVALID KEY to remove record

from file

15-62

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

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

Is there a problem here?

15-64

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

Indexed Files - SELECT clauses◦ ORGANIZATION IS INDEXED◦ ACCESS IS RANDOM

For nonsequential updates, inquiries◦ ACCESS IS SEQUENTIAL

For creating, reporting, updating sequentially

15-67

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

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

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

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

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

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

Updating a relative file◦ ACCESS IS RANDOM◦ OPEN for I-O◦ Use READ, WRITE, REWRITE, or DELETE with

INVALID KEY clauses

15-74