Information Technology Topic
File Access Methods
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- File access is how a program reaches a record. Sequential access starts at the first record and reads them in order. Direct access goes straight to the wanted record without reading the ones before it
- To reach record k by sequential access you read k records, but by direct access you read 1. A sequential read position only moves forward, so asking for an earlier record means starting again from the first
- A plain sequential file allows only sequential access. A random file and an index-sequential file allow direct access, and index-sequential also allows reading in key order. Direct access always needs something extra: a calculation or an index
Section 1Organisation and access are different questions
File organisation is how the records are laid out in storage. File access is how a program reaches the records. Think of a row of school lockers. The layout of the lockers is the organisation. Finding yours by opening every door in turn, or by walking straight to number 24, is the access. The syllabus names two access methods: sequential access and direct access. The layout limits the access you can use. A plain sequential file can only be read in order. Other layouts allow direct access too. The note on which access each organisation allows sorts this out.
Section 2Sequential access
Sequential access means starting at the first record and reading the records in order, one after another. To reach record 100 the program must pass records 1 to 99 first. It is the usual way to read a file, and it is the only way for a plain sequential file. This topic uses a simple model: the program's read position only moves forward. If it asks for a record that is behind the read position, it has to start again from the first record. Here's the trick: sequential access is not wasteful when you need every record anyway. If a program is going to use all 800 records, reading them in order is the cheapest way to do it.
Section 3Direct access
Direct access, also called random access, means going straight to the wanted record without reading the ones before it. The program is given the key or the record number and works out where the record is stored. Only one record is read, so the number of reads does not depend on where the record sits in the file, or on how big the file is. Easy to mix up: 'random' here does not mean chosen by chance. It means the program can jump to any record, in any order. You can fetch record 450, then record 12, then record 300, with no starting again.
Section 4Which access each organisation allows
Sequential file: sequential access only. Random file: direct access, because the slot is worked out from the key or the record number. A file of this kind can also be read through slot by slot if a program needs to. Index-sequential file: direct access by key through the index, or sequential access reading the records in key order. That is why it is used when a file needs both. So direct access needs something extra that can say where a record is, a calculation or an index. Without it, only sequential access is possible. Whether the extra is worth having depends on the job, and the last topic in this section looks at that choice.
Section 5Counting reads, restarts and sorted requests
By sequential access, record k costs k reads when you start from the first record. By direct access it costs 1 read. Watch out for the restart. Suppose a program with a forward-only read position asks for records 120, 450 and then 300. Record 120 costs 120 reads. Record 450 carries on from where it stopped, so it costs 450 - 120 = 330 more reads. Record 300 is behind the read position, so the program starts again at the first record and reads 300. The total is 120 + 330 + 300 = 750 reads. Here's the trick: if the requests can be put into the same order as the file (120, 300, 450), one pass does the lot: 450 reads. Direct access needs just 3 reads, whatever the order.
Section 6When each method wins
Sequential access wins when the whole file, or most of it, is processed in one run in key order. There is no need to jump around, and the file stays simple. Direct access wins when the program needs one record quickly, or a few records scattered through a big file: an enquiry at a counter, or a booking being checked. The cost of direct access is the extra that makes it possible: an index to store and keep up to date, or a calculation and some empty slots. Sequential access needs nothing extra. Saying 'direct access is faster' is only half an answer. Say faster for which job.
Examples
A club in Mandeville keeps 30 member records, numbered 1 to 30, in a sequential file. (a) State how many records are read to reach record 18 by sequential access. (b) State how many records are read to reach record 18 by direct access. (c) Calculate how many fewer reads direct access needs. (d) Calculate the average number of reads for sequential access if every record is equally likely to be wanted.
(a) Sequential access reads records 1 to 18, including the wanted one: 18 reads. (b) Direct access goes straight to record 18 and reads only that: 1 read. (c) 18 - 1 = 17 fewer reads. (d) The reads run from 1 for the first record to 30 for the last, so the average is (30 + 1) ÷ 2 = 15.5 reads.
A call centre in Montego Bay keeps 800 customer records, numbered 1 to 800. A program is asked for records 120, 450 and 300, in that order. With sequential access its read position only moves forward, and if it needs an earlier record it starts again from the first. (a) Calculate the total reads for sequential access, in the order the requests arrive. (b) The requests are sorted into file order (120, 300, 450) first. Calculate the reads for sequential access in one pass. (c) Calculate the reads with direct access. (d) Find how many reads sorting the requests saves, and state when sequential access is still a sensible choice.
(a) Record 120: 120 reads, and the read position is now at 120. Record 450: carry on from 121 to 450, which is 450 - 120 = 330 reads. The read position is now at 450. Record 300: it is behind the read position, so start again and read 1 to 300: 300 reads. Total: 120 + 330 + 300 = 750 reads. (b) In file order, one pass reads up to the furthest record: 120, then 300, then 450. The pass ends at record 450, so 450 reads. (c) Direct access reads one record per request: 3 reads. (d) Sorting saves 750 - 450 = 300 reads. Sequential access is still sensible when most or all of the records are needed in file order, because then every record has to be read anyway and nothing is wasted.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 5, Specific Objective 6 · Syllabus scope
- GnuCOBOL Programmer's Guide for Version 2.2 (7 September 2017) · GnuCOBOL project (G. L. Cutler, updates V. B. Coen) · Chapter 1, section 1.3.8 Files (sequential, relative and indexed files and how their records are reached), checked against (https://gnucobol.sourceforge.io/info/gnucobpg.info) · Facts
- GnuCOBOL Programmer's Guide for Version 2.2 (7 September 2017) · GnuCOBOL project (G. L. Cutler, updates V. B. Coen) · Chapter 4, section 4.2.1 SELECT (ACCESS MODE clause: sequential and random access), checked against (https://gnucobol.sourceforge.io/info/gnucobpg.info) · Facts