-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.go
195 lines (157 loc) · 3.87 KB
/
board.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cube
import (
"github.com/bahadrix/cardinalitycube/cube/pb"
"sync"
)
// Board is a table like data structure which consists of rows.
// It is of course thread safe.
type Board struct {
cube *Cube
rowMap map[string]*Row
rowLock sync.RWMutex
cellLock sync.Mutex
}
// A BoardSnapshot contains rows data of specific time
type BoardSnapshot map[string]*RowSnapshot
// NewBoard creates a new board for given cube
func NewBoard(cube *Cube) *Board {
return &Board{
cube: cube,
rowMap: make(map[string]*Row),
}
}
// GetCell returns cell that resides in given row.
// If row or cell not found function returns nil
func (b *Board) GetCell(rowName string, cellName string, createIfNotExists bool) *Cell {
var cell *Cell
b.rowLock.RLock()
row, _ := b.rowMap[rowName]
b.rowLock.RUnlock()
if row == nil {
if !createIfNotExists {
return nil
}
b.rowLock.Lock() // row sync in ----
row, _ = b.rowMap[rowName]
if row == nil {
row = NewRow()
b.rowMap[rowName] = row
}
b.rowLock.Unlock() // row sync out ---
}
cell = row.GetCell(cellName)
if cell == nil && createIfNotExists {
b.cellLock.Lock() // cell sync in ----
cell = row.GetCell(cellName)
if cell == nil {
cell = b.cube.generateCell()
row.SetCell(cellName, cell)
}
b.cellLock.Unlock() // cell sync out ---
}
return cell
}
// GetRowSnapshot Returns snapshot of given row.
// Blocks row while getting its snapshot
func (b *Board) GetRowSnapshot(rowName string) *RowSnapshot {
b.rowLock.RLock()
row, _ := b.rowMap[rowName]
b.rowLock.RUnlock()
if row == nil {
return nil
}
return row.GetSnapshot()
}
// GetSnapshot return board's snapshot.
// Blocks whole board while getting snapshot.
func (b *Board) GetSnapshot() *BoardSnapshot {
ss := make(BoardSnapshot)
b.rowLock.RLock()
for key, row := range b.rowMap {
ss[key] = row.GetSnapshot()
}
b.rowLock.RUnlock()
return &ss
}
// CheckRowExists return true if row exists in board.
func (b *Board) CheckRowExists(rowName string) bool {
b.rowLock.RLock()
_, exists := b.rowMap[rowName]
b.rowLock.RUnlock()
return exists
}
// DropRow drops given row from board if it exists
func (b *Board) DropRow(rowName string) {
b.rowLock.Lock()
_, rowExists := b.rowMap[rowName]
if rowExists {
delete(b.rowMap, rowName)
}
b.rowLock.Unlock()
}
// GetRowKeys returns row names. Read blocking operation.
func (b *Board) GetRowKeys() []string {
b.rowLock.RLock()
keys := make([]string, 0, len(b.rowMap))
for key := range b.rowMap {
keys = append(keys, key)
}
b.rowLock.RUnlock()
return keys
}
// GetRowCount returns roe count.
func (b *Board) GetRowCount() int {
return len(b.rowMap)
}
// GetCellKeys returns cell keys of row. Read blocking operation.
func (b *Board) GetCellKeys(rowName string) (keys []string) {
b.rowLock.RLock()
r, rowExists := b.rowMap[rowName]
b.rowLock.RUnlock()
if !rowExists {
return
}
return r.GetCellKeys()
}
// GetCellCount returns cell count of row. Read blocking operation.
func (b *Board) GetCellCount(rowName string) int {
b.rowLock.RLock()
r, rowExists := b.rowMap[rowName]
b.rowLock.RUnlock()
if !rowExists {
return 0
}
return r.GetCellCount()
}
func (b *Board) Dump() (*pb.BoardData, error) {
b.rowLock.RLock()
defer b.rowLock.RUnlock()
dataMap := make(map[string]*pb.RowData, len(b.rowMap))
var err error
for k, r := range b.rowMap {
dataMap[k], err = r.Dump()
if err != nil {
return nil, err
}
}
return &pb.BoardData{RowMap:dataMap}, err
}
func (b *Board) LoadData(data *pb.BoardData) error {
b.rowLock.Lock()
defer b.rowLock.Unlock()
for rowName, rowData := range data.RowMap {
row, rowExists := b.rowMap[rowName]
if !rowExists {
row = NewRow()
b.rowMap[rowName] = row
}
for cellName, cellData := range rowData.CellMap {
cell, err := b.cube.deserializeCell(cellData.CoreData)
if err != nil {
return err
}
row.SetCell(cellName, cell)
}
}
return nil
}