-
Notifications
You must be signed in to change notification settings - Fork 4
/
state.go
109 lines (87 loc) · 1.81 KB
/
state.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package state
import (
"sync"
//"fmt"
//"code.google.com/p/leveldb-go/leveldb"
//"encoding/binary"
)
type Operation uint8
//type OperationId int32
const (
NONE Operation = iota
PUT
GET
DELETE
RLOCK
WLOCK
)
type Value int64
const NIL Value = 0
type Key int64
type Command struct {
ClientId uint32
OpId int32
Op Operation
K Key
V Value
}
type State struct {
mutex *sync.Mutex
Store map[Key]Value
//DB *leveldb.DB
}
func InitState() *State {
/*
d, err := leveldb.Open("/Users/iulian/git/epaxos-batching/dpaxos/bin/db", nil)
if err != nil {
fmt.Printf("Leveldb open failed: %v\n", err)
}
return &State{d}
*/
//return &State{new(sync.Mutex), make(map[Key]Value)}
return &State{new(sync.Mutex), make(map[Key]Value, 100000)}
//return &State{new(sync.Mutex), make(map[Key]Value, 10000000)}
//return &State{new(sync.Mutex), make(map[Key]Value, 100000000)}
}
func Conflict(gamma *Command, delta *Command) bool {
if gamma.K == delta.K {
if gamma.Op == PUT || delta.Op == PUT {
return true
}
}
return false
}
func ConflictBatch(batch1 []Command, batch2 []Command) bool {
for i := 0; i < len(batch1); i++ {
for j := 0; j < len(batch2); j++ {
if Conflict(&batch1[i], &batch2[j]) {
return true
}
}
}
return false
}
func IsRead(command *Command) bool {
return command.Op == GET
}
func (c *Command) Execute(st *State) Value {
//fmt.Printf("Executing (%d, %d)\n", c.K, c.V)
//var key, value [8]byte
// st.mutex.Lock()
// defer st.mutex.Unlock()
switch c.Op {
case PUT:
/*
binary.LittleEndian.PutUint64(key[:], uint64(c.K))
binary.LittleEndian.PutUint64(value[:], uint64(c.V))
st.DB.Set(key[:], value[:], nil)
*/
st.Store[c.K] = c.V
return c.V
case GET:
if val, present := st.Store[c.K]; present {
return val
}
}
return NIL
}