VSAM RRDS Direct Addressing

Direct addressing in a Relative Record Data Set (RRDS) means you tell VSAM exactly which record you want by its position number—the relative record number (RRN). You do not search by key or read one record after another until you find it. You supply the RRN (e.g. 5 for the fifth slot), and the access method goes straight to that slot or to the index entry for that RRN and performs the operation. That is what makes RRDS useful when your application thinks in terms of slot numbers or record numbers: direct access by RRN is fast and simple. This page explains what direct addressing is, how it differs from key-based and RBA-based access, how to use it in fixed-length and variable-length RRDS, and how it compares to sequential and dynamic access.

What Is Direct Addressing?

In direct addressing you identify the record by its position. In an RRDS the position is the RRN: 1 for the first slot, 2 for the second, and so on. When you want the record in slot 7, you set the RRN to 7 (for example in the COBOL RELATIVE KEY) and issue a READ. The access method does not scan from the beginning or search an index by key. It uses the RRN to compute which control interval and which slot within that CI contain the record (fixed-length RRDS), or it uses the index component to map RRN to the record (variable-length RRDS). Either way, access is by position, not by content. So "direct" here means "go directly to that record by its number."

Direct Addressing vs Key and RBA

In a KSDS you access a record by its primary key. The system searches the index to find the key and then retrieves the record. In an ESDS you can access by RBA—the byte offset of the record from the start of the file. In an RRDS you access by RRN—the slot number. So the three ways to point at a record in VSAM are: by key (KSDS), by RBA (ESDS, and sometimes KSDS for positioning), and by RRN (RRDS). Key is content-based: you supply a value stored in the record. RBA is a byte address. RRN is a slot index: simple and integer-based. Direct addressing in RRDS is always by RRN.

How each VSAM type supports direct-style access
TypeHowNotes
RRDSRRN (slot number)Direct addressing by position; 1-based slot index.
KSDSKeyYou supply key value; index finds the record.
ESDSRBA (byte offset)You supply byte offset from start of data.

How Direct Addressing Works in Fixed-Length RRDS

In a fixed-length RRDS the data component is divided into slots of equal size. Slot 1 is at a known offset, slot 2 is at the next offset, and so on. The offset of slot N can be computed from the record length and the slot number (and possibly the CI size and control information). So when you set the RRN to 5 and issue READ, the access method computes where slot 5 is, reads the control interval that contains that slot (if not already in the buffer), and returns the record in slot 5. If the slot is empty (no record was ever written there, or the record was deleted and not yet reused), you get a "record not found" or equivalent status. There is no index to search; the RRN is the address.

How Direct Addressing Works in Variable-Length RRDS

In a variable-length RRDS (VRRDS) records can have different lengths, so there are no fixed slots on disk. The index component maps each RRN to the actual location of the record. When you set the RRN to 5 and issue READ, the access method looks up RRN 5 in the index and uses the stored pointer to read the record. So direct addressing in VRRDS still means "give me the record with this RRN," but the system uses the index to find it rather than computing a slot offset. The programming interface is the same: you set the RRN and read or write.

Access Modes: Direct, Sequential, Dynamic

RRDS supports three ways to use the file. In direct (random) access you supply the RRN for each operation. In sequential access you open the file and read in RRN order (1, 2, 3, …); you do not set the RRN for each record—the system advances it. In dynamic access you mix both: for example you position at RRN 100 with a direct READ, then issue READ NEXT to get 101, 102, 103 without setting the RRN each time. So direct addressing is the mode where you explicitly choose the RRN for each I/O.

RRDS access modes
ModeDescription
Direct (random)You supply the RRN; VSAM goes to that slot or index entry. No scan, no key search.
SequentialRead records in RRN order (1, 2, 3, …). Position at start or after a direct READ.
DynamicMix direct and sequential: e.g. position at RRN 100 with direct READ, then READ NEXT for 101, 102, …

Operations Using Direct Addressing

For each of the main operations you set the RRN (e.g. in RELATIVE KEY) and then issue the command. READ returns the record in that slot or a not-found condition. WRITE puts a record in that slot (or assigns that RRN in VRRDS). After a successful READ you can REWRITE to update the record in place; the RRN does not change. DELETE frees the slot; if the cluster is defined with REUSE, a later WRITE to the same RRN can put a new record in that slot.

Operations with direct addressing (set RRN first)
OperationDescription
READSet RRN in RELATIVE KEY, issue READ. Returns record at that slot or "not found" if empty.
WRITESet RRN, issue WRITE. Puts record in that slot (fixed) or assigns that RRN (variable).
REWRITEAfter READ, REWRITE updates the record in the same slot; RRN unchanged.
DELETESet RRN, issue DELETE. Frees the slot; with REUSE it can be reused for a new WRITE at same RRN.

COBOL Example: Direct Read and Update

In COBOL you define the file with ORGANIZATION IS RELATIVE and a RELATIVE KEY data item. For direct access you move the desired RRN to the RELATIVE KEY and then perform the I/O. The following example reads the record at RRN 10 and then rewrites it with updated data.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SELECT RRDSFILE ASSIGN TO RRDSFILE ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS WS-RRN FILE STATUS IS WS-STATUS. ... 01 WS-RRN PIC 9(8) COMP. 01 WS-REC PIC X(80). ... MOVE 10 TO WS-RRN READ RRDSFILE INVALID KEY DISPLAY 'No record at RRN 10' NOT INVALID KEY *> Update some field in WS-REC REWRITE RRDSREC FROM WS-REC INVALID KEY DISPLAY 'REWRITE failed' END-REWRITE END-READ

ACCESS MODE IS RANDOM means you will use direct addressing: each READ, WRITE, REWRITE, or DELETE is driven by the value in RELATIVE KEY. You must set WS-RRN to the desired slot number before each operation when using random access.

When Direct Addressing Fails: Empty Slots

In a fixed-length RRDS a slot may be empty. If you READ with an RRN that points to an empty slot, the operation fails with a "record not found" or INVALID KEY condition. So your program must either know which slots are in use (e.g. from a separate index or list) or handle the not-found case. When you WRITE with direct addressing you are placing a record in that slot; if the slot already has a record, the WRITE may fail (duplicate) unless the rules for your RRDS allow replacement. With REUSE, a slot that was freed by DELETE can be written again at the same RRN.

Performance: Why Direct Addressing Is Fast

For a single record, direct addressing by RRN avoids a key search (as in KSDS) and avoids a sequential scan. In fixed-length RRDS the slot location is computed from the RRN and record length; one or a small number of I/Os bring in the right control interval. In variable-length RRDS the index lookup by RRN is efficient because the index is organized for that. So when your application naturally has a record number or slot id—for example a table row number, or a hash that maps to a slot—RRDS with direct addressing gives you fast single-record access without maintaining a key in the record itself.

Choosing Direct vs Sequential

Use direct addressing when you need to jump to specific records by number: for example lookup by employee number 1–10000 where the number is the RRN, or a cache where the slot id is known. Use sequential access when you need to process all records in order (e.g. batch report in RRN order). Use dynamic when you need both: e.g. position at a given RRN and then read forward. The same RRDS file can be opened for random, sequential, or dynamic access depending on the program.

Key Takeaways

  • Direct addressing in RRDS means you supply the RRN (slot number) and the access method goes straight to that record.
  • Set the RELATIVE KEY (or equivalent) to the desired RRN, then issue READ, WRITE, REWRITE, or DELETE.
  • In fixed-length RRDS the slot position is computed from RRN; in variable-length RRDS the index maps RRN to the record.
  • Only RRDS uses RRN for direct addressing; KSDS uses key, ESDS uses RBA.
  • Empty slots return record-not-found on READ; with REUSE, deleted slots can be reused for a new WRITE at the same RRN.

Explain Like I'm Five

Imagine a row of numbered cubbyholes. To get the thing in cubbyhole 5, you don't look at every cubbyhole or read the labels—you just go to number 5. That's direct addressing: you know the number (RRN), and you go straight there. The number is the address. If cubbyhole 5 is empty, you find nothing. If you take something out (delete), the cubbyhole is still number 5 and you can put something new in later (reuse).

Test Your Knowledge

Test Your Knowledge

1. What do you set to perform direct access in an RRDS?

  • The primary key
  • The RBA
  • The RRN (e.g. in RELATIVE KEY)
  • The CI number

2. How does VSAM find the record when you use direct addressing in a fixed-length RRDS?

  • It searches the index by key
  • It computes the slot position from the RRN and goes to that slot
  • It scans sequentially until it finds the RRN
  • It uses the RBA

3. Which VSAM dataset type uses RRN for direct addressing?

  • KSDS only
  • ESDS only
  • RRDS only
  • All of them
Published
Updated
Read time4 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: IBM z/OS 2.5 documentationSources: IBM DFSMS Access Method Services, z/OS VSAM documentationApplies to: z/OS 2.5