BACKGROUND
In database terminology, a transaction is an atomic unit of work, a unit of
work that is either done completely or not done at all. For example, if a bank
application is transfering funds from one account to another, the balances of
two accounts must be updated simultaneously. If a software or hardware failure
were to cause the application to only finish updating one of them, the state of
the system would no longer be consistent. To avoid this problem, a set of
updates that moves the system from one valid state to another is grouped into a
transaction, and the entire transaction is either committed (accepted) or
rolled back (discarded).
PROBLEM STATEMENT
You are to emulate the operation of a simplified transactional database. The
simplfied database consists of 10 integral values (initially equal to 0) which
are refered to as rows 0 through 9 (inclusive). A sequence of commands will
modify the rows in a transactional manner, and you are to return the sum of the
rows in the final state of the database.
- The command "BEGIN" starts a transaction. Transactions can be nested. At any
point in time there is a "top" transaction which is the most recently started
transaction that has not yet been finished.
- The command "UPDATE row value" means that the value of the specified row
should be updated (the update may be undone by a following ROLLBACK).
- The command "COMMIT" means that the top transaction should be finished and
the updates during the top transaction accepted. If the top transaction was a
nested one, then the updates are accepted into the context of the new top
transaction.
- The command "ROLLBACK" means that the state of the database should be reset
to as it was before the beginning of the top transaction and the top
transaction should be finished.
Any changes that have not been committed or rolled back after all of the
commands have been processed should be included in the final result.
An UPDATE can only occur inside a transaction, and each COMMIT or ROLLBACK will
match with a BEGIN given earlier in the command set (although it is not
necessary for a BEGIN to have a matching COMMIT or ROLLBACK). TopCoder will
verify that the sequence of commands is valid, and that there are at between 0
and 20 commands (inclusive) in the input String[].
Create a class TransactionalDb with a method updateRows. updateRows will be
passed a String[] of commands, which are to be executed in order. It should
return the sum of the values of the 10 rows after all of the commands have been
executed.
DEFINITION
Class name: TransactionalDb
Method name: updateRows
Parameters: String[]
Returns: int
Here is the method signature (be sure your method is public):
updateRows(String[] cmds)
TopCoder will ensure the validity of the inputs. Inputs are valid if all of the
following criteria are met:
* cmds will contain between 0 and 20 elements, inclusive.
* each element of cmds will be of the form "BEGIN", "UPDATE <row> <value>",
"COMMIT", or "ROLLBACK", where <row> is a single numeric digit (0 to 9,
inclusive) and <value> is between 0 and 10000, inclusive.
* the sequence of commands is a valid sequence, according to the rules listed
above.
EXAMPLES
Consider the set of commands {"BEGIN", "UPDATE 0 100", "COMMIT", "BEGIN",
"UPDATE 0 200", "UPDATE 1 400", "ROLLBACK"}.
1. BEGIN - A new transaction is started.
2. UPDATE 0 100 - row[0] is set to 100.
3. COMMIT - The transaction is closed.
4. BEGIN - A new transaction is started.
5. UPDATE 0 200 - row[0] is set to 200.
6. UPDATE 1 400 - row[1] is set to 400.
7. ROLLBACK - The transaction is discarded, so the state of the rows reverts
back to as it was at the beginning of step 4.
The sum of the final row values in this example is 100, so your method should
return 100.
If the command set is {"BEGIN", "BEGIN", "UPDATE 4 100", "COMMIT", "COMMIT"},
the return value should be 100.
If the command set is {"BEGIN", "BEGIN", "UPDATE 4 100", "COMMIT", "ROLLBACK"},
the return value should be 0.
If the command set is {"BEGIN", "BEGIN", "UPDATE 4 100", "ROLLBACK", "COMMIT"},
the return value should be 0.
If the command set is {"BEGIN", "BEGIN", "UPDATE 4 100", "ROLLBACK",
"ROLLBACK"}, the return value should be 0.
If the command set is {"BEGIN", "UPDATE 1 100", "UPDATE 2 200"}, the return
value should be 300.
If the command set is {"BEGIN"}, the return value should be 0.
If the command set is {}, the return value should be 0.