Browse Source

move StringSet to utils package

tags/v2.3.0-rc1
Shivaram Lingamneni 3 years ago
parent
commit
df8be72c6f
9 changed files with 50 additions and 40 deletions
  1. 9
    7
      irc/channelmanager.go
  2. 6
    5
      irc/channelreg.go
  3. 2
    2
      irc/client.go
  4. 3
    1
      irc/client_test.go
  5. 3
    3
      irc/config.go
  6. 0
    11
      irc/types.go
  7. 6
    8
      irc/utils/semaphores.go
  8. 17
    0
      irc/utils/types.go
  9. 4
    3
      irc/znc.go

+ 9
- 7
irc/channelmanager.go View File

@@ -5,6 +5,8 @@ package irc
5 5
 
6 6
 import (
7 7
 	"sync"
8
+
9
+	"github.com/oragono/oragono/irc/utils"
8 10
 )
9 11
 
10 12
 type channelManagerEntry struct {
@@ -23,17 +25,17 @@ type ChannelManager struct {
23 25
 	sync.RWMutex // tier 2
24 26
 	// chans is the main data structure, mapping casefolded name -> *Channel
25 27
 	chans               map[string]*channelManagerEntry
26
-	chansSkeletons      StringSet // skeletons of *unregistered* chans
27
-	registeredChannels  StringSet // casefolds of registered chans
28
-	registeredSkeletons StringSet // skeletons of registered chans
29
-	purgedChannels      StringSet // casefolds of purged chans
28
+	chansSkeletons      utils.StringSet // skeletons of *unregistered* chans
29
+	registeredChannels  utils.StringSet // casefolds of registered chans
30
+	registeredSkeletons utils.StringSet // skeletons of registered chans
31
+	purgedChannels      utils.StringSet // casefolds of purged chans
30 32
 	server              *Server
31 33
 }
32 34
 
33 35
 // NewChannelManager returns a new ChannelManager.
34 36
 func (cm *ChannelManager) Initialize(server *Server) {
35 37
 	cm.chans = make(map[string]*channelManagerEntry)
36
-	cm.chansSkeletons = make(StringSet)
38
+	cm.chansSkeletons = make(utils.StringSet)
37 39
 	cm.server = server
38 40
 
39 41
 	cm.loadRegisteredChannels(server.Config())
@@ -47,8 +49,8 @@ func (cm *ChannelManager) loadRegisteredChannels(config *Config) {
47 49
 	}
48 50
 
49 51
 	rawNames := cm.server.channelRegistry.AllChannels()
50
-	registeredChannels := make(StringSet, len(rawNames))
51
-	registeredSkeletons := make(StringSet, len(rawNames))
52
+	registeredChannels := make(utils.StringSet, len(rawNames))
53
+	registeredSkeletons := make(utils.StringSet, len(rawNames))
52 54
 	for _, name := range rawNames {
53 55
 		cfname, err := CasefoldChannel(name)
54 56
 		if err == nil {

+ 6
- 5
irc/channelreg.go View File

@@ -4,15 +4,16 @@
4 4
 package irc
5 5
 
6 6
 import (
7
+	"encoding/json"
7 8
 	"fmt"
8 9
 	"strconv"
9 10
 	"strings"
10 11
 	"time"
11 12
 
12
-	"encoding/json"
13
+	"github.com/tidwall/buntdb"
13 14
 
14 15
 	"github.com/oragono/oragono/irc/modes"
15
-	"github.com/tidwall/buntdb"
16
+	"github.com/oragono/oragono/irc/utils"
16 17
 )
17 18
 
18 19
 // this is exclusively the *persistence* layer for channel registration;
@@ -140,8 +141,8 @@ func (reg *ChannelRegistry) AllChannels() (result []string) {
140 141
 }
141 142
 
142 143
 // PurgedChannels returns the set of all casefolded channel names that have been purged
143
-func (reg *ChannelRegistry) PurgedChannels() (result map[string]empty) {
144
-	result = make(map[string]empty)
144
+func (reg *ChannelRegistry) PurgedChannels() (result utils.StringSet) {
145
+	result = make(utils.StringSet)
145 146
 
146 147
 	prefix := fmt.Sprintf(keyChannelPurged, "")
147 148
 	reg.server.store.View(func(tx *buntdb.Tx) error {
@@ -150,7 +151,7 @@ func (reg *ChannelRegistry) PurgedChannels() (result map[string]empty) {
150 151
 				return false
151 152
 			}
152 153
 			channel := strings.TrimPrefix(key, prefix)
153
-			result[channel] = empty{}
154
+			result.Add(channel)
154 155
 			return true
155 156
 		})
156 157
 	})

+ 2
- 2
irc/client.go View File

@@ -64,7 +64,7 @@ type Client struct {
64 64
 	destroyed          bool
65 65
 	modes              modes.ModeSet
66 66
 	hostname           string
67
-	invitedTo          StringSet
67
+	invitedTo          utils.StringSet
68 68
 	isSTSOnly          bool
69 69
 	languages          []string
70 70
 	lastActive         time.Time            // last time they sent a command that wasn't PONG or similar
@@ -1641,7 +1641,7 @@ func (client *Client) Invite(casefoldedChannel string) {
1641 1641
 	defer client.stateMutex.Unlock()
1642 1642
 
1643 1643
 	if client.invitedTo == nil {
1644
-		client.invitedTo = make(StringSet)
1644
+		client.invitedTo = make(utils.StringSet)
1645 1645
 	}
1646 1646
 
1647 1647
 	client.invitedTo.Add(casefoldedChannel)

+ 3
- 1
irc/client_test.go View File

@@ -5,11 +5,13 @@ package irc
5 5
 
6 6
 import (
7 7
 	"testing"
8
+
9
+	"github.com/oragono/oragono/irc/utils"
8 10
 )
9 11
 
10 12
 func TestGenerateBatchID(t *testing.T) {
11 13
 	var session Session
12
-	s := make(StringSet)
14
+	s := make(utils.StringSet)
13 15
 
14 16
 	count := 100000
15 17
 	for i := 0; i < count; i++ {

+ 3
- 3
irc/config.go View File

@@ -624,8 +624,8 @@ type Config struct {
624 624
 // OperClass defines an assembled operator class.
625 625
 type OperClass struct {
626 626
 	Title        string
627
-	WhoisLine    string    `yaml:"whois-line"`
628
-	Capabilities StringSet // map to make lookups much easier
627
+	WhoisLine    string          `yaml:"whois-line"`
628
+	Capabilities utils.StringSet // map to make lookups much easier
629 629
 }
630 630
 
631 631
 // OperatorClasses returns a map of assembled operator classes from the given config.
@@ -663,7 +663,7 @@ func (conf *Config) OperatorClasses() (map[string]*OperClass, error) {
663 663
 
664 664
 			// create new operclass
665 665
 			var oc OperClass
666
-			oc.Capabilities = make(StringSet)
666
+			oc.Capabilities = make(utils.StringSet)
667 667
 
668 668
 			// get inhereted info from other operclasses
669 669
 			if len(info.Extends) > 0 {

+ 0
- 11
irc/types.go View File

@@ -28,17 +28,6 @@ func (clients ClientSet) Has(client *Client) bool {
28 28
 	return ok
29 29
 }
30 30
 
31
-type StringSet map[string]empty
32
-
33
-func (s StringSet) Has(str string) bool {
34
-	_, ok := s[str]
35
-	return ok
36
-}
37
-
38
-func (s StringSet) Add(str string) {
39
-	s[str] = empty{}
40
-}
41
-
42 31
 // MemberSet is a set of members with modes.
43 32
 type MemberSet map[*Client]*modes.ModeSet
44 33
 

+ 6
- 8
irc/utils/semaphores.go View File

@@ -10,27 +10,25 @@ import (
10 10
 	"time"
11 11
 )
12 12
 
13
-type e struct{}
14
-
15 13
 // Semaphore is a counting semaphore.
16 14
 // A semaphore of capacity 1 can be used as a trylock.
17
-type Semaphore (chan e)
15
+type Semaphore (chan empty)
18 16
 
19 17
 // Initialize initializes a semaphore to a given capacity.
20 18
 func (semaphore *Semaphore) Initialize(capacity int) {
21
-	*semaphore = make(chan e, capacity)
19
+	*semaphore = make(chan empty, capacity)
22 20
 }
23 21
 
24 22
 // Acquire acquires a semaphore, blocking if necessary.
25 23
 func (semaphore *Semaphore) Acquire() {
26
-	(*semaphore) <- e{}
24
+	(*semaphore) <- empty{}
27 25
 }
28 26
 
29 27
 // TryAcquire tries to acquire a semaphore, returning whether the acquire was
30 28
 // successful. It never blocks.
31 29
 func (semaphore *Semaphore) TryAcquire() (acquired bool) {
32 30
 	select {
33
-	case (*semaphore) <- e{}:
31
+	case (*semaphore) <- empty{}:
34 32
 		return true
35 33
 	default:
36 34
 		return false
@@ -47,7 +45,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
47 45
 
48 46
 	timer := time.NewTimer(timeout)
49 47
 	select {
50
-	case (*semaphore) <- e{}:
48
+	case (*semaphore) <- empty{}:
51 49
 		acquired = true
52 50
 	case <-timer.C:
53 51
 		acquired = false
@@ -61,7 +59,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
61 59
 // Note that if the context is already expired, the acquire may succeed anyway.
62 60
 func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
63 61
 	select {
64
-	case (*semaphore) <- e{}:
62
+	case (*semaphore) <- empty{}:
65 63
 		acquired = true
66 64
 	case <-ctx.Done():
67 65
 		acquired = false

+ 17
- 0
irc/utils/types.go View File

@@ -0,0 +1,17 @@
1
+// Copyright (c) 2020 Shivaram Lingamneni
2
+// released under the MIT license
3
+
4
+package utils
5
+
6
+type empty struct{}
7
+
8
+type StringSet map[string]empty
9
+
10
+func (s StringSet) Has(str string) bool {
11
+	_, ok := s[str]
12
+	return ok
13
+}
14
+
15
+func (s StringSet) Add(str string) {
16
+	s[str] = empty{}
17
+}

+ 4
- 3
irc/znc.go View File

@@ -10,6 +10,7 @@ import (
10 10
 	"time"
11 11
 
12 12
 	"github.com/oragono/oragono/irc/history"
13
+	"github.com/oragono/oragono/irc/utils"
13 14
 )
14 15
 
15 16
 const (
@@ -62,7 +63,7 @@ func timeToZncWireTime(t time.Time) (result string) {
62 63
 type zncPlaybackTimes struct {
63 64
 	start   time.Time
64 65
 	end     time.Time
65
-	targets StringSet // nil for "*" (everything), otherwise the channel names
66
+	targets utils.StringSet // nil for "*" (everything), otherwise the channel names
66 67
 	setAt   time.Time
67 68
 }
68 69
 
@@ -122,7 +123,7 @@ func zncPlaybackPlayHandler(client *Client, command string, params []string, rb
122 123
 		end = zncWireTimeToTime(params[3])
123 124
 	}
124 125
 
125
-	var targets StringSet
126
+	var targets utils.StringSet
126 127
 	var nickTargets []string
127 128
 
128 129
 	// three cases:
@@ -145,7 +146,7 @@ func zncPlaybackPlayHandler(client *Client, command string, params []string, rb
145 146
 	if params[1] == "*" {
146 147
 		playPrivmsgs = true // XXX nil `targets` means "every channel"
147 148
 	} else {
148
-		targets = make(StringSet)
149
+		targets = make(utils.StringSet)
149 150
 		for _, targetName := range strings.Split(targetString, ",") {
150 151
 			if targetName == "*self" {
151 152
 				playPrivmsgs = true

Loading…
Cancel
Save