2019-12-19 12:39:42 +00:00
|
|
|
package storage
|
|
|
|
|
2020-03-27 23:02:36 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-12-19 12:39:42 +00:00
|
|
|
|
2020-03-27 23:02:36 +00:00
|
|
|
"git.eeqj.de/sneak/feta/toot"
|
|
|
|
)
|
2019-12-19 12:39:42 +00:00
|
|
|
|
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 {
|
2020-03-28 02:57:58 +00:00
|
|
|
err := ts.StoreToot(item)
|
2019-12-19 12:39:42 +00:00
|
|
|
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
|
2020-03-28 02:57:58 +00:00
|
|
|
func (ts *TootFSStorage) TootExists(t *toot.Toot) bool {
|
2019-12-19 12:39:42 +00:00
|
|
|
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
|
2020-03-28 02:57:58 +00:00
|
|
|
func (ts *TootFSStorage) StoreToot(t *toot.Toot) error {
|
2019-12-19 12:39:42 +00:00
|
|
|
path := t.DiskStoragePath()
|
|
|
|
full := ts.root + "/" + path
|
2019-12-19 15:16:13 +00:00
|
|
|
dir := filepath.Dir(full)
|
|
|
|
err := os.MkdirAll(dir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-19 12:39:42 +00:00
|
|
|
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
|
2020-03-28 02:57:58 +00:00
|
|
|
toots map[string]*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)
|
2020-03-28 02:57:58 +00:00
|
|
|
ts.toots = make(map[string]*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
|
2020-03-28 02:57:58 +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
|
2020-03-28 02:57:58 +00:00
|
|
|
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
|
|
|
|
}
|