|
| 1 | +package ood.design; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Stack; |
| 6 | + |
| 7 | +public class Transactions { |
| 8 | + |
| 9 | + private static class Transaction { |
| 10 | + Map<String, Integer> store; |
| 11 | + Map<String, Integer> originalStore; |
| 12 | + |
| 13 | + Transaction(Map<String, Integer> store, Map<String, Integer> originalStore) { |
| 14 | + this.store = store; |
| 15 | + this.originalStore = originalStore; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + private Map<String, Integer> store = new HashMap<>(); |
| 20 | + private Map<String, Integer> originalStore; |
| 21 | + private Stack<Transaction> transactions = new Stack<>(); |
| 22 | + |
| 23 | + public void set(String key, int value) { |
| 24 | + if (!transactions.empty()) { |
| 25 | + transactions.peek().store.put(key, value); |
| 26 | + } else { |
| 27 | + store.put(key, value); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public int get(String key) { |
| 32 | + if (!transactions.empty()) { |
| 33 | + Map<String, Integer> transaction = transactions.peek().store; |
| 34 | + if (transaction.containsKey(key)) { |
| 35 | + return transaction.get(key); |
| 36 | + } |
| 37 | + } |
| 38 | + return store.get(key); |
| 39 | + } |
| 40 | + |
| 41 | + public void delete(String key) { |
| 42 | + if (!transactions.empty()) { |
| 43 | + transactions.peek().store.remove(key); |
| 44 | + } else { |
| 45 | + store.remove(key); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public void begin() { |
| 50 | + Map<String, Integer> storeCopy; |
| 51 | + if(!transactions.isEmpty()){ |
| 52 | + storeCopy = new HashMap<>(transactions.peek().store); |
| 53 | + transactions.peek().originalStore = new HashMap<>(transactions.peek().store); |
| 54 | + } else { |
| 55 | + this.originalStore = new HashMap<>(store); |
| 56 | + storeCopy = new HashMap<>(store); |
| 57 | + } |
| 58 | + transactions.push(new Transaction(new HashMap<>(storeCopy), new HashMap<>(storeCopy))); |
| 59 | + } |
| 60 | + |
| 61 | + public void commit() { |
| 62 | + if (transactions.empty()) { |
| 63 | + System.out.println("No transaction to commit"); |
| 64 | + return; |
| 65 | + } |
| 66 | + Transaction currentTransaction = transactions.pop(); |
| 67 | + if (!transactions.empty()) { |
| 68 | + transactions.peek().store = currentTransaction.store; |
| 69 | + } else this.store = currentTransaction.store; |
| 70 | + } |
| 71 | + |
| 72 | + public void rollback() { |
| 73 | + if (transactions.empty()) { |
| 74 | + this.store = this.originalStore; |
| 75 | + return; |
| 76 | + } else { |
| 77 | + transactions.peek().store = transactions.peek().originalStore; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args){ |
| 82 | + Transactions transactions = new Transactions(); |
| 83 | + transactions.set("jivan",1); |
| 84 | + transactions.set("jivan",2); |
| 85 | + |
| 86 | + transactions.begin(); |
| 87 | + transactions.set("jivan",3); |
| 88 | + System.out.println("starting n2"); |
| 89 | + transactions.begin(); |
| 90 | + transactions.set("jivan",4); |
| 91 | + transactions.begin(); |
| 92 | + transactions.set("jivan",5); |
| 93 | + transactions.commit(); |
| 94 | + transactions.rollback(); |
| 95 | + transactions.set("jivan",6); |
| 96 | + transactions.commit(); |
| 97 | + transactions.rollback(); |
| 98 | + transactions.commit(); |
| 99 | + System.out.println(transactions.get("jivan")); |
| 100 | + |
| 101 | + } |
| 102 | +} |
0 commit comments