1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2026-01-28 05:55:56 +00:00

Refactor datastore into it's own package for #67

This commit is contained in:
James Hillyerd
2017-12-26 14:54:49 -08:00
parent cc47895d71
commit 3a4fd3f093
17 changed files with 93 additions and 84 deletions

View File

@@ -12,6 +12,7 @@ import (
"time"
"github.com/jhillyerd/inbucket/config"
"github.com/jhillyerd/inbucket/datastore"
"github.com/jhillyerd/inbucket/log"
)
@@ -55,7 +56,7 @@ type FileDataStore struct {
}
// NewFileDataStore creates a new DataStore object using the specified path
func NewFileDataStore(cfg config.DataStoreConfig) DataStore {
func NewFileDataStore(cfg config.DataStoreConfig) datastore.DataStore {
path := cfg.Path
if path == "" {
log.Errorf("No value configured for datastore path")
@@ -73,14 +74,14 @@ func NewFileDataStore(cfg config.DataStoreConfig) DataStore {
// DefaultFileDataStore creates a new DataStore object. It uses the inbucket.Config object to
// construct it's path.
func DefaultFileDataStore() DataStore {
func DefaultFileDataStore() datastore.DataStore {
cfg := config.GetDataStoreConfig()
return NewFileDataStore(cfg)
}
// MailboxFor retrieves the Mailbox object for a specified email address, if the mailbox
// does not exist, it will attempt to create it.
func (ds *FileDataStore) MailboxFor(emailAddress string) (Mailbox, error) {
func (ds *FileDataStore) MailboxFor(emailAddress string) (datastore.Mailbox, error) {
name, err := ParseMailboxName(emailAddress)
if err != nil {
return nil, err
@@ -96,8 +97,8 @@ func (ds *FileDataStore) MailboxFor(emailAddress string) (Mailbox, error) {
}
// AllMailboxes returns a slice with all Mailboxes
func (ds *FileDataStore) AllMailboxes() ([]Mailbox, error) {
mailboxes := make([]Mailbox, 0, 100)
func (ds *FileDataStore) AllMailboxes() ([]datastore.Mailbox, error) {
mailboxes := make([]datastore.Mailbox, 0, 100)
infos1, err := ioutil.ReadDir(ds.mailPath)
if err != nil {
return nil, err
@@ -159,14 +160,14 @@ func (mb *FileMailbox) String() string {
// GetMessages scans the mailbox directory for .gob files and decodes them into
// a slice of Message objects.
func (mb *FileMailbox) GetMessages() ([]Message, error) {
func (mb *FileMailbox) GetMessages() ([]datastore.Message, error) {
if !mb.indexLoaded {
if err := mb.readIndex(); err != nil {
return nil, err
}
}
messages := make([]Message, len(mb.messages))
messages := make([]datastore.Message, len(mb.messages))
for i, m := range mb.messages {
messages[i] = m
}
@@ -174,7 +175,7 @@ func (mb *FileMailbox) GetMessages() ([]Message, error) {
}
// GetMessage decodes a single message by Id and returns a Message object
func (mb *FileMailbox) GetMessage(id string) (Message, error) {
func (mb *FileMailbox) GetMessage(id string) (datastore.Message, error) {
if !mb.indexLoaded {
if err := mb.readIndex(); err != nil {
return nil, err
@@ -191,7 +192,7 @@ func (mb *FileMailbox) GetMessage(id string) (Message, error) {
}
}
return nil, ErrNotExist
return nil, datastore.ErrNotExist
}
// Purge deletes all messages in this mailbox