Merge pull request #3585 from xiang90/fix_hash

storage: fix hash by iterating kv
This commit is contained in:
Xiang Li 2015-09-23 11:39:21 -07:00
commit 0813a0f2d1
3 changed files with 33 additions and 7 deletions

View File

@ -15,6 +15,8 @@
package backend package backend
import ( import (
"fmt"
"hash/crc32"
"io" "io"
"log" "log"
"time" "time"
@ -25,6 +27,7 @@ import (
type Backend interface { type Backend interface {
BatchTx() BatchTx BatchTx() BatchTx
Snapshot(w io.Writer) (n int64, err error) Snapshot(w io.Writer) (n int64, err error)
Hash() (uint32, error)
ForceCommit() ForceCommit()
Close() error Close() error
} }
@ -84,6 +87,33 @@ func (b *backend) Snapshot(w io.Writer) (n int64, err error) {
return n, err return n, err
} }
func (b *backend) Hash() (uint32, error) {
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
err := b.db.View(func(tx *bolt.Tx) error {
c := tx.Cursor()
for next, _ := c.First(); next != nil; next, _ = c.Next() {
b := tx.Bucket(next)
if b == nil {
return fmt.Errorf("cannot get hash of bucket %s", string(next))
}
h.Write(next)
b.ForEach(func(k, v []byte) error {
h.Write(k)
h.Write(v)
return nil
})
}
return nil
})
if err != nil {
return 0, err
}
return h.Sum32(), nil
}
func (b *backend) run() { func (b *backend) run() {
defer close(b.donec) defer close(b.donec)

View File

@ -16,7 +16,6 @@ package storage
import ( import (
"errors" "errors"
"hash/crc32"
"io" "io"
"log" "log"
"math" "math"
@ -289,12 +288,8 @@ func (s *store) Compact(rev int64) error {
} }
func (s *store) Hash() (uint32, error) { func (s *store) Hash() (uint32, error) {
h := crc32.New(crc32.MakeTable(crc32.Castagnoli)) s.b.ForceCommit()
_, err := s.Snapshot(h) return s.b.Hash()
if err != nil {
return 0, err
}
return h.Sum32(), nil
} }
func (s *store) Snapshot(w io.Writer) (int64, error) { func (s *store) Snapshot(w io.Writer) (int64, error) {

View File

@ -722,6 +722,7 @@ type fakeBackend struct {
} }
func (b *fakeBackend) BatchTx() backend.BatchTx { return b.tx } func (b *fakeBackend) BatchTx() backend.BatchTx { return b.tx }
func (b *fakeBackend) Hash() (uint32, error) { return 0, nil }
func (b *fakeBackend) Snapshot(w io.Writer) (n int64, err error) { return 0, errors.New("unsupported") } func (b *fakeBackend) Snapshot(w io.Writer) (n int64, err error) { return 0, errors.New("unsupported") }
func (b *fakeBackend) ForceCommit() {} func (b *fakeBackend) ForceCommit() {}
func (b *fakeBackend) Close() error { return nil } func (b *fakeBackend) Close() error { return nil }