Commit c17545f6 authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

refactor: *Entry -> Entry

in many places, entries are assigned from one slice to another and in
different goroutines. In one place, entries were modified (in the
queue). To avoid shared mutable state, probably best to handle entries
by value.

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 82a02928
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
type BitSwapMessage interface { type BitSwapMessage interface {
// Wantlist returns a slice of unique keys that represent data wanted by // Wantlist returns a slice of unique keys that represent data wanted by
// the sender. // the sender.
Wantlist() []*Entry Wantlist() []Entry
// Blocks returns a slice of unique blocks // Blocks returns a slice of unique blocks
Blocks() []*blocks.Block Blocks() []*blocks.Block
...@@ -48,7 +48,7 @@ type Exportable interface { ...@@ -48,7 +48,7 @@ type Exportable interface {
type impl struct { type impl struct {
full bool full bool
wantlist map[u.Key]*Entry wantlist map[u.Key]Entry
blocks map[u.Key]*blocks.Block // map to detect duplicates blocks map[u.Key]*blocks.Block // map to detect duplicates
} }
...@@ -59,7 +59,7 @@ func New() BitSwapMessage { ...@@ -59,7 +59,7 @@ func New() BitSwapMessage {
func newMsg() *impl { func newMsg() *impl {
return &impl{ return &impl{
blocks: make(map[u.Key]*blocks.Block), blocks: make(map[u.Key]*blocks.Block),
wantlist: make(map[u.Key]*Entry), wantlist: make(map[u.Key]Entry),
full: true, full: true,
} }
} }
...@@ -90,8 +90,8 @@ func (m *impl) Full() bool { ...@@ -90,8 +90,8 @@ func (m *impl) Full() bool {
return m.full return m.full
} }
func (m *impl) Wantlist() []*Entry { func (m *impl) Wantlist() []Entry {
var out []*Entry var out []Entry
for _, e := range m.wantlist { for _, e := range m.wantlist {
out = append(out, e) out = append(out, e)
} }
...@@ -120,7 +120,7 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) { ...@@ -120,7 +120,7 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
e.Priority = priority e.Priority = priority
e.Cancel = cancel e.Cancel = cancel
} else { } else {
m.wantlist[k] = &Entry{ m.wantlist[k] = Entry{
Entry: wantlist.Entry{ Entry: wantlist.Entry{
Key: k, Key: k,
Priority: priority, Priority: priority,
......
...@@ -13,7 +13,7 @@ type ThreadSafe struct { ...@@ -13,7 +13,7 @@ type ThreadSafe struct {
// not threadsafe // not threadsafe
type Wantlist struct { type Wantlist struct {
set map[u.Key]*Entry set map[u.Key]Entry
} }
type Entry struct { type Entry struct {
...@@ -23,7 +23,7 @@ type Entry struct { ...@@ -23,7 +23,7 @@ type Entry struct {
Priority int Priority int
} }
type entrySlice []*Entry type entrySlice []Entry
func (es entrySlice) Len() int { return len(es) } func (es entrySlice) Len() int { return len(es) }
func (es entrySlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] } func (es entrySlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
...@@ -37,7 +37,7 @@ func NewThreadSafe() *ThreadSafe { ...@@ -37,7 +37,7 @@ func NewThreadSafe() *ThreadSafe {
func New() *Wantlist { func New() *Wantlist {
return &Wantlist{ return &Wantlist{
set: make(map[u.Key]*Entry), set: make(map[u.Key]Entry),
} }
} }
...@@ -62,13 +62,13 @@ func (w *ThreadSafe) Contains(k u.Key) bool { ...@@ -62,13 +62,13 @@ func (w *ThreadSafe) Contains(k u.Key) bool {
return w.Wantlist.Contains(k) return w.Wantlist.Contains(k)
} }
func (w *ThreadSafe) Entries() []*Entry { func (w *ThreadSafe) Entries() []Entry {
w.lk.RLock() w.lk.RLock()
defer w.lk.RUnlock() defer w.lk.RUnlock()
return w.Wantlist.Entries() return w.Wantlist.Entries()
} }
func (w *ThreadSafe) SortedEntries() []*Entry { func (w *ThreadSafe) SortedEntries() []Entry {
w.lk.RLock() w.lk.RLock()
defer w.lk.RUnlock() defer w.lk.RUnlock()
return w.Wantlist.SortedEntries() return w.Wantlist.SortedEntries()
...@@ -78,7 +78,7 @@ func (w *Wantlist) Add(k u.Key, priority int) { ...@@ -78,7 +78,7 @@ func (w *Wantlist) Add(k u.Key, priority int) {
if _, ok := w.set[k]; ok { if _, ok := w.set[k]; ok {
return return
} }
w.set[k] = &Entry{ w.set[k] = Entry{
Key: k, Key: k,
Priority: priority, Priority: priority,
} }
...@@ -93,7 +93,7 @@ func (w *Wantlist) Contains(k u.Key) bool { ...@@ -93,7 +93,7 @@ func (w *Wantlist) Contains(k u.Key) bool {
return ok return ok
} }
func (w *Wantlist) Entries() []*Entry { func (w *Wantlist) Entries() []Entry {
var es entrySlice var es entrySlice
for _, e := range w.set { for _, e := range w.set {
es = append(es, e) es = append(es, e)
...@@ -101,7 +101,7 @@ func (w *Wantlist) Entries() []*Entry { ...@@ -101,7 +101,7 @@ func (w *Wantlist) Entries() []*Entry {
return es return es
} }
func (w *Wantlist) SortedEntries() []*Entry { func (w *Wantlist) SortedEntries() []Entry {
var es entrySlice var es entrySlice
for _, e := range w.set { for _, e := range w.set {
es = append(es, e) es = append(es, e)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment