/
block.go
319 lines (285 loc) · 7.88 KB
/
block.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
package wap
import (
"github.com/moontrade/nogc"
"io"
"unsafe"
)
type _slice struct {
Data uintptr
Len int
Cap int
}
type RecordHeader struct {
id int64
timestamp int64
start int64
end int64
}
type BlockHeader struct {
streamID uint64
id uint64
headID uint64
headStart int64
blocks uint64
records uint64
storage uint64
storageU uint64
created int64
completed int64
start int64
end int64
min uint64
max uint64
count uint16
size uint16
sizeU uint16
sizeX uint16
record uint16
block BlockSize
encoding Encoding
layout BlockLayout
kind StreamKind
format Format
data struct{}
}
type BlockHeaderMut struct {
BlockHeader
}
type FixedBlockHeaderMut struct {
BlockHeaderMut
}
func (self *BlockHeader) Offset(offset uintptr) unsafe.Pointer {
return nogc.Pointer(uintptr(unsafe.Pointer(&self.data)) + offset).Unsafe()
}
func (self *FixedBlockHeaderMut) Append(blockSize BlockSize, data unsafe.Pointer, size uintptr) error {
self.block = blockSize
if uintptr(self.size)+size > uintptr(self.block) {
return io.ErrShortWrite
}
dst := unsafe.Add(unsafe.Pointer(&self.data), self.size)
self.count += 1
self.size += uint16(size)
self.sizeU = self.size
self.record = uint16(size)
nogc.Copy(dst, data, size)
return nil
}
func (self *BlockHeaderMut) Append(blockSize BlockSize, data unsafe.Pointer, size uintptr) error {
self.block = blockSize
if uintptr(self.size)+size+4 > uintptr(self.block) {
return io.ErrShortWrite
}
dst := unsafe.Add(unsafe.Pointer(&self.data), self.size)
self.count += 1
self.size += uint16(size) + 4
self.sizeU = self.size
*(*uint16)(dst) = uint16(size)
nogc.Copy(unsafe.Add(dst, 2), data, size)
*(*uint16)(unsafe.Add(dst, 2+size)) = uint16(size)
return nil
}
func (self *BlockHeader) Count() uint16 {
return self.count
}
func (self *BlockHeaderMut) AddCount(value uint16) {
self.count += value
}
func (self *BlockHeaderMut) SetCount(count uint16) {
self.count = count
}
func (self *BlockHeader) Size() uint16 {
return self.size
}
func (self *BlockHeaderMut) SetSize(value uint16) {
self.size = value
}
func (self *BlockHeaderMut) SetRecord(value uint16) {
self.record = value
}
func (self *BlockHeader) SizeU() uint16 {
return self.sizeU
}
func (self *BlockHeaderMut) SetSizeU(value uint16) {
self.sizeU = value
}
func (self *BlockHeader) BlockSize() BlockSize {
return self.block
}
func (self *BlockHeaderMut) SetBlockSize(value BlockSize) {
self.block = value
}
func (self *BlockHeader) Layout() BlockLayout {
return self.layout
}
func (self *BlockHeaderMut) SetLayout(value BlockLayout) {
self.layout = value
}
type FixedBlock struct {
BlockHeader
}
func (h *FixedBlock) Record(index uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(unsafe.Pointer(&h.data)) + (index * uintptr(h.record)))
}
func (h *FixedBlock) Reader() *FixedBlockReader {
return &FixedBlockReader{
header: &h.BlockHeader,
index: 0,
record: int(h.record),
}
}
type FixedBlockMut struct {
BlockHeader
}
func (h *FixedBlockMut) Record(index uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(unsafe.Pointer(&h.data)) + (index * uintptr(h.record)))
}
type ColumnBlock struct {
FixedBlock
}
func (h *ColumnBlock) Slice(max, offset uintptr) unsafe.Pointer {
return unsafe.Pointer(&_slice{
Data: uintptr(unsafe.Pointer(&h.data)) + (offset * max),
Len: int(h.count),
Cap: int(max),
})
}
func (h *ColumnBlock) Item(max, offset, size, index uintptr) unsafe.Pointer {
return unsafe.Pointer(
uintptr(unsafe.Pointer(&h.data)) +
(offset * max) +
(size * index))
}
//func (h *ColumnBlock) I8(max, offset, size, index uintptr) int8 {
// return *(*int8)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) I8Column(max, offset uintptr) []int8 {
// return *(*[]int8)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) U8(max, offset, size, index uintptr) uint8 {
// return *(*uint8)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) U8Column(max, offset uintptr) []uint8 {
// return *(*[]uint8)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) I16(max, offset, size, index uintptr) int16 {
// return *(*int16)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) I16Column(max, offset uintptr) []int16 {
// return *(*[]int16)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) U16(max, offset, size, index uintptr) uint16 {
// return *(*uint16)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) U16Column(max, offset uintptr) []uint16 {
// return *(*[]uint16)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) I32(max, offset, size, index uintptr) int32 {
// return *(*int32)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) I32Column(max, offset uintptr) []int32 {
// return *(*[]int32)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) U32(max, offset, size, index uintptr) uint32 {
// return *(*uint32)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) U32Column(max, offset uintptr) []uint32 {
// return *(*[]uint32)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) I64(max, offset, size, index uintptr) int64 {
// return *(*int64)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) I64Column(max, offset uintptr) []int64 {
// return *(*[]int64)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) U64(max, offset, size, index uintptr) uint64 {
// return *(*uint64)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) U64Column(max, offset uintptr) []uint64 {
// return *(*[]uint64)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) F32(max, offset, size, index uintptr) float32 {
// return *(*float32)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) F32Column(max, offset uintptr) []float32 {
// return *(*[]float32)(h.Slice(max, offset))
//}
//
//func (h *ColumnBlock) F64(max, offset, size, index uintptr) float64 {
// return *(*float64)(h.Item(max, offset, size, index))
//}
//
//func (h *ColumnBlock) F64Column(max, offset uintptr) []float64 {
// return *(*[]float64)(h.Slice(max, offset))
//}
type ColumnBlockMut struct {
FixedBlockHeaderMut
}
func (h *ColumnBlockMut) Slice(max, offset uintptr) unsafe.Pointer {
return unsafe.Pointer(&_slice{
Data: uintptr(unsafe.Pointer(&h.data)) + (offset * max),
Len: int(h.count),
Cap: int(max),
})
}
func (h *ColumnBlockMut) Item(max, offset, size, index uintptr) unsafe.Pointer {
return unsafe.Pointer(
uintptr(unsafe.Pointer(&h.data)) +
(offset * max) +
(size * index))
}
//func (h *ColumnBlockMut) SetI8(max, offset, size, index uintptr, value int8) {
// *(*int8)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetU8(max, offset, size, index uintptr, value uint8) {
// *(*uint8)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetI16(max, offset, size, index uintptr, value int16) {
// *(*int16)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetU16(max, offset, size, index uintptr, value uint16) {
// *(*uint16)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetI32(max, offset, size, index uintptr, value int32) {
// *(*int32)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetU32(max, offset, size, index uintptr, value uint32) {
// *(*uint32)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetI64(max, offset, size, index uintptr, value int64) {
// *(*int64)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetU64(max, offset, size, index uintptr, value uint64) {
// *(*uint64)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetF32(max, offset, size, index uintptr, value float32) {
// *(*float32)(h.Item(max, offset, size, index)) = value
//}
//
//func (h *ColumnBlockMut) SetF64(max, offset, size, index uintptr, value float64) {
// *(*float64)(h.Item(max, offset, size, index)) = value
//}