150 likes | 262 Views
Relative Files. Relative Files. Data - Records are accessed in a file relative to the beginning of the file ACCT# 0001 will the first record in the file ACCT# 0534 will be the 534th record in the file Disk space must be reserved in advance
E N D
Relative Files • Data - • Records are accessed in a file relative to the beginning of the file • ACCT# 0001 will the first record in the file • ACCT# 0534 will be the 534th record in the file • Disk space must be reserved in advance • Every record should be primed HIGH-VALUES or LOW-VALUES • Some files/records the Record key readily translates into a relative file location • Customer records - ACCT # • Employee records - Employee#
Relative Files • Other - Record key may require some sort of conversion (hashing) to determine the relative position • SSNumber - 321301670 - 321,301,670th record ?? • The remainder algorithm • Key / N ( where N is the number of expected records) • Take the remainder + 1 to determine relative key • Example if N = 5500 take 321301670 / 5500 • Remainder = 2670 Relative key = 2671 • Not every record location is filled with data
Relative - SELECT CLAUSE • ORGANIZATION IS RELATIVE • ACCESS IS SEQUENTIALRANDOMDYNAMICRELATIVE KEY IS data-nameidentifies a WS item that contains the record number to be accessed • Unsigned Numeric • > 0 and <= number of records pre-allocated • Optional is ACCESS IS SEQUENTIAL
Relative Files- SELECT CLAUSE • [FILE STATUS IS data-name-3] • An optional working storage item to record the status of I/O operations • Common Status Codes • 00 - Successful • 22 - Duplicate Key • 24 - Boundary Violation • 30 - Permanent error
ALLOCATING a Relative File • Pre-format a file with number of records needed • RELATIVE file is defined for SEQUENTIAL access • Use a WRITE statement to add records • MOVE LOW-VALUES TO CUTOMER-MASTER-RECORD PERFORM 1000 TIMES WRITE CUSTOMER-MASTER-RECORD END-PERFORM
LOADING A RELATIVE FILE • Assumption Customer-Number is unique - input file not necessarily sorted • ACCESS IS RANDOM • Calculate the relative key if necessary • Need to READ the relative file first to be sure a record does not exist in the location • If the record is all LOW-VALUES okay to add at this location using a REWRITE
Pseudocode for Loading Relative File Open filesDO WHILE data remains READ record from sequential file AT END Indicate no more data NOT AT END Move Acct-No to WS-Relative-Key Read Customer-Master If Customer-Master-Record Low-Values Move Data from input to Output REWrite Customer-Master-record Invalid Key Write an error message End-Rewrite ENDIF ENDREADENDDOClose filesStop run
Writing a Relative File • File defined as SEQUENTIAL access • RELATIVE KEY clause optional • File OPEN OUTPUT • WRITE record-name INVALID KEY imperative statements NOT INVALID KEY imperative statementsEND-WRITE
Reading a Relative File Sequentially • SEQUENTIAL access • File OPEN INPUT • READ file-name AT END imperative statements NOT AT END imperative statementsEND-READ
Reading a Relative File - Randomly • RANDOM access • File OPEN INPUT • RELATIVE KEY must be set prior to READ • READ file-name INVALID KEY imperative statements NOT INVALID KEY imperative statementsEND-READmust check the record contents to verify this is the record wanted
Updating a Relative File • Access defined as SEQUENTIAL or RANDOM • File OPEN I-O • A Read must precede any write • REWRITE record-name INVALID KEY imperative statements NOT INVALID KEY imperative statementsEND-REWRITE
Deleting records from a RELATIVE File • If access is SEQUENTIAL a READ must precede the DELETE • If RANDOM the RELATIVE KEY must be set prior to the DELETE • DELETE file-name INVALID KEY imperative statements NOT INVALID KEY imperative statementsEND-DELETE
DYNAMIC Access - START • File can be read both sequentially and randomly • START file-name KEY IS {=, >, >=} data-name INVALID KEY imperative statements NOT INVALID KEY imperative statementsEND-START • START simply positions the file’s record pointer a READ must follow
DYNAMIC Access - READ • READ file-name NEXT RECORD AT END imperative statements NOT AT END imperative statementsEND-READ