Skip to content

Commit

Permalink
Merge pull request etcd-io#3585 from xiang90/fix_hash
Browse files Browse the repository at this point in the history
storage: fix hash by iterating kv
  • Loading branch information
xiang90 committed Sep 23, 2015
2 parents 370ce37 + 385e175 commit 0813a0f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
30 changes: 30 additions & 0 deletions storage/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package backend

import (
"fmt"
"hash/crc32"
"io"
"log"
"time"
Expand All @@ -25,6 +27,7 @@ import (
type Backend interface {
BatchTx() BatchTx
Snapshot(w io.Writer) (n int64, err error)
Hash() (uint32, error)
ForceCommit()
Close() error
}
Expand Down Expand Up @@ -84,6 +87,33 @@ func (b *backend) Snapshot(w io.Writer) (n int64, err error) {
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() {
defer close(b.donec)

Expand Down
9 changes: 2 additions & 7 deletions storage/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package storage

import (
"errors"
"hash/crc32"
"io"
"log"
"math"
Expand Down Expand Up @@ -289,12 +288,8 @@ func (s *store) Compact(rev int64) error {
}

func (s *store) Hash() (uint32, error) {
h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
_, err := s.Snapshot(h)
if err != nil {
return 0, err
}
return h.Sum32(), nil
s.b.ForceCommit()
return s.b.Hash()
}

func (s *store) Snapshot(w io.Writer) (int64, error) {
Expand Down
1 change: 1 addition & 0 deletions storage/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ type fakeBackend struct {
}

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) ForceCommit() {}
func (b *fakeBackend) Close() error { return nil }
Expand Down

0 comments on commit 0813a0f

Please sign in to comment.