-
Notifications
You must be signed in to change notification settings - Fork 23
/
feedset.go
81 lines (66 loc) · 1.52 KB
/
feedset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-FileCopyrightText: 2021 The Go-SSB Authors
//
// SPDX-License-Identifier: MIT
package ssb
import (
"fmt"
"sync"
librarian "github.com/ssbc/margaret/indexes"
refs "github.com/ssbc/go-ssb-refs"
"github.com/ssbc/go-ssb-refs/tfk"
"github.com/ssbc/go-ssb/internal/storedrefs"
)
type strFeedMap map[librarian.Addr]struct{}
type StrFeedSet struct {
mu *sync.Mutex
set strFeedMap
}
func NewFeedSet(size int) *StrFeedSet {
return &StrFeedSet{
mu: new(sync.Mutex),
set: make(strFeedMap, size),
}
}
func (fs *StrFeedSet) AddRef(ref refs.FeedRef) error {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.set[storedrefs.Feed(ref)] = struct{}{}
return nil
}
func (fs *StrFeedSet) Delete(ref refs.FeedRef) error {
fs.mu.Lock()
defer fs.mu.Unlock()
delete(fs.set, storedrefs.Feed(ref))
return nil
}
func (fs *StrFeedSet) Count() int {
fs.mu.Lock()
defer fs.mu.Unlock()
return len(fs.set)
}
func (fs StrFeedSet) List() ([]refs.FeedRef, error) {
fs.mu.Lock()
defer fs.mu.Unlock()
var lst = make([]refs.FeedRef, len(fs.set))
i := 0
for feed := range fs.set {
var sr tfk.Feed
err := sr.UnmarshalBinary([]byte(feed))
if err != nil {
return nil, fmt.Errorf("failed to decode map entry: %w", err)
}
// log.Printf("dbg List(%d) %s", i, ref.Ref())
lst[i], err = sr.Feed()
if err != nil {
return nil, fmt.Errorf("failed to decode map entry: %w", err)
}
i++
}
return lst, nil
}
func (fs StrFeedSet) Has(ref refs.FeedRef) bool {
fs.mu.Lock()
defer fs.mu.Unlock()
_, has := fs.set[storedrefs.Feed(ref)]
return has
}