• Aucun résultat trouvé

Maintaining a Direct File

-R e l a t i v e

Oisk File R e c o r d N u m b e r t . F

2 3 4 5

Figure 12. Random Processing by Relative Record Number

Maintaining a Direct File

Three file maintenance functions can be used to are created:

1. Adding records

2. Tagging records for deletion 3. Updating records

@ *",r, information is inserted in the record if update is indicated.

keep direct files current after they

Updated disk is written.

r->g 8 e ro

'9

I I

Adding Records

U n l i k e s e q u e n t i a l a n d in d e x e d f i l e s , d i r e c t f i l e s c a n h a v e s p a c e a v a i l a b l e b e t w e e n existing records for records to be added. To add records to the file, the relative record number for the added record must first be determined. The location is then read into storage. lf the location is blank, the record is stored. otherwise, if the location already contains a record. the new record is stored as a synonym.

Tagging Records for Deletion

A s in o t h e r fi l e s , r e c o r d s i n d i r e c t f i l e s c a n b e id e n t i f i e d f o r d e l e t i o n b y a d e l e t e c o d e . T h i s c o d e is u s u a l l y a s i n g l e c h a r a c t e r a t a p a r t i c u l a r l o c a t i o n i n t h e r e c o r d . When the file is processed, your program must check for the delete code; if the code is present, the record can be bypassed.

Since the delete code indicates that the record has been deleted, however, the record location is available for a new record. Either the location can contain a synonym, or it can be reused by assigning the relative record number to a new record. lf the file c o n t a i n s s y n o n y m s , b e c a r e f u l n o t t o d e l e t e s y n o n y m c h a i n i n g i n f o r m a t i o n w h e n you delete a record and reuse the location.

Updating Records

When you update records in a file, you can add or change some data on the record, The record to be updated is read into storage, changed, and written back on the disk i n i t s o r i g i n a l l o c a t i o n . R e c o r d s i n a d i r e c t f i l e c a n b e u p d a t e d c o n s e c u t i v e l v o r r a n d o m l y .

R e c o r d s a r e u s u a l l y u p d a t e d c o n s e c u t i v e l y w h e n y o u w a n t to u p d a t e a l l o r m o s t o f t h e r e c o r d s i n t h e fi l e . R e c o r d s a r e u p d a t e d i n o r d e r . H o w e v e r , s y n o n y m r e c o r d s i n a c o n s e c u t i v e l y p r o c e s s e d d i r e c t f i l e m a y r e q u i r e s p e c i a l h a n d l i n g .

To update your file randomly, you must specify the relative record number of the record you want. The relative record number is used to find the record in the file so it can be moved into storage for updating.

M A N I P U L A T I N G D I R E C T F I L E D A T A

D i r e c t f i l e o r g a n i z a t i o n o n t h e S y s t e m / 3 o f f e r s y o u a f l e x i b l e t o o l f o r d a t a m a n i p u -l a t i o n t h a t i s n o t a v a i l a b l e i n t h e o t h e r o r g a n i z a t i o n m e t h o d s . W i t h d i r e c t o r g a n i z a -t i o n , y o u c a n :

o Access a file consecutirrely more than once in the same program.

o Load a file, then retrieve the records in the same program.

Tie together strings of related records so they can be retrieved as a group when they are not necessarily stored together in the file.

B u i l d a n d r e t r i e v e m e s s a g e q u e u e s i n a c o m m u n i c a t i o n s s y s t e m . Use a direct f ile for large arrays.

D i r e c t F i l e s 2 5

U s i n g th e t e c h n i q u e s d i s c u s s e d I n t h i s s e c t i o n , a d i r e c t f i l e c a n b e u s e d o v e r a n d o v e r w i t h o u t b e i n g r e - c r e a t e d ; e x i s t i n g r e c o r d s a r e r e - w r i t t e n w h e n t h e f i l e i s u s e d . C o n s e q u e n t l y , i t i s u s u a l l y c o n v e n i e n t t o c r e a t e th e f i l e w i t h a p r o g r a m th a t d o e s n o t l o a d a n y d a t a . T h e n a l l o f t h e a c c e s s i n g p r o g r a m s c a n d e f i n e t h e f i l e a s a n u p -d a t e , c h a i n e -d . d i r e c t , o r r a n d o m f i l e . T h e e x a m p l e s i n t h i s s e c t i o n a s s u m e a p r e v i o u s -ly created file.

T h e t e c h n i q u e s d e s c r i b e d n o r m a l l y r e q u i r e th a t r e c o r d s b e p l a c e d in t h e f i l e i n c o n -s e c u t i v e r e c o r d lo c a t i o n s . T h e p r o g r a m s w i l l u s e o n e o r m o r e c o u n t e r s (n u m e r i c t o t a l f i e l d s ) to k e e p t r a c k o f t h e n e x t r e l a t i v e r e c o r d n u m b e r .

Accessing a File Consecutively

T o a c c e s s a f i l e c o n s e c u t i v e l y m o r e t h a n o n c e i n t h e s a m e p r o g r a m , th e p r o g r a m in -crements the record number counter by one each time a record is accessed, and then c h a i n s t o t h e f i l e . T h i s a c t i o n i s r e p e a t e d u n t i l t h e l a s t r e c o r d is r e a d . T h e c o u n t e r

is then reset to zero and the process is repeated. The.program recognizes the last r e c o r d in t h e f i l e b y ( 1 ) i d e n t i f y i n g th e l a s t r e c o r d w i t h a s p e c i f i c c o d e a n d t e s t i n g f o r t h a t c o d e , o r ( 2 ) b y t e s t i n g fo r t h e f i r s t b l o c k r e c o r d in t h e f i l e , o r ( 3 ) b y k n o w -ing the record number of the last record.

Loadingf and Retrieving Records in the Same Program

I n u p d a t e m o d e , t h e r e c o r d n u m b e r c o u n t e r i s u s e d to l o a d r e c o r d s i n c o n s e c u t i v e record locations. After records have been loaded, they can be retrieved by record n u m b e r u s i n g th e c h a i n o p e r a t i o n .

Connecting Strings of Related Records

T h i s t e c h n i q u e , k n o w n a s c h a i n i n g , r e q u i r e s t h a t e a c h r e c o r d in t h e f i l e c o n t a i n a n e x t r a f i e l d . T h a t f i e l d w i l l c o n t a i n t h e r e c o r d n u m b e r o f t h e n e x t r e c o r d in t h e s t r i n g . A b l a n k o r z e r o f i e l d c a n b e u s e d to i d e n t i f y t h e l a s t r e c o r d in a s t r i n g . T h e c h a i n i n g te c h n i q u e w o r k s w e l l i n a n a c c o u n t s re c e i v a b l e a p p l i c a t i o n . F o r e x -a m p l e , -a c u s t o m e r m -a s t e r fi l e i s i n d e x e d b y c u s t o m e r n u m b e r . T r -a n s -a c t i o n s a r e a d d e d c o n s e c u t i v e l y t o a d i r e c t f i l e a s t h e y o c c u r a n d a r e a p p l i e d to a b a l a n c e f i e l d i n t h e c u s t o m e r m a s t e r re c o r d . A n i n q u i r y t o t h e m a s t e r fi l e w i l l c a u s e t h e b a l a n c e i n f o r m a t i o n a n d a l l t r a n s a c t i o n s f o r t h a t c u s t o m e r to b e d i s p l a y e d .

T h i s i s a c c o m p l i s h e d b y a d d i n g tw o f i e l d s to e a c h c u s t o m e r m a s t e r re c o r d . T h e s e fields contain the record numbers of the first and last transaction records (respect-i v e l y ) f o r t h a t c u s t o m e r (respect-in t h e t r a n s a c t (respect-i o n f i l e . T h e s e fi e l d s a r e s e t t o b l a n k o r z e r o a t t h e b e g i n n i n g o f t h e a c c o u n t i n g p e r i o d a n d r e m a i n s e t a t z e r o u n t i l t h e f i r s t transaction is posted for that customer.

- Q u 5 1 6 r n e r M a s t e r R e c o r d F o r m a l F i r s t

T r a n s a c t i o n R e c o r d N u m b e r

Last T r a n s a c t i o n

R e c o r d N u m b e r Customer Data

z o

Record 1 in the transaction file is reserved for storing the record number of the n e x t a v a i l a b l e r e c o r d s p a c e in t h e f i l e a t t h e t i m e t h e f i l e i s c l o s e d . W h e n t h e f i l e i s initialized at the start of the accounting period, record 2 is the next available record, when transactions are added to the file, record I is read at the beginning of the job b y t h e p r o g r a m , t o e s t a b l i s h w h e r e t h e n e x t t r a n s a c t i o n w i l l b e p l a c e d . T h e v a l u e stored in record 1 is increased by one each time a record is added (the new value is w r i t t e n b a c k i n t o r e c o r d 1 a t L R t i m e ) .

R e c o r d n u m b e r - 1

Each transaction record contains a number record to the same customer.

that is used to locate the next transaction

R e c o r d F o r m a t N e x t

T r a n s a c t i o n R e c o r d N u m b e r

Two routines are needed to load transaction records into the file. One loads the first transaction for a customer; the other loads all subsequent records for the customer.

A s s u m i n g ( 1 ) t h e tr a n s a c t i o n f i l e i s th e p r i m a r y f i l e , ( 2 ) t h e c u s t o m e r m a s t e r r e c o r d has been accessed by a CHAIN operation. and (3) the first transaction record

n u m b e r f i e l d is b l a n k o r z e r o , t h e f o l l o w i n g i s a n e x a m p l e o f h o w th e f i r s t t r a n s a c t i o n record is loaded and the records set for a customer:

1 . U s i n g t h e n e x t a v a i l a b l e r e c o r d n u m b e r ( f r o m re c o r d 1 ) c h a i n t o t h e t r a n s a c t i o n f i l e .

Put the new transaction record out in the record space.

Place the next available record number in both the first and last number fields of the master record.

Add one to the next available record number.

lf one transaction had been loaded for customers X, A, and D, the files would appear a s f o l l o w s :

I n i t i a l i z e d T r a n s a c t i o n F i l e

--*1 2 | | | | |

M a s t e r F i l e l l T r a n s a c t i o n F i l e lZ 2 .

3 .

Record 1 2 3

L_!j Pointer to next available record (in storage)

4 1 4 Customer X

I C r r , o r n . r X I l c u s t o m e r R l l C r s t o . e r o 1 | r l

6

Direct Files 27

T h e fo l l o w i n g d e s c r i b e s h o w s u b s e q u e n t r e c o r d s a r e a d d e d :

1 . U s i n g t h e n e x t a v a i r a b l e r e c o r d n u m b e r , a d d th e n e w tr a n s a c t i o n t o t h e f i l e 2. Using the last record number field from the master record, chain to the last

transaction for that customer.

U p d a t e t h i s r e c o r d b y p l a c i n g t h e n e x t a v a i l a b l e r e c o r d n u m b e r i n i t s n e x t t r a n s a c t i o n r e c o r d n u m b e r f i e l d .

P l a c e t h e n e x t a v a i l a b l e r e c o r d n u m b e r i n t h e l a s t t r a n s a c t i o n r e c o r d n u m b e r field of the master record.

5 . A d d o n e t o t h e n e x t a v a i l a b l e r e c o r d n u m b e r .

Assume that one transaction has been added for customer X, one added for customer D , a n d a n o t h e r a d d e d fo r c u s t o m e r X . T h e f i l e s w o u l d then appear as follows:

t l r l t l t t l

M a s t e r F i l e - 1 n , , ,

t ' , r t o r n u r a lg I s l l C u s t o m e r p l 4 | 6 l l c r r t o . n " r x l z I z l T r a n s a c t i o n r i r e _ l z l l c u s t x I s I c u s t n l l c u s t o l o

C u s t X C u s t D C u s t X R e c o r d n u m b e r

-l g I - N . " , a v a i -l a b -l e r e c o r d ( i n s t o r a g e )

R e m e m b e r t h a t t h e n e x t a v a i l a b l e r e c o r d n u m b e r w i l l b e w r i t t e n i n t o r e c o r d 1 a t L R t i m e .

Message Oueuing in a System/3 Direct File

In a communications environment, it is often necessary to store messages as they a r e r e c e i v e d a n d m a k e t h e m a v a i l a b l e f o r p r o c e s s i n g a t a l a t e r ti m e . T h i s t e c h n i q u e known as message queuing, can be readily used with direct files, with the following restrictions:

o Variable length messages must be blocked by the user to f it the fixed length disk reco rd.

o u e u e d m e s s a g e s w i l l b e p r o c e s s e d o n a f i r s t i n - f i r s t o u t b a s i s w i t h i n a g i v e n q u e u e . R e c o r d s (m e s s a g e s ) a r e p l a c e d in t h e q u e u e s i n t n e s a m e m a n n e r a s t r a n s a c t i o n s were placed in the transaction file in the accounts receivable example presented e a r l i e r in t h i s s e c t i o n .

T h r e e p o i n t e r s (r e c o r d n u m b e r s ) a r e n o r m a l l y r e q u i r e d fo r e a c h q u e u e in t h e file: a pointer to the first record in the queue, a pointer to the last record in the queue, and a pointer to the next record in the queue to be processed.

O u e u e 1

3 .

4 .

F irst Record P o i n t e r l F irst Record Poi nterX

Last Record Poi nter 1 Last Record P o i n t e r y

N e x t Record P o i n t e r . l Next Record PointerX Oueue X

These pointers are usually maintained in arrays, with th€ queue numbers used for subscripts. Besides the three pointers previously mentioned, a pointer is required to the next available record in the file. When the file is closed, all pointers are stored in a reserved record in a file.

The next record pointer allows the processing program to retrieve records consecu-tively from a given queue. This pointer is initially set equcl to the first record point-er. and is then changed each time a record is retrieved frorn the queue. This pointer may be maintained within the processing programg instead of in the file, to allow multiple processing programs to access the same gueue. Each using program would keep track of its own processing position within a quoue.

Uring a Direst File for Large Arrays

Arrays that are too large to be held in main storago nrey be storod on disk as a direct file. The subscript value becomes the record number of the data stored in the file. There is no minimum record size in System/3 d.ie k files. Data fields in an array may be stored as individual records in a direct file.

Direct Files 29

C H A P T E R 5 . R E C O R D A D D R E S S F I L E S

Record address files are input files that indicate which records are to be read from disk files and the order in which the records are to be read. There are two types of record address files:

F i l e s c o n t a i n i n g r e l a t i v e r e c o r d n u m b e r s F i l e s c o n t a i n i n g r e c o r d k e y li m i t s

F i l e s C o n t a i n i n g R e l a t i v e R e c o r d N u m b e r s ( A D D R O U T F i l e s )

A r e c o r d a d d r e s s f i l e th a t c o n t a i n s r e l a t i v e r e c o r d n u m b e r s i s c a l l e d a n A D D R O U T (address out) file. ADDROUT files are comprised of binary 3-byte relative record n u m b e r s t h a t in d i c a t e t h e r e l a t i v e p o s i t i o n ( f i r s t , t w e n t i e t h , n i n e t y - n i n t h ) o f records in the file to be processed.

Creating an ADDROUT File

An ADDROUT file is created by the Disk Sort program. The input for the Sort p r o g r a m i s a f i l e w h i c h m a y b e o r g a n i z e d a s a s e q u e n t i a l . i n d e x e d , o r d i r e c t f i l e . The output from the Sort program is a new file consisting of relative record numbers.

This file of relative record numbers may then be used during the processing of the original file to provide accessing of the file in a sequence different from the se-quence in which the file is stored on disk. For more information, see the l8M System/3 Disk Sort Reference Manual, SC21-7522.

T h e fo l l o w i n g t h r e e p o i n t s s h o u l d b e c o n s i d e r e d w h e n u s i n g A D D R O U T f i l e s : 1 . One file can be sorted in several sequences, based on different control fields

i n e a c h r e c o r d o f t h a t f i l e . T o a v o i d s o r t i n g th e e n t i r e f i l e e a c h ti m e a different sequence is required, several ADDROUT files can be created by sorting the input file to be used in your programs in several ways. For example, you have a transaction file in order by stock number. By perform-i n g t w o A D D R O U T s o r t s o n t h e t r a n s a c t perform-i o n f i l e , y o u c o u l d h a v e o n e A D D R O U T f i l e s e q u e n c e d b y c u s t o m e r n u m b e r a n d a n o t h e r b y i n v o i c e n u m b e r . C o n -sequently, you can access the transaction file by several sequences: stock n u m b e r , c u s t o m e r n u m b e r , o r i n v o i c e n u m b e r .

A n A D D R O U T f i l e r e q u i r e s l e s s d i s k s p a c e t h a n t h e o u t p u t f i l e o f a t a g - a l o n g s o r t b e c a u s e t h e o u t p u t r e c o r d s o f t h e A D D R O U T f i l e a r e o n l y t h r e e b y t e s long (see sorting a file, in Chapter 6).

l f a n A D D R O U T f i l e i s u s e d to p r o c e s s a m u l t i v o l u m e fi l e ( R P G l l a n d C O B O L o n l y ) , a l l v o l u m e s o f t h a t f i l e m u s t b e m o u n t e d d u r i n g p r o c e s s i n g because the next record required may be on any volume.

2 .

3 .

Processing by an ADDROUT File

A l l t y p e s o f f i l e o r g a n i z a t i o n s ( s e q u e n t i a l , i n d e x e d , o r d i r e c t ) u s e d a s p r i m a r y o r s e c o n d a r y f i l e s c a n b e p r o c e s s e d b y A D D R O U T f i l e s . F o r R P G ll , w h e n a n o b j e c t p r o g r a m u s e s a n A D D R O U T f i l e t o p r o c e s s a n o t h e r f i l e , it r e a d s a r e l a t i v e r e c o r d number from the ADDROUT file, then locates and reads the record situated at that relative position in the file being processed. Only those records whose relative record numbers are located in the ADDROUT file are processed. Records are r e a d i n t h i s m a n n e r u n t i l th e e n d o f t h e A D D R O U T fi l e i s r e a c h e d . F i g u r e 1 3 s h o w s a n A D D R O U T f i l e u s e d t o p r o c e s s a d i s k fi l e .

/Uote.' COBOL uses only direct f ile organization for this application.

A d i f f e r e n t a p p r o a c h i s n e e d e d w h e n u s i n g F O R T R A N a n d C O B O L . Y o u w o u l d d e f i n e t h e A D D R O U T f i l e a s a n i n p u t fi l e , a n d th e c o r r e s p o n d i n g d i r e c t f i l e a s a n o t h e r i n p u t f i l e . Y o u r p r o g r a m w o u l d th e n r e a d f r o m A D D R O U T a n d p u t t h e i n p u t d a t a in t o t h e a s s o c i a t e d v a r i a b l e ( s p e c i f i e d i n t h e f i l e d e f i n i t i o n s t a t e m e n t ) f o r t h e d i r e c t f i l e . Execution of a READ statement would then retrieve the desired record from the d i r e c t f i l e . Y o u m a y te r m i n a t e r e a d i n g f r o m A D D R O U T e i t h e r a t i t s E O F o r p r i o r t o i t s E O F . Y o u m u s t lo g i c a l l y d e t e r m i n e E O F fo r y o u r o w n s i t u a t i o n ( f o r e x a m p l e , by a record count).

A D D R O U T f i l e (containing relat record numbers)

Documents relatifs