Last active
May 25, 2016 20:36
-
-
Save 72squared/179d13f6d7a170a786606db388f37be9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aerospike | |
import unittest | |
import uuid | |
import time | |
from base64 import urlsafe_b64encode | |
def xid(): | |
return urlsafe_b64encode(uuid.uuid4().bytes).rstrip('=') | |
class FirstTest(unittest.TestCase): | |
def setUp(self): | |
config = {'hosts': [('127.0.0.1', 3000)]} | |
self.client = aerospike.client(config).connect() | |
self.key = ('test', 'demo', 'k1') | |
self.client.udf_put('sortedsets.lua') | |
for _ in xrange(0, 1000): | |
self.client.apply(self.key, 'sortedsets', 'add', ['zset', xid(), time.time()]) | |
def test(self): | |
start = time.time() | |
res = self.client.apply(self.key, 'sortedsets', 'range', ['zset', 0, 5]) | |
elapsed = time.time() - start | |
print res | |
print "elapsed: %.4f" % elapsed | |
print "records: %s" % self.client.apply(self.key, 'sortedsets', 'card', ['zset']) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- testing hello world | |
function add(rec, bin, member, score) | |
local t = rec[bin] | |
if (t == nil) then | |
t = map() | |
end | |
t[member] = score | |
rec[bin] = t | |
local length = #t | |
if aerospike:exists(rec) then | |
aerospike:update(rec) | |
else | |
aerospike:create(rec) | |
end | |
return length | |
end | |
function rem(rec, bin, member) | |
local t = rec[bin] | |
if (t == nil) then | |
return 0 | |
end | |
local changed = 0 | |
if (t[member] ~= nil) then | |
changed = 1 | |
end | |
map.remove(t, member) | |
if aerospike:exists(rec) then | |
aerospike:update(rec) | |
else | |
aerospike:create(rec) | |
end | |
return changed | |
end | |
function card(rec, bin) | |
local t = rec[bin] | |
if (t == nil) then | |
return 0 | |
end | |
return #t | |
end | |
function range(rec, bin, offset, limit) | |
local t = rec[bin] | |
if (t == nil) then | |
return list() | |
end | |
local keys = {} | |
for k in map.keys(t) do | |
keys[#keys+1] = k | |
end | |
-- if order function given, sort by it by passing the table and keys a, b, | |
-- otherwise just sort the keys | |
table.sort(keys, function (a,b) return t[b] > t[a] end) | |
-- return the iterator function | |
local items = list(#t) | |
if (offset == nil) then | |
offset = 0 | |
end | |
local i = offset | |
if (limit == nil) or (limit == -1) then | |
limit = #keys | |
end | |
while (i < #t) and (#items < limit) do | |
i = i + 1 | |
items[#items+1] = list {keys[i], t[keys[i]] } | |
end | |
return items | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment