Monday, 28 July 2014

COBOL Relative File Organization: RRDS and RRN Examples

MOVE 125 TO WS-RRN followed by READ ORDER-FILE asks a COBOL relative file for relative record number 125. The number is carried in a separate RELATIVE KEY item; it is not an embedded business key inside the record.

COBOL relative file organization mapping a relative record number through RELATIVE KEY to an RRDS slot
An RRN identifies a record position; COBOL passes that number through the RELATIVE KEY item.

What is COBOL relative file organization?

A relative file is a direct-access file whose records are identified by position relative to the beginning of the file. Record number 1 is the first relative position, record number 10 is the tenth, and so on. On z/OS, Enterprise COBOL implements relative organization with a VSAM relative-record data set, or RRDS.

The RRN remains associated with that record until the record is deleted. The file can contain gaps: sequential processing visits existing records in ascending RRN order and skips positions that do not contain records.

Key distinction: an indexed file stores a business key in every record and maintains an index. A relative file uses a numeric position supplied separately from the record.

Use the COBOL file-organization overview when comparing sequential, indexed, and relative designs. This page owns the COBOL and RRDS details for relative files.

SELECT and FD entries for a relative file

The ORGANIZATION IS RELATIVE clause establishes the file model. ACCESS MODE selects sequential, random, or dynamic processing. A relative key is always required for random and dynamic access, and it is required for sequential access when the program uses START.

SELECT ORDER-FILE ASSIGN TO ORDERDD ORGANIZATION IS RELATIVE ACCESS MODE IS DYNAMIC RELATIVE KEY IS WS-RRN FILE STATUS IS WS-ORDER-STATUS. FD ORDER-FILE RECORD CONTAINS 80 CHARACTERS. 01 ORDER-RECORD. 05 ORDER-ID PIC 9(8). 05 ORDER-STATUS PIC X. 05 ORDER-DATA PIC X(71). WORKING-STORAGE SECTION. 01 WS-RRN PIC 9(6). 01 WS-ORDER-STATUS PIC XX.

WS-RRN is an unsigned integer item and is outside the FD record description. The RRN controls where COBOL reads or writes; ORDER-ID is ordinary application data and does not position the RRDS.

Fixed-length RRDS versus variable-length RRDS

CharacteristicFixed-length RRDSVariable-length RRDS
VSAM definitionNUMBERED with equal average and maximum RECORDSIZENUMBERED with different average and maximum RECORDSIZE
PlacementThe data set is divided into fixed-length slots.Records are ordered by RRN but do not occupy fixed slots.
Empty or deleted positionAn empty slot can receive a new record with that RRN.Deleted or shortened record space becomes reusable free space.
IndexNo prime or alternate index.VSAM uses an index internally; COBOL still processes it as relative organization.
Record lengthFixedVariable up to the defined maximum

Neither form supports alternate indexes or spanned records. If the application needs lookup by customer number, surname, or another value stored in the record, the COBOL indexed-file guide is usually the better design reference.

Sequential, random, and dynamic access

ModeHow records are selectedBest fit
SEQUENTIALExisting records are returned in ascending RRN order.Batch scan of most or all records.
RANDOMThe program places the requested RRN in the relative-key item.Independent lookups when only a small part of the file is processed.
DYNAMICIndividual statements switch between random lookup and sequential retrieval.Position by RRN, then process a range of existing records.

If the access-mode clause is omitted, COBOL assumes sequential access. The sequential-file guide covers physical sequential organization; it should not be confused with sequential access to an RRDS.

Random READ by relative record number

For random or dynamic access, set the relative-key item before READ. A relative-file READ must not contain a KEY IS phrase. If the requested RRN has no record, the operation raises an invalid-key condition.

MOVE 125 TO WS-RRN READ ORDER-FILE INVALID KEY DISPLAY 'RRN ' WS-RRN ' NOT FOUND, STATUS=' WS-ORDER-STATUS NOT INVALID KEY PERFORM PROCESS-ORDER END-READ

The updated COBOL READ statement guide explains READ INTO, AT END, INVALID KEY, and failed-read safety across file organizations.

Sequential READ skips empty RRNs

Sequential access follows the ascending RRNs of records that exist. If records 3 and 6 are absent, a scan can return 1, 2, 4, 5, and 7 without presenting empty records to the program. When a relative-key clause is present, each successful sequential READ updates the item with the RRN returned.

MOVE 'N' TO WS-EOF OPEN INPUT ORDER-FILE PERFORM UNTIL WS-EOF = 'Y' READ ORDER-FILE AT END MOVE 'Y' TO WS-EOF NOT AT END DISPLAY 'PROCESSING RRN ' WS-RRN PERFORM PROCESS-ORDER END-READ END-PERFORM CLOSE ORDER-FILE

Dynamic START and READ NEXT

Dynamic access can position the file at or after a requested RRN and then read forward. NEXT RECORD is required for sequential retrieval when ACCESS MODE IS DYNAMIC.

MOVE 100 TO WS-RRN START ORDER-FILE KEY IS NOT LESS THAN WS-RRN INVALID KEY MOVE 'Y' TO WS-EOF END-START PERFORM UNTIL WS-EOF = 'Y' READ ORDER-FILE NEXT RECORD AT END MOVE 'Y' TO WS-EOF NOT AT END PERFORM PROCESS-ORDER END-READ END-PERFORM

A successful START establishes the file position; it does not transfer the record into the FD area. The following READ NEXT makes the positioned record available.

WRITE, REWRITE, and DELETE examples

Open the file for I-O when existing records may be changed or deleted. A random WRITE uses the RRN in WS-RRN. If that position already contains a record, the WRITE fails with an invalid-key condition.

MOVE 125 TO WS-RRN MOVE WS-NEW-ORDER TO ORDER-RECORD WRITE ORDER-RECORD INVALID KEY DISPLAY 'RRN ALREADY USED OR OUT OF RANGE, STATUS=' WS-ORDER-STATUS END-WRITE

A common update pattern reads the RRN, changes the record, and rewrites it. With random or dynamic access, a DELETE identifies the relative record through the current relative-key value.

MOVE 125 TO WS-RRN READ ORDER-FILE INVALID KEY DISPLAY 'ORDER NOT FOUND' NOT INVALID KEY MOVE 'C' TO ORDER-STATUS REWRITE ORDER-RECORD INVALID KEY DISPLAY 'REWRITE FAILED, STATUS=' WS-ORDER-STATUS END-REWRITE END-READ MOVE 900 TO WS-RRN DELETE ORDER-FILE INVALID KEY DISPLAY 'DELETE FAILED, STATUS=' WS-ORDER-STATUS END-DELETE

The COBOL file operations guide covers OPEN modes and the broader READ, WRITE, REWRITE, DELETE, and CLOSE lifecycle.

Common FILE STATUS values

StatusRelative-file conditionTypical check
00Successful operationContinue processing.
10No next record during sequential retrievalEnd the scan.
22A WRITE attempted to use an occupied relative positionChoose another RRN or treat it as an existing record.
23A random READ, START, or direct operation could not find the requested recordHandle the missing RRN.
24A WRITE exceeded the file boundary or the RRN did not fit the relative-key itemCheck the data-set limit and key size.
47READ attempted when the file was not open for INPUT or I-OCorrect the OPEN mode and control flow.
Do not treat INVALID KEY as the complete diagnosis: test FILE STATUS so the program can distinguish an absent RRN, a duplicate position, and a boundary error.

Defining an RRDS with IDCAMS

The COBOL declaration must match a data set defined as RRDS. For a fixed 80-byte record, an IDCAMS definition can use NUMBERED and equal average and maximum record sizes.

DEFINE CLUSTER - (NAME(USER1.ORDER.RRDS) - NUMBERED - RECORDSIZE(80 80) - TRACKS(5 2)) - DATA(NAME(USER1.ORDER.RRDS.DATA))

Site SMS rules can supply or override allocation attributes, so production definitions should follow local storage standards. See VSAM DEFINE CLUSTER examples for the wider IDCAMS syntax.

When relative organization fits

  • Good fit: the application already has a stable numeric slot, such as a bounded table position, terminal number, or internally assigned record number.
  • Good fit: direct insert, lookup, and delete by that slot are frequent.
  • Poor fit: identifiers are sparse, very large, or change over time.
  • Poor fit: users need lookup through alternate business fields.
  • Poor fit: a transformation can map two business keys to the same RRN unless the program implements and tests collision handling.

A randomizing calculation can produce an RRN, but it does not make collisions disappear. When key uniqueness and alternate access paths belong to the storage design, a KSDS is usually easier to maintain.

Common relative-file mistakes

  • Defining the relative-key item inside the FD record.
  • Coding KEY IS on a READ for a relative file.
  • Assuming empty RRNs are returned as blank records during a sequential scan.
  • Using dynamic access for an application that performs only independent random lookups.
  • Writing to an occupied RRN without handling status 22.
  • Choosing a key size too small for the largest RRN that the data set can use.

Official IBM references

Frequently asked questions

What is relative file organization in COBOL?

A relative file identifies each record by a relative record number, or RRN, that represents its position relative to the beginning of the file. On z/OS, COBOL relative files use VSAM RRDS organization.

Is the RELATIVE KEY stored inside the record?

No. The RELATIVE KEY data item is defined outside the file record description. It supplies or receives the relative record number used for an I/O request.

Can a COBOL relative file be read sequentially and randomly?

Yes. Relative organization supports sequential, random, and dynamic access. Sequential reads follow ascending RRNs of records that exist; random access uses the RRN placed in the RELATIVE KEY item.

When should an application use an RRDS instead of a KSDS?

Use an RRDS when the application can derive a stable, reasonably dense numeric record number. Use a KSDS when records are naturally identified by business keys, need alternate access paths, or have sparse changing identifiers.

Working rule: choose relative organization only when the application can derive a stable RRN and can explicitly handle empty, occupied, and out-of-range positions.

No comments:

Post a Comment