Skip to content

Commit

Permalink
Add version number for schema of persisted data
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed Feb 9, 2016
1 parent adf4ddd commit 2e988d7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions ipam/allocator.go
Expand Up @@ -30,9 +30,11 @@ const (
)

var (
topBucket = []byte("top")
nameIdent = []byte("peername")
ringBucket = []byte("ring")
versionIdent = []byte("version")
persistenceVersion = []byte{2, 0}
topBucket = []byte("top")
nameIdent = []byte("peername")
ringBucket = []byte("ring")
)

// operation represents something which Allocator wants to do, but
Expand Down Expand Up @@ -100,14 +102,20 @@ func openDB(ourName mesh.PeerName, dbPrefix string) (*bolt.DB, error) {
}
err = db.Update(func(tx *bolt.Tx) error {
nameVal := []byte(ourName.String())
// top-level bucket has peerName
// top-level bucket has peerName and persistence version
if top := tx.Bucket(topBucket); top == nil {
top, err := tx.CreateBucket(topBucket)
if err != nil {
return err
}
top.Put(nameIdent, nameVal)
top.Put(versionIdent, persistenceVersion)
} else {
if checkVersion := top.Get(versionIdent); checkVersion != nil {
if checkVersion[0] != persistenceVersion[0] {
common.Log.Fatalf("[allocator] Cannot use persistence file %s - version %x", dbPathname, checkVersion)
}
}
if checkPeerName := top.Get(nameIdent); !bytes.Equal(checkPeerName, nameVal) {
common.Log.Infof("[allocator] Deleting persisted data for peername %s", checkPeerName)
tx.DeleteBucket(ringBucket)
Expand Down

0 comments on commit 2e988d7

Please sign in to comment.