forked from coreos/gzran
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.go
53 lines (43 loc) · 1.11 KB
/
index.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
package gzran
import (
"encoding/gob"
"io"
"sort"
)
// Index collects decompressor state at offset Points.
// gzseek.Reader adds points to the index on the fly as decompression proceeds.
type Index []Point
// LoadIndex deserializes an Index from the given io.Reader.
func LoadIndex(r io.Reader) (Index, error) {
dec := gob.NewDecoder(r)
idx := make(Index, 0)
err := dec.Decode(&idx)
return idx, err
}
// WriteTo serializes the index to the given io.Writer.
// It can be deserialized with LoadIndex.
func (idx Index) WriteTo(w io.Writer) error {
enc := gob.NewEncoder(w)
return enc.Encode(idx)
}
func (idx Index) lastUncompressedOffset() int64 {
if len(idx) == 0 {
return 0
}
return idx[len(idx)-1].UncompressedOffset
}
func (idx Index) closestPointBefore(offset int64) Point {
j := sort.Search(len(idx), func(j int) bool {
return idx[j].UncompressedOffset > offset
})
if j == 0 {
return Point{}
}
return idx[j-1]
}
// Point holds the decompressor state at a given offset within the uncompressed data.
type Point struct {
CompressedOffset int64
UncompressedOffset int64
DecompressorState []byte
}