150 likes | 162 Views
Learn about the file organization method that converts key fields to actual disk addresses, allowing for direct access to records without the need for index lookups. This method enables both sequential and random access to records.
E N D
Chapter 15 Relative Files
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
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] RANDOMRELATIVE KEY IS DYNAMIC data-name-1 [FILE STATUS IS data-name-2].
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
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).
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.
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
Processing Relative Files • REWRITE … INVALID KEY to update • DELETE … INVALID KEY to remove record from file
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
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
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
ORGANIZATION: RELATIVEACCESS MODE: SEQUENTIAL ENVIRONMENT DIVISION. . . . INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EXAMPLE-FILE ASSIGN TO “EXAM.DAT” ORGANIZATION IS RELATIVE ACCESS MODE IS SEQUENTIAL. . . . DATA DIVISION. FILE SECTION. FD EXAMPLE-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 100 CHARACTERS. * 01 EXAMPLE-REC. . . . I/O STATEMENTS BY OPEN MODE OPEN MODE: INPUT OPEN INPUT EXAMPLE-FILE. READ EXAMPLE-FILE AT END MOVE “Y” TO WS-EOF-FLAG. CLOSE EXAMPLE-FILE.
ORGANIZATION: RELATIVEACCESS MODE: SEQUENTIAL OPEN MODE: OUTPUT OPEN OUTPUT EXAMPLE-FILE. WRITE EXAMPLE-REC. CLOSE EXAMPLE-FILE. OPEN MODE: INPUT-OUTPUT OPEN I-O EXAMPLE-FILE. DELETE EXAMPLE-FILE RECORD. READ EXAMPLE-FILE AT END MOVE “Y” TO WS-EOF-FLAG. REWRITE EXAMPLE-REC. CLOSE EXAMPLE-FILE.
ORGANIZATION: RELATIVEACCESS MODE: RANDOM ENVIRONMENT DIVISION. . . . INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EXAMPLE-FILE ASSIGN TO “EXAM.DAT” ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS WS-KEY. . . . DATA DIVISION. FILE SECTION. FD EXAMPLE-FILE LABEL RECORDS ARE STANDARD RECORD CONTAINS 100 CHARACTERS. * 01 EXAMPLE-REC. . . . WORKING-STORAGE SECTION. • WS-KEY PIC 999 VALUE ZEROS. … I/O STATEMENTS BY OPEN MODE OPEN MODE: INPUT OPEN INPUT EXAMPLE-FILE. COMPUTE WS-KEY = expression. READ EXAMPLE-FILE INVALID KEY MOVE “Y” TO WS-READ-ERROR-FLAG. CLOSE EXAMPLE-FILE.
ORGANIZATION: RELATIVEACCESS MODE: RANDOM OPEN MODE: OUTPUT OPEN OUTPUT EXAMPLE-FILE. COMPUTE WS-KEY = expression. WRITE EXAMPLE-REC INVALID KEY MOVE “Y” TO WS-WRITE-ERR-FLAG NOT INVALID KEY ADD 1 TO WS-WRITE-CTR. CLOSE EXAMPLE-FILE. OPEN MODE: INPUT-OUTPUT OPEN I-O EXAMPLE-FILE. COMPUTE WS-KEY = expression. DELETE EXAMPLE-FILE RECORD INVALID KEY MOVE “Y” TO WS-DELETE-ERR-FLAG. COMPUTE WS-KEY = expression. READ EXAMPLE-FILE AT INVALID KEY MOVE “Y” TO WS-READ-ERR-FLAG. REWRITE EXAMPLE-REC INVALID KEY MOVE “Y” TO WS-REWRITE-ERR-FLAG. COMPUTE WS-KEY = expression. WRITE EXAMPLE-REC INVALID KEY MOVE “Y” TO WS-WRITE-ERR-FLAG. CLOSE EXAMPLE-FILE.