This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
forked from Imgur/incus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_store.go
107 lines (80 loc) · 1.83 KB
/
memory_store.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import "errors"
type MemoryStore struct {
clients map[string]map[string]*Socket
pages map[string]map[string]*Socket
clientCount int64
}
func (this *MemoryStore) Save(sock *Socket) error {
user, exists := this.clients[sock.UID]
if !exists {
this.clientCount++
userMap := make(map[string]*Socket)
userMap[sock.SID] = sock
this.clients[sock.UID] = userMap
return nil
}
_, exists = user[sock.SID]
user[sock.SID] = sock
if !exists {
this.clientCount++
}
return nil
}
func (this *MemoryStore) Remove(sock *Socket) error {
user, exists := this.clients[sock.UID]
if !exists { // only subtract if the client was in the store in the first place.
return nil
}
_, exists = user[sock.SID]
delete(user, sock.SID)
if exists {
this.clientCount--
}
if len(user) == 0 {
delete(this.clients, sock.UID)
}
return nil
}
func (this *MemoryStore) Client(UID string) (map[string]*Socket, error) {
var client, exists = this.clients[UID]
if !exists {
return nil, errors.New("ClientID doesn't exist")
}
return client, nil
}
func (this *MemoryStore) Clients() map[string]map[string]*Socket {
return this.clients
}
func (this *MemoryStore) Count() (int64, error) {
return this.clientCount, nil
}
func (this *MemoryStore) SetPage(sock *Socket) error {
page, exists := this.pages[sock.Page]
if !exists {
pageMap := make(map[string]*Socket)
pageMap[sock.SID] = sock
this.pages[sock.Page] = pageMap
return nil
}
page[sock.SID] = sock
return nil
}
func (this *MemoryStore) UnsetPage(sock *Socket) error {
page, exists := this.pages[sock.Page]
if !exists {
return nil
}
delete(page, sock.SID)
if len(page) == 0 {
delete(this.pages, sock.Page)
}
return nil
}
func (this *MemoryStore) getPage(page string) map[string]*Socket {
var p, exists = this.pages[page]
if !exists {
return nil
}
return p
}