Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Refactor solver store helpers #449

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions pkg/solver/store/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package store

import (
"fmt"
"os"
"strings"
"sync"

Expand All @@ -21,20 +20,11 @@ type SolverStoreMemory struct {
logWriters map[string]jsonl.Writer
}

func getMatchID(resourceOffer string, jobOffer string) string {
return fmt.Sprintf("%s-%s", resourceOffer, jobOffer)
}

func NewSolverStoreMemory() (*SolverStoreMemory, error) {
logWriters := make(map[string]jsonl.Writer)

kinds := []string{"job_offers", "resource_offers", "deals", "decisions", "results"}
for k := range kinds {
logfile, err := os.OpenFile(fmt.Sprintf("/var/tmp/lilypad_%s.jsonl", kinds[k]), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
logWriters[kinds[k]] = jsonl.NewWriter(logfile)
logWriters, err := store.GetLogWriters(kinds)
if err != nil {
return nil, err
}

return &SolverStoreMemory{
Expand Down Expand Up @@ -84,7 +74,7 @@ func (s *SolverStoreMemory) AddResult(result data.Result) (*data.Result, error)
func (s *SolverStoreMemory) AddMatchDecision(resourceOffer string, jobOffer string, deal string, result bool) (*data.MatchDecision, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
id := getMatchID(resourceOffer, jobOffer)
id := store.GetMatchID(resourceOffer, jobOffer)
_, ok := s.matchDecisionMap[id]
if ok {
return nil, fmt.Errorf("that match already exists")
Expand Down Expand Up @@ -245,7 +235,7 @@ func (s *SolverStoreMemory) GetResult(id string) (*data.Result, error) {
func (s *SolverStoreMemory) GetMatchDecision(resourceOffer string, jobOffer string) (*data.MatchDecision, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
id := getMatchID(resourceOffer, jobOffer)
id := store.GetMatchID(resourceOffer, jobOffer)
decision, ok := s.matchDecisionMap[id]
if !ok {
return nil, nil
Expand Down
26 changes: 25 additions & 1 deletion pkg/solver/store/types.go → pkg/solver/store/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package store

import "github.com/lilypad-tech/lilypad/pkg/data"
import (
"fmt"
"os"

"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/lilypad-tech/lilypad/pkg/jsonl"
)

type GetJobOffersQuery struct {
JobCreator string `json:"job_creator"`
Expand Down Expand Up @@ -71,3 +77,21 @@ type SolverStore interface {
RemoveJobOffer(id string) error
RemoveResourceOffer(id string) error
}

func GetMatchID(resourceOffer string, jobOffer string) string {
return fmt.Sprintf("%s-%s", resourceOffer, jobOffer)
}

func GetLogWriters(kinds []string) (map[string]jsonl.Writer, error) {
logWriters := make(map[string]jsonl.Writer)

for k := range kinds {
logfile, err := os.OpenFile(fmt.Sprintf("/var/tmp/lilypad_%s.jsonl", kinds[k]), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil, err
}
logWriters[kinds[k]] = jsonl.NewWriter(logfile)
}

return logWriters, nil
}
Loading