Вы находитесь на странице: 1из 19

Algorithm #4

CHANGE, ADD, and DELETE


transactions;
Several transactions per master
record

Prepared by Perla P. Cosme 1


Recall Algorithm #1
Get_Next_Master
Get_Next_Trans
While 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
}

Prepared by Perla P. Cosme 2


Recall Algorithm #2
Get_Next_Master
Get_Next_Trans
While 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
}

Prepared by Perla P. Cosme 3


Recall Algorithm #3
Get_Next_Master
Get_Next_Trans
While 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
}

Prepared by Perla P. Cosme 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 key
b.Master key = transaction key
c.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 key
b.Master key = transaction key
c.Master key > transaction key
Why (b)? Justify.
Prepared by Perla P. Cosme 6
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


Recall Algorithm
Get_Next_Master
#3
Get_Next_Trans
While 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

Prepared by Perla P. Cosme 9


Get_Next_Master
Get_Next_Trans
While NOT (master_key==sentinel AND trans_key==sentinel)
if (master_key < trans_key)
{ output master record to new master file
Get_Next_master To DELETE, we
} simply skip writing
else if (master_key == trans_key) such record into the
{Case update_code new master file by
‘A’: print “duplicate record” getting the next
Get_Next_Trans master record
‘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

Prepared by Perla P. Cosme 10


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 Question
If 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_Master
Get_Next_Trans
While 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
We shall MODIFY the
Get_Next_Trans
function called
Default: print “invalid update code”
NOMATCH to handle
Get_Next_Trans
the ADD transaction
}
and the succeeding
else /* no matching record in the master file */
ADD and/or CHANGE
NOMATCH
transactions on the
same newly created
Prepared by Perla P. Cosme record. 13
This is
Function NOMATCH NOMATCH of
Case update_code Algorithm #3
‘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 now the
Case update_code
Function NOMATCH modified
version of
‘A’: Build new record from transaction record NOMATCH to fit
new_key = trans_key to the
delete_rec = FALSE requirements
Get_Next_Trans of Algorithm
while (trans_key != sentinel AND trans_key == new_key AND #4
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
Let’s simulate the SFO using
Algorithm #4

We shall use the same example as


what we presented previously.

Prepared by Perla P. Cosme 16


The Fourth Algorithm
CHANGE, ADD, and DELETE transactions;
several transactions per master record

MF 3 6 10 11

TF 1 3 11 11 11 11 18 18 18 18

A C A C D A A C D C
Prepared by Perla P. Cosme 17
????
Questions?
Comments?
Suggestions?  and 
Open Forum

Prepared by Perla P. Cosme 18


Coming up next …. Quiz (Chapter IV)

Prepared by Perla P. Cosme 19

Вам также может понравиться