Skip to content

Commit 4a5e0a5

Browse files
because this should always return a namespace, we will give up
1 parent 317a58c commit 4a5e0a5

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

world/world.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
)
1010

1111
var (
12-
NamespaceErrorNotFound = errors.New("namespace not found")
12+
NamespaceErrorNotFound = errors.New("namespace not found")
13+
ErrFailedToCreateNamespace = errors.New("failed to create namespace")
1314
)
1415

1516
type Stats struct {
@@ -36,9 +37,8 @@ func NewWorld() *World {
3637

3738
func (m *World) Delete(ns string, locId string) {
3839
namespace := m.getNamespace(ns)
39-
4040
if namespace == nil {
41-
return
41+
panic(NamespaceErrorNotFound)
4242
}
4343

4444
namespace.DeleteLocation(locId)
@@ -49,7 +49,7 @@ func (m *World) Save(ns string, locId string, lat float64, lon float64) error {
4949
namespace := m.getNamespace(ns)
5050

5151
if namespace == nil {
52-
return NamespaceErrorNotFound
52+
panic(NamespaceErrorNotFound)
5353
}
5454

5555
_, err := namespace.SaveLocation(locId, lat, lon)
@@ -58,17 +58,22 @@ func (m *World) Save(ns string, locId string, lat float64, lon float64) error {
5858
}
5959

6060
func (m *World) getNamespace(ns string) *Namespace {
61-
m.mu.RLock()
61+
m.mu.Lock()
62+
6263
namespace, ok := m.namespaces[ns]
63-
m.mu.RUnlock()
6464

6565
if !ok {
6666
namespace = NewNamespace(ns)
67-
m.mu.Lock()
6867
m.namespaces[ns] = namespace
68+
}
69+
70+
if namespace == nil {
6971
m.mu.Unlock()
72+
panic(ErrFailedToCreateNamespace)
7073
}
7174

75+
m.mu.Unlock()
76+
7277
return namespace
7378
}
7479

@@ -102,6 +107,7 @@ func NewWorldFromBytes(buf []byte) *World {
102107
func (m *World) Merge(w *World) {
103108
w.mu.Lock()
104109
defer w.mu.Unlock()
110+
105111
for ns, n := range w.namespaces {
106112
for locId, loc := range n.locations {
107113
err := m.Save(ns, locId, loc.Lat(), loc.Lon())
@@ -116,7 +122,7 @@ func (m *World) GetLocation(ns string, id string) (Location, bool) {
116122
namespace := m.getNamespace(ns)
117123

118124
if namespace == nil {
119-
return Location{}, false
125+
panic(NamespaceErrorNotFound)
120126
}
121127

122128
location, ok := namespace.GetLocation(id)
@@ -131,7 +137,7 @@ func (m *World) QueryRange(ns string, lat1, lat2, lon1, lon2 float64) []*Locatio
131137
namespace := m.getNamespace(ns)
132138

133139
if namespace == nil {
134-
return []*Location{}
140+
panic(NamespaceErrorNotFound)
135141
}
136142

137143
return namespace.QueryRange(lat1, lat2, lon1, lon2)

0 commit comments

Comments
 (0)