/
encode.go
76 lines (69 loc) · 2.02 KB
/
encode.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
// Copyright 2021 Michael J. Fromberger. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package index
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"io"
"github.com/creachadair/ffs/index/indexpb"
)
// Encode encodes idx as a protocol buffer message for storage.
func Encode(idx *Index) *indexpb.Index {
var buf bytes.Buffer
w, _ := zlib.NewWriterLevel(&buf, zlib.BestCompression)
// N.B. The only possible error is an invalid level.
for _, seg := range idx.bits {
var val [8]byte
binary.BigEndian.PutUint64(val[:], seg)
w.Write(val[:])
}
w.Close()
return &indexpb.Index{
NumKeys: uint64(idx.numKeys),
Seeds: idx.seeds,
NumSegments: uint64(len(idx.bits)),
SegmentData: buf.Bytes(),
}
}
// Decode decodes an encoded index from protobuf.
func Decode(pb *indexpb.Index) (*Index, error) {
idx := &Index{
numKeys: int(pb.NumKeys),
seeds: pb.Seeds,
hash: (*Options)(nil).hashFunc(), // the default
// TODO(creachadair): Check the hash_func value.
}
// Compressed segments.
rc, err := zlib.NewReader(bytes.NewReader(pb.SegmentData))
if err != nil {
return nil, err
}
defer rc.Close()
bits, err := io.ReadAll(rc)
if err != nil {
return nil, err
}
nseg := int(pb.NumSegments)
if len(bits) != 8*nseg {
return nil, fmt.Errorf("invalid segment data: got %d bytes, want %d", len(bits), 8*nseg)
}
idx.bits = make(bitVector, nseg)
idx.nbits = 64 * pb.NumSegments
for i := 0; i < nseg; i++ {
idx.bits[i] = binary.BigEndian.Uint64(bits[8*i:])
}
return idx, nil
}