2019-12-19 12:39:42 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import "errors"
|
|
|
|
import "io/ioutil"
|
|
|
|
import "os"
|
|
|
|
import "strings"
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
import "github.com/sneak/feta/toot"
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// TootStorageBackend is the interface to which storage backends must
|
|
|
|
// conform for storing toots
|
2019-12-19 12:39:42 +00:00
|
|
|
type TootStorageBackend interface {
|
|
|
|
TootExists(t toot.Toot) bool
|
|
|
|
StoreToot(t toot.Toot) error
|
|
|
|
StoreToots(tc []*toot.Toot) error
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// TootFSStorage is a TootStorageBackend that writes to the local
|
|
|
|
// filesystem.
|
2019-12-19 12:39:42 +00:00
|
|
|
type TootFSStorage struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// NewTootFSStorage returns a *TootFSStorage for writing toots to the
|
|
|
|
// local filesystem
|
2019-12-19 12:39:42 +00:00
|
|
|
func NewTootFSStorage(root string) *TootFSStorage {
|
|
|
|
ts := new(TootFSStorage)
|
|
|
|
ts.root = root
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// StoreToots writes a slice of pointers to toots to disk
|
2019-12-19 12:39:42 +00:00
|
|
|
func (ts *TootFSStorage) StoreToots(tc []*toot.Toot) error {
|
|
|
|
var returnErrors []string
|
|
|
|
for _, item := range tc {
|
|
|
|
err := ts.StoreToot(*item)
|
|
|
|
if err != nil {
|
|
|
|
returnErrors = append(returnErrors, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(returnErrors) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New(strings.Join(returnErrors, "; "))
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// TootExists checks to see if we have already written a toot to disk or
|
|
|
|
// not. Note that the ingester de-dupes with a table in memory so that this
|
|
|
|
// will only really get used on app restarts
|
2019-12-19 12:39:42 +00:00
|
|
|
func (ts *TootFSStorage) TootExists(t toot.Toot) bool {
|
|
|
|
path := t.DiskStoragePath()
|
|
|
|
full := ts.root + "/" + path
|
|
|
|
_, err := os.Stat(full)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// StoreToot writes a single toot to disk
|
2019-12-19 12:39:42 +00:00
|
|
|
func (ts *TootFSStorage) StoreToot(t toot.Toot) error {
|
|
|
|
path := t.DiskStoragePath()
|
|
|
|
full := ts.root + "/" + path
|
|
|
|
return ioutil.WriteFile(full, t.Original, 0644)
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// TootMemoryStorage is a TootStorageBackend that just stores all ingested
|
|
|
|
// toots in ram forever until the computer fills up and catches fire and explodes
|
2019-12-19 12:39:42 +00:00
|
|
|
type TootMemoryStorage struct {
|
|
|
|
sync.Mutex
|
2019-12-19 13:20:23 +00:00
|
|
|
toots map[toot.Hash]toot.Toot
|
2019-12-19 12:39:42 +00:00
|
|
|
//maxSize uint // FIXME support eviction
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// NewTootMemoryStorage returns a *TootMemoryStorage for storing toots in
|
|
|
|
// ram forever
|
2019-12-19 12:39:42 +00:00
|
|
|
func NewTootMemoryStorage() *TootMemoryStorage {
|
|
|
|
ts := new(TootMemoryStorage)
|
2019-12-19 13:20:23 +00:00
|
|
|
ts.toots = make(map[toot.Hash]toot.Toot)
|
2019-12-19 12:39:42 +00:00
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// StoreToot saves a single toot into an in-memory hashtable
|
2019-12-19 12:39:42 +00:00
|
|
|
func (ts *TootMemoryStorage) StoreToot(t toot.Toot) {
|
2019-12-19 13:20:23 +00:00
|
|
|
if ts.TootExists(t) {
|
2019-12-19 12:39:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ts.Lock()
|
|
|
|
defer ts.Unlock()
|
2019-12-19 13:20:23 +00:00
|
|
|
ts.toots[t.Hash] = t
|
2019-12-19 12:39:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:20:23 +00:00
|
|
|
// TootExists checks to see if we have a toot in memory already
|
|
|
|
func (ts *TootMemoryStorage) TootExists(t toot.Toot) bool {
|
2019-12-19 12:39:42 +00:00
|
|
|
ts.Lock()
|
|
|
|
defer ts.Unlock()
|
2019-12-19 13:20:23 +00:00
|
|
|
if _, ok := ts.toots[t.Hash]; ok { //this syntax is so gross
|
2019-12-19 12:39:42 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|