The Great Rewrite (fixes #36, #61, #94, #101)

Rewrite of the file model and pulling mechanism. Needs lots of cleanup
and bugfixes, now...
This commit is contained in:
Jakob Borg
2014-03-28 14:36:57 +01:00
parent 3700eb1e61
commit f87b1520e8
47 changed files with 2137 additions and 1902 deletions

View File

@@ -1,18 +1,30 @@
// Package cid provides a manager for mappings between node ID:s and connection ID:s.
package cid
import "sync"
type Map struct {
toCid map[string]int
sync.Mutex
toCid map[string]uint
toName []string
}
var (
LocalName = "<local>"
LocalID uint = 0
)
func NewMap() *Map {
return &Map{
toCid: make(map[string]int),
toCid: map[string]uint{"<local>": 0},
toName: []string{"<local>"},
}
}
func (m *Map) Get(name string) int {
func (m *Map) Get(name string) uint {
m.Lock()
defer m.Unlock()
cid, ok := m.toCid[name]
if ok {
return cid
@@ -22,22 +34,45 @@ func (m *Map) Get(name string) int {
for i, n := range m.toName {
if n == "" {
m.toName[i] = name
m.toCid[name] = i
return i
m.toCid[name] = uint(i)
return uint(i)
}
}
// Add it to the end since we didn't find a free slot
m.toName = append(m.toName, name)
cid = len(m.toName) - 1
cid = uint(len(m.toName) - 1)
m.toCid[name] = cid
return cid
}
func (m *Map) Name(cid uint) string {
m.Lock()
defer m.Unlock()
return m.toName[cid]
}
func (m *Map) Names() []string {
m.Lock()
var names []string
for _, name := range m.toName {
if name != "" {
names = append(names, name)
}
}
m.Unlock()
return names
}
func (m *Map) Clear(name string) {
m.Lock()
cid, ok := m.toCid[name]
if ok {
m.toName[cid] = ""
delete(m.toCid, name)
}
m.Unlock()
}

27
cid/cid_test.go Normal file
View File

@@ -0,0 +1,27 @@
package cid
import "testing"
func TestGet(t *testing.T) {
m := NewMap()
if i := m.Get("foo"); i != 1 {
t.Errorf("Unexpected id %d != 1", i)
}
if i := m.Get("bar"); i != 2 {
t.Errorf("Unexpected id %d != 2", i)
}
if i := m.Get("foo"); i != 1 {
t.Errorf("Unexpected id %d != 1", i)
}
if i := m.Get("bar"); i != 2 {
t.Errorf("Unexpected id %d != 2", i)
}
if LocalID != 0 {
t.Error("LocalID should be 0")
}
if i := m.Get(LocalName); i != LocalID {
t.Errorf("Unexpected id %d != %c", i, LocalID)
}
}