Refactoring and dashboard state in DB
This commit is contained in:
+43
-100
@@ -9,106 +9,19 @@ import (
|
||||
"git.phc.dm.unipi.it/phc/storage/utils"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
// Create a new object by reading from the given io.Reader and returns its new id
|
||||
Create(r io.Reader) (string, error)
|
||||
const RandomIdSize = 32
|
||||
|
||||
// Read the object identified by "id" into the given io.Writer
|
||||
Read(id string, w io.Writer) error
|
||||
|
||||
// Update the object identified by "id" by reading from the given io.Reader
|
||||
Update(id string, r io.Reader) error
|
||||
|
||||
// Delete the object with the given id
|
||||
Delete(id string) error
|
||||
func split(baseDir string, prefixSize int, id string) (prefix, rest string) {
|
||||
return id[:prefixSize], id[prefixSize:]
|
||||
}
|
||||
|
||||
//
|
||||
// In Memory Byte Store
|
||||
//
|
||||
func Create(baseDir string, prefixSize int, r io.Reader) (string, error) {
|
||||
id := utils.GenerateRandomString(RandomIdSize)
|
||||
prefix, rest := split(baseDir, prefixSize, id)
|
||||
|
||||
type memStore struct {
|
||||
objects map[string][]byte
|
||||
}
|
||||
os.MkdirAll(path.Join(baseDir, prefix), os.ModePerm)
|
||||
|
||||
func NewMemStore() Store {
|
||||
return &memStore{
|
||||
objects: map[string][]byte{},
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new object by reading from the given io.Reader and returns its new id
|
||||
func (m *memStore) Create(r io.Reader) (string, error) {
|
||||
id := utils.GenerateRandomString(16)
|
||||
|
||||
data, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
m.objects[id] = data
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Read the object identified by "id" into the given io.Writer
|
||||
func (m *memStore) Read(id string, w io.Writer) error {
|
||||
data, found := m.objects[id]
|
||||
if !found {
|
||||
return fmt.Errorf("object with id %q not found", id)
|
||||
}
|
||||
|
||||
if _, err := w.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update the object identified by "id" by reading from the given io.Reader
|
||||
func (m *memStore) Update(id string, r io.Reader) error {
|
||||
data, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.objects[id] = data
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete the object with the given id
|
||||
func (m *memStore) Delete(id string) error {
|
||||
if _, found := m.objects[id]; !found {
|
||||
return fmt.Errorf("object with id %q not found", id)
|
||||
}
|
||||
|
||||
delete(m.objects, id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// Dir Store
|
||||
//
|
||||
|
||||
type DirStore struct {
|
||||
// BaseDir is the root folder of this Store
|
||||
BaseDir string
|
||||
|
||||
// Prefix is the number of letters to use for the first layer of folders
|
||||
Prefix int
|
||||
}
|
||||
|
||||
func (d *DirStore) split(id string) (prefix, rest string) {
|
||||
return id[:d.Prefix], id[d.Prefix:]
|
||||
}
|
||||
|
||||
func (d *DirStore) Create(r io.Reader) (string, error) {
|
||||
id := utils.GenerateRandomString(16)
|
||||
prefix, rest := d.split(id)
|
||||
|
||||
os.MkdirAll(path.Join(d.BaseDir, prefix), os.ModePerm)
|
||||
|
||||
f, err := os.Create(path.Join(d.BaseDir, prefix, rest))
|
||||
f, err := os.Create(path.Join(baseDir, prefix, rest))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -120,10 +33,10 @@ func (d *DirStore) Create(r io.Reader) (string, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (d *DirStore) Read(id string, w io.Writer) error {
|
||||
prefix, rest := d.split(id)
|
||||
func Read(baseDir string, prefixSize int, id string, w io.Writer) error {
|
||||
prefix, rest := split(baseDir, prefixSize, id)
|
||||
|
||||
f, err := os.Open(path.Join(d.BaseDir, prefix, rest))
|
||||
f, err := os.Open(path.Join(baseDir, prefix, rest))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -135,10 +48,40 @@ func (d *DirStore) Read(id string, w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DirStore) Update(id string, r io.Reader) error {
|
||||
func All(baseDir string) ([]string, error) {
|
||||
entries, err := os.ReadDir(baseDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
objects := []string{}
|
||||
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() {
|
||||
return nil, fmt.Errorf(`invalid store structure`)
|
||||
}
|
||||
|
||||
objectsEntries, err := os.ReadDir(path.Join(baseDir, e.Name()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, obj := range objectsEntries {
|
||||
if obj.IsDir() {
|
||||
return nil, fmt.Errorf(`invalid store structure`)
|
||||
}
|
||||
|
||||
objects = append(objects, e.Name()+obj.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
func Update(baseDir string, prefixSize int, id string, r io.Reader) error {
|
||||
panic("TODO: Not implemented")
|
||||
}
|
||||
|
||||
func (d *DirStore) Delete(id string) error {
|
||||
func Delete(baseDir string, prefixSize int, id string) error {
|
||||
panic("TODO: Not implemented")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user