Skip to content

Commit 80b8f61

Browse files
committed
add methods
1 parent 5a9a53e commit 80b8f61

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

dist.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,38 @@ func (d *Distributed) IsOwned(c Cell) bool {
143143
return addr == c.Host
144144
}
145145

146+
// WhereIsMyParent finds and returns parent distributed cell.
147+
// The child object must be less resolution than the parent's parent.
148+
func (d *Distributed) WhereIsMyParent(child h3.H3Index) (c Cell, err error) {
149+
d.mu.RLock()
150+
defer d.mu.RUnlock()
151+
curLevel := h3.Resolution(child)
152+
if curLevel < d.level {
153+
return c, fmt.Errorf("h3geodist: child resolution got %d, expected > %d",
154+
curLevel, d.level)
155+
}
156+
cell := h3.ToParent(child, d.level)
157+
addr, ok := d.lookup(cell)
158+
if !ok {
159+
return c, fmt.Errorf("h3geodist: distributed cell %v not found", cell)
160+
}
161+
c.H3ID = cell
162+
c.Host = addr
163+
return
164+
}
165+
166+
// LookupFromLatLon returns distributed cell.
167+
func (d *Distributed) LookupFromLatLon(lat float64, lon float64) (c Cell, err error) {
168+
d.mu.RLock()
169+
defer d.mu.RUnlock()
170+
cell := h3.FromGeo(h3.GeoCoord{Latitude: lat, Longitude: lon}, d.level)
171+
addr, ok := d.lookup(cell)
172+
if !ok {
173+
return c, fmt.Errorf("h3geodist: distributed cell %v not found", cell)
174+
}
175+
return Cell{H3ID: cell, Host: addr}, nil
176+
}
177+
146178
// ReplicaFor returns a list of hosts for replication.
147179
func (d *Distributed) ReplicaFor(cell h3.H3Index, n int) ([]string, error) {
148180
d.mu.RLock()

dist_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,57 @@ func TestDistributed_Stats(t *testing.T) {
241241
}
242242
}
243243

244+
func TestDistributed_LookupFromLatLon(t *testing.T) {
245+
h3dist, err := New(Level3)
246+
if err != nil {
247+
t.Fatal(err)
248+
}
249+
if err := h3dist.Add("127.0.0.1"); err != nil {
250+
t.Fatal(err)
251+
}
252+
dcell, err := h3dist.LookupFromLatLon(42.9269778, -72.2796935)
253+
if err != nil {
254+
t.Fatal(err)
255+
}
256+
if have, want := dcell.Host, "127.0.0.1"; have != want {
257+
t.Fatalf("have %s, want %s", have, want)
258+
}
259+
}
260+
261+
func TestDistributed_WhereIsMyParent(t *testing.T) {
262+
h3dist, err := New(Level3)
263+
if err != nil {
264+
t.Fatal(err)
265+
}
266+
267+
_ = h3dist.Add("127.0.0.1")
268+
269+
childLevel3 := h3.FromString("83821cfffffffff")
270+
dcell0, err := h3dist.WhereIsMyParent(childLevel3)
271+
if err != nil {
272+
t.Fatal(err)
273+
}
274+
if have, want := dcell0.Host, "127.0.0.1"; have != want {
275+
t.Fatalf("have %s, want %s", have, want)
276+
}
277+
278+
childLevel4 := h3.FromString("8482111ffffffff")
279+
dcell1, err := h3dist.WhereIsMyParent(childLevel4)
280+
if err != nil {
281+
t.Fatal(err)
282+
}
283+
if have, want := dcell1.Host, "127.0.0.1"; have != want {
284+
t.Fatalf("have %s, want %s", have, want)
285+
}
286+
287+
// child res > parent res
288+
childLevel1 := h3.FromString("81757ffffffffff")
289+
_, err = h3dist.WhereIsMyParent(childLevel1)
290+
if err == nil {
291+
t.Fatalf("have nil, expected error")
292+
}
293+
}
294+
244295
func TestDefault(t *testing.T) {
245296
h3dist, _ := New(Level1, WithVNodes(3))
246297

0 commit comments

Comments
 (0)