|
| 1 | +package manager |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + s "github.com/null-char/transact/store" |
| 7 | +) |
| 8 | + |
| 9 | +// Transaction consists of a next pointer which points to the next transaction in the stack |
| 10 | +// (nil if current Transaction is at the top) and a local store which resembles the data that needs to be updated |
| 11 | +// into our global store if the transaction is committed. |
| 12 | +type Transaction struct { |
| 13 | + localStore *s.Store |
| 14 | + parent *Transaction |
| 15 | +} |
| 16 | + |
| 17 | +// TransactionManager manages the transaction stack |
| 18 | +type TransactionManager struct { |
| 19 | + globalStore *s.Store |
| 20 | + transactions *Transaction |
| 21 | + activeTransaction *Transaction // represents top of the stack |
| 22 | + numTransactions uint |
| 23 | +} |
| 24 | + |
| 25 | +// Empty returns a bool indicating whether or not the transaction stack is empty (no ongoing transactions) |
| 26 | +func (tm TransactionManager) Empty() bool { |
| 27 | + return tm.transactions == nil |
| 28 | +} |
| 29 | + |
| 30 | +// getActiveStore returns the global store if there are no active transactions and the local store (of the active transaction) if there is one |
| 31 | +func (tm TransactionManager) getActiveStore() *s.Store { |
| 32 | + if tm.Empty() { |
| 33 | + return tm.globalStore |
| 34 | + } |
| 35 | + |
| 36 | + return tm.activeTransaction.localStore |
| 37 | +} |
| 38 | + |
| 39 | +// PushTransaction pushes a new transaction onto the stack inheriting the local store of its parent (if it has a parent) |
| 40 | +func (tm *TransactionManager) PushTransaction() { |
| 41 | + if tm.Empty() { |
| 42 | + t := &Transaction{localStore: s.MakeNewStore(), parent: nil} |
| 43 | + tm.transactions = t |
| 44 | + tm.activeTransaction = t |
| 45 | + } else { |
| 46 | + store := s.MakeNewStore() |
| 47 | + |
| 48 | + // We duplicate the data of our parent here |
| 49 | + for _, pair := range tm.activeTransaction.localStore.GetKVPairs() { |
| 50 | + store.Set(pair.First, pair.Second) |
| 51 | + } |
| 52 | + |
| 53 | + t := &Transaction{localStore: store} |
| 54 | + t.parent = tm.activeTransaction |
| 55 | + tm.activeTransaction = t |
| 56 | + } |
| 57 | + |
| 58 | + tm.numTransactions++ |
| 59 | +} |
| 60 | + |
| 61 | +// PopTransaction pops the active transaction (top of the stack) if there is one otherwise we have a stack underflow so we just log some error |
| 62 | +func (tm *TransactionManager) PopTransaction() { |
| 63 | + if tm.Empty() { |
| 64 | + fmt.Println("ERROR: There are no transactions to pop. Stack is empty.") |
| 65 | + } else { |
| 66 | + tm.activeTransaction = tm.activeTransaction.parent |
| 67 | + tm.numTransactions-- |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +func (tm *TransactionManager) Set(key string, value s.Mappable) { |
| 73 | + s := tm.getActiveStore() |
| 74 | + s.Set(key, value) |
| 75 | +} |
| 76 | + |
| 77 | +func (tm *TransactionManager) Get(key string) (s.Mappable, bool) { |
| 78 | + s := tm.getActiveStore() |
| 79 | + return s.Get(key) |
| 80 | +} |
0 commit comments