github.com/Hnampk/fabric@v2.1.1+incompatible/gossip/util/msgs.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package util 8 9 import ( 10 "sync" 11 12 "github.com/hyperledger/fabric/gossip/common" 13 "github.com/hyperledger/fabric/gossip/protoext" 14 ) 15 16 // MembershipStore struct which encapsulates 17 // membership message store abstraction 18 type MembershipStore struct { 19 m map[string]*protoext.SignedGossipMessage 20 sync.RWMutex 21 } 22 23 // NewMembershipStore creates new membership store instance 24 func NewMembershipStore() *MembershipStore { 25 return &MembershipStore{m: make(map[string]*protoext.SignedGossipMessage)} 26 } 27 28 // MsgByID returns a message stored by a certain ID, or nil 29 // if such an ID isn't found 30 func (m *MembershipStore) MsgByID(pkiID common.PKIidType) *protoext.SignedGossipMessage { 31 m.RLock() 32 defer m.RUnlock() 33 if msg, exists := m.m[string(pkiID)]; exists { 34 return msg 35 } 36 return nil 37 } 38 39 // Size of the membership store 40 func (m *MembershipStore) Size() int { 41 m.RLock() 42 defer m.RUnlock() 43 return len(m.m) 44 } 45 46 // Put associates msg with the given pkiID 47 func (m *MembershipStore) Put(pkiID common.PKIidType, msg *protoext.SignedGossipMessage) { 48 m.Lock() 49 defer m.Unlock() 50 m.m[string(pkiID)] = msg 51 } 52 53 // Remove removes a message with a given pkiID 54 func (m *MembershipStore) Remove(pkiID common.PKIidType) { 55 m.Lock() 56 defer m.Unlock() 57 delete(m.m, string(pkiID)) 58 } 59 60 // ToSlice returns a slice backed by the elements 61 // of the MembershipStore 62 func (m *MembershipStore) ToSlice() []*protoext.SignedGossipMessage { 63 m.RLock() 64 defer m.RUnlock() 65 members := make([]*protoext.SignedGossipMessage, len(m.m)) 66 i := 0 67 for _, member := range m.m { 68 members[i] = member 69 i++ 70 } 71 return members 72 }