Debian Codegraph @

couchbase/couchbase.go

raw symbols SCIP
1 // Copyright 2013 Beego Authors
2 // Copyright 2014 The Macaron Authors
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License"): you may
5 // not use this file except in compliance with the License. You may obtain
6 // a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 // License for the specific language governing permissions and limitations
14 // under the License.
15
16 package session
17
18 import (
19 "strings"
20 "sync"
21
22 "github.com/couchbaselabs/go-couchbase"
23
24 "github.com/go-macaron/session"
25 )
26
27 // CouchbaseSessionStore represents a couchbase session store implementation.
28 type CouchbaseSessionStore struct {
29 b *couchbase.Bucket
30 sid string
31 lock sync.RWMutex
32 data map[interface{}]interface{}
33 maxlifetime int64
34 }
35
36 // Set sets value to given key in session.
37 func (s *CouchbaseSessionStore) Set(key, val interface{}) error {
38 s.lock.Lock()
39 defer s.lock.Unlock()
40
41 s.data[key] = val
42 return nil
43 }
44
45 // Get gets value by given key in session.
46 func (s *CouchbaseSessionStore) Get(key interface{}) interface{} {
47 s.lock.RLock()
48 defer s.lock.RUnlock()
49
50 return s.data[key]
51 }
52
53 // Delete delete a key from session.
54 func (s *CouchbaseSessionStore) Delete(key interface{}) error {
55 s.lock.Lock()
56 defer s.lock.Unlock()
57
58 delete(s.data, key)
59 return nil
60 }
61
62 // ID returns current session ID.
63 func (s *CouchbaseSessionStore) ID() string {
64 return s.sid
65 }
66
67 // Release releases resource and save data to provider.
68 func (s *CouchbaseSessionStore) Release() error {
69 defer s.b.Close()
70
71 data, err := session.EncodeGob(s.data)
72 if err != nil {
73 return err
74 }
75
76 return s.b.Set(s.sid, int(s.maxlifetime), data)
77 }
78
79 // Flush deletes all session data.
80 func (s *CouchbaseSessionStore) Flush() error {
81 s.lock.Lock()
82 defer s.lock.Unlock()
83
84 s.data = make(map[interface{}]interface{})
85 return nil
86 }
87
88 // CouchbaseProvider represents a couchbase session provider implementation.
89 type CouchbaseProvider struct {
90 maxlifetime int64
91 connStr string
92 pool string
93 bucket string
94 b *couchbase.Bucket
95 }
96
97 func (cp *CouchbaseProvider) getBucket() *couchbase.Bucket {
98 c, err := couchbase.Connect(cp.connStr)
99 if err != nil {
100 return nil
101 }
102
103 pool, err := c.GetPool(cp.pool)
104 if err != nil {
105 return nil
106 }
107
108 bucket, err := pool.GetBucket(cp.bucket)
109 if err != nil {
110 return nil
111 }
112
113 return bucket
114 }
115
116 // Init initializes memory session provider.
117 // connStr is couchbase server REST/JSON URL
118 // e.g. http://host:port/, Pool, Bucket
119 func (p *CouchbaseProvider) Init(maxlifetime int64, connStr string) error {
120 p.maxlifetime = maxlifetime
121 configs := strings.Split(connStr, ",")
122 if len(configs) > 0 {
123 p.connStr = configs[0]
124 }
125 if len(configs) > 1 {
126 p.pool = configs[1]
127 }
128 if len(configs) > 2 {
129 p.bucket = configs[2]
130 }
131
132 return nil
133 }
134
135 // Read returns raw session store by session ID.
136 func (p *CouchbaseProvider) Read(sid string) (session.RawStore, error) {
137 p.b = p.getBucket()
138
139 var doc []byte
140
141 err := p.b.Get(sid, &doc)
142 var kv map[interface{}]interface{}
143 if doc == nil {
144 kv = make(map[interface{}]interface{})
145 } else {
146 kv, err = session.DecodeGob(doc)
147 if err != nil {
148 return nil, err
149 }
150 }
151
152 cs := &CouchbaseSessionStore{b: p.b, sid: sid, data: kv, maxlifetime: p.maxlifetime}
153 return cs, nil
154 }
155
156 // Exist returns true if session with given ID exists.
157 func (p *CouchbaseProvider) Exist(sid string) bool {
158 p.b = p.getBucket()
159 defer p.b.Close()
160
161 var doc []byte
162
163 if err := p.b.Get(sid, &doc); err != nil || doc == nil {
164 return false
165 } else {
166 return true
167 }
168 }
169
170 // Destory deletes a session by session ID.
171 func (p *CouchbaseProvider) Destory(sid string) error {
172 p.b = p.getBucket()
173 defer p.b.Close()
174
175 p.b.Delete(sid)
176 return nil
177 }
178
179 // Regenerate regenerates a session store from old session ID to new one.
180 func (p *CouchbaseProvider) Regenerate(oldsid, sid string) (session.RawStore, error) {
181 p.b = p.getBucket()
182
183 var doc []byte
184 if err := p.b.Get(oldsid, &doc); err != nil || doc == nil {
185 p.b.Set(sid, int(p.maxlifetime), "")
186 } else {
187 err := p.b.Delete(oldsid)
188 if err != nil {
189 return nil, err
190 }
191 _, _ = p.b.Add(sid, int(p.maxlifetime), doc)
192 }
193
194 err := p.b.Get(sid, &doc)
195 if err != nil {
196 return nil, err
197 }
198 var kv map[interface{}]interface{}
199 if doc == nil {
200 kv = make(map[interface{}]interface{})
201 } else {
202 kv, err = session.DecodeGob(doc)
203 if err != nil {
204 return nil, err
205 }
206 }
207
208 cs := &CouchbaseSessionStore{b: p.b, sid: sid, data: kv, maxlifetime: p.maxlifetime}
209 return cs, nil
210 }
211
212 // Count counts and returns number of sessions.
213 func (p *CouchbaseProvider) Count() int {
214 // FIXME
215 return 0
216 }
217
218 // GC calls GC to clean expired sessions.
219 func (p *CouchbaseProvider) GC() {}
220
221 func init() {
222 session.Register("couchbase", &CouchbaseProvider{})
223 }