-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcell.go
More file actions
87 lines (75 loc) · 1.88 KB
/
cell.go
File metadata and controls
87 lines (75 loc) · 1.88 KB
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
package balancer
import (
"sync"
"sync/atomic"
)
//cell contains information about load produced by data items,
//link to the cell group and information about relocated data to different from defined by SFC cells.
type cell struct {
mu sync.RWMutex
id uint64 //unique id of the cell
load *uint64
off map[string]uint64 // location of Relocated DataItem. DataItem.ID -> cell.ID
cg *CellGroup
}
//NewCell - allocates new instances of cell and attaches it to the cell group.
func NewCell(id uint64, cg *CellGroup) *cell {
c := cell{
id: id,
cg: cg,
load: new(uint64),
off: make(map[string]uint64),
}
if cg != nil {
cg.AddCell(&c)
}
return &c
}
//ID returns cell ID.
func (c *cell) ID() uint64 {
return c.id
}
//SetGroup sets the group of the cell
func (c *cell) SetGroup(cg *CellGroup) {
c.mu.Lock()
defer c.mu.Unlock()
c.cg = cg
}
//Group returns the group to which the cell attached
func (c *cell) Group() *CellGroup {
c.mu.RLock()
defer c.mu.RUnlock()
return c.cg
}
//Load return load of cell
func (c *cell) Load() (load uint64) {
return atomic.LoadUint64(c.load)
}
//Truncate - emptifies load of the cell
func (c *cell) Truncate() {
atomic.StoreUint64(c.load, 0)
}
//AddLoad increase load of the cell
func (c *cell) AddLoad(l uint64) {
atomic.AddUint64(c.load, l)
}
//Remove removes the DataItem from the cell
func (c *cell) RemoveLoad(l uint64) {
//TODO: overflow check
atomic.AddUint64(c.load, ^(l - 1))
}
//Relocate sets the sprecified DataItem as moved and store
//index of the new cell
func (c *cell) Relocate(d DataItem, ncID uint64) {
c.mu.Lock()
defer c.mu.Unlock()
c.off[d.ID()] = ncID
}
//Relocated returns the id of the cell to which DataItem was relocated
//if DataItem was relocated it also returns the true otherwise false
func (c *cell) Relocated(did string) (uint64, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
cid, ok := c.off[did]
return cid, ok
}