Skip to content

Commit

Permalink
backend: provide a safer, less efficient way of querying the InMemory…
Browse files Browse the repository at this point in the history
…Database
  • Loading branch information
roobre committed Jul 14, 2023
1 parent 0282fa8 commit 3846126
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
18 changes: 14 additions & 4 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,21 @@ type InMemoryDatabase struct {
}

// Transaction provides a thread-safe, read-only view of the data in the database.
func (db *InMemoryDatabase) Transaction(readF func(data *Data)) {
func (db *InMemoryDatabase) Transaction(readF func(data Data)) {
db.mx.Lock()
defer db.mx.Unlock()

readF(&db.data)
dataCopy := Data{}
dataCopy.Doughs = append(dataCopy.Doughs, db.data.Doughs...)
dataCopy.OliveOils = append(dataCopy.OliveOils, db.data.OliveOils...)
dataCopy.Tomatoes = append(dataCopy.Tomatoes, db.data.Tomatoes...)
dataCopy.Mozzarellas = append(dataCopy.Mozzarellas, db.data.Mozzarellas...)
dataCopy.Toppings = append(dataCopy.Toppings, db.data.Toppings...)
dataCopy.Tools = append(dataCopy.Tools, db.data.Tools...)
dataCopy.Adjectives = append(dataCopy.Adjectives, db.data.Adjectives...)
dataCopy.ClassicNames = append(dataCopy.ClassicNames, db.data.ClassicNames...)
dataCopy.Quotes = append(dataCopy.Quotes, db.data.Quotes...)
db.mx.Unlock()

readF(dataCopy)
}

func (db *InMemoryDatabase) PopulateFromFile(path string) error {
Expand Down
30 changes: 15 additions & 15 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,16 @@ func (s *Server) WithCatalog(db *database.InMemoryDatabase) *Server {
isVegetarian := r.URL.Query().Get("is_vegetarian")

var ingredients []pizza.Ingredient
db.Transaction(func(data *database.Data) {
db.Transaction(func(data database.Data) {
switch ingredientType {
case "olive_oil":
ingredients = append(ingredients, data.OliveOils...)
ingredients = data.OliveOils
case "tomato":
ingredients = append(ingredients, data.Tomatoes...)
ingredients = data.Tomatoes
case "mozzarella":
ingredients = append(ingredients, data.Mozzarellas...)
ingredients = data.Mozzarellas
case "topping":
ingredients = append(ingredients, data.Toppings...)
ingredients = data.Toppings
}
})

Expand Down Expand Up @@ -299,8 +299,8 @@ func (s *Server) WithCatalog(db *database.InMemoryDatabase) *Server {
logger.Info("Doughs requested")

var doughs []pizza.Dough
db.Transaction(func(data *database.Data) {
doughs = append(doughs, data.Doughs...)
db.Transaction(func(data database.Data) {
doughs = data.Doughs
})

err := json.NewEncoder(w).Encode(map[string][]pizza.Dough{"doughs": doughs})
Expand All @@ -316,8 +316,8 @@ func (s *Server) WithCatalog(db *database.InMemoryDatabase) *Server {
logger.Info("Tools requested")

var tools []string
db.Transaction(func(data *database.Data) {
tools = append(tools, data.Tools...)
db.Transaction(func(data database.Data) {
tools = data.Tools
})

err := json.NewEncoder(w).Encode(map[string][]string{"tools": tools})
Expand Down Expand Up @@ -434,8 +434,8 @@ func (s *Server) WithCopy(db *database.InMemoryDatabase) *Server {
logger.Info("Quotes requested")

var quotes []string
db.Transaction(func(data *database.Data) {
quotes = append(quotes, data.Quotes...)
db.Transaction(func(data database.Data) {
quotes = data.Quotes
})

err := json.NewEncoder(w).Encode(map[string][]string{"quotes": quotes})
Expand All @@ -451,8 +451,8 @@ func (s *Server) WithCopy(db *database.InMemoryDatabase) *Server {
logger.Info("Names requested")

var names []string
db.Transaction(func(data *database.Data) {
names = append(names, data.ClassicNames...)
db.Transaction(func(data database.Data) {
names = data.ClassicNames
})

err := json.NewEncoder(w).Encode(map[string][]string{"names": names})
Expand All @@ -468,8 +468,8 @@ func (s *Server) WithCopy(db *database.InMemoryDatabase) *Server {
logger.Info("Adjectives requested")

var adjs []string
db.Transaction(func(data *database.Data) {
adjs = append(adjs, data.Adjectives...)
db.Transaction(func(data database.Data) {
adjs = data.Adjectives
})

err := json.NewEncoder(w).Encode(map[string][]string{"adjectives": adjs})
Expand Down

0 comments on commit 3846126

Please sign in to comment.