-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtxio.go
70 lines (58 loc) · 1.47 KB
/
txio.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package block_stm
import "encoding/base64"
const (
ReadKindMap = 0
ReadKindStorage = 1
)
type ReadDescriptor struct {
Path []byte
Kind int
V Version
}
type WriteDescriptor struct {
Path []byte
V Version
Val []byte
}
type TxnInput []ReadDescriptor
type TxnOutput []WriteDescriptor
// hasNewWrite: returns true if the current set has a new write compared to the input
func (txo TxnOutput) hasNewWrite(cmpSet []WriteDescriptor) bool {
if len(txo) == 0 {
return false
} else if len(cmpSet) == 0 || len(txo) > len(cmpSet) {
return true
}
cmpMap := map[string]bool{base64.StdEncoding.EncodeToString(cmpSet[0].Path): true}
for i := 1; i < len(cmpSet); i++ {
cmpMap[base64.StdEncoding.EncodeToString(cmpSet[i].Path)] = true
}
for _, v := range txo {
if !cmpMap[base64.StdEncoding.EncodeToString(v.Path)] {
return true
}
}
return false
}
type TxnInputOutput struct {
inputs []TxnInput
outputs []TxnOutput
}
func (io *TxnInputOutput) readSet(txnIdx int) []ReadDescriptor {
return io.inputs[txnIdx]
}
func (io *TxnInputOutput) writeSet(txnIdx int) []WriteDescriptor {
return io.outputs[txnIdx]
}
func MakeTxnInputOutput(numTx int) *TxnInputOutput {
return &TxnInputOutput{
inputs: make([]TxnInput, numTx),
outputs: make([]TxnOutput, numTx),
}
}
func (io *TxnInputOutput) recordRead(txId int, input []ReadDescriptor) {
io.inputs[txId] = input
}
func (io *TxnInputOutput) recordWrite(txId int, output []WriteDescriptor) {
io.outputs[txId] = output
}