-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgravity.go
210 lines (180 loc) · 5.2 KB
/
gravity.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package gravity
import (
"encoding/binary"
"errors"
"fmt"
"ohalloc/treap"
"sync"
"sync/atomic"
)
type Gravity struct {
sync.RWMutex
mem []byte // Entire mem in bytes
fsm *freeSpaceManager // Manages the free space
size uint64 // Total size of memory (same as len(mem))
key uint64 // Unique key for each data
vmap *vmap // Stores key to position of data
}
const (
headerLen = uint64(8) // length of header to store the size of data
keyLen = uint64(8) // Size of key for the data
)
var (
WrongReadPosition = errors.New("wrong read position")
)
func NewGravity(mem []byte) (*Gravity, error) {
size := uint64(len(mem))
if size <= headerLen+keyLen {
return nil, errors.New("input byte too small")
}
g := &Gravity{
mem: mem,
fsm: newFSM(),
size: size,
vmap: newShardedStore(),
key: uint64(1),
}
err := g.fsm.add(&treap.FreeSpace{Start: 0, End: size - 1})
return g, err
}
// getKey increments the key atomically and returns the value
func (g *Gravity) getKey() uint64 {
return atomic.AddUint64(&g.key, 1)
}
// Write adds data to the memory and returns a key.
// The key acts as a reference to read the data
func (g *Gravity) Write(data []byte) (key uint64, err error) {
g.Lock()
defer g.Unlock()
key = g.getKey()
err = g.write(key, data)
return
}
func (g *Gravity) write(k uint64, data []byte) error {
// get data size
dl := uint64(len(data))
totalLen := headerLen + keyLen + dl
// try to fetch freespace for size
fss, err := g.fsm.poolExtract(totalLen)
if err != nil {
return err
}
// merge all freespaces to satisfy the data size
fs := g.merge(fss)
// remember to put the freespace back to the pool
defer func() {
err := g.fsm.poolPut(fs)
if err == illegalPoolPut {
panic(fmt.Sprintf("Error while adding to pool: %v \n", err))
}
}()
// write to the memory
npos := fs.Start
err = g.writeAt(npos, data, k)
if err != nil {
return err
}
// store virtual position
g.vmap.store(k, npos)
fs.Start += totalLen
return nil
}
// Reads the value stored in the position corresponding to the key
func (g *Gravity) Read(key uint64) ([]byte, error) {
g.RLock()
defer g.RUnlock()
pos, err := g.loadFromVPos(key)
if err != nil {
return nil, err
}
if pos >= g.size {
return nil, WrongReadPosition
}
dl := binary.LittleEndian.Uint64(g.mem[pos : pos+headerLen])
pos += headerLen + keyLen
b := make([]byte, dl)
n := copy(b, g.mem[pos:pos+dl])
if n != int(dl) {
return nil, errors.New(fmt.Sprintf("expected to write %v but wrote %v ", dl, n))
}
return b, nil
}
// Frees the memory held by the data pointed by key
func (g *Gravity) Free(key uint64) error {
g.Lock()
pos, err := g.loadAndDeleteFromVPos(key)
if err != nil {
g.Unlock()
return err
}
dl := binary.LittleEndian.Uint64(g.mem[pos : pos+headerLen])
g.Unlock()
totalDataSize := headerLen + keyLen + dl
return g.fsm.add(&treap.FreeSpace{Start: pos, End: pos + totalDataSize - 1})
}
// TotalFreeSpace indicates the remaining free space available
func (g *Gravity) TotalFreeSpace() uint64 {
return g.fsm.totalFreeSpaceSize()
}
// merge joins multiple freespaces to form a single large free space. i.e, all freespaces shifted to the right
// by moving the data to the left
func (g *Gravity) merge(fss []*treap.FreeSpace) *treap.FreeSpace {
// obtain lock as data is moved and vmap is updated
for i := 0; i < len(fss)-1; i++ {
fs := fss[i]
nfs := fss[i+1]
// eg. f[2-8]--d[9-12]--f[13-17]
// after shifting d[9-12] to the left
// d[2-5]-f[6-17] : Here 6 is destEnd
destEnd := fs.Start + (nfs.Start - fs.End - 1) // start of free slot + size of the data
g.readAndShift(fs.End+1, nfs.Start, fs.Start, destEnd)
nfs.Start = destEnd
}
return fss[len(fss)-1]
}
// Writes the data at given position and returns the key
func (g *Gravity) writeAt(pos uint64, data []byte, k uint64) error {
dl := uint64(len(data))
binary.LittleEndian.PutUint64(g.mem[pos:pos+headerLen], dl)
pos += headerLen
binary.LittleEndian.PutUint64(g.mem[pos:pos+keyLen], k)
pos += keyLen
n := copy(g.mem[pos:pos+dl], data)
if n != len(data) {
return errors.New(fmt.Sprintf("expected to write %v but wrote %v ", dl, n))
}
return nil
}
// readAndShift reads all the data from srcStart:srcEnd and moves them to dstStart:dstEnd
func (g *Gravity) readAndShift(srcStart uint64, srcEnd uint64, dstStart uint64, dstEnd uint64) {
// update existing data pos
runningDataLength := uint64(0)
start := srcStart
for start < srcEnd {
if start+headerLen+keyLen > g.size {
panic("Trying to move src beyond size")
}
dl := binary.LittleEndian.Uint64(g.mem[start : start+headerLen])
// rewire key position
key := binary.LittleEndian.Uint64(g.mem[start+headerLen : start+headerLen+keyLen])
g.vmap.store(key, dstStart+runningDataLength)
currentLen := dl + headerLen + keyLen
runningDataLength += currentLen
start += currentLen
}
copy(g.mem[dstStart:dstEnd], g.mem[srcStart:srcEnd])
}
func (g *Gravity) loadFromVPos(key uint64) (uint64, error) {
pos, ok := g.vmap.load(key)
if !ok {
return 0, WrongReadPosition
}
return pos, nil
}
func (g *Gravity) loadAndDeleteFromVPos(key uint64) (uint64, error) {
pos, ok := g.vmap.loadAndDelete(key)
if !ok {
return 0, WrongReadPosition
}
return pos, nil
}