2023-06-28 17:21:59 +02:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
2023-07-10 13:23:46 +02:00
|
|
|
"sync"
|
2023-06-28 17:21:59 +02:00
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/probo/hasher/sha256"
|
|
|
|
"git.andreafazzi.eu/andrea/probo/store/memory"
|
|
|
|
)
|
|
|
|
|
2023-10-07 11:43:12 +02:00
|
|
|
var (
|
|
|
|
ErrorMetaHeaderIsNotPresent = errors.New("Meta header was not found in file.")
|
|
|
|
|
|
|
|
DefaultQuizzesDir = "quizzes"
|
|
|
|
DefaultCollectionsDir = "collections"
|
|
|
|
)
|
2023-09-22 10:29:10 +02:00
|
|
|
|
2023-06-28 17:21:59 +02:00
|
|
|
type FileProboCollectorStore struct {
|
|
|
|
Dir string
|
|
|
|
|
|
|
|
memoryStore *memory.MemoryProboCollectorStore
|
2023-10-07 11:43:12 +02:00
|
|
|
|
|
|
|
quizzesPaths map[string]string
|
|
|
|
collectionsPaths map[string]string
|
|
|
|
|
|
|
|
quizzesDir string
|
|
|
|
collectionsDir string
|
2023-07-10 13:23:46 +02:00
|
|
|
|
|
|
|
// A mutex is used to synchronize read/write access to the map
|
|
|
|
lock sync.RWMutex
|
2023-06-28 17:21:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error) {
|
|
|
|
s := new(FileProboCollectorStore)
|
|
|
|
|
2023-07-10 13:23:46 +02:00
|
|
|
s.Dir = dirname
|
|
|
|
|
2023-10-07 11:43:12 +02:00
|
|
|
s.quizzesDir = filepath.Join(s.Dir, DefaultQuizzesDir)
|
|
|
|
s.collectionsDir = filepath.Join(s.Dir, DefaultCollectionsDir)
|
|
|
|
|
2023-07-10 13:23:46 +02:00
|
|
|
err := s.Reindex()
|
2023-06-28 17:21:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-10 13:23:46 +02:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2023-10-07 11:43:12 +02:00
|
|
|
func (s *FileProboCollectorStore) Reindex() error {
|
|
|
|
s.memoryStore = memory.NewMemoryProboCollectorStore(
|
|
|
|
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
|
|
|
|
)
|
|
|
|
|
|
|
|
s.quizzesPaths = make(map[string]string)
|
|
|
|
s.collectionsPaths = make(map[string]string)
|
|
|
|
|
|
|
|
err := s.reindexQuizzes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.reindexCollections()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-07-10 13:23:46 +02:00
|
|
|
}
|
2023-06-28 17:21:59 +02:00
|
|
|
|
2023-07-10 13:23:46 +02:00
|
|
|
return nil
|
2023-06-28 17:21:59 +02:00
|
|
|
}
|