2022-06-24 18:56:06 +02:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.andreafazzi.eu/andrea/probo/hasher"
|
|
|
|
"git.andreafazzi.eu/andrea/probo/models"
|
2023-11-13 21:01:12 +01:00
|
|
|
"git.andreafazzi.eu/andrea/probo/store"
|
2022-06-24 18:56:06 +02:00
|
|
|
)
|
|
|
|
|
2023-11-13 21:01:12 +01:00
|
|
|
type Store[T Storable] struct {
|
|
|
|
ids map[string]T
|
|
|
|
hashes map[string]T
|
2023-11-05 14:36:33 +01:00
|
|
|
|
2023-11-13 21:01:12 +01:00
|
|
|
// A mutex is used to synchronize read/write access to the map
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
2023-11-05 14:36:33 +01:00
|
|
|
|
2023-11-13 21:01:12 +01:00
|
|
|
func NewStore[T store.Storable]() *Store[T] {
|
|
|
|
store := new(Store[T])
|
2022-06-29 16:08:55 +02:00
|
|
|
|
2023-11-13 21:01:12 +01:00
|
|
|
store.ids = make(map[string]T)
|
2022-06-24 18:56:06 +02:00
|
|
|
|
2023-11-13 21:01:12 +01:00
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
type QuizStore struct {
|
|
|
|
*Store[*Quiz]
|
|
|
|
|
|
|
|
questions *Store[*Question]
|
|
|
|
answers *Store[*Answer]
|
2022-06-24 18:56:06 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 16:08:55 +02:00
|
|
|
func NewMemoryProboCollectorStore(hasher hasher.Hasher) *MemoryProboCollectorStore {
|
|
|
|
s := new(MemoryProboCollectorStore)
|
2022-06-24 18:56:06 +02:00
|
|
|
|
|
|
|
s.hasher = hasher
|
2022-06-29 16:08:55 +02:00
|
|
|
|
|
|
|
s.questionsHashes = make(map[string]*models.Question)
|
|
|
|
s.answersHashes = make(map[string]*models.Answer)
|
|
|
|
s.quizzesHashes = make(map[string]*models.Quiz)
|
|
|
|
|
2022-06-24 18:56:06 +02:00
|
|
|
s.quizzes = make(map[string]*models.Quiz)
|
2023-10-07 11:43:12 +02:00
|
|
|
s.collections = make(map[string]*models.Collection)
|
2023-11-05 14:36:33 +01:00
|
|
|
s.participants = make(map[string]*models.Participant)
|
2022-06-24 18:56:06 +02:00
|
|
|
|
|
|
|
return s
|
|
|
|
}
|