Top Banner
15 -1 Chapter 15
64

15- 1 Chapter 15. To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

Dec 13, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

15-1

Chapter 15

Page 2: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 3: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

15-3

Page 4: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

15-4

Page 5: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 6: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 7: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 8: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 9: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 10: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 11: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 12: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 13: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 14: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 15: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 16: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 17: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 18: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 19: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 20: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 21: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

[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

Page 22: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 23: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 24: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

Start Dynamic Access Relative addressing

◦ Efficient◦ Uses key to generate address

15-34

Page 25: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

15-35

Page 26: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 27: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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]

Page 28: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 29: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 30: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 31: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 32: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 33: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 34: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 35: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 36: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 37: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 38: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 39: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 40: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 41: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 42: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

DECLARATIVES.section-name SECTION.

USE AFTER STANDARDEXCEPTION PROCEDUREERROR

ON file-name-1 …END DECLARATIVES.

15-52

Page 43: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 44: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

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

15-54

Page 45: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 46: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 47: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 48: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 49: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 50: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 51: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 52: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

from file

15-62

Page 53: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 54: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 55: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 56: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

15-66

Page 57: 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 Files - SELECT clauses◦ ORGANIZATION IS INDEXED◦ ACCESS IS RANDOM

For nonsequential updates, inquiries◦ ACCESS IS SEQUENTIAL

For creating, reporting, updating sequentially

15-67

Page 58: 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 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

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

Page 60: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 61: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 62: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 63: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

Page 64: 15- 1 Chapter 15.  To familiarize you with ◦ Methods of disk file organization ◦ Random processing of disk files ◦ How to create, update, and access.

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

INVALID KEY clauses

15-74