/
packedwriter.go
101 lines (85 loc) · 2.07 KB
/
packedwriter.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package packed
import (
"errors"
"github.com/geange/lucene-go/core/store"
"github.com/geange/lucene-go/core/util/packed/bulkoperation"
"github.com/geange/lucene-go/core/util/packed/common"
)
var _ Writer = &PackedWriter{}
type PackedWriter struct {
*BaseWriter
finished bool
format Format
encoder common.BulkOperation
nextBlocks []byte
nextValues []uint64
iterations int
off int
written int
}
func NewPackedWriter(format Format, out store.DataOutput, valueCount, bitsPerValue, mem int) *PackedWriter {
encoder, err := Of(format, bitsPerValue)
if err != nil {
return nil
}
iterations := bulkoperation.ComputeIterations(encoder, valueCount, mem)
packedWriter := &PackedWriter{
finished: false,
format: format,
encoder: encoder,
nextBlocks: make([]byte, iterations*encoder.ByteBlockCount()),
nextValues: make([]uint64, iterations*encoder.ByteValueCount()),
iterations: iterations,
off: 0,
written: 0,
}
packedWriter.BaseWriter = newBaseWriter(packedWriter, out, valueCount, bitsPerValue)
return packedWriter
}
func (p *PackedWriter) GetFormat() Format {
return p.format
}
func (p *PackedWriter) Add(v uint64) error {
if p.valueCount != -1 && p.written >= p.valueCount {
return errors.New("writing past end of stream")
}
p.nextValues[p.off] = v
p.off++
if p.off == len(p.nextValues) {
if err := p.flush(); err != nil {
return err
}
}
p.written++
return nil
}
func (p *PackedWriter) Finish() error {
if p.valueCount != -1 {
for p.written < p.valueCount {
err := p.Add(0)
if err != nil {
return err
}
}
}
if err := p.flush(); err != nil {
return err
}
p.finished = true
return nil
}
func (p *PackedWriter) flush() error {
p.encoder.EncodeBytes(p.nextValues[0:], p.nextBlocks[0:], p.iterations)
blockCount := p.format.ByteCount(VERSION_CURRENT, p.off, p.bitsPerValue)
if _, err := p.out.Write(p.nextBlocks[:blockCount]); err != nil {
return err
}
for i := range p.nextValues {
p.nextValues[i] = 0
}
p.off = 0
return nil
}
func (p *PackedWriter) Ord() int {
return p.written - 1
}