forked from DCSO/bloom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
105 lines (84 loc) · 2.2 KB
/
io.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
102
103
104
105
// DCSO go bloom filter
// Copyright (c) 2017, DCSO GmbH
package bloom
import (
"bufio"
"bytes"
gz "compress/gzip"
"io"
"os"
)
// LoadFromBytes reads a binary Bloom filter representation from a byte array
// and returns a BloomFilter struct pointer based on it.
// If 'gzip' is true, then compressed input will be expected.
func LoadFromBytes(input []byte, gzip bool) (*BloomFilter, error) {
return LoadFromReader(bytes.NewReader(input), gzip)
}
// LoadFilter reads a binary Bloom filter representation from a file
// and returns a BloomFilter struct pointer based on it.
// If 'gzip' is true, then compressed input will be expected.
func LoadFilter(path string, gzip bool) (*BloomFilter, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return LoadFromReader(file, gzip)
}
// LoadFromReader reads a binary Bloom filter representation from an io.Reader
// and returns a BloomFilter struct pointer based on it.
// If 'gzip' is true, then compressed input will be expected.
func LoadFromReader(inReader io.Reader, gzip bool) (*BloomFilter, error) {
var err error
var reader io.Reader
var gzipReader *gz.Reader
var ioReader *bufio.Reader
if gzip {
gzipReader, err = gz.NewReader(inReader)
if err != nil {
return nil, err
}
defer gzipReader.Close()
reader = gzipReader
} else {
ioReader = bufio.NewReader(inReader)
reader = ioReader
}
var filter BloomFilter
if err = filter.Read(reader); err != nil {
return nil, err
}
return &filter, nil
}
// WriteFilter writes a binary Bloom filter representation for a given struct
// to a file. If 'gzip' is true, then a compressed file will be written.
func WriteFilter(filter *BloomFilter, path string, gzip bool) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
file.Seek(0, 0)
var writer io.Writer
var gzipWriter *gz.Writer
var ioWriter *bufio.Writer
if gzip {
gzipWriter = gz.NewWriter(file)
defer gzipWriter.Close()
writer = gzipWriter
} else {
ioWriter = bufio.NewWriter(file)
writer = ioWriter
}
err = filter.Write(writer)
if err != nil {
return err
}
if gzip {
gzipWriter.Flush()
} else {
ioWriter.Flush()
}
file.Sync()
return nil
}