Use type func(string) AttrStore in place of interface AttrStoreGenerator for simplicity

This commit is contained in:
Cody Soyland 2018-03-23 13:51:50 -05:00
parent b66bedd1ef
commit f9efa6c579
12 changed files with 33 additions and 63 deletions

19
attr.go
View file

@ -30,11 +30,6 @@ const (
AttrTypeFloat = 4
)
// AttrStoreGenerator represents an interface for generating an AttrStore.
type AttrStoreGenerator interface {
New(string) AttrStore
}
// AttrStore represents an interface for handling row/column attributes.
type AttrStore interface {
Path() string
@ -48,25 +43,13 @@ type AttrStore interface {
}
func init() {
NopAttrStoreGenerator = &nopAttrStoreGenerator{
Name: "NooP",
}
NopAttrStore = &nopAttrStore{}
}
// NopAttrStoreGenerator represents an AttrStoreGenerator that return a no-op AttrStore.
var NopAttrStoreGenerator AttrStoreGenerator
// NopAttrStore represents an AttrStore that doesn't do anything.
var NopAttrStore AttrStore
// nopAttrStoreGenerator represents a no-op implementation of the AttrStoreGenerator interface.
type nopAttrStoreGenerator struct {
Name string
}
// New is a no-op implementation of AttrStoreGenerator New method.
func (g *nopAttrStoreGenerator) New(string) AttrStore {
func NewNopAttrStore(string) AttrStore {
return &nopAttrStore{}
}

View file

@ -62,14 +62,6 @@ func (c *AttrCache) Set(id uint64, attrs map[string]interface{}) {
c.attrs[id] = attrs
}
// AttrStoreGenerator represents a bolt implementation of the AttrStoreGenerator interface.
type AttrStoreGenerator struct{}
// New is a bolt implementation of AttrStoreGenerator New method.
func (g *AttrStoreGenerator) New(path string) pilosa.AttrStore {
return NewAttrStore(path)
}
// AttrStore represents a storage layer for attributes.
type AttrStore struct {
mu sync.RWMutex
@ -78,11 +70,6 @@ type AttrStore struct {
attrCache *AttrCache
}
// NewAttrStoreGenerator returns a new instance of AttrStoreGenerator.
func NewAttrStoreGenerator() *AttrStoreGenerator {
return &AttrStoreGenerator{}
}
// NewAttrCache returns a new instance of AttrCache.
func NewAttrCache() *AttrCache {
return &AttrCache{
@ -91,7 +78,7 @@ func NewAttrCache() *AttrCache {
}
// NewAttrStore returns a new instance of AttrStore.
func NewAttrStore(path string) *AttrStore {
func NewAttrStore(path string) pilosa.AttrStore {
return &AttrStore{
path: path,
attrCache: NewAttrCache(),

View file

@ -702,7 +702,7 @@ func TestFragment_TopN_CacheSize(t *testing.T) {
Fragment: frag,
RowAttrStore: test.MustOpenAttrStore(),
}
f.Fragment.RowAttrStore = f.RowAttrStore.AttrStore
f.Fragment.RowAttrStore = f.RowAttrStore
if err := f.Open(); err != nil {
panic(err)
}

View file

@ -56,7 +56,7 @@ type Holder struct {
Broadcaster Broadcaster
AttrStoreGenerator AttrStoreGenerator
NewAttrStore func(string) AttrStore
// Close management
wg sync.WaitGroup
@ -85,7 +85,7 @@ func NewHolder() *Holder {
Broadcaster: NopBroadcaster,
Stats: NopStatsClient,
AttrStoreGenerator: NopAttrStoreGenerator,
NewAttrStore: NewNopAttrStore,
CacheFlushInterval: DefaultCacheFlushInterval,
@ -377,8 +377,8 @@ func (h *Holder) newIndex(path, name string) (*Index, error) {
index.LogOutput = h.LogOutput
index.Stats = h.Stats.WithTags(fmt.Sprintf("index:%s", index.Name()))
index.broadcaster = h.Broadcaster
index.AttrStoreGenerator = h.AttrStoreGenerator
index.columnAttrStore = h.AttrStoreGenerator.New(filepath.Join(index.path, ".data"))
index.NewAttrStore = h.NewAttrStore
index.columnAttrStore = h.NewAttrStore(filepath.Join(index.path, ".data"))
return index, nil
}

View file

@ -55,7 +55,7 @@ type Index struct {
remoteMaxSlice uint64
remoteMaxInverseSlice uint64
AttrStoreGenerator AttrStoreGenerator
NewAttrStore func(string) AttrStore
// Column attribute storage and cache.
columnAttrStore AttrStore
@ -85,8 +85,8 @@ func NewIndex(path, name string) (*Index, error) {
remoteMaxSlice: 0,
remoteMaxInverseSlice: 0,
AttrStoreGenerator: NopAttrStoreGenerator,
columnAttrStore: NopAttrStore,
NewAttrStore: NewNopAttrStore,
columnAttrStore: NopAttrStore,
columnLabel: DefaultColumnLabel,
@ -531,7 +531,7 @@ func (i *Index) newFrame(path, name string) (*Frame, error) {
f.LogOutput = i.LogOutput
f.Stats = i.Stats.WithTags(fmt.Sprintf("frame:%s", name))
f.broadcaster = i.broadcaster
f.rowAttrStore = i.AttrStoreGenerator.New(filepath.Join(f.path, ".data"))
f.rowAttrStore = i.NewAttrStore(filepath.Join(f.path, ".data"))
return f, nil
}

View file

@ -74,7 +74,7 @@ type Server struct {
GCNotifier GCNotifier
AttrStoreGenerator AttrStoreGenerator
NewAttrStore func(string) AttrStore
// Background monitoring intervals.
AntiEntropyInterval time.Duration
@ -109,7 +109,7 @@ func NewServer() *Server {
GCNotifier: NopGCNotifier,
AttrStoreGenerator: NopAttrStoreGenerator,
NewAttrStore: NewNopAttrStore,
AntiEntropyInterval: DefaultAntiEntropyInterval,
MetricInterval: 0,

View file

@ -147,8 +147,8 @@ func (m *Command) SetupServer() error {
// Configure data directory (for Cluster .topology)
m.Server.Cluster.Path = m.Config.DataDir
m.Server.AttrStoreGenerator = boltdb.NewAttrStoreGenerator()
m.Server.Holder.AttrStoreGenerator = m.Server.AttrStoreGenerator
m.Server.NewAttrStore = boltdb.NewAttrStore
m.Server.Holder.NewAttrStore = boltdb.NewAttrStore
// Configure holder.
m.Server.Logger().Printf("Using data from: %s\n", m.Config.DataDir)

View file

@ -21,17 +21,17 @@ import (
"sync"
"testing"
"github.com/pilosa/pilosa"
"github.com/pilosa/pilosa/boltdb"
)
// AttrStore represents a test wrapper for pilosa.AttrStore.
type AttrStore struct {
//*pilosa.AttrStore
*boltdb.AttrStore
pilosa.AttrStore
}
// NewAttrStore returns a new instance of AttrStore.
func NewAttrStore() *AttrStore {
func NewAttrStore(string) pilosa.AttrStore {
f, err := ioutil.TempFile("", "pilosa-attr-")
if err != nil {
panic(err)
@ -39,7 +39,7 @@ func NewAttrStore() *AttrStore {
f.Close()
os.Remove(f.Name())
return &AttrStore{AttrStore: boltdb.NewAttrStore(f.Name())}
return &AttrStore{boltdb.NewAttrStore(f.Name())}
}
func BenchmarkAttrStore_Duplicate(b *testing.B) {
@ -75,8 +75,8 @@ func BenchmarkAttrStore_Duplicate(b *testing.B) {
}
// MustOpenAttrStore returns a new, opened attribute store at a temporary path. Panic on error.
func MustOpenAttrStore() *AttrStore {
s := NewAttrStore()
func MustOpenAttrStore() pilosa.AttrStore {
s := NewAttrStore("")
if err := s.Open(); err != nil {
panic(err)
}

View file

@ -27,7 +27,7 @@ const SliceWidth = pilosa.SliceWidth
// Fragment is a test wrapper for pilosa.Fragment.
type Fragment struct {
*pilosa.Fragment
RowAttrStore *AttrStore
RowAttrStore pilosa.AttrStore
}
// NewFragment returns a new instance of Fragment with a temporary path.
@ -43,7 +43,7 @@ func NewFragment(index, frame, view string, slice uint64, cacheType string) *Fra
RowAttrStore: MustOpenAttrStore(),
}
f.Fragment.CacheType = cacheType
f.Fragment.RowAttrStore = f.RowAttrStore.AttrStore
f.Fragment.RowAttrStore = f.RowAttrStore
return f
}
@ -78,7 +78,7 @@ func (f *Fragment) Reopen() error {
f.Fragment = pilosa.NewFragment(path, f.Index(), f.Frame(), f.View(), f.Slice())
f.Fragment.CacheType = cacheType
f.Fragment.RowAttrStore = f.RowAttrStore.AttrStore
f.Fragment.RowAttrStore = f.RowAttrStore
if err := f.Open(); err != nil {
return err
}

View file

@ -39,7 +39,7 @@ func NewHolder() *Holder {
h := &Holder{Holder: pilosa.NewHolder()}
h.Path = path
h.Holder.LogOutput = &h.LogOutput
h.Holder.AttrStoreGenerator = boltdb.NewAttrStoreGenerator()
h.Holder.NewAttrStore = boltdb.NewAttrStore
return h
}
@ -66,7 +66,7 @@ func (h *Holder) Reopen() error {
h.Holder = pilosa.NewHolder()
h.Holder.Path = path
h.Holder.LogOutput = logOutput
h.Holder.AttrStoreGenerator = boltdb.NewAttrStoreGenerator()
h.Holder.NewAttrStore = boltdb.NewAttrStore
if err := h.Holder.Open(); err != nil {
return err
}

View file

@ -50,8 +50,8 @@ func NewMain() *Main {
m := &Main{Command: server.NewCommand(os.Stdin, os.Stdout, os.Stderr)}
m.Server.Network = *Network
m.Server.AttrStoreGenerator = boltdb.NewAttrStoreGenerator()
m.Server.Holder.AttrStoreGenerator = m.Server.AttrStoreGenerator
m.Server.NewAttrStore = NewAttrStore
m.Server.Holder.NewAttrStore = NewAttrStore
m.Config.DataDir = path
m.Config.Bind = "http://localhost:0"
m.Config.Cluster.Disabled = true
@ -139,8 +139,8 @@ func (m *Main) Reopen() error {
config := m.Config
m.Command = server.NewCommand(os.Stdin, os.Stdout, os.Stderr)
m.Server.Network = *Network
m.Server.AttrStoreGenerator = boltdb.NewAttrStoreGenerator()
m.Server.Holder.AttrStoreGenerator = m.Server.AttrStoreGenerator
m.Server.NewAttrStore = boltdb.NewAttrStore
m.Server.Holder.NewAttrStore = m.Server.NewAttrStore
m.Config = config
// Run new program.

View file

@ -26,7 +26,7 @@ import (
// View is a test wrapper for pilosa.View.
type View struct {
*pilosa.View
RowAttrStore *test.AttrStore
RowAttrStore pilosa.AttrStore
}
// NewView returns a new instance of View with a temporary path.
@ -40,7 +40,7 @@ func NewView(index, frame, name string) *View {
View: pilosa.NewView(path, index, frame, name, pilosa.DefaultCacheSize),
RowAttrStore: test.MustOpenAttrStore(),
}
v.View.RowAttrStore = v.RowAttrStore.AttrStore
v.View.RowAttrStore = v.RowAttrStore
return v
}
@ -68,7 +68,7 @@ func (v *View) Reopen() error {
}
v.View = pilosa.NewView(path, v.Index(), v.Frame(), v.Name(), pilosa.DefaultCacheSize)
v.View.RowAttrStore = v.RowAttrStore.AttrStore
v.View.RowAttrStore = v.RowAttrStore
if err := v.Open(); err != nil {
return err
}