Advertisement
Guest User

Untitled

a guest
Aug 7th, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. local MAX_INT = 4294967294 - 2
  2.  
  3. local function EXISTS(rec, bin)
  4. if aerospike:exists(rec)
  5. and rec[bin] ~= nil
  6. and type(rec) == "userdata"
  7. and record.ttl(rec) < (MAX_INT - 60) then
  8. return true
  9. end
  10. return false
  11. end
  12.  
  13. local function UPDATE(rec)
  14. if aerospike:exists(rec) then
  15. aerospike:update(rec)
  16. else
  17. aerospike:create(rec)
  18. end
  19. end
  20.  
  21. function LPOP (rec, bin, count)
  22. if (EXISTS(rec, bin)) then
  23. local l = rec[bin]
  24. local new_l = list.drop(l, count)
  25. rec[bin] = new_l
  26. UPDATE(rec)
  27. return list.take(l, count)
  28. end
  29. return nil
  30. end
  31.  
  32. function LPUSH(rec, bin, value)
  33. local l = rec[bin]
  34. if (l == nil) then
  35. l = list()
  36. end
  37. list.prepend(l, value)
  38. rec[bin] = l
  39. local length = #l
  40. UPDATE(rec)
  41. return length
  42. end
  43.  
  44. function LSIZE(rec, bin)
  45. if (EXISTS(rec, bin)) then
  46. local l = rec[bin]
  47. return #l
  48. end
  49. return nil
  50. end
  51.  
  52. function RPOP (rec, bin, count)
  53. if (EXISTS(rec, bin)) then
  54. local l = rec[bin]
  55. local result_list = nil
  56. if (#l <= count) then
  57. result_list = rec[bin]
  58. rec[bin] = nil
  59. else
  60. local start = #l - count
  61. result_list = list.drop(l, start)
  62. rec[bin] = list.take(l, start)
  63. end
  64. UPDATE(rec)
  65. if (result_list ~= nil) then
  66. return result_list
  67. else
  68. return list()
  69. end
  70. end
  71. return nil
  72. end
  73.  
  74. function RPUSH (rec, bin, value)
  75. local l = rec[bin]
  76. if (l == nil) then
  77. l = list()
  78. end
  79. list.append(l, value)
  80. rec[bin] = l
  81. local length = #l
  82. UPDATE(rec)
  83. return length
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement