1 / 39

Disk File Organization

Disk File Organization. File is collection of records Three major ways records stored or organized on disk - Sequential File Organization - Indexed File Organization - Relative File Organization. Indexed File Organization. Consists of two files Data file - records in sequence

Télécharger la présentation

Disk File Organization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Disk File Organization • File is collection of records • Three major ways records stored or organized on disk - Sequential File Organization - Indexed File Organization - Relative File Organization

  2. Indexed File Organization • Consists of two files • Data file - records in sequence • Index file - contains value of • Each key field • Disk address of record with that corresponding key field • For random access, look up key field in index file to find address • Then access record in data file directly

  3. Relative File Organization • When records created, key field used to compute a disk address where record is written • To randomly access records • User enters key field • Disk address computed from key field • Record then accessed directly • No index needed

  4. Magnetic Disks, Disk Drives • Storage media for data • Can be accessed at very high speeds • Have metal oxide coating that stores data as magnetized bits • Disks may be floppy disk, hard disk or disk pack

  5. Creating an Indexed File • Records written in sequence by key field as for sequential disk file • Once index file created, records can be accessed randomly

  6. Relative files: • A relative file is organized in such a way that each record is identified by a relative record number. • The relative record number specifies the position of the record from the beginning of the file. • The relative record number 1 identifies the first record; the relative record number2 identifies the second record and so on. • A relative file can be accessed either sequentially or randomly. • When the file is accessed sequentially, the records accessed in the increasing order of their relative record numbers.

  7. Relative files: • When a file is accessed randomly, the programmer must specify the relative record number. • In the case of relative organization, the reading as well as the writing can be done randomly. • The handling of relative files requires some special codes in the FILE CONTROL paragraph.

  8. FILE-CONTROL Paragraph for Relative files • The general format for the SELECT Clause for a relative file is, SELECT File-name ASSIGN TO implementor-name [; RESERVE INTEGER-1 [ AREA ]] AREAS ; ORGANIZATION IS RELATIVE [; ACCESS MODE IS { SEQUENTIAL[, RELATIVE KEY IS dataname1}] { RANDOM}, RELATIVE KEY IS dataname2 DYNAMIC [; FILE STATUS IS dataname-3]

  9. The ORGANIZATION Clause specifies that the said file is a relative file. Whether the file should be used sequentially or randomly, should be specified through the word SEQUENTIAL or RANDOM in the ACCESS MODE Clause. • The Clause ACCESS MODE IS DYNAMIC indicates that the file is accessed sequentially and/or randomly in the PROCEDURE DIVISION.

  10. The Phrase RELATIVE KEY must be specified when the access mode is RANDOM or DYNAMIC. • The dataname-1 is called the relative key data item and it indicates the field that contains the relative record number. • dataname-1 must be an unsigned integer and may be qualified.

  11. PROCEDURE DIVISION Statements for Relative Files • In addition to the OPEN, CLOSE, READ, WRITE and REWRITE, two other verbs, namely DELETE and START is also used in the relative files. • READ STATEMENT The general format for the READ statement are, FORMAT-1 READ file-name RECORD [ INTO identifier ] [; AT END IMPERATIVE STATEMENT]

  12. READ statement • FORMAT-2 READ file-name RECORD [ INTO identifier ] [; INVALID KEY imperative statement] • FORMAT-3 READ file-name [ NEXT ] RECORD [ INTO identifier ] [; AT END imperative statement]

  13. READ statement • As usual, a READ statement reads a record of the file. The file must be open in either the INPUT or I-O mode. • FORMAT-1 is the normal form of the READ statement and this is the only form that is applicable to sequential files. In the case of relative files, this format is used when the access mode is sequential. • FORMAT-2 is used when the access mode is either random or dynamic. The record to be read is identified from the content of the relative key data item. • For eg; Suppose REL-FILE and REL-KEY are the names for the relative file and relative key data item respectively.

  14. MOVE 50 TO Rel-Key. READ Rel-Key RECORD INVALID KEY GO TO Para-Invalid. • The imperative statement of the INVALID KEY phrase is executed when the read is unsuccessful. • Reading is considered unsuccessful if attempt is made to read a record, 1.From an empty record position of the relative file. 2.From outside the extremely defined boundaries the file. • FORMAT-3 of the READ statement can be used when the access mode is dynamic and the records are to be read sequentially. • As a result of the execution of this statement, the next record from this file is read.

  15. WRITE STATEMENT • The WRITE statement for a relative file has the format, WRITE Record-name [ FROM identifier ] [; INVALID KEY imperative-statement] • At the time of execution of the WRITE statement the file must be open either in the OUTPUT or I-O mode. • When the file is accessed randomly, the statement releases the record to that relative record position on the file which is indicated by the record number in the relative key data item.

  16. Example • Suppose REL-OUTPUT & REL-KEY are the record name and relative key data item name for a relative file opened in the I-O mode. MOVE 50 TO Rel-Key. WRITE Rel-Output INVALID KEY GO TO Para-Invalid. • The imperative statement of the INVALID KEY is executed in the following cases, • When an attempt is made to write beyond the externally-defined boundaries of the file. • When an attempt is made to write in the record position which already contains a valid record.

  17. REWRITE STATEMENT The REWRITE statement has the following format, REWRITE Record-name [ FROM identifier ] [; INVALID KEY imperative-statement ] • The REWRITE statement is used t replace an existing record by the contents of the record specified in the record name. The file must be opened in the I-O mode. When the access mode is RANDOM or DYNAMIC, the record to be replaced is identified by the contents of the relative key data item.

  18. REWRITE STATEMENT • When the access mode is SEQUENTIAL, prior to the execution of the REWRITE statement, a READ statement on the file must be successfully executed. There should not be any other input-output statement on the file in between the READ and REWRITE statement. • The imperative statement after the INVALID KEY is executed when an attempt is made to replace a record position which is empty.

  19. DELETE STATEMENT The format of the delete statement is as follows, DELETE file-name RECORD [; INVALID KEY imperative-statement] • The DELETE statement deletes the data contained in the specified record position of a relative file. The said is no longer available. • When the ACCESS MODE is SEQUENTIAL the execution of the DELETE statement must be preceded by the execution of a READ statement on the file and the INVALID KEY phrase should not be specified.

  20. DELETE Statement • The INVALID KEY condition arises when an attempt is made to delete the record of an empty record position. • The execution of a DELETE statement does not affect the contents of the record area.

  21. START Statement • To begin processing relative/indexed file sequentially starting from any record location • Print file beginning with customer record with Acct-No = 025 • Print all customers with Cst-Last-Name beginning with letter 'S'

  22. START Statement Format START file-name-1 IS = KEY IS > data-name-1 IS NOT < IS >= [INVALID KEY imperative-statement-1] [NOTINVALID KEY imperative-statement-2] [END-START]

  23. START Statement • To begin processing with record whose account number equals 025 Move 025 To Acct-No Start Accts-Receivable Invalid Key Display 'Acct-No 025 not found' Not Invalid Key Perform 300-Proc-Rec End-Start

  24. START Statement • START locates record with Acct-No = 025 • INVALID KEY clause executed only if no such record found • START locates record but does not READ it • 300-Proc-Rec must include READ … AT END to bring record into storage for processing

  25. START Statement • KEY clause can be omitted only if checking for value equal to RECORD KEY value • To locate record with Acct-No > 100: Move 100 To Acct-No Start Accts-Receivable Key > Acct-No Invalid Key … ...

  26. START STATEMENT • The START statement enables the programmer to position the relative file at some specified point so that subsequent sequential operations on the file can start from this point instead of the beginning. • The KEY IS phrase indicates now this file is to be positioned. • The data name in this phrase must be the data name in the RELATIVE KEY phrase of the SELECT clause. • When the EQUAL TO or NOT LESS THAN condition is specified, the file is positioned at the point indicated by the relative key-data item. • When the GREATER THAN condition is specified, the file is positioned at the next relative position of the position indicated by the relative key data item. Thus

  27. Example START my-file KEY IS GREATER THAN REL-KEY ; INVALID KEY GO TO Invalid-Para. • It will position the file at the fifty-first record position if the relative key data item REL-KEY contains 50. • The INVALID KEY condition arises if the specified record position is empty. • The START statement requires that the file must be opened in the INPUT or I-O mode. • The access mode can only be SEQUENTIAL or DYNAMIC.

  28. 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].

  29. 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

  30. 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).

  31. 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.

  32. 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

  33. Processing Relative Files • REWRITE … INVALID KEY to update • DELETE … INVALID KEY to remove record from file

  34. Indexed Files: • The real power of COBOL data processing lies in indexed files. It can store the same information as a sequential file, but a separate index is created that keeps the records sorted in alphabetical or numerical order. Most of the disc files which are accessed randomly are created using the indexed file organization. • When you create an indexed file, you have to specify which fields to use for indexing. Along with the data field, an index is also created that associates an actual disk address with each key field in the data file.

  35. use these index fields to retrieve records rapidly in the sequence that is different from the actual order of the records in the file. COBOL Language Instruction For Indexed Files. • The two divisions ENVIRONMENT DIVISION and PRCEDURE DIVISION involve special instructions for indexed files.

  36. ENVIRONMENT DIVISION. SELECT file-name-1 ASSIGN TO implementor-name RESERVE integer-1 AREAS ORGANISATION IS INDEXED ACCESS MODE IS SEQ/ Random/DYNAMIC RECORD KEY IS data -name-1 [ALTERNATE RECORD KEY IS data-name-2 [WITH DUPLICATES] ]…. [FILE STATUS IS data-name-3]

  37. Procedure Division Statements • READ Statement. READ file-name RECORD [ INTO identifier ] [; KEY IS data-name ] [; INVALID KEY imperative statement]

  38. WRITE Statement Same as Relative, WRITE Record-name [ FROM identifier ] [; INVALID KEY imperative-statement] The Invalid key condition arises in the following cases. • Beyond the externally-defined bounderies of the file.

  39. 2. Value of record key is not greater than the value of the record key for the previous record written. 3. When the value of the record key is equal to the record key of a record already present in the file. REWRITE, DELETE, START statements are same.

More Related