Skip to content

Commit

Permalink
refactor: Refactor solver store helpers (#449)
Browse files Browse the repository at this point in the history
* refactor: Rename types.go to store.go

* refactor: Move GetMatchID to parent for reuse

We will also want this helper function in the database implementation.

* refactor: Move log writer creation to helper function
  • Loading branch information
bgins authored Nov 25, 2024
1 parent bff3def commit f909cfc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
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
}

0 comments on commit f909cfc

Please sign in to comment.