Skip to content
Snippets Groups Projects
Select Git revision
  • 4bb43dc291e7db3e711d967722539468044bea50
  • develop default protected
  • congestioncontrol
  • merge-v-data-collection-spammer-0.8.2
  • WIP-merge-v-data-collection-spammer-0.8.2
  • merge-v-data-collection-spammer-0.7.7
  • tmp
  • test-masterpow-fixing
  • test-masterpow
  • test-echo
  • v-data-collection
  • v-data-collection-spammer
  • tmp-dump-spam-info
  • dump-msg-info-0.3.1
  • test-dump-message-info
  • spammer-exprandom
  • extra/tutorial
  • without_tipselection
  • hacking-docker-network
  • hacking-docker-network-0.2.3
  • master
  • v0.2.3
22 results

valuebundlefactory.go

Blame
  • neighborMap.go 1.78 KiB
    package gossip
    
    import (
    	"sync"
    )
    
    // NeighborMap is the mapping of neighbor identifier and their neighbor struct
    // It uses a mutex to handle concurrent access to its internal map
    type NeighborMap struct {
    	sync.RWMutex
    	internal map[string]*Neighbor
    }
    
    // NewPeerMap returns a new PeerMap
    func NewNeighborMap() *NeighborMap {
    	return &NeighborMap{
    		internal: make(map[string]*Neighbor),
    	}
    }
    
    // Len returns the number of peers stored in a PeerMap
    func (nm *NeighborMap) Len() int {
    	nm.RLock()
    	defer nm.RUnlock()
    	return len(nm.internal)
    }
    
    // GetMap returns the content of the entire internal map
    func (nm *NeighborMap) GetMap() map[string]*Neighbor {
    	newMap := make(map[string]*Neighbor)
    	nm.RLock()
    	defer nm.RUnlock()
    	for k, v := range nm.internal {
    		newMap[k] = v
    	}
    	return newMap
    }
    
    // GetMap returns the content of the entire internal map
    func (nm *NeighborMap) GetSlice() []*Neighbor {
    	newSlice := make([]*Neighbor, nm.Len())
    	nm.RLock()
    	defer nm.RUnlock()
    	i := 0
    	for _, v := range nm.internal {
    		newSlice[i] = v
    		i++
    	}
    	return newSlice
    }
    
    // Load returns the peer for a given key.
    // It also return a bool to communicate the presence of the given
    // peer into the internal map
    func (nm *NeighborMap) Load(key string) (value *Neighbor, ok bool) {
    	nm.RLock()
    	defer nm.RUnlock()
    	result, ok := nm.internal[key]
    	return result, ok
    }
    
    // Delete removes the entire entry for a given key and return true if successful
    func (nm *NeighborMap) Delete(key string) (deletedPeer *Neighbor, ok bool) {
    	deletedPeer, ok = nm.Load(key)
    	if !ok {
    		return nil, false
    	}
    	nm.Lock()
    	defer nm.Unlock()
    	delete(nm.internal, key)
    	return deletedPeer, true
    }
    
    // Store adds a new peer to the PeerMap
    func (nm *NeighborMap) Store(key string, value *Neighbor) {
    	nm.Lock()
    	defer nm.Unlock()
    	nm.internal[key] = value
    }