New Jersey Institute of Technology (NJIT)
Computer and Information Science Department (CIS)
CIS365: File Structures and Management, Fall 1999
Professor: Michael Bieber


Traditional File Update Algorithm - Version 4

 

NOTES

(a) The OLD-MASTER file and TRANSACTION file records must be sorted in the order of the same key field.

(b) This algorithm assumes one TRANSACTION record per master file record.

(c) This replaces the algorithm presented in the Uckan text, Volume 1 chapter 10 or Volume 2 chapter 1.

 

MAIN PROCEDURE

PERFORM initialize

PERFORM update-loop UNTIL no TRANSACTION records remain and no OLD-MASTER records remain
(i.e., TRANSACTION-EOF-FLAG = "True" and MASTER-EOF-FLAG = "True")

PERFORM close-files

 

INITIALIZE

Open OLD-MASTER file for input.
Open TRANSACTION file for input.
Open NEW-MASTER file for output.
Open ERROR file for output.
Move "true" to read-transaction-flag.
Move "true" to read-master-flag.
Move "false" to MASTER-EOF-FLAG.
Move "false" to TRANSACTION-EOF-FLAG.

 

UPDATE-LOOP

If READ-TRANSACTION-FLAG = "true" then
Read a TRANSACTION record (from secondary storage to memory).

If READ-MASTER-FLAG = "true" then
Read an OLD-MASTER record (from secondary storage to memory).

 

* only master records remain

If no TRANSACTION record remains (TRANSACTION-EOF-FLAG = "True") then

(a) copy the OLD-MASTER record to the NEW-MASTER record

(b) write the NEW-MASTER record (from memory to secondary storage)

(c) copy (read and write) every remaining OLD-MASTER record to the NEW-MASTER file.

 

* update according to transaction record type

If the TRANSACTION-TYPE of the TRANSACTION record = "MODIFY" then perform modify-master-record.
If the TRANSACTION-TYPE of the TRANSACTION record = "DELETE" then perform delete-master-record.
If the TRANSACTION-TYPE of the TRANSACTION record = "INSERT" then perform insert-transaction-record.

 

MODIFY-MASTER-RECORD

* modify-condition a

If there is an OLD-MASTER file record (MASTER-EOF-FLAG = "false") and the key of the OLD-MASTER record is less than the key of the TRANSACTION record then no TRANSACTION applies to this master record.

(a) copy the OLD-MASTER record to the NEW-MASTER record

(b) write the NEW-MASTER record (from memory to secondary storage)

(c) move "false" to read-transaction-flag and "true" to read-master-flag

 

* modify-condition b

If no more OLD-MASTER file records exist (MASTER-EOF-FLAG = "True") or the key of the OLD-MASTER record is greater than the key of the TRANSACTION record then the modify TRANSACTION is for an nonexistent master record, and thus in error.

(a) write an appropriate error message to the ERROR file.

(b) move "true" to read-transaction-flag and "false" to read-master-flag

 

* modify-condition c

If the key of the OLD-MASTER file record matches the key of the TRANSACTION record, then we should modify the OLD-MASTER record's values.

(a) copy the OLD-MASTER record to the NEW-MASTER record

(b) modify the values in the NEW-MASTER record according to the TRANSACTION record

(c) write the NEW-MASTER record (from memory to secondary storage)

(d) move "true" to read-transaction-flag and "true" to read-master-flag

 

DELETE-MASTER-RECORD

* delete-condition a

If there is an OLD-MASTER file record (MASTER-EOF-FLAG = "false") and the key of the OLD-MASTER record is less than the key of the TRANSACTION record then no TRANSACTION applies to this master record.

(a) copy the OLD-MASTER record to the NEW-MASTER record

(b) write the NEW-MASTER record (from memory to secondary storage)

(c) move "false" to read-transaction-flag and "true" to read-master-flag

 

* delete-condition b

If no more OLD-MASTER file records exist (MASTER-EOF-FLAG = "True") or the key of the OLD-MASTER record is greater than the key of the TRANSACTION record then the delete TRANSACTION is for an nonexistent master record, and thus in error.

(a) write an appropriate error message to the ERROR file.

(b) move "true" to read-transaction-flag and "false" to read-master-flag

 

* delete-condition c

If the key of the OLD-MASTER file record matches the key of the TRANSACTION record, then we should delete the OLD-MASTER record by not copying it to the NEW-MASTER file.

(a) move "true" to read-transaction-flag and "true" to read-master-flag

 

INSERT-TRANSACTION-RECORD

* insert-condition a

If there is an OLD-MASTER file record (MASTER-EOF-FLAG = "false") and the key of the OLD-MASTER record is less than the key of the TRANSACTION record then no TRANSACTION applies to this master record.

(a) copy the OLD-MASTER record to the NEW-MASTER record

(b) write the NEW-MASTER record (from memory to secondary storage)

(c) move "false" to read-transaction-flag and "true" to read-master-flag

 

* insert-condition b

If no more OLD-MASTER file records exist (MASTER-EOF-FLAG = "True") or the key of the OLD-MASTER record is greater than the key of the TRANSACTION record then we insert the TRANSACTION record to the NEW-MASTER file.

(a) copy the TRANSACTION record to the NEW-MASTER record

(b) write the NEW-MASTER record (from memory to secondary storage)

(c) move "true" to read-transaction-flag and "false" to read-master-flag

 

* insert-condition c

If the key of the OLD-MASTER file record matches the key of the TRANSACTION record, then we're trying to insert a record that already exists, and thus have an error.

(a) write an appropriate error message to the ERROR file.

(b) move "true" to read-transaction-flag and "false" to read-master-flag


last update: 9/17/99

This page: http://web.njit.edu/~bieber/CIS365/file-update-algorithm.html