2017-12-13 22:12:29 +01:00
|
|
|
// Copyright (c) 2017 Couchbase, Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-12-12 17:21:55 +01:00
|
|
|
package zap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/Smerity/govarint"
|
|
|
|
)
|
|
|
|
|
|
|
|
type chunkedIntCoder struct {
|
|
|
|
final []byte
|
|
|
|
chunkSize uint64
|
|
|
|
chunkBuf bytes.Buffer
|
|
|
|
encoder *govarint.Base128Encoder
|
|
|
|
chunkLens []uint64
|
|
|
|
currChunk uint64
|
2018-02-24 18:38:45 +01:00
|
|
|
|
|
|
|
buf []byte
|
2017-12-12 17:21:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newChunkedIntCoder returns a new chunk int coder which packs data into
|
|
|
|
// chunks based on the provided chunkSize and supports up to the specified
|
|
|
|
// maxDocNum
|
|
|
|
func newChunkedIntCoder(chunkSize uint64, maxDocNum uint64) *chunkedIntCoder {
|
|
|
|
total := maxDocNum/chunkSize + 1
|
|
|
|
rv := &chunkedIntCoder{
|
|
|
|
chunkSize: chunkSize,
|
|
|
|
chunkLens: make([]uint64, total),
|
2018-01-27 23:38:04 +01:00
|
|
|
final: make([]byte, 0, 64),
|
2017-12-12 17:21:55 +01:00
|
|
|
}
|
|
|
|
rv.encoder = govarint.NewU64Base128Encoder(&rv.chunkBuf)
|
|
|
|
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset lets you reuse this chunked int coder. buffers are reset and reused
|
|
|
|
// from previous use. you cannot change the chunk size or max doc num.
|
|
|
|
func (c *chunkedIntCoder) Reset() {
|
|
|
|
c.final = c.final[:0]
|
|
|
|
c.chunkBuf.Reset()
|
|
|
|
c.currChunk = 0
|
|
|
|
for i := range c.chunkLens {
|
|
|
|
c.chunkLens[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add encodes the provided integers into the correct chunk for the provided
|
|
|
|
// doc num. You MUST call Add() with increasing docNums.
|
|
|
|
func (c *chunkedIntCoder) Add(docNum uint64, vals ...uint64) error {
|
|
|
|
chunk := docNum / c.chunkSize
|
|
|
|
if chunk != c.currChunk {
|
|
|
|
// starting a new chunk
|
2018-02-28 19:09:12 +01:00
|
|
|
c.Close()
|
|
|
|
c.chunkBuf.Reset()
|
2017-12-12 17:21:55 +01:00
|
|
|
c.currChunk = chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range vals {
|
|
|
|
_, err := c.encoder.PutU64(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:58:42 +01:00
|
|
|
func (c *chunkedIntCoder) AddBytes(docNum uint64, buf []byte) error {
|
|
|
|
chunk := docNum / c.chunkSize
|
|
|
|
if chunk != c.currChunk {
|
|
|
|
// starting a new chunk
|
|
|
|
c.Close()
|
|
|
|
c.chunkBuf.Reset()
|
|
|
|
c.currChunk = chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.chunkBuf.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:21:55 +01:00
|
|
|
// Close indicates you are done calling Add() this allows the final chunk
|
|
|
|
// to be encoded.
|
|
|
|
func (c *chunkedIntCoder) Close() {
|
|
|
|
c.encoder.Close()
|
|
|
|
encodingBytes := c.chunkBuf.Bytes()
|
|
|
|
c.chunkLens[c.currChunk] = uint64(len(encodingBytes))
|
|
|
|
c.final = append(c.final, encodingBytes...)
|
2018-02-28 19:09:12 +01:00
|
|
|
c.currChunk = uint64(cap(c.chunkLens)) // sentinel to detect double close
|
2017-12-12 17:21:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write commits all the encoded chunked integers to the provided writer.
|
|
|
|
func (c *chunkedIntCoder) Write(w io.Writer) (int, error) {
|
2018-02-24 18:38:45 +01:00
|
|
|
bufNeeded := binary.MaxVarintLen64 * (1 + len(c.chunkLens))
|
|
|
|
if len(c.buf) < bufNeeded {
|
|
|
|
c.buf = make([]byte, bufNeeded)
|
|
|
|
}
|
|
|
|
buf := c.buf
|
|
|
|
|
|
|
|
// write out the number of chunks & each chunkLen
|
2017-12-12 17:21:55 +01:00
|
|
|
n := binary.PutUvarint(buf, uint64(len(c.chunkLens)))
|
2018-02-24 18:38:45 +01:00
|
|
|
for _, chunkLen := range c.chunkLens {
|
|
|
|
n += binary.PutUvarint(buf[n:], uint64(chunkLen))
|
|
|
|
}
|
|
|
|
|
|
|
|
tw, err := w.Write(buf[:n])
|
2017-12-12 17:21:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return tw, err
|
|
|
|
}
|
2018-02-24 18:38:45 +01:00
|
|
|
|
2017-12-12 17:21:55 +01:00
|
|
|
// write out the data
|
2018-02-24 18:38:45 +01:00
|
|
|
nw, err := w.Write(c.final)
|
2017-12-12 17:21:55 +01:00
|
|
|
tw += nw
|
|
|
|
if err != nil {
|
|
|
|
return tw, err
|
|
|
|
}
|
|
|
|
return tw, nil
|
|
|
|
}
|
2018-03-08 00:00:38 +01:00
|
|
|
|
|
|
|
func (c *chunkedIntCoder) FinalSize() int {
|
|
|
|
return len(c.final)
|
|
|
|
}
|