-
Notifications
You must be signed in to change notification settings - Fork 0
/
zst.go
71 lines (57 loc) · 1.34 KB
/
zst.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
package pgn
import (
"bufio"
"os"
"github.com/inhies/go-bytesize"
"github.com/klauspost/compress/zstd"
)
type ZstPGN struct {
file *os.File
pgn *bufio.Scanner
inputReader *ByteCountingReader
outputReader *ByteCountingReader
close closeFn
path string
size bytesize.ByteSize
}
func NewZstPGN(path string) *ZstPGN {
return &ZstPGN{
path: path,
}
}
func (s *ZstPGN) Open() error {
var err error
reader, size, close, err := openSource(s.path)
if err != nil {
return err
}
// Wrap the file and the zstd Reader in ByteCountingReader to estimate the data size by output/input ratio
s.inputReader = &ByteCountingReader{reader: reader}
zstReader, err := zstd.NewReader(s.inputReader)
if err != nil {
return err
}
s.outputReader = &ByteCountingReader{reader: zstReader}
s.close = close
s.size = size
s.pgn = bufio.NewScanner(bufio.NewReader(s.outputReader))
return nil
}
func (s *ZstPGN) Close() error {
return s.file.Close()
}
func (s *ZstPGN) Scan() bool {
return s.pgn.Scan()
}
func (s *ZstPGN) Text() string {
return s.pgn.Text()
}
func (s *ZstPGN) Size() bytesize.ByteSize {
if s.inputReader.bytesRead > 0 {
return s.size * (s.outputReader.bytesRead / s.inputReader.bytesRead)
}
return s.size
}
func (s *ZstPGN) BytesRead() bytesize.ByteSize {
return s.outputReader.bytesRead
}