forked from glycerine/offheap
/
somestruct_offheap_gen_test.go
462 lines (384 loc) · 11.3 KB
/
somestruct_offheap_gen_test.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/cheekybits/genny
package offheap
import (
"errors"
"fmt"
"hash/crc32"
"unsafe"
"github.com/remerge/offheap/util"
)
type EmptyStructSomeStruct struct{}
type HashTableMetadataSomeStruct struct {
MagicNumber uint64
ArraySize uint64
Population uint64
Checksum uint32
}
type HashTableCustomMetadataSomeStruct struct {
EmptyStructSomeStruct
}
type HashTableSomeStruct struct {
*HashTableMetadataSomeStruct
*HashTableCustomMetadataSomeStruct
zeroCell uintptr
cells uintptr
cellSize uintptr
offheap []byte
offheapCells []byte
mmm util.MmapMalloc
}
const MAGIC_NUMBERSomeStruct = 0x123456789ABCDEF
var minSizeSomeStruct int64 = int64(unsafe.Sizeof(HashTableMetadataSomeStruct{}) + unsafe.Sizeof(CellSomeStruct{}) + unsafe.Sizeof(HashTableCustomMetadataSomeStruct{}))
// Create a new hash table, able to hold initialSize count of keys.
func NewHashTableSomeStruct(initialSize uint64) *HashTableSomeStruct {
if initialSize == 0 {
initialSize = 1
}
h, _ := NewHashTableSomeStructFileBacked(initialSize, "")
return h
}
func OpenHashTableSomeStructFileBacked(filepath string) (*HashTableSomeStruct, error) {
// TODO - rework alllocHashTable and get rid of the mmap library as we do things twice
exists, size := util.FileInfo(filepath)
if exists && size < minSizeSomeStruct {
return nil, errors.New(fmt.Sprintf("mmaped file size is too small, file is damaged. file=%s", filepath))
}
h := allocHashTableSomeStructFileBacked(0, filepath)
// check metadata
if h.MagicNumber != MAGIC_NUMBERSomeStruct {
h.DestroyHashTable()
return nil, errors.New(fmt.Sprintf("mmaped file header is wrong. Magic number missing. file=%s", filepath))
}
sum := crc32.ChecksumIEEE(h.offheapCells)
if h.Checksum != sum {
// checksum error
wanted := h.Checksum
h.DestroyHashTable()
return nil, errors.New(fmt.Sprintf("mmaped file checksum is invalid. want=%d have=%d file=%s", wanted, sum, filepath))
}
return h, nil
}
func NewHashTableSomeStructFileBacked(initialSize uint64, filepath string) (*HashTableSomeStruct, error) {
// fresh
initialSize = util.UpperPowerOfTwo(initialSize)
h := allocHashTableSomeStructFileBacked(initialSize, filepath)
h.MagicNumber = MAGIC_NUMBERSomeStruct
h.ArraySize = initialSize
h.Population = 0
return h, nil
}
func allocHashTableSomeStructFileBacked(initialSize uint64, filepath string) *HashTableSomeStruct {
metaSize := unsafe.Sizeof(HashTableMetadataSomeStruct{})
cellSize := unsafe.Sizeof(CellSomeStruct{})
customSize := unsafe.Sizeof(HashTableCustomMetadataSomeStruct{})
var toAlloc int64 = -1
if initialSize > 0 {
toAlloc = int64(metaSize + customSize + uintptr(initialSize+1)*cellSize)
}
mmm := *util.Malloc(toAlloc, filepath)
baseP := unsafe.Pointer(&mmm.Mem[0])
base := (uintptr)(baseP)
zeroCell := base + metaSize + customSize
h := &HashTableSomeStruct{
cellSize: cellSize,
offheap: mmm.Mem,
offheapCells: mmm.Mem[metaSize:],
mmm: mmm,
zeroCell: zeroCell,
cells: zeroCell + cellSize,
}
// check metadata
h.HashTableMetadataSomeStruct = (*HashTableMetadataSomeStruct)(baseP)
h.HashTableCustomMetadataSomeStruct = (*HashTableCustomMetadataSomeStruct)((unsafe.Pointer)(base + metaSize))
return h
}
type CellSomeStruct struct {
unHashedKey uint64
Value SomeStruct
}
var _emptySomeStruct SomeStruct
// ZeroValue sets the cell's value to all zeros.
func (cell *CellSomeStruct) ZeroValue() {
*(&cell.Value) = _emptySomeStruct
}
func (t *HashTableSomeStruct) Bytes() []byte {
return t.offheap
}
func (t *HashTableSomeStruct) UpdateChecksum() {
t.Checksum = crc32.ChecksumIEEE(t.offheapCells)
}
// Save syncs the memory mapped file to disk using MmapMalloc::BlockUntilSync()
func (t *HashTableSomeStruct) Save() {
t.UpdateChecksum()
t.mmm.BlockUntilSync()
}
func (t *HashTableSomeStruct) cellAt(pos uint64) *CellSomeStruct {
return (*CellSomeStruct)(unsafe.Pointer(t.cells + uintptr(pos)*t.cellSize))
}
// DestroyHashTable frees the memory-mapping, returning the
// memory containing the hash table and its cells to the OS.
// By default the save-to-file-on-disk functionality in malloc.go is
// not used, but that can be easily activated. See malloc.go.
// Deferencing any cells/pointers into the hash table after
// destruction will result in crashing your process, almost surely.
func (t *HashTableSomeStruct) DestroyHashTable() {
t.mmm.Free()
}
// Lookup a cell based on a uint64 key value. Returns nil if key not found.
func (t *HashTableSomeStruct) Lookup(key uint64) *CellSomeStruct {
var cell *CellSomeStruct
if key == 0 {
if t.zero().unHashedKey == 1 {
return t.zero()
}
return nil
}
h := util.IntegerHash(uint64(key)) % t.ArraySize
for {
cell = t.cellAt(h)
if cell.unHashedKey == key {
return cell
}
if cell.unHashedKey == 0 {
return nil
}
h++
if h == t.ArraySize {
h = 0
}
}
}
// Insert a key and get back the CellSomeStruct for that key, so
// as to enable assignment of Value within that CellSomeStruct, for
// the specified key. The 2nd return value is false if
// key already existed (and thus required no addition); if
// the key already existed you can inspect the existing
// value in the *CellSomeStruct returned.
func (t *HashTableSomeStruct) zero() *CellSomeStruct {
return (*CellSomeStruct)(unsafe.Pointer(uintptr(t.zeroCell)))
}
func (t *HashTableSomeStruct) Insert(key uint64) (*CellSomeStruct, bool) {
var cell *CellSomeStruct
if key == 0 {
zc := t.zero()
// inuse
isNew := zc.unHashedKey == 0
if isNew {
t.Population++
zc.unHashedKey = 1
}
return zc, isNew
}
// if key != 0 {
for {
h := util.IntegerHash(uint64(key)) % t.ArraySize
for {
cell = t.cellAt(h)
if cell.unHashedKey == key {
// already exists
return cell, false
}
if cell.unHashedKey == 0 {
if (t.Population+1)*4 >= t.ArraySize*3 {
t.Repopulate(t.ArraySize * 2)
// resized, so start all over
break
}
t.Population++
cell.unHashedKey = key
return cell, true
}
h++
if h == t.ArraySize {
h = 0
}
}
}
}
// DeleteCellSomeStruct deletes the cell pointed to by cell.
func (t *HashTableSomeStruct) DeleteCellSomeStruct(cell *CellSomeStruct) {
pos := uint64((uintptr(unsafe.Pointer(cell)) - uintptr(unsafe.Pointer(t.cells))) / uintptr(unsafe.Sizeof(CellSomeStruct{})))
// Delete from regular cells
if pos < 0 || pos >= t.ArraySize {
panic(fmt.Sprintf("cell out of bounds: pos %v was < 0 or >= t.ArraySize == %v", pos, t.ArraySize))
}
if t.cellAt(pos).unHashedKey == 0 {
panic("zero unHashedKey in non-zero CellSomeStruct!")
}
// Remove this cell by shuffling neighboring cells so there are no gaps in anyone's probe chain
nei := pos + 1
if nei >= t.ArraySize {
nei = 0
}
var neighbor *CellSomeStruct
var circular_offset_ideal_pos int64
var circular_offset_ideal_nei int64
var cellPos *CellSomeStruct
for {
neighbor = t.cellAt(nei)
if neighbor.unHashedKey == 0 {
// There's nobody to swap with. Go ahead and clear this cell, then return
cellPos = t.cellAt(pos)
cellPos.unHashedKey = 0
cellPos.ZeroValue()
t.Population--
return
}
ideal := util.IntegerHash(neighbor.unHashedKey) % t.ArraySize
if pos >= ideal {
circular_offset_ideal_pos = int64(pos) - int64(ideal)
} else {
// pos < ideal, so pos - ideal is negative, wrap-around has happened.
circular_offset_ideal_pos = int64(t.ArraySize) - int64(ideal) + int64(pos)
}
if nei >= ideal {
circular_offset_ideal_nei = int64(nei) - int64(ideal)
} else {
// nei < ideal, so nei - ideal is negative, wrap-around has happened.
circular_offset_ideal_nei = int64(t.ArraySize) - int64(ideal) + int64(nei)
}
if circular_offset_ideal_pos < circular_offset_ideal_nei {
// Swap with neighbor, then make neighbor the new cell to remove.
*t.cellAt(pos) = *neighbor
pos = nei
}
nei++
if nei >= t.ArraySize {
nei = 0
}
}
}
// Clear does not resize the table, but zeroes-out all entries and the custom metadata
func (t *HashTableSomeStruct) Clear() {
// (Does not resize the array)
// Clear regular cells
for i := range t.offheapCells {
t.offheapCells[i] = 0
}
t.Population = 0
// Clear zero cell
zc := t.zero()
zc.unHashedKey = 0
zc.ZeroValue()
}
// Compact will compress the hashtable so that it is at most
// 75% full.
func (t *HashTableSomeStruct) Compact() {
t.Repopulate(util.UpperPowerOfTwo((t.Population*4 + 3) / 3))
}
// DeleteKey will delete the contents of the cell associated with key.
func (t *HashTableSomeStruct) DeleteKey(key uint64) {
if key == 0 {
zc := t.zero()
if zc.unHashedKey == 1 {
t.Population--
zc.unHashedKey = 0
zc.ZeroValue()
}
return
}
value := t.Lookup(key)
if value != nil {
t.DeleteCellSomeStruct(value)
}
}
// Repopulate expands the hashtable to the desiredSize count of cells.
func (t *HashTableSomeStruct) Repopulate(desiredSize uint64) {
if desiredSize&(desiredSize-1) != 0 {
panic("desired size must be a power of 2")
}
if t.Population*4 > desiredSize*3 {
panic("must have t.Population * 4 <= desiredSize * 3")
}
// Allocate new table
s := NewHashTableSomeStruct(desiredSize)
// copy custom metadata
*s.HashTableCustomMetadataSomeStruct = *t.HashTableCustomMetadataSomeStruct
if t.zero().unHashedKey == 1 {
zc := s.zero()
zc.Value = t.zero().Value
zc.unHashedKey = 1
s.Population++
}
// Iterate through old table t, copy into new table s.
var c *CellSomeStruct
for i := uint64(0); i < t.ArraySize; i++ {
c = t.cellAt(i)
if c.unHashedKey == 0 {
continue
}
// Insert this element into new table
cell, ok := s.Insert(c.unHashedKey)
if !ok {
panic(fmt.Sprintf("key@%d, '%v' already exists in fresh table s: should be impossible: %#v", i, c.unHashedKey, cell))
}
*cell = *c
}
t.DestroyHashTable()
*t = *s
}
/*
SomeStructIterator
sample use: given a HashTable h, enumerate h's contents with:
for it := offheap.NewIterator(h); it.Cur != nil; it.Next() {
found = append(found, it.Cur.unHashedKey)
}
*/
type SomeStructIterator struct {
H *HashTableSomeStruct
Pos int64
Cur *CellSomeStruct
}
var _zeroCellSomeStruct *CellSomeStruct
// NewIterator creates a new iterator for HashTable tab.
func (h *HashTableSomeStruct) NewIterator() *SomeStructIterator {
it := &SomeStructIterator{
H: h,
Cur: &CellSomeStruct{},
Pos: -1, // means we are at the ZeroCell to start with
}
if it.H.Population == 0 {
it.Cur = nil
it.Pos = -2
return it
}
zc := h.zero()
if zc.unHashedKey == 1 {
it.Cur = zc
} else {
it.Next()
}
return it
}
// Done checks to see if we have already iterated through all cells
// in the table. Equivalent to checking it.Cur == nil.
func (it *SomeStructIterator) Done() bool {
if it.Cur == nil {
return true
}
return false
}
// Next advances the iterator so that it.Cur points to the next
// filled cell in the table, and returns that cell. Returns nil
// once there are no more cells to be visited.
func (it *SomeStructIterator) Next() *CellSomeStruct {
// Already finished?
if it.Cur == nil {
return nil
}
// Iterate through the regular cells
it.Pos++
for uint64(it.Pos) != it.H.ArraySize {
it.Cur = it.H.cellAt(uint64(it.Pos))
if it.Cur.unHashedKey != 0 {
return it.Cur
}
it.Pos++
}
// Finished
it.Cur = nil
it.Pos = -2
return nil
}