Merge pull request #130 from mdlayher/crc32-once

Add newCRC32 function to simplify hash initialization
This commit is contained in:
Fabian Reinartz 2017-08-26 18:18:48 +02:00 committed by GitHub
commit 1b86bbb48a
4 changed files with 12 additions and 9 deletions

View file

@ -18,7 +18,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"hash" "hash"
"hash/crc32"
"io" "io"
"os" "os"
@ -136,7 +135,7 @@ func newChunkWriter(dir string) (*chunkWriter, error) {
cw := &chunkWriter{ cw := &chunkWriter{
dirFile: dirFile, dirFile: dirFile,
n: 0, n: 0,
crc32: crc32.New(crc32.MakeTable(crc32.Castagnoli)), crc32: newCRC32(),
segmentSize: defaultChunkSegmentSize, segmentSize: defaultChunkSegmentSize,
} }
return cw, nil return cw, nil

View file

@ -18,7 +18,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"hash" "hash"
"hash/crc32"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -177,7 +176,7 @@ func newIndexWriter(dir string) (*indexWriter, error) {
// Caches. // Caches.
symbols: make(map[string]uint32, 1<<13), symbols: make(map[string]uint32, 1<<13),
seriesOffsets: make(map[uint32]uint64, 1<<16), seriesOffsets: make(map[uint32]uint64, 1<<16),
crc32: crc32.New(crc32.MakeTable(crc32.Castagnoli)), crc32: newCRC32(),
} }
if err := iw.writeMeta(); err != nil { if err := iw.writeMeta(); err != nil {
return nil, err return nil, err

View file

@ -16,7 +16,6 @@ package tsdb
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"hash/crc32"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -37,7 +36,7 @@ const (
func writeTombstoneFile(dir string, tr tombstoneReader) error { func writeTombstoneFile(dir string, tr tombstoneReader) error {
path := filepath.Join(dir, tombstoneFilename) path := filepath.Join(dir, tombstoneFilename)
tmp := path + ".tmp" tmp := path + ".tmp"
hash := crc32.New(crc32.MakeTable(crc32.Castagnoli)) hash := newCRC32()
f, err := os.Create(tmp) f, err := os.Create(tmp)
if err != nil { if err != nil {
@ -114,7 +113,7 @@ func readTombstones(dir string) (tombstoneReader, error) {
} }
// Verify checksum // Verify checksum
hash := crc32.New(crc32.MakeTable(crc32.Castagnoli)) hash := newCRC32()
if _, err := hash.Write(d.get()); err != nil { if _, err := hash.Write(d.get()); err != nil {
return nil, errors.Wrap(err, "write to hash") return nil, errors.Wrap(err, "write to hash")
} }

10
wal.go
View file

@ -112,6 +112,12 @@ func init() {
castagnoliTable = crc32.MakeTable(crc32.Castagnoli) castagnoliTable = crc32.MakeTable(crc32.Castagnoli)
} }
// newCRC32 initializes a CRC32 hash with a preconfigured polynomial, so the
// polynomial may be easily changed in one location at a later time, if necessary.
func newCRC32() hash.Hash32 {
return crc32.New(castagnoliTable)
}
// OpenSegmentWAL opens or creates a write ahead log in the given directory. // OpenSegmentWAL opens or creates a write ahead log in the given directory.
// The WAL must be read completely before new data is written. // The WAL must be read completely before new data is written.
func OpenSegmentWAL(dir string, logger log.Logger, flushInterval time.Duration) (*SegmentWAL, error) { func OpenSegmentWAL(dir string, logger log.Logger, flushInterval time.Duration) (*SegmentWAL, error) {
@ -133,7 +139,7 @@ func OpenSegmentWAL(dir string, logger log.Logger, flushInterval time.Duration)
donec: make(chan struct{}), donec: make(chan struct{}),
stopc: make(chan struct{}), stopc: make(chan struct{}),
segmentSize: walSegmentSizeBytes, segmentSize: walSegmentSizeBytes,
crc32: crc32.New(castagnoliTable), crc32: newCRC32(),
} }
if err := w.initSegments(); err != nil { if err := w.initSegments(); err != nil {
return nil, err return nil, err
@ -524,7 +530,7 @@ func newWALReader(w *SegmentWAL, l log.Logger) *walReader {
logger: l, logger: l,
wal: w, wal: w,
buf: make([]byte, 0, 128*4096), buf: make([]byte, 0, 128*4096),
crc32: crc32.New(crc32.MakeTable(crc32.Castagnoli)), crc32: newCRC32(),
} }
} }