Transactional Stack & Recursion

Bilgisayarımdan çıkan bir başka soru ile sizinleyim. Sorumuz şu şekilde, öyle bir stack implement etmek istiyorsunuz ki bu aynı zamanda transactional davranışlar göstersin. Diğer değişle (begin, commit, rollback) fonksiyonları içeren bir stack yazalım.

Hemen örneğe geçelim.

1
2
3
4
5
6
7
> yeni transaction aç
> 5 ekle
> yeni transaction aç
> 3 ekle
> 6 ekle
> transaction ı rollback et
> transactıon ı commit et

Bunu elle işletelim.

1
2
3
4
5
6
7
> yeni transaction aç -> []
> 5 ekle -> [5]
> yeni transaction aç -> [5, []]
> 3 ekle -> [5, [3]]
> 6 ekle -> [5, [3, 6]]
> transaction ı rollback et -> [5]
> transactıon ı commit et -> [5]

Python 2.7 ne kadar tipler belli olmasa da aşağıdakine benzer bir veri yapısı kullandım.

1
2
3
4
5
6
7
8
9
class TransactionalStack:
    content = [] # stack<Any>
    nested_tx = [] # stack<TransactionalStack>
    def push(self, value: Any): pass
    def pop(self): pass
    def top(self): pass
    def begin(self): pass
    def commit(self): pass
    def rollback(self): pass

Böylelikle istediğiniz kadar iç içe transaction kullanabilirsiniz. Kodun tamamını paylaşayım.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class TransactionalStack(object):
    def __init__(self, content = []):
        self.content = [content]
        self.nested_tx = [self]

    def begin(self):
        tx = TransactionalStack(self.content)
        self.nested_tx.append(tx)

    def push(self, value):
        self.nested_tx[-1].content.append(value)

    def pop(self):
        last_tx_values = self.nested_tx[-1].content
        return (last_tx_values.pop() if last_tx_values else 0)

    def top(self):
        last_tx_values = self.nested_tx[-1].content
        return (last_tx_values.pop() if last_tx_values else 0)

    def commit(self):
        last_tx = self.nested_tx[-1]
        if last_tx != self:
            last_tx = self.nested_tx.pop()
            self.nested_tx[-1].content = last_tx.content
            return True
        return False

    def rollback(self):
        last_tx = self.nested_tx[-1]
        if last_tx != self:
            self.nested_tx.pop()
            return True
        return False

ts = TransactionalStack()
ts.push(4)
ts.begin() # tx1 basladi
ts.push(7) # tx1 degerleri [4, [7]]
ts.begin() # tx2 basladi
ts.push(2) # tx2 degeleri [4, [7, [2]]]
print ts.rollback() # tx2 rolledback edildi [4, [7]]
print ts.top() # tx1' en ustteki degeri 7
ts.begin() # tx3 basladi
ts.push(10) # degerleri [4, [7, [10]]]
print ts.commit() # tx3 commit edildi
print ts.top() # tx3 un en ustteki degeri
print ts.rollback() # tx2 rollback edildi
print ts.top() # tx1 en ustteki degeri
print ts.commit() # false # acik transaction kalmadi.