当前位置: 首页>>代码示例>>Golang>>正文


Golang Keyspace.Union方法代码示例

本文整理汇总了Golang中github.com/degdb/degdb/protocol.Keyspace.Union方法的典型用法代码示例。如果您正苦于以下问题:Golang Keyspace.Union方法的具体用法?Golang Keyspace.Union怎么用?Golang Keyspace.Union使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/degdb/degdb/protocol.Keyspace的用法示例。


在下文中一共展示了Keyspace.Union方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: MinimumCoveringPeers

// MinimumCoveringPeers returns a set of peers that minimizes overlap. This is similar to the Set Covering Problem and is NP-hard.
// This is a greedy algorithm. While the keyspace is not entirely covered, scan through all peers and pick the peer that will add the most to the set while still having the start in the selected set.
// TODO(wiz): Make this more optimal.
// TODO(wiz): achieve n-redundancy
func (s *Server) MinimumCoveringPeers() []*Conn {
	usedPeers := make(map[string]bool)
	var peers []*Conn
	var keyspace *protocol.Keyspace
	for i := 0; i < len(s.Peers) && !keyspace.Maxed(); i++ {
		var bestPeer *Conn
		var increase uint64
		// By definition, ranging through peer map will go in random order.
	Peers:
		for id, conn := range s.Peers {
			if conn == nil || conn.Peer == nil || usedPeers[id] {
				continue
			}
			peer := conn.Peer
			if keyspace == nil {
				peers = append(peers, conn)
				keyspace = peer.Keyspace
				break Peers
			}
			incr := keySpaceIncrease(keyspace, peer.Keyspace)
			if incr > increase {
				increase = incr
				bestPeer = conn
			}
		}
		if bestPeer != nil {
			peers = append(peers, bestPeer)
			keyspace = keyspace.Union(bestPeer.Peer.Keyspace)
			usedPeers[bestPeer.Peer.Id] = true
			// break?
		}
	}
	return peers
}
开发者ID:BobbWu,项目名称:degdb,代码行数:38,代码来源:network.go

示例2: keySpaceIncrease

// keySpaceIncrease calculates the increase in keyspace if b was to be unioned.
func keySpaceIncrease(a, b *protocol.Keyspace) uint64 {
	unionMag := a.Union(b).Mag()
	aMag := a.Mag()
	if unionMag > aMag {
		return unionMag - aMag
	}
	return 0
}
开发者ID:BobbWu,项目名称:degdb,代码行数:9,代码来源:network.go


注:本文中的github.com/degdb/degdb/protocol.Keyspace.Union方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。