Files
decent-websrv/internal/mesh/catalog.go
jbergner dc3abf661f
Some checks failed
release-tag / release-image (push) Failing after 1m28s
init
2025-09-24 10:32:22 +02:00

47 lines
917 B
Go

package mesh
import "sync"
type Catalog struct {
mu sync.RWMutex
byID map[string]NodeInfo
}
func NewCatalog() *Catalog { return &Catalog{byID: map[string]NodeInfo{}} }
func (c *Catalog) Set(n NodeInfo) { c.mu.Lock(); c.byID[n.NodeID] = n; c.mu.Unlock() }
func (c *Catalog) Replace(nodes []NodeInfo) {
c.mu.Lock()
defer c.mu.Unlock()
c.byID = map[string]NodeInfo{}
for _, n := range nodes {
c.byID[n.NodeID] = n
}
}
func (c *Catalog) All() []NodeInfo {
c.mu.RLock()
defer c.mu.RUnlock()
out := make([]NodeInfo, 0, len(c.byID))
for _, n := range c.byID {
out = append(out, n)
}
return out
}
func (c *Catalog) IDs() []string {
c.mu.RLock()
defer c.mu.RUnlock()
out := make([]string, 0, len(c.byID))
for id := range c.byID {
out = append(out, id)
}
return out
}
func (c *Catalog) ByID(id string) (NodeInfo, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
n, ok := c.byID[id]
return n, ok
}