Presentation is loading. Please wait.

Presentation is loading. Please wait.

Indexed and Relative File Processing

Similar presentations


Presentation on theme: "Indexed and Relative File Processing"— Presentation transcript:

1 Indexed and Relative File Processing
Chapter 15 Indexed and Relative File Processing

2 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

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

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

5 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

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

7 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

8 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

9 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

10 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

11 SELECT Statement SELECT file-name-1 ASSIGN TO implementor-name-1
Format 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

12 SELECT Statement ORGANIZATION INDEXED ACCESS MODE SEQUENTIAL
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

13 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

14 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 WRITE … INVALID KEY WRITE record-name-1 [FROM identifier-1]
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

16 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

17 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

18 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

19 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

20 REWRITE … INVALID KEY REWRITE record-name-1 [FROM identifier-1]
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

21 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

22 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

23 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

24 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

25 Printing Indexed File Randomly
May need to access only records about which inquiries have been made Specify ACCESS IS RANDOM

26 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

27 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 next slide

28 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

29 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

30 ALTERNATE RECORD KEY [ALTERNATE RECORD KEY IS
Format [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

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

32 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

33 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 (in this case Acct-No) to find record

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

35 START Statement [INVALID KEY imperative-statement-1] START file-name-1
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]

36 START Statement Move 025 To Acct-No Start Accts-Receivable Invalid Key
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.

37 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

38 START Statement Start Accts-Receivable Key > Acct-No Invalid Key …
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 … ...

39 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

40 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

41 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

42 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

43 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

44 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

45 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

46 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

47 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 Complete list on page 681

48 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

49

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

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

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

53 DECLARATIVES Example B000-Regular-Processing Section. B100-Main-Paragraph. … Read Indexed-Pay-File Write Indexed-Pay-File

54 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

55 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

56 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].

57 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

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

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

60 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

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

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

63 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 Is there a problem here?

64 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

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

66 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

67 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

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

69 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

70 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

71 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

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


Download ppt "Indexed and Relative File Processing"

Similar presentations


Ads by Google