Skip to content

Commit

Permalink
Refactor Open and DB
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Sep 13, 2017
1 parent d2cbc2d commit 3488114
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 253 deletions.
12 changes: 6 additions & 6 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestBucket(t *testing.T) {
t.Fatal(err)
}

require.Nil(t, db.root.GetBucket(readTx, "none"))
require.Nil(t, db.GetBucket(readTx, "none"))

b, err := db.root.CreateBucketIfNotExists(readTx, "new")
b, err := db.CreateBucketIfNotExists(readTx, "new")

// Cannot create buckets in a read transaction
require.Error(t, err)
Expand All @@ -36,9 +36,9 @@ func TestBucket(t *testing.T) {
t.Fatal(err)
}

require.Nil(t, db.root.GetBucket(writeTx, "none"))
require.Nil(t, db.GetBucket(writeTx, "none"))

b, err = db.root.CreateBucketIfNotExists(writeTx, "new")
b, err = db.CreateBucketIfNotExists(writeTx, "new")

require.NoError(t, err)
require.NotNil(t, b)
Expand All @@ -59,8 +59,8 @@ func TestBucket(t *testing.T) {
t.Fatal(err)
}

require.NotNil(t, db.root.GetBucket(readTx, "new"))
require.Nil(t, db.root.GetBucket(readTx, "c"))
require.NotNil(t, db.GetBucket(readTx, "new"))
require.Nil(t, db.GetBucket(readTx, "c"))
require.NotNil(t, n2.GetBucket(readTx, "c"))

readTx.Rollback()
Expand Down
8 changes: 4 additions & 4 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func TestNode(t *testing.T) {
node1, ok := n1.(*node)
require.True(t, ok)
require.Equal(t, db, node1.s)
require.NotEqual(t, db.root, n1)
require.Equal(t, []string{"a"}, db.root.rootBucket)
require.Equal(t, []string{"b", "c"}, node1.rootBucket)
require.NotEqual(t, db.Node, n1)
require.Equal(t, []string{"a"}, db.Node.(*node).rootBucket)
require.Equal(t, []string{"a", "b", "c"}, node1.rootBucket)
n2 := n1.From("d", "e")
node2, ok := n2.(*node)
require.True(t, ok)
require.Equal(t, []string{"b", "c", "d", "e"}, node2.rootBucket)
require.Equal(t, []string{"a", "b", "c", "d", "e"}, node2.rootBucket)
}

func TestNodeWithTransaction(t *testing.T) {
Expand Down
63 changes: 45 additions & 18 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,47 @@ import (
)

// BoltOptions used to pass options to BoltDB.
func BoltOptions(mode os.FileMode, options *bolt.Options) func(*DB) error {
return func(d *DB) error {
d.boltMode = mode
d.boltOptions = options
func BoltOptions(mode os.FileMode, options *bolt.Options) func(*Options) error {
return func(opts *Options) error {
opts.boltMode = mode
opts.boltOptions = options
return nil
}
}

// Codec used to set a custom encoder and decoder. The default is JSON.
func Codec(c codec.MarshalUnmarshaler) func(*DB) error {
return func(d *DB) error {
d.codec = c
func Codec(c codec.MarshalUnmarshaler) func(*Options) error {
return func(opts *Options) error {
opts.codec = c
// d.root.codec = c
return nil
}
}

// Batch enables the use of batch instead of update for read-write transactions.
func Batch() func(*DB) error {
return func(d *DB) error {
d.batchMode = true
func Batch() func(*Options) error {
return func(opts *Options) error {
opts.batchMode = true
// d.root.batchMode = true
return nil
}
}

// Root used to set the root bucket. See also the From method.
func Root(root ...string) func(*DB) error {
return func(d *DB) error {
d.rootBucket = root
func Root(root ...string) func(*Options) error {
return func(opts *Options) error {
opts.rootBucket = root
// d.root.rootBucket = root
return nil
}
}

// UseDB allow Storm to use an existing open Bolt.DB.
// UseDB allows Storm to use an existing open Bolt.DB.
// Warning: storm.DB.Close() will close the bolt.DB instance.
func UseDB(b *bolt.DB) func(*DB) error {
return func(d *DB) error {
d.Path = b.Path()
d.Bolt = b
func UseDB(b *bolt.DB) func(*Options) error {
return func(opts *Options) error {
opts.path = b.Path()
opts.bolt = b
return nil
}
}
Expand All @@ -71,3 +74,27 @@ func Reverse() func(*index.Options) {
opts.Reverse = true
}
}

// Options are used to customize the way Storm opens a database.
type Options struct {
// Handles encoding and decoding of objects
codec codec.MarshalUnmarshaler

// Bolt file mode
boltMode os.FileMode

// Bolt options
boltOptions *bolt.Options

// Enable batch mode for read-write transaction, instead of update mode
batchMode bool

// The root bucket name
rootBucket []string

// Path of the database file
path string

// Bolt is still easily accessible
bolt *bolt.DB
}
4 changes: 2 additions & 2 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestSave(t *testing.T) {
val := bucket.Get(i)
require.NotNil(t, val)

content, err := db.codec.Marshal(&v)
content, err := db.Codec().Marshal(&v)
require.NoError(t, err)
require.Equal(t, content, val)
return nil
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestSaveDifferentBucketRoot(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()

require.Len(t, db.rootBucket, 0)
require.Len(t, db.Node.(*node).rootBucket, 0)

dbSub := db.From("sub").(*node)

Expand Down
Loading

0 comments on commit 3488114

Please sign in to comment.