Algorithm 4
-
Upload
giordan-sy -
Category
Self Improvement
-
view
182 -
download
0
Transcript of Algorithm 4
Algorithm #4
CHANGE, ADD, and DELETE transactions;
Several transactions per master record
1Prepared by Perla P. Cosme
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
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
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
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
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.
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
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
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
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
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
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
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.
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
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
Let’s simulate the SFO using Algorithm #4
We shall use the same example as what we presented previously.
16Prepared by Perla P. Cosme
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
Questions? Comments?
Suggestions? and Open Forum
Prepared by Perla P. Cosme 18
????
Coming up next …. Quiz (Chapter IV)
Prepared by Perla P. Cosme 19