probo/store/memory/memory.go

182 lines
4.1 KiB
Go
Raw Normal View History

2022-06-24 18:56:06 +02:00
package memory
import (
2022-06-29 16:08:55 +02:00
"fmt"
2022-06-24 18:56:06 +02:00
"sync"
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/hasher"
"git.andreafazzi.eu/andrea/probo/models"
2022-06-27 15:11:37 +02:00
"github.com/google/uuid"
2022-06-24 18:56:06 +02:00
)
2022-06-29 16:08:55 +02:00
type MemoryProboCollectorStore struct {
quizzes map[string]*models.Quiz
2022-06-24 18:56:06 +02:00
2022-06-29 16:08:55 +02:00
questionsHashes map[string]*models.Question
answersHashes map[string]*models.Answer
quizzesHashes map[string]*models.Quiz
hasher hasher.Hasher
2022-06-24 18:56:06 +02:00
// A mutex is used to synchronize read/write access to the map
lock sync.RWMutex
}
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)
return s
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) getQuizFromHash(hash string) *models.Quiz {
s.lock.RLock()
defer s.lock.RUnlock()
quiz, ok := s.quizzesHashes[hash]
if ok {
return quiz
}
return nil
}
func (s *MemoryProboCollectorStore) getQuizFromID(id string) *models.Quiz {
2022-06-24 18:56:06 +02:00
s.lock.RLock()
defer s.lock.RUnlock()
quiz, ok := s.quizzes[id]
if ok {
return quiz
}
return nil
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) getQuestionFromHash(hash string) *models.Question {
2022-06-24 18:56:06 +02:00
s.lock.RLock()
defer s.lock.RUnlock()
2022-06-29 16:08:55 +02:00
question, ok := s.questionsHashes[hash]
2022-06-24 18:56:06 +02:00
if ok {
return question
}
return nil
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) getAnswerFromHash(hash string) *models.Answer {
2022-06-24 18:56:06 +02:00
s.lock.RLock()
defer s.lock.RUnlock()
2022-06-29 16:08:55 +02:00
answer, ok := s.answersHashes[hash]
2022-06-24 18:56:06 +02:00
if ok {
return answer
}
return nil
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) createQuizFromHash(id string, hash string, quiz *models.Quiz) *models.Quiz {
2022-06-24 18:56:06 +02:00
s.lock.Lock()
defer s.lock.Unlock()
quiz.ID = id
2023-06-28 17:21:59 +02:00
quiz.Hash = hash
2022-06-29 16:08:55 +02:00
s.quizzesHashes[hash] = quiz
s.quizzes[id] = quiz
2022-06-24 18:56:06 +02:00
return quiz
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) createQuestionFromHash(hash string, question *models.Question) *models.Question {
2022-06-24 18:56:06 +02:00
s.lock.Lock()
defer s.lock.Unlock()
2022-06-29 16:08:55 +02:00
s.questionsHashes[hash] = question
2022-06-24 18:56:06 +02:00
return question
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) createAnswerFromHash(hash string, answer *models.Answer) *models.Answer {
2022-06-24 18:56:06 +02:00
s.lock.Lock()
defer s.lock.Unlock()
2022-06-29 16:08:55 +02:00
s.answersHashes[hash] = answer
2022-06-24 18:56:06 +02:00
return answer
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
2022-06-24 18:56:06 +02:00
result := make([]*models.Quiz, 0)
2022-09-06 14:41:32 +02:00
for id := range s.quizzes {
result = append(result, s.getQuizFromID(id))
2022-06-24 18:56:06 +02:00
}
return result, nil
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) createOrUpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) {
2022-06-28 13:49:35 +02:00
hashes := s.hasher.QuizHashes(r.Quiz)
quizHash := hashes[len(hashes)-1]
2022-06-29 16:08:55 +02:00
quiz := s.getQuizFromHash(quizHash)
2022-06-28 13:49:35 +02:00
if quiz != nil { // Quiz is already present in the store
return quiz, nil
}
2022-06-29 16:08:55 +02:00
if id != "" {
quiz = s.getQuizFromID(id)
if quiz == nil { // Quiz is already present in the store
return nil, fmt.Errorf("Quiz ID %v doesn't exist in the store!", id)
}
} else {
id = uuid.New().String()
quiz = new(models.Quiz)
}
2022-06-28 13:49:35 +02:00
questionHash := hashes[0]
2022-06-29 16:08:55 +02:00
q := s.getQuestionFromHash(questionHash)
2022-06-28 13:49:35 +02:00
if q == nil { // if the question is not in the store then we should add it
2022-06-29 16:08:55 +02:00
q = s.createQuestionFromHash(questionHash, &models.Question{
2022-06-28 13:49:35 +02:00
ID: uuid.New().String(),
2022-06-29 16:08:55 +02:00
Text: r.Quiz.Question.Text,
2022-06-28 13:49:35 +02:00
})
}
// Populate Question field
quiz.Question = q
2022-06-29 16:08:55 +02:00
for i, answer := range r.Quiz.Answers {
2022-06-28 13:49:35 +02:00
answerHash := hashes[i+1]
2022-06-29 16:08:55 +02:00
a := s.getAnswerFromHash(answerHash)
2022-06-28 13:49:35 +02:00
if a == nil { // if the answer is not in the store add it
2022-06-29 16:08:55 +02:00
a = s.createAnswerFromHash(answerHash, &models.Answer{
2022-06-28 13:49:35 +02:00
ID: uuid.New().String(),
Text: answer.Text,
})
2022-06-29 16:08:55 +02:00
}
if answer.Correct {
2022-09-06 14:41:32 +02:00
quiz.Correct = a
2022-06-28 13:49:35 +02:00
}
quiz.Answers = append(quiz.Answers, a)
}
2022-06-29 16:08:55 +02:00
return s.createQuizFromHash(id, quizHash, quiz), nil
2022-06-28 13:49:35 +02:00
}
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) CreateQuiz(r *client.CreateUpdateQuizRequest) (*models.Quiz, error) {
return s.createOrUpdateQuiz(r, "")
}
2022-06-24 18:56:06 +02:00
2022-06-29 16:08:55 +02:00
func (s *MemoryProboCollectorStore) UpdateQuiz(r *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) {
return s.createOrUpdateQuiz(r, id)
2022-06-24 18:56:06 +02:00
}