-
Notifications
You must be signed in to change notification settings - Fork 3
/
slimbytes.go
60 lines (47 loc) · 1.07 KB
/
slimbytes.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
package slimarray
import (
"errors"
)
var (
BytesTooLarge = errors.New("total bytes exceeds max value of uint32")
TooManyRows = errors.New("row count exceeds max value of int32")
)
// NewBytes creates SlimBytes, which is an array of byte slice,
// from a series of records.
//
// Since 0.1.14
func NewBytes(records [][]byte) (*SlimBytes, error) {
n := int64(len(records))
size := int64(0)
for _, rec := range records {
size += int64(len(rec))
}
if n > 0x7fffffff {
return nil, TooManyRows
}
if size > 0xffffffff {
return nil, BytesTooLarge
}
packed := make([]byte, 0, size)
pos := make([]uint32, 0, n+1)
for _, rec := range records {
pos = append(pos, uint32(len(packed)))
packed = append(packed, rec...)
}
pos = append(pos, uint32(len(packed)))
posArr := NewU32(pos)
bs := &SlimBytes{
Positions: posArr,
Records: packed,
}
return bs, nil
}
// Get the i-th record.
//
// A Get costs about 17 ns
//
// Since 0.1.14
func (b *SlimBytes) Get(i int32) []byte {
byteOffset, byteEnd := b.Positions.Get2(i)
return b.Records[byteOffset:byteEnd]
}