Skip to content

Commit

Permalink
add v1, source to src, remove sys
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarah Christoff committed Apr 21, 2021
1 parent fedb9bb commit f3754d9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ require (
github.com/boltdb/bolt v1.3.1
github.com/hashicorp/go-msgpack v0.5.5
github.com/hashicorp/raft v1.1.0
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988 // indirect
)
)
4 changes: 1 addition & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,4 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988 h1:EjgCl+fVlIaPJSori0ikSz3uV0DOHKWOJFpv1sAAhBM=
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
14 changes: 7 additions & 7 deletions v2/bolt_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"time"

boltdbbolt "github.com/boltdb/bolt"
v1 "github.com/boltdb/bolt"
"github.com/hashicorp/raft"
"go.etcd.io/bbolt"
)
Expand Down Expand Up @@ -271,15 +271,15 @@ func (b *BoltStore) Sync() error {
return b.conn.Sync()
}

// MigratetoV2 reads in the source file path of a BoltDB file
// MigratetoV2 reads in the source file path of a BoltDB file
// and outputs all the data migrated to a Bbolt destination file
func MigratetoV2(source, destination string) (*BoltStore, error) {
_, err := os.Stat(destination)
if err == nil {
return nil, fmt.Errorf("file exists in destination %v", destination)
}

sourceDb, err := boltdbbolt.Open(source, dbFileMode, &boltdbbolt.Options{
srcDb, err := v1.Open(source, dbFileMode, &v1.Options{
ReadOnly: true,
Timeout: 1 * time.Minute,
})
Expand All @@ -288,11 +288,11 @@ func MigratetoV2(source, destination string) (*BoltStore, error) {
}

//Start a connection to the source
sourcetx, err := sourceDb.Begin(false)
srctx, err := srcDb.Begin(false)
if err != nil {
return nil, fmt.Errorf("failed connecting to source database: %v", err)
}
defer sourcetx.Rollback()
defer srctx.Rollback()

//Create the destination
destDb, err := New(Options{Path: destination})
Expand All @@ -312,9 +312,9 @@ func MigratetoV2(source, destination string) (*BoltStore, error) {
//Loop over both old buckets and set them in the new
buckets := [][]byte{dbConf, dbLogs}
for _, b := range buckets {
sourceB := sourcetx.Bucket(b)
srcB := srctx.Bucket(b)
destB := desttx.Bucket(b)
err = sourceB.ForEach(func(k, v []byte) error {
err = srcB.ForEach(func(k, v []byte) error {
return destB.Put(k, v)
})
if err != nil {
Expand Down
23 changes: 11 additions & 12 deletions v2/bolt_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,15 @@ func TestBoltStore_MigratetoV2(t *testing.T) {
}
defer os.RemoveAll(dir)

var sourceFile, destFile string
sourceFile = filepath.Join(dir, "/sourcepath")
destFile = filepath.Join(dir, "/destpath")
srcFile := filepath.Join(dir, "/sourcepath")
destFile := filepath.Join(dir, "/destpath")

// Successfully creates and returns a store
sourceDb, err := v1.NewBoltStore(sourceFile)
srcDb, err := v1.NewBoltStore(srcFile)
if err != nil {
t.Fatalf("failed creating source database: %s", err)
}
defer sourceDb.Close()
defer srcDb.Close()

// Set a mock raft log
logs := []*raft.Log{
Expand All @@ -444,19 +443,19 @@ func TestBoltStore_MigratetoV2(t *testing.T) {
}

//Store logs source
if err := sourceDb.StoreLogs(logs); err != nil {
if err := srcDb.StoreLogs(logs); err != nil {
t.Fatalf("failed storing logs in source database: %s", err)
}
sourceResult := new(raft.Log)
if err := sourceDb.GetLog(2, sourceResult); err != nil {
srcResult := new(raft.Log)
if err := srcDb.GetLog(2, srcResult); err != nil {
t.Fatalf("failed getting log from source database: %s", err)
}

if err := sourceDb.Close(); err != nil {
if err := srcDb.Close(); err != nil {
t.Fatalf("failed closing source database: %s", err)
}

destDb, err := MigratetoV2(sourceFile, destFile)
destDb, err := MigratetoV2(srcFile, destFile)
if err != nil {
t.Fatalf("did not migrate successfully, err %v", err)
}
Expand All @@ -467,8 +466,8 @@ func TestBoltStore_MigratetoV2(t *testing.T) {
t.Fatalf("failed getting log from destination database: %s", err)
}

if !reflect.DeepEqual(sourceResult, destResult) {
t.Errorf("BoltDB log did not equal Bbolt log, Boltdb %v, Bbolt: %v", sourceResult, destResult)
if !reflect.DeepEqual(srcResult, destResult) {
t.Errorf("BoltDB log did not equal Bbolt log, Boltdb %v, Bbolt: %v", srcResult, destResult)
}

}

0 comments on commit f3754d9

Please sign in to comment.