1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2026-01-07 19:57:06 +00:00

storage: More refactoring for #69

- retention: Start from pkg main instead of server/smtp
- file: Remove DefaultStore() constructor
- storage: AllMailboxes replaced with VisitMailboxes for #69
- test: Stub VisitMailboxes for #80
This commit is contained in:
James Hillyerd
2018-03-10 22:05:10 -08:00
parent 9c18f1fb30
commit d9b5e40c87
9 changed files with 106 additions and 107 deletions

View File

@@ -74,13 +74,6 @@ func New(cfg config.DataStoreConfig) storage.Store {
return &Store{path: path, mailPath: mailPath, messageCap: cfg.MailboxMsgCap}
}
// DefaultStore creates a new DataStore object. It uses the inbucket.Config object to
// construct it's path.
func DefaultStore() storage.Store {
cfg := config.GetDataStoreConfig()
return New(cfg)
}
// GetMessage returns the messages in the named mailbox, or an error.
func (fs *Store) GetMessage(mailbox, id string) (storage.Message, error) {
mb, err := fs.MailboxFor(mailbox)
@@ -125,12 +118,12 @@ func (fs *Store) MailboxFor(emailAddress string) (storage.Mailbox, error) {
indexPath: indexPath}, nil
}
// AllMailboxes returns a slice with all Mailboxes
func (fs *Store) AllMailboxes() ([]storage.Mailbox, error) {
mailboxes := make([]storage.Mailbox, 0, 100)
// VisitMailboxes accepts a function that will be called with the messages in each mailbox while it
// continues to return true.
func (fs *Store) VisitMailboxes(f func([]storage.Message) (cont bool)) error {
infos1, err := ioutil.ReadDir(fs.mailPath)
if err != nil {
return nil, err
return err
}
// Loop over level 1 directories
for _, inf1 := range infos1 {
@@ -138,7 +131,7 @@ func (fs *Store) AllMailboxes() ([]storage.Mailbox, error) {
l1 := inf1.Name()
infos2, err := ioutil.ReadDir(filepath.Join(fs.mailPath, l1))
if err != nil {
return nil, err
return err
}
// Loop over level 2 directories
for _, inf2 := range infos2 {
@@ -146,7 +139,7 @@ func (fs *Store) AllMailboxes() ([]storage.Mailbox, error) {
l2 := inf2.Name()
infos3, err := ioutil.ReadDir(filepath.Join(fs.mailPath, l1, l2))
if err != nil {
return nil, err
return err
}
// Loop over mailboxes
for _, inf3 := range infos3 {
@@ -156,15 +149,20 @@ func (fs *Store) AllMailboxes() ([]storage.Mailbox, error) {
idx := filepath.Join(mbpath, indexFileName)
mb := &Mailbox{store: fs, dirName: mbdir, path: mbpath,
indexPath: idx}
mailboxes = append(mailboxes, mb)
msgs, err := mb.GetMessages()
if err != nil {
return err
}
if !f(msgs) {
return nil
}
}
}
}
}
}
}
return mailboxes, nil
return nil
}
// LockFor returns the RWMutex for this mailbox, or an error.

View File

@@ -12,6 +12,7 @@ import (
"time"
"github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/storage"
"github.com/stretchr/testify/assert"
)
@@ -97,12 +98,12 @@ func TestFSDirStructure(t *testing.T) {
}
}
// Test FileDataStore.AllMailboxes()
func TestFSAllMailboxes(t *testing.T) {
// TestFSVisitMailboxes tests VisitMailboxes
func TestFSVisitMailboxes(t *testing.T) {
ds, logbuf := setupDataStore(config.DataStoreConfig{})
defer teardownDataStore(ds)
for _, name := range []string{"abby", "bill", "christa", "donald", "evelyn"} {
boxes := []string{"abby", "bill", "christa", "donald", "evelyn"}
for _, name := range boxes {
// Create day old message
date := time.Now().Add(-24 * time.Hour)
deliverMessage(ds, name, "Old Message", date)
@@ -112,9 +113,17 @@ func TestFSAllMailboxes(t *testing.T) {
deliverMessage(ds, name, "New Message", date)
}
mboxes, err := ds.AllMailboxes()
seen := 0
err := ds.VisitMailboxes(func(messages []storage.Message) bool {
seen++
count := len(messages)
if count != 2 {
t.Errorf("got: %v messages, want: 2", count)
}
return true
})
assert.Nil(t, err)
assert.Equal(t, len(mboxes), 5)
assert.Equal(t, 5, seen)
if t.Failed() {
// Wait for handler to finish logging