/
models.go
99 lines (84 loc) · 2.33 KB
/
models.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
package sqlitestore
import (
"time"
"zombiezen.com/go/sqlite"
)
// Session
type Session struct {
ID string
Data string // Data is expected to be a base64-encoded string
CreationTime time.Time
ModificationTime time.Time
ExpirationTime time.Time
}
func (s Session) newInsertion() map[string]interface{} {
return map[string]interface{}{
"$id": s.ID,
"$data": s.Data,
"$creation_time": s.CreationTime.UnixMilli(),
"$modification_time": s.ModificationTime.UnixMilli(),
"$expiration_time": s.ExpirationTime.UnixMilli(),
}
}
func (s Session) newUpdate() map[string]interface{} {
return map[string]interface{}{
"$id": s.ID,
"$data": s.Data,
"$modification_time": s.ModificationTime.UnixMilli(),
}
}
func (s Session) newDelete() map[string]interface{} {
return map[string]interface{}{
"$id": s.ID,
}
}
func (s Session) newDeletePastCreationTimeout(timeout time.Duration) map[string]interface{} {
return map[string]interface{}{
"$expiration_timeout": timeout.Milliseconds(),
"$expiration_time_threshold": s.ExpirationTime.UnixMilli(),
}
}
func (s Session) newDeletePastExpiration() map[string]interface{} {
return map[string]interface{}{
"$expiration_time_threshold": s.ExpirationTime.UnixMilli(),
}
}
func newSessionSelection(id string) map[string]interface{} {
return map[string]interface{}{
"$id": id,
}
}
// Sessions
type sessionsSelector struct {
ids []string
sessions map[string]Session
}
func newSessionsSelector() *sessionsSelector {
return &sessionsSelector{
ids: make([]string, 0),
sessions: make(map[string]Session),
}
}
func (sel *sessionsSelector) Step(s *sqlite.Stmt) error {
id := s.GetText("id")
if _, ok := sel.sessions[id]; !ok {
sel.sessions[id] = Session{
ID: s.GetText("id"),
Data: s.GetText("data"),
CreationTime: time.UnixMilli(s.GetInt64("creation_time")),
ModificationTime: time.UnixMilli(s.GetInt64("modification_time")),
ExpirationTime: time.UnixMilli(s.GetInt64("expiration_time")),
}
if id != "" {
sel.ids = append(sel.ids, id)
}
}
return nil
}
func (sel *sessionsSelector) Sessions() []Session {
sessions := make([]Session, len(sel.ids))
for i, id := range sel.ids {
sessions[i] = sel.sessions[id]
}
return sessions
}