17
Algorithm #3 CHANGE and ADD transactions; Several transactions per master record 1 Prepared by Perla P. Cosme

Algorithm 3

  • Upload
    gwsy93

  • View
    37

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Algorithm  3

Algorithm #3

CHANGE and ADD transactions; Several transactions per master

record

1Prepared by Perla P. Cosme

Page 2: 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) { 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  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) { 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  3

Think about this ….

If Algorithm #3 is to include ADD transaction on the existing CHANGE transaction in the Algorithm #2, then when are we allowed to ADD or create a record?

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

Prepared by Perla P. Cosme 4

Page 5: Algorithm  3

Think about this ….

If Algorithm #3 is to include ADD transaction on the existing CHANGE transaction in Algorithm #2, then when are we allowed to ADD or create a record?

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

Prepared by Perla P. Cosme 5

Why (c)? Justify.

Page 6: Algorithm  3

Justification why the Answer to the previous Question cannot be (a)

Let us prove by citing an example:MF: 6 7 8

TF 8 ANote: if master key (7) < transaction key (8), and the

transaction is ADD, you might add a record whose primary key is the same as the one you are about to create/add.

Prepared by Perla P. Cosme 6

Page 7: Algorithm  3

Justification why the Answer to the previous Question cannot be (b)

Let us show one typical example:MF: 3 6 11

TF: 3 3C A

Note: if master key (3) is equal to transaction key (3), you will be creating duplicate records in the master file. (The master file should never have duplicates.)

Prepared by Perla P. Cosme 7

Page 8: Algorithm  3

Question: How should Algorithm #2 look like now to reflect the error in adding a record when master key = transaction key?

Prepared by Perla P. Cosme 8

Page 9: 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 Default: print “invalid update code” Get_Next_Trans } else /* no matching record in the master file */ { print “no matching record in the master file” Get_Next_Trans }

9Prepared by Perla P. Cosme

Page 10: Algorithm  3

Justification why the Answer to the previous Question is (c)

• It is in the third case (i.e., master key > transaction key) where we are allowed to add a new record. Hence, it will be in such section of the algorithm where we are going to concentrate making the modifications to Algorithm #2 to fit to our new requirements – CHANGE and ADD transactions with multiple transactions per master record.

Prepared by Perla P. Cosme 10

Page 11: 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 Default: print “invalid update code” Get_Next_Trans } else { // no matching record in the master file print “no matching record in the master file” Get_Next_Trans }

11Prepared by Perla P. Cosme

This is the Algorithm #3 that explicitly

includes the error-handling routine

when master key is equal to transaction

key

Page 12: 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 Default: print “invalid update code” Get_Next_Trans } else { // no matching record in the master file print “no matching record in the master file” Get_Next_Trans }

12Prepared by Perla P. Cosme

These two (2) lines will still be

maintained but only when the update

code is C.

Page 13: Algorithm  3

We shall introduce a function called NOMATCH

to handle the ADD transaction and the

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

record.

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 }

13Prepared by Perla P. Cosme

Page 14: Algorithm  3

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

These are the lines that were retained from Algorithm #2.

Page 15: Algorithm  3

Let’s simulate the SFO using Algorithm #3

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

15Prepared by Perla P. Cosme

Page 16: Algorithm  3

The Third AlgorithmCHANGE and ADD transactions;

several transactions per master record

MF 3 6 10 11

TF 3 3 11 11 11 11 18C A A C C A A

16Prepared by Perla P. Cosme

Page 17: Algorithm  3

Questions? Comments?

Suggestions? and Open Forum

Prepared by Perla P. Cosme 17

????