Algorithm 4

19
Algorithm #4 CHANGE, ADD, and DELETE transactions; Several transactions per master record 1 Prepared by Perla P. Cosme

Transcript of Algorithm 4

Page 1: Algorithm  4

Algorithm #4

CHANGE, ADD, and DELETE transactions;

Several transactions per master record

1Prepared by Perla P. Cosme

Page 2: Algorithm  4

Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) { make a change in the master record output master record in new master file Get_Next_Trans Get_Next_Master } else { print “no matching record in the master file” Get_Next_Trans }

Recall Algorithm #1

2Prepared by Perla P. Cosme

Page 3: Algorithm  4

Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) { make a change in the master record // output master record in new master file Get_Next_Trans //Get_Next_Master } else { print “no matching record in the master file” Get_Next_Trans }

Recall Algorithm #2

3Prepared by Perla P. Cosme

Page 4: Algorithm  4

Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) {Case update_code ‘A’: print “duplicate record” Get_Next_Trans ‘C’: make a change in the master record Get_Next_Trans Default: print “invalid update code” Get_Next_Trans } else { // no matching record in the master file NOMATCH }

4Prepared by Perla P. Cosme

Recall Algorithm #3

Page 5: Algorithm  4

Think about this ….

If Algorithm #4 is to include DELETE transaction on the existing CHANGE and ADD transactions in Algorithm #3, then when are we allowed to DELETE a record?

a. Master key < transaction keyb.Master key = transaction keyc. Master key > transaction key

Prepared by Perla P. Cosme 5

Page 6: Algorithm  4

Think about this ….

If Algorithm #4 is to include DELETE transaction on the existing CHANGE and ADD transactions in Algorithm #3, then when are we allowed to DELETE a record?

a. Master key < transaction keyb.Master key = transaction keyc. Master key > transaction key

Prepared by Perla P. Cosme 6

Why (b)? Justify.

Page 7: Algorithm  4

Question: How should Algorithm #3 look like now to reflect the DELETION of a record when master key = transaction key?

Prepared by Perla P. Cosme 7

Page 8: Algorithm  4

Since it is when master key = transaction key that we are allowed to delete, then it is in the second case where we put our modification to Algorithm #3.

Prepared by Perla P. Cosme 8

Page 9: Algorithm  4

Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) {Case update_code ‘A’: print “duplicate record” Get_Next_Trans ‘C’: make a change in the master record Get_Next_Trans Default: print “invalid update code” Get_Next_Trans } else /* no matching record in the master file */

NOMATCH

9Prepared by Perla P. Cosme

Recall Algorithm #3

Page 10: Algorithm  4

Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) {Case update_code ‘A’: print “duplicate record” Get_Next_Trans ‘C’: make a change in the master record Get_Next_Trans ‘D’: Get_Next_Master

Get_Next_Trans Default: print “invalid update code” Get_Next_Trans } else /* no matching record in the master file */ NOMATCH

10Prepared by Perla P. Cosme

To DELETE, we simply skip writing such record into the new master file by getting the next master

record

Page 11: Algorithm  4

Question: Do we need to modify the section when master key > transaction key to adopt the requirements of Algorithm #4?

Answer: YES! (why?)

Prepared by Perla P. Cosme 11

Page 12: Algorithm  4

Follow up QuestionIf we are to modify NOMATCH, what will be our

guide?

Hints:1.Are we allowed to delete if master key >

transaction key?2.Are we allowed that a newly created (ADD

transaction) record maybe deleted?3. Is it possible that once a record has been deleted,

we can ADD it back?

Prepared by Perla P. Cosme 12

Page 13: Algorithm  4

Get_Next_MasterGet_Next_TransWhile NOT (master_key==sentinel AND trans_key==sentinel) if (master_key < trans_key) { output master record to new master file Get_Next_master } else if (master_key == trans_key) {Case update_code ‘A’: print “duplicate record” Get_Next_Trans ‘C’: make a change in the master record Get_Next_Trans ‘D’: Get_Next_Master

Get_Next_Trans Default: print “invalid update code” Get_Next_Trans } else /* no matching record in the master file */ NOMATCH

13Prepared by Perla P. Cosme

We shall MODIFY the function called NOMATCH

to handle the ADD transaction and the

succeeding ADD and/or CHANGE transactions on the same newly created

record.

Page 14: Algorithm  4

Function NOMATCHCase update_code ‘A’: Build new record from transaction record new_key = trans_key Get_Next_Trans while (trans_key != sentinel AND trans_key == new_key) Case update_code ‘A’: print “duplicate add” ‘C’: make change in the newly created record Default: print “invalid update code” Get_Next_Trans output new record to new master file ‘C’: print “no matching master record for transaction key” Get_Next_Trans Default: print “invalid update code” Get_Next_Trans

Prepared by Perla P. Cosme 14

This is NOMATCH of Algorithm #3

Page 15: Algorithm  4

Function NOMATCHCase update_code ‘A’: Build new record from transaction record new_key = trans_key

delete_rec = FALSE Get_Next_Trans while (trans_key != sentinel AND trans_key == new_key AND NOT(delete_rec)) Case update_code ‘A’: print “duplicate add” ‘C’: make change in the newly created record

‘D’: delete_rec = TRUE Default: print “invalid update code” Get_Next_Trans if NOT (delete_rec) output new record to new master file ‘C’, ‘D’: print “no matching master record for transaction key” Get_Next_Trans Default: print “invalid update code” Get_Next_Trans Prepared by Perla P. Cosme 15

This is now the modified version

of NOMATCH to fit to the

requirements of Algorithm #4

Page 16: Algorithm  4

Let’s simulate the SFO using Algorithm #4

We shall use the same example as what we presented previously.

16Prepared by Perla P. Cosme

Page 17: Algorithm  4

The Fourth AlgorithmCHANGE, ADD, and DELETE transactions;

several transactions per master record

MF 3 6 10 11

TF 1 3 11 11 11 11 18 18 18 18A C A C D A A C D C

17Prepared by Perla P. Cosme

Page 18: Algorithm  4

Questions? Comments?

Suggestions? and Open Forum

Prepared by Perla P. Cosme 18

????

Page 19: Algorithm  4

Coming up next …. Quiz (Chapter IV)

Prepared by Perla P. Cosme 19