probo/store/file/file.go

69 lines
1.3 KiB
Go

package file
import (
"errors"
"path/filepath"
"sync"
"git.andreafazzi.eu/andrea/probo/hasher/sha256"
"git.andreafazzi.eu/andrea/probo/store/memory"
)
var (
ErrorMetaHeaderIsNotPresent = errors.New("Meta header was not found in file.")
DefaultQuizzesDir = "quizzes"
DefaultCollectionsDir = "collections"
)
type FileProboCollectorStore struct {
Dir string
memoryStore *memory.MemoryProboCollectorStore
quizzesPaths map[string]string
collectionsPaths map[string]string
quizzesDir string
collectionsDir string
// A mutex is used to synchronize read/write access to the map
lock sync.RWMutex
}
func NewFileProboCollectorStore(dirname string) (*FileProboCollectorStore, error) {
s := new(FileProboCollectorStore)
s.Dir = dirname
s.quizzesDir = filepath.Join(s.Dir, DefaultQuizzesDir)
s.collectionsDir = filepath.Join(s.Dir, DefaultCollectionsDir)
err := s.Reindex()
if err != nil {
return nil, err
}
return s, nil
}
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
}
return nil
}