-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.go
162 lines (128 loc) · 2.53 KB
/
pull.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package sqlite
import (
"context"
"sync"
)
var globalPullMutex sync.Mutex
func init() {
globalPullMutex = sync.Mutex{}
}
type Pull struct {
mx sync.RWMutex
conns map[string]*SQL
sessionMode bool
requested map[string]bool
}
// global variable
var pull *Pull
func New(sessionMode bool, ctx ...context.Context) *Pull {
out := &Pull{
mx: sync.RWMutex{},
conns: map[string]*SQL{},
requested: map[string]bool{},
sessionMode: sessionMode,
}
if len(ctx) > 0 {
go func(ctx context.Context) {
<-ctx.Done()
out.DeleteOld()
out.Close()
}(ctx[0])
}
return out
}
func Init(sessionMode bool, ctx ...context.Context) {
globalPullMutex.Lock()
defer globalPullMutex.Unlock()
if pull == nil {
pull = New(sessionMode, ctx...)
}
}
// --------------------------------
func Close() error {
return pull.Close()
}
func Upsert(fileName, id string, body []byte) error {
return pull.Upsert(fileName, id, body)
}
func Select(fileName, id string) ([]byte, error) {
return pull.Select(fileName, id)
}
func (p *Pull) Close() error {
p.mx.Lock()
defer p.mx.Unlock()
for _, c := range p.conns {
if c != nil {
if err := c.Close(); err != nil {
return err
}
}
c = nil
}
// finally clean
p.conns = map[string]*SQL{}
return nil
}
func (p *Pull) DeleteOld() (int64, error) {
p.mx.Lock()
defer p.mx.Unlock()
if !p.sessionMode {
return 0, nil
}
total := int64(0)
for _, c := range p.conns {
if c == nil {
continue
}
deleted, err := c.DeleteOld(p.requested)
if err != nil {
return 0, err
}
total += deleted
}
return total, nil
}
// Add creates new connection and adds to pull
func (p *Pull) Add(fileName string) (*SQL, error) {
p.mx.Lock()
defer p.mx.Unlock()
if old, find := p.conns[fileName]; find {
return old, nil
}
c, err := conn(fileName)
if err == nil {
p.conns[fileName] = c
}
return c, err
}
// Add creates new connection and adds to pull
func (p *Pull) Get(fileName string) (*SQL, error) {
p.mx.RLock()
old, find := p.conns[fileName]
if find {
p.mx.RUnlock()
return old, nil
}
p.mx.RUnlock()
return p.Add(fileName)
}
// Upsert just inserts or update one record
func (p *Pull) Upsert(fileName, id string, body []byte) error {
c, err := p.Get(fileName)
if err != nil {
return err
}
if p.sessionMode {
p.mx.Lock()
p.requested[id] = true
p.mx.Unlock()
}
return c.Upsert(id, body)
}
func (p *Pull) Select(fileName, id string) ([]byte, error) {
c, err := p.Get(fileName)
if err != nil {
return nil, err
}
return c.Select(id)
}