github.com/creachadair/ffs@v0.17.3/index/encode.go (about)

     1  // Copyright 2021 Michael J. Fromberger. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package index
    16  
    17  import (
    18  	"bytes"
    19  	"compress/zlib"
    20  	"encoding/binary"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/creachadair/ffs/index/indexpb"
    25  )
    26  
    27  // Encode encodes idx as a protocol buffer message for storage.
    28  func Encode(idx *Index) *indexpb.Index {
    29  	var buf bytes.Buffer
    30  	w, _ := zlib.NewWriterLevel(&buf, zlib.BestSpeed)
    31  	// N.B. The only possible error is an invalid level.
    32  	for _, seg := range idx.bits {
    33  		var val [8]byte
    34  		binary.BigEndian.PutUint64(val[:], seg)
    35  		w.Write(val[:])
    36  	}
    37  	w.Close()
    38  	return &indexpb.Index{
    39  		NumKeys:     uint64(idx.numKeys),
    40  		Seeds:       idx.seeds,
    41  		NumSegments: uint64(len(idx.bits)),
    42  		SegmentData: buf.Bytes(),
    43  	}
    44  }
    45  
    46  // Decode decodes an encoded index from protobuf.
    47  func Decode(pb *indexpb.Index) (*Index, error) {
    48  	idx := &Index{
    49  		numKeys: int(pb.NumKeys),
    50  		seeds:   pb.Seeds,
    51  		hash:    (*Options)(nil).hashFunc(), // the default
    52  
    53  		// TODO(creachadair): Check the hash_func value.
    54  	}
    55  
    56  	// Compressed segments.
    57  	rc, err := zlib.NewReader(bytes.NewReader(pb.SegmentData))
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	defer rc.Close()
    62  	bits, err := io.ReadAll(rc)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	nseg := int(pb.NumSegments)
    67  	if len(bits) != 8*nseg {
    68  		return nil, fmt.Errorf("invalid segment data: got %d bytes, want %d", len(bits), 8*nseg)
    69  	}
    70  	idx.bits = make(bitVector, nseg)
    71  	idx.nbits = 64 * pb.NumSegments
    72  	for i := 0; i < nseg; i++ {
    73  		idx.bits[i] = binary.BigEndian.Uint64(bits[8*i:])
    74  	}
    75  	return idx, nil
    76  }