Files
poseidon/pkg/execution/local_storage.go
sirkrypt0 36dc99f019 Don't embed the execution.Storer interface into a runner
Previously, the execution.Storer interface was embedded in the Runner interface.
However, this resulted in calls like runner.Add(...) to add an execution to the
store which is kind of ugly. Thus, we decided to add only the required functions to
the runner interface and make the execution.Storer a field of the implementation.
2021-07-30 14:28:06 +02:00

43 lines
1.0 KiB
Go

package execution
import (
"gitlab.hpi.de/codeocean/codemoon/poseidon/pkg/dto"
"sync"
)
// localStorage stores execution objects in the local application memory.
// ToDo: Create implementation that use some persistent storage like a database.
type localStorage struct {
sync.RWMutex
executions map[ID]*dto.ExecutionRequest
}
// NewLocalStorage responds with an Storer implementation.
// This implementation stores the data thread-safe in the local application memory.
func NewLocalStorage() *localStorage {
return &localStorage{
executions: make(map[ID]*dto.ExecutionRequest),
}
}
func (s *localStorage) Add(id ID, executionRequest *dto.ExecutionRequest) {
s.Lock()
defer s.Unlock()
s.executions[id] = executionRequest
}
func (s *localStorage) Exists(id ID) bool {
s.Lock()
defer s.Unlock()
_, ok := s.executions[id]
return ok
}
func (s *localStorage) Pop(id ID) (*dto.ExecutionRequest, bool) {
s.Lock()
defer s.Unlock()
request, ok := s.executions[id]
delete(s.executions, id)
return request, ok
}