-
Notifications
You must be signed in to change notification settings - Fork 0
/
append.go
373 lines (327 loc) · 10.3 KB
/
append.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
package pixi
/*
import (
"bytes"
"compress/flate"
"io"
)
type AppendTile struct {
Dirty bool
Data []byte
}
type AppendDataset struct {
Pixi
WritingTileIndex uint
WritingTile AppendTile
ReadCache map[uint]*AppendTile
MaxInCache uint
Backing io.ReadWriteSeeker
}
// Creates a new append dataset. It initializes the internal data structures and sets up the backing store.
func NewAppendDataset(d Pixi, backing io.ReadWriteSeeker, maxInCache uint) (*AppendDataset, error) {
appendSet := &AppendDataset{Pixi: d}
appendSet.Backing = backing
appendSet.ReadCache = make(map[uint]*AppendTile)
appendSet.MaxInCache = maxInCache
appendSet.WritingTileIndex = 0
appendSet.WritingTile = AppendTile{Data: make([]byte, appendSet.TileSize(0))}
appendSet.ReadCache[appendSet.WritingTileIndex] = &appendSet.WritingTile
diskTileCount := appendSet.Tiles()
if appendSet.Separated {
diskTileCount *= len(appendSet.Fields)
}
appendSet.TileBytes = make([]int64, diskTileCount)
appendSet.TileOffsets = make([]int64, diskTileCount)
appendSet.TileOffsets[0] = appendSet.DiskDataStart()
if err := WriteSummary(backing, appendSet.Pixi); err != nil {
return nil, err
}
return appendSet, nil
}
func ReadAppend(r io.ReadWriteSeeker, ds Pixi, maxInCache uint) (AppendDataset, error) {
appended := AppendDataset{Pixi: ds, ReadCache: make(map[uint]*AppendTile), Backing: r, MaxInCache: maxInCache}
return appended, nil
}
func (d *AppendDataset) GetRawSample(dimIndices []uint) ([]byte, error) {
if len(d.Dimensions) != len(dimIndices) {
return nil, DimensionsError{len(d.Dimensions), len(dimIndices)}
}
tileIndex, inTileIndex := d.dimIndicesToTileIndices(dimIndices)
if d.Separated {
sample := make([]byte, d.SampleSize())
sampleOffset := 0
for fieldId, field := range d.Fields {
fieldTile := tileIndex + uint(d.Tiles())*uint(fieldId)
fieldOffset := inTileIndex * uint(field.Size())
cached, err := d.getTile(fieldTile)
if err != nil {
return nil, err
}
copy(sample[sampleOffset:], cached.Data[fieldOffset:])
sampleOffset += int(field.Size())
}
return sample, nil
} else {
fieldOffset := inTileIndex * uint(d.SampleSize())
cached, err := d.getTile(tileIndex)
if err != nil {
return nil, err
}
return cached.Data[fieldOffset : fieldOffset+uint(d.SampleSize())], nil
}
}
func (d *AppendDataset) GetSample(dimIndices []uint) ([]any, error) {
raw, err := d.GetRawSample(dimIndices)
if err != nil {
return nil, err
}
sample := make([]any, len(d.Fields))
fieldOffset := 0
for fieldId, field := range d.Fields {
fieldVal := field.Read(raw[fieldOffset:])
sample[fieldId] = fieldVal
fieldOffset += int(field.Size())
}
return sample, nil
}
func (d *AppendDataset) GetSampleField(dimIndices []uint, fieldId uint) (any, error) {
if len(d.Dimensions) != len(dimIndices) {
return nil, DimensionsError{len(d.Dimensions), len(dimIndices)}
}
tileIndex, inTileIndex := d.dimIndicesToTileIndices(dimIndices)
fieldOffset := inTileIndex
if d.Separated {
tileIndex += uint(d.Tiles()) * uint(fieldId)
fieldOffset *= uint(d.Fields[fieldId].Size())
} else {
fieldOffset *= uint(d.SampleSize())
for _, field := range d.Fields[:fieldId] {
fieldOffset += uint(field.Size())
}
}
cached, err := d.getTile(tileIndex)
if err != nil {
return nil, err
}
return d.Fields[fieldId].Read(cached.Data[fieldOffset:]), nil
}
func (d *AppendDataset) SetSample(dimIndices []uint, sample []any) error {
if len(d.Dimensions) != len(dimIndices) {
return DimensionsError{len(d.Dimensions), len(dimIndices)}
}
if d.Separated {
return UnsupportedError("cannot write a full sample in a separated append-only data set")
}
tileIndex, inTileIndex := d.dimIndicesToTileIndices(dimIndices)
// check if we need to move to the next tile or if we're out of range
if tileIndex != d.WritingTileIndex {
if tileIndex == d.WritingTileIndex+1 {
err := d.writeTile(d.WritingTile.Data, d.WritingTileIndex)
if err != nil {
return err
}
d.WritingTileIndex += 1
d.WritingTile = AppendTile{Data: make([]byte, d.TileSize(int(d.WritingTileIndex)))}
err = d.addTileToCache(d.WritingTileIndex, d.WritingTile.Data)
if err != nil {
return err
}
} else {
return RangeError{Specified: int(tileIndex), ValidMin: int(d.WritingTileIndex), ValidMax: int(d.WritingTileIndex)}
}
}
inTileIndex *= uint(d.SampleSize())
for fieldId, field := range d.Fields {
field.Write(d.WritingTile.Data[inTileIndex:], sample[fieldId])
inTileIndex += uint(field.Size())
}
return nil
}
func (d *AppendDataset) SetSampleField(dimIndices []uint, fieldId uint, fieldVal any) error {
if len(d.Dimensions) != len(dimIndices) {
return DimensionsError{len(d.Dimensions), len(dimIndices)}
}
tileIndex, inTileIndex := d.dimIndicesToTileIndices(dimIndices)
fieldOffset := inTileIndex
if d.Separated {
tileIndex += uint(d.Tiles()) * uint(fieldId)
fieldOffset *= uint(d.Fields[fieldId].Size())
} else {
fieldOffset *= uint(d.SampleSize())
for _, field := range d.Fields[:fieldId] {
fieldOffset += uint(field.Size())
}
}
// check if we need to move to the next tile or if we're out of range
if tileIndex != d.WritingTileIndex {
if tileIndex == d.WritingTileIndex+1 {
err := d.writeTile(d.WritingTile.Data, d.WritingTileIndex)
if err != nil {
return err
}
d.WritingTileIndex += 1
d.WritingTile = AppendTile{Data: make([]byte, d.TileSize(int(d.WritingTileIndex)))}
err = d.addTileToCache(d.WritingTileIndex, d.WritingTile.Data)
if err != nil {
return err
}
} else {
return RangeError{Specified: int(tileIndex), ValidMin: int(d.WritingTileIndex), ValidMax: int(d.WritingTileIndex)}
}
}
d.Fields[fieldId].Write(d.WritingTile.Data[fieldOffset:], fieldVal)
return nil
}
func (d *AppendDataset) Finalize() error {
// last tile won't have been written yet
err := d.writeTile(d.WritingTile.Data, d.WritingTileIndex)
if err != nil {
return err
}
d.WritingTileIndex += 1
d.WritingTile = AppendTile{}
_, err = d.Backing.Seek(0, io.SeekStart)
if err != nil {
return err
}
err = WriteSummary(d.Backing, d.Pixi)
if err != nil {
return err
}
return d.addTileToCache(d.WritingTileIndex, nil)
}
func (d *AppendDataset) addTileToCache(tileIndex uint, data []byte) error {
if len(d.ReadCache) >= int(d.MaxInCache) {
err := d.evict()
if err != nil {
return err
}
}
d.ReadCache[tileIndex] = &AppendTile{Data: data, Dirty: false}
return nil
}
func (d *AppendDataset) getTile(tileIndex uint) (*AppendTile, error) {
// TODO: locking for safe concurrent access
var cached *AppendTile
if tile, ok := d.ReadCache[tileIndex]; ok {
cached = tile
} else {
loaded, err := d.loadTile(tileIndex)
if err != nil {
return nil, err
} else {
cached = loaded
}
}
return cached, nil
}
// Load a tile from the cache or disk, if it's not in memory.
//
// This function is responsible for loading a tile into memory if it's not already there.
// It does this by first checking if the tile exists in the cache, and if so, returns it directly.
// If not, it reads the tile from disk and caches it before returning.
func (d *AppendDataset) loadTile(tileIndex uint) (*AppendTile, error) {
read, err := d.readTile(tileIndex)
if err != nil {
return nil, err
}
err = d.addTileToCache(tileIndex, read)
return d.ReadCache[tileIndex], err
}
// Evicts the oldest cached tile and writes its data to the underlying storage.
// This method is used when the cache exceeds its maximum size.
// It ensures that all changes made by this dataset are persisted.
// Return an error if there was an issue with persisting or evicting a tile, nil otherwise
func (d *AppendDataset) evict() error {
if len(d.ReadCache) == 0 {
return nil
}
var first uint
for k := range d.ReadCache {
first = k
break
}
delete(d.ReadCache, first)
return nil
}
// Read a tile from the backing storage.
// This function reads a tile from the underlying storage and returns its data as a byte slice.
// The offset of the tile in the storage is determined by the `tileIndex`.
func (d *AppendDataset) readTile(tileIndex uint) ([]byte, error) {
d.Backing.Seek(d.TileOffsets[tileIndex], io.SeekStart)
uncompressedLen := d.TileSize(int(tileIndex))
switch d.Compression {
case CompressionNone:
buf := make([]byte, uncompressedLen)
_, err := d.Backing.Read(buf)
if err != nil {
return nil, err
}
return buf, nil
case CompressionFlate:
buf := make([]byte, 0, uncompressedLen)
bufRd := bytes.NewBuffer(buf)
gzRdr := flate.NewReader(d.Backing)
defer gzRdr.Close()
_, err := io.Copy(bufRd, gzRdr)
if err != nil {
return nil, err
}
return bufRd.Bytes(), nil
}
return nil, UnsupportedError("unknown compression type")
}
// This function takes in a byte slice and a tile index as input, and writes the contents of the slice to the backing storage at the specified tile offset.
// The function is responsible for handling both uncompressed and compressed data.
// If there was an issue with writing the tile, this function will return an error. Otherwise, it returns nil.
func (d *AppendDataset) writeTile(data []byte, tileIndex uint) error {
offset := d.TileOffsets[tileIndex]
d.Backing.Seek(offset, io.SeekStart)
tileSize := 0
switch d.Compression {
case CompressionNone:
written, err := d.Backing.Write(data)
if err != nil {
return err
}
tileSize = written
case CompressionFlate:
buf := new(bytes.Buffer)
gzWtr, err := flate.NewWriter(buf, flate.BestCompression)
if err != nil {
return err
}
_, err = gzWtr.Write(data)
if err != nil {
gzWtr.Close()
return err
}
gzWtr.Close()
tileSize = buf.Len()
_, err = io.Copy(d.Backing, buf)
if err != nil {
return err
}
}
// make sure to update the byte counts for this tile
d.TileBytes[tileIndex] = int64(tileSize)
if tileIndex < uint(d.DiskTiles())-1 {
d.TileOffsets[tileIndex+1] = offset + int64(tileSize)
}
return nil
}
// This function takes a slice of dimension indices and converts them into a tile index.
func (d *AppendDataset) dimIndicesToTileIndices(dimIndices []uint) (tileIndex uint, inTileIndex uint) {
tileIndex = uint(0)
inTileIndex = uint(0)
tileMul := uint(1)
inTileMul := uint(1)
for dInd, index := range dimIndices {
tileIndex += (index / uint(d.Dimensions[dInd].TileSize)) * tileMul
inTileIndex += (index % uint(d.Dimensions[dInd].TileSize)) * inTileMul
tileMul *= uint(d.Dimensions[dInd].Tiles())
inTileMul *= uint(d.Dimensions[dInd].TileSize)
}
return
}
*/