3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

move StringSet to utils package

This commit is contained in:
Shivaram Lingamneni 2020-08-04 21:46:16 -04:00
parent ddac7d94a8
commit df8be72c6f
9 changed files with 50 additions and 40 deletions

View File

@ -5,6 +5,8 @@ package irc
import (
"sync"
"github.com/oragono/oragono/irc/utils"
)
type channelManagerEntry struct {
@ -23,17 +25,17 @@ type ChannelManager struct {
sync.RWMutex // tier 2
// chans is the main data structure, mapping casefolded name -> *Channel
chans map[string]*channelManagerEntry
chansSkeletons StringSet // skeletons of *unregistered* chans
registeredChannels StringSet // casefolds of registered chans
registeredSkeletons StringSet // skeletons of registered chans
purgedChannels StringSet // casefolds of purged chans
chansSkeletons utils.StringSet // skeletons of *unregistered* chans
registeredChannels utils.StringSet // casefolds of registered chans
registeredSkeletons utils.StringSet // skeletons of registered chans
purgedChannels utils.StringSet // casefolds of purged chans
server *Server
}
// NewChannelManager returns a new ChannelManager.
func (cm *ChannelManager) Initialize(server *Server) {
cm.chans = make(map[string]*channelManagerEntry)
cm.chansSkeletons = make(StringSet)
cm.chansSkeletons = make(utils.StringSet)
cm.server = server
cm.loadRegisteredChannels(server.Config())
@ -47,8 +49,8 @@ func (cm *ChannelManager) loadRegisteredChannels(config *Config) {
}
rawNames := cm.server.channelRegistry.AllChannels()
registeredChannels := make(StringSet, len(rawNames))
registeredSkeletons := make(StringSet, len(rawNames))
registeredChannels := make(utils.StringSet, len(rawNames))
registeredSkeletons := make(utils.StringSet, len(rawNames))
for _, name := range rawNames {
cfname, err := CasefoldChannel(name)
if err == nil {

View File

@ -4,15 +4,16 @@
package irc
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"encoding/json"
"github.com/tidwall/buntdb"
"github.com/oragono/oragono/irc/modes"
"github.com/tidwall/buntdb"
"github.com/oragono/oragono/irc/utils"
)
// this is exclusively the *persistence* layer for channel registration;
@ -140,8 +141,8 @@ func (reg *ChannelRegistry) AllChannels() (result []string) {
}
// PurgedChannels returns the set of all casefolded channel names that have been purged
func (reg *ChannelRegistry) PurgedChannels() (result map[string]empty) {
result = make(map[string]empty)
func (reg *ChannelRegistry) PurgedChannels() (result utils.StringSet) {
result = make(utils.StringSet)
prefix := fmt.Sprintf(keyChannelPurged, "")
reg.server.store.View(func(tx *buntdb.Tx) error {
@ -150,7 +151,7 @@ func (reg *ChannelRegistry) PurgedChannels() (result map[string]empty) {
return false
}
channel := strings.TrimPrefix(key, prefix)
result[channel] = empty{}
result.Add(channel)
return true
})
})

View File

@ -64,7 +64,7 @@ type Client struct {
destroyed bool
modes modes.ModeSet
hostname string
invitedTo StringSet
invitedTo utils.StringSet
isSTSOnly bool
languages []string
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) {
defer client.stateMutex.Unlock()
if client.invitedTo == nil {
client.invitedTo = make(StringSet)
client.invitedTo = make(utils.StringSet)
}
client.invitedTo.Add(casefoldedChannel)

View File

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

View File

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

View File

@ -28,17 +28,6 @@ func (clients ClientSet) Has(client *Client) bool {
return ok
}
type StringSet map[string]empty
func (s StringSet) Has(str string) bool {
_, ok := s[str]
return ok
}
func (s StringSet) Add(str string) {
s[str] = empty{}
}
// MemberSet is a set of members with modes.
type MemberSet map[*Client]*modes.ModeSet

View File

@ -10,27 +10,25 @@ import (
"time"
)
type e struct{}
// Semaphore is a counting semaphore.
// A semaphore of capacity 1 can be used as a trylock.
type Semaphore (chan e)
type Semaphore (chan empty)
// Initialize initializes a semaphore to a given capacity.
func (semaphore *Semaphore) Initialize(capacity int) {
*semaphore = make(chan e, capacity)
*semaphore = make(chan empty, capacity)
}
// Acquire acquires a semaphore, blocking if necessary.
func (semaphore *Semaphore) Acquire() {
(*semaphore) <- e{}
(*semaphore) <- empty{}
}
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
// successful. It never blocks.
func (semaphore *Semaphore) TryAcquire() (acquired bool) {
select {
case (*semaphore) <- e{}:
case (*semaphore) <- empty{}:
return true
default:
return false
@ -47,7 +45,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
timer := time.NewTimer(timeout)
select {
case (*semaphore) <- e{}:
case (*semaphore) <- empty{}:
acquired = true
case <-timer.C:
acquired = false
@ -61,7 +59,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
// Note that if the context is already expired, the acquire may succeed anyway.
func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
select {
case (*semaphore) <- e{}:
case (*semaphore) <- empty{}:
acquired = true
case <-ctx.Done():
acquired = false

17
irc/utils/types.go Normal file
View File

@ -0,0 +1,17 @@
// Copyright (c) 2020 Shivaram Lingamneni
// released under the MIT license
package utils
type empty struct{}
type StringSet map[string]empty
func (s StringSet) Has(str string) bool {
_, ok := s[str]
return ok
}
func (s StringSet) Add(str string) {
s[str] = empty{}
}

View File

@ -10,6 +10,7 @@ import (
"time"
"github.com/oragono/oragono/irc/history"
"github.com/oragono/oragono/irc/utils"
)
const (
@ -62,7 +63,7 @@ func timeToZncWireTime(t time.Time) (result string) {
type zncPlaybackTimes struct {
start time.Time
end time.Time
targets StringSet // nil for "*" (everything), otherwise the channel names
targets utils.StringSet // nil for "*" (everything), otherwise the channel names
setAt time.Time
}
@ -122,7 +123,7 @@ func zncPlaybackPlayHandler(client *Client, command string, params []string, rb
end = zncWireTimeToTime(params[3])
}
var targets StringSet
var targets utils.StringSet
var nickTargets []string
// three cases:
@ -145,7 +146,7 @@ func zncPlaybackPlayHandler(client *Client, command string, params []string, rb
if params[1] == "*" {
playPrivmsgs = true // XXX nil `targets` means "every channel"
} else {
targets = make(StringSet)
targets = make(utils.StringSet)
for _, targetName := range strings.Split(targetString, ",") {
if targetName == "*self" {
playPrivmsgs = true