Problem:
Use the following program as an example and a template, when you need to write a COBOL program that will rebuild an existing indexed file. The program reads the file from beginning to end and writes each record to a new file.
This is a way of creating a fresh version of the indexes and their B-trees, and of removing records that were marked for deletion but weren't yet physically deleted, and of otherwise getting the file into a new and fresh state. It is an alternative to using the "rebuild infile, outfile" command.
Resolution:
The example "rebuild" program below is based on the demonstration program "stockioa.cbl", which gets installed with the Server Express product, and can be found in the directory $COBDIR/demo/locking. Before running the example "rebuild" program below, perform the following steps:
mkdir /tmp/test-rbld
cd $COBDIR/demo/locking
cp stockioa.cbl /tmp/test-rbld
cd /tmp/test-rbld
cob stockioa.cbl
cobrun stockioa
Press "1", then function key 4, to add a record to the file, then press ESC, then "n" for "not wish to restart". These actions will create an indexed file named "MUSTOCK.DAT".
Note that the following is a complete program (not a fragment), and will compile as written:
000001 $SET IDXFORMAT"8"
000002 $SET KEYCOMPRESS"7"
000003 $SET DATACOMPRESS"1"
000004
000005 SELECT STOCK-FILE-OLD ASSIGN TO "MUSTOCK.DAT"
000006 ORGANIZATION INDEXED
000007 ACCESS DYNAMIC
000008 RECORD KEY STOCK-KEY-OLD.
000009
000010 SELECT STOCK-FILE-NEW ASSIGN TO "MUSTOCK-NEW"
000011 ORGANIZATION INDEXED
000012 ACCESS DYNAMIC
000013 RECORD KEY STOCK-KEY-NEW.
000014
000015 DATA DIVISION.
000016 FILE SECTION.
000017
000018 FD STOCK-FILE-OLD.
000019 01 STOCK-RECORD-OLD.
000020 03 STOCK-KEY-OLD PIC 9(06).
000021 03 STOCK-DATA.
000022 05 STOCK-DESCRIPTION-1 PIC X(53).
000023 05 STOCK-DESCRIPTION-2 PIC X(53).
000024 05 STOCK-DESCRIPTION-3 PIC X(53).
000025 05 STOCK-HELD PIC 9(06).
000026 05 STOCK-COST PIC 9(06)V99.
000027
000028 FD STOCK-FILE-NEW.
000029 01 STOCK-RECORD-NEW.
000030 03 STOCK-KEY-NEW PIC 9(06).
000031 03 STOCK-DATA.
000032 05 STOCK-DESCRIPTION-1 PIC X(53).
000033 05 STOCK-DESCRIPTION-2 PIC X(53).
000034 05 STOCK-DESCRIPTION-3 PIC X(53).
000035 05 STOCK-HELD PIC 9(06).
000036 05 STOCK-COST PIC 9(06)V99.
000037
000038 WORKING-STORAGE SECTION.
000039 01 COUNT-WS PIC 9(8) VALUE ZERO.
000040 01 DONE-CHAR PIC X VALUE "N".
000041 88 DONE VALUE "Y".
000042 PROCEDURE DIVISION.
000043 OPEN INPUT STOCK-FILE-OLD, OUTPUT STOCK-FILE-NEW
000044 READ STOCK-FILE-OLD NEXT
000045 AT END MOVE "Y" TO DONE-CHAR
000046 END-READ
000047 PERFORM UNTIL DONE
000048 MOVE STOCK-RECORD-OLD TO STOCK-RECORD-NEW
000049 WRITE STOCK-RECORD-NEW
000050 ADD 1 TO COUNT-WS
000051 READ STOCK-FILE-OLD NEXT
000052 AT END MOVE "Y" TO DONE-CHAR
000053 END-PERFORM
000054 DISPLAY " COUNT = ", COUNT-WS.
000055 STOP RUN.
Note that, at the beginning (lines 1 thru 3), we can specify IDXFORMAT and data and key compression if we like. These settings have no affect on the input file -- they affect only the properties of the output file. The settings are not required; they are included here just as an example. By setting them, we can "convert" the old file into something new by giving the output file some new characteristics if we like.
The first step to writing such a program is to obtain the SELECT and FD for the file in question. Next, make a copy of the SELECT and FD and change the name of the overall record name, the record key, and alternate key fields (if any), by appending "OLD" to the old version and "NEW" to the new. In this example the file does not contain alternate keys, but if it did, their names would have to be qualified with "OLD" and "NEW" for uniqueness.
It is not necessary to change the name of every field in the FD; it is only necessary to change the overall record name, the record key, and the alternate key fields (if any). The other fields can maintain the same names because they are never explicitly referred to; at line 48, the entire record is moved at once from old to new.
Note that the program does not declare a FILE-STATUS data item, and does not check the file status after each I-O operation. This is an efficient approach, because in the absence of a file-status item, if any serious I-O error happens to occur, the runtime system takes it upon itself to automatically stop the program and put out a meaningful error including an explanatory text message.
#RMCOBOL
#ServerExpress
#COBOL
#AcuCobol
#netexpress