MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / cmdRPOPLPUSH

Function cmdRPOPLPUSH

lists.go:197–293  ·  view source on GitHub ↗
(m uhaha.Machine, args []string)

Source from the content-addressed store, hash-verified

195 n, err := ldb.LClear([]byte(args[1]))
196 if err != nil {
197 return nil, err
198 }
199 return redcon.SimpleInt(n), nil
200}
201
202func cmdRPOPLPUSH(m uhaha.Machine, args []string) (interface{}, error) {
203
204 if len(args) != 3 {
205 return nil, uhaha.ErrWrongNumArgs
206 }
207 source, dest := []byte(args[1]), []byte(args[2])
208
209 var ttl int64 = -1
210 var expireTime int64 = -1
211 if bytes.Compare(source, dest) == 0 {
212 var err error
213 ttl, err = ldb.LTTL(source)
214 if err != nil {
215 return nil, err
216 }
217 if ttl != -1 {
218 expireTime = m.Now().Unix() + ttl
219 }
220 }
221
222 // Special handling for same source and destination (rotation)
223 if bytes.Compare(source, dest) == 0 {
224 // Use global mutex for same source/destination operations
225 // to ensure absolute atomicity
226 sameKeyMutex.Lock()
227 defer sameKeyMutex.Unlock()
228
229 // Get the last element
230 data, err := ldb.RPop(source)
231 if err != nil {
232 return nil, err
233 }
234
235 if data == nil {
236 return nil, nil
237 }
238
239 // Push it back to the front
240 if _, err := ldb.LPush(source, data); err != nil {
241 // If push fails, revert the pop
242 ldb.RPush(source, data)
243 return nil, err
244 }
245
246 //reset ttl using absolute time
247 if expireTime != -1 {
248 ldb.LExpireAt(source, expireTime)
249 }
250
251 return data, nil
252 }
253
254 // For different source and destination, we need to lock both keys

Callers

nothing calls this directly

Calls 2

keyMutexIndexFunction · 0.85
CompareMethod · 0.45

Tested by

no test coverage detected