Merge pull request #433 from AudriusButkevicius/dup

Remove non-existing nodes from repositories
This commit is contained in:
Jakob Borg 2014-07-23 07:58:03 +02:00
commit 680896e4c4
2 changed files with 27 additions and 0 deletions

View File

@ -304,10 +304,19 @@ func Load(rd io.Reader, myID protocol.NodeID) (Configuration, error) {
}
}
// Build a list of available nodes
existingNodes := make(map[protocol.NodeID]bool)
existingNodes[myID] = true
for _, node := range cfg.Nodes {
existingNodes[node.NodeID] = true
}
// Ensure this node is present in all relevant places
// Ensure that any loose nodes are not present in the wrong places
cfg.Nodes = ensureNodePresent(cfg.Nodes, myID)
for i := range cfg.Repositories {
cfg.Repositories[i].Nodes = ensureNodePresent(cfg.Repositories[i].Nodes, myID)
cfg.Repositories[i].Nodes = ensureExistingNodes(cfg.Repositories[i].Nodes, existingNodes)
}
// An empty address list is equivalent to a single "dynamic" entry
@ -392,3 +401,20 @@ func ensureNodePresent(nodes []NodeConfiguration, myID protocol.NodeID) []NodeCo
return nodes
}
func ensureExistingNodes(nodes []NodeConfiguration, existingNodes map[protocol.NodeID]bool) []NodeConfiguration {
i := 0
for _, node := range nodes {
if _, ok := existingNodes[node.NodeID]; !ok {
last := len(nodes) - 1
nodes[i] = nodes[last]
nodes = nodes[:last]
} else {
i++
}
}
sort.Sort(NodeConfigurationList(nodes))
return nodes
}

View File

@ -70,6 +70,7 @@ func TestNodeConfig(t *testing.T) {
<configuration version="2">
<repository id="test" directory="~/Sync" ro="true">
<node id="AIR6LPZ7K4PTTUXQSMUUCPQ5YWOEDFIIQJUG7772YQXXR5YD6AWQ"/>
<node id="C4YBIESWDUAIGU62GOSRXCRAAJDWVE3TKCPMURZE2LH5QHAF576A"/>
<node id="P56IOI7MZJNU2IQGDREYDM2MGTMGL3BXNPQ6W5BTBBZ4TJXZWICQ"/>
</repository>
<node id="AIR6LPZ7K4PTTUXQSMUUCPQ5YWOEDFIIQJUG7772YQXXR5YD6AWQ" name="node one">