GetTwoNeighbors returns the two closest distinct nodes to the name input in the circle.
(name string)
| 270 | |
| 271 | // GetTwoNeighbors returns the two closest distinct nodes to the name input in the circle. |
| 272 | func (c *Consistent) GetTwoNeighbors(name string) (proto.Node, proto.Node, error) { |
| 273 | c.RLock() |
| 274 | defer c.RUnlock() |
| 275 | c.cacheLock.RLock() |
| 276 | defer c.cacheLock.RUnlock() |
| 277 | |
| 278 | if len(c.circle) == 0 { |
| 279 | return proto.Node{}, proto.Node{}, ErrEmptyCircle |
| 280 | } |
| 281 | key := hashKey(name) |
| 282 | i := c.search(key) |
| 283 | a := *c.circle[c.sortedHashes[i]] |
| 284 | |
| 285 | if len(c.sortedHashes)/c.NumberOfReplicas == 1 { |
| 286 | return a, proto.Node{}, nil |
| 287 | } |
| 288 | |
| 289 | start := i |
| 290 | var b proto.Node |
| 291 | for i = start + 1; i != start; i++ { |
| 292 | if i >= len(c.sortedHashes) { |
| 293 | i = 0 |
| 294 | } |
| 295 | b = *c.circle[c.sortedHashes[i]] |
| 296 | if b.ID != a.ID { |
| 297 | break |
| 298 | } |
| 299 | } |
| 300 | return a, b, nil |
| 301 | } |
| 302 | |
| 303 | // GetNeighborsEx returns the N closest distinct nodes to the name input in the circle. |
| 304 | func (c *Consistent) GetNeighborsEx(name string, n int, roles proto.ServerRoles) ([]proto.Node, error) { |