MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / GetTwoNeighbors

Method GetTwoNeighbors

consistent/consistent.go:272–301  ·  view source on GitHub ↗

GetTwoNeighbors returns the two closest distinct nodes to the name input in the circle.

(name string)

Source from the content-addressed store, hash-verified

270
271// GetTwoNeighbors returns the two closest distinct nodes to the name input in the circle.
272func (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.
304func (c *Consistent) GetNeighborsEx(name string, n int, roles proto.ServerRoles) ([]proto.Node, error) {

Callers 8

TestGetTwoFunction · 0.80
TestGetTwoEmptyFunction · 0.80
TestGetTwoQuickFunction · 0.80
TestGetTwoOnlyTwoQuickFunction · 0.80
TestSetFunction · 0.80
BenchmarkGetTwoFunction · 0.80
BenchmarkGetTwoLargeFunction · 0.80

Calls 2

searchMethod · 0.95
hashKeyFunction · 0.85

Tested by 8

TestGetTwoFunction · 0.64
TestGetTwoEmptyFunction · 0.64
TestGetTwoQuickFunction · 0.64
TestGetTwoOnlyTwoQuickFunction · 0.64
TestSetFunction · 0.64
BenchmarkGetTwoFunction · 0.64
BenchmarkGetTwoLargeFunction · 0.64