def main(): client = Client() try: run_benchmark(client) finally: client.close()
def __init__(self, name='huey', host='127.0.0.1', port=31337, **storage_kwargs): super(SimpleStorage, self).__init__(name=name, **storage_kwargs) self.client = Client(host=host, port=port) self.client.connect()
def main(): server, t = make_server() time.sleep(0.1) client = Client() client.connect() try: run_benchmark(client) finally: client.shutdown() client.close()
class TestMiniRedisDatabase(unittest.TestCase): def setUp(self): self.c = Client(host=TEST_HOST, port=TEST_PORT) self.c.connect() def tearDown(self): self.c.close() def test_list(self): lq = KeyPartial(self.c, 'queue') lq.lpush('i1') lq.lpush('i2') lq.rpush('i3') lq.rpush('i4') result = lq.lrange(0) self.assertEqual(result, ['i2', 'i1', 'i3', 'i4']) self.assertEqual(lq.lpop(), 'i2') self.assertEqual(lq.rpop(), 'i4') self.assertEqual(lq.llen(), 2) self.assertEqual(lq.lrem('i3'), 1) self.assertEqual(lq.lrem('i3'), 0) lq.lpush('a1', 'a2', 'a3', 'a4') self.assertEqual(lq.lindex(2), 'a2') lq.lset(2, 'x') self.assertEqual(lq.lrange(1, 3), ['a3', 'x']) lq.ltrim(1, 4) self.assertEqual(lq.lrange(0), ['a3', 'x', 'a1']) self.assertEqual(lq.lflush(), 3) def test_kv(self): kp = KeyPartial(self.c, 'k1') kp.set(['alpha', 'beta', 'gamma']) self.assertEqual(kp.get(), ['alpha', 'beta', 'gamma']) res = kp.append(['pi', b'omega']) self.assertEqual(res, ['alpha', 'beta', 'gamma', 'pi', b'omega']) kp.set([b'foo', b'bar', b'baz']) self.assertEqual(kp.get(), [b'foo', b'bar', b'baz']) def test_incr_decr(self): self.assertEqual(self.c.incr('i'), 1) self.assertEqual(self.c.decr('i'), 0) self.assertEqual(self.c.incrby('i2', 3), 3) self.assertEqual(self.c.incrby('i2', 2), 5)
def setUp(self): self.c = Client(host=TEST_HOST, port=TEST_PORT) self.c.flush()
class TestMiniRedisDatabase(unittest.TestCase): def setUp(self): self.c = Client(host=TEST_HOST, port=TEST_PORT) self.c.flush() def test_list(self): lq = KeyPartial(self.c, 'queue') lq.lpush('i1') lq.lpush('i2') lq.rpush('i3') lq.rpush('i4') result = lq.lrange(0) self.assertEqual(result, ['i2', 'i1', 'i3', 'i4']) self.assertEqual(lq.lpop(), 'i2') self.assertEqual(lq.rpop(), 'i4') self.assertEqual(lq.llen(), 2) self.assertEqual(lq.lrem('i3'), 1) self.assertEqual(lq.lrem('i3'), 0) lq.lpush('a1', 'a2', 'a3', 'a4') self.assertEqual(lq.lindex(2), 'a2') lq.lset(2, 'x') self.assertEqual(lq.lrange(1, 3), ['a3', 'x']) lq.ltrim(1, 4) self.assertEqual(lq.lrange(0), ['a3', 'x', 'a1']) self.assertEqual(lq.lflush(), 3) def test_kv(self): kp = KeyPartial(self.c, 'k1') kp.set(['alpha', 'beta', 'gamma']) self.assertEqual(kp.get(), ['alpha', 'beta', 'gamma']) res = kp.append(['pi', b'omega']) self.assertEqual(res, ['alpha', 'beta', 'gamma', 'pi', b'omega']) kp.set([b'foo', b'bar', b'baz']) self.assertEqual(kp.get(), [b'foo', b'bar', b'baz']) def test_incr_decr(self): self.assertEqual(self.c.incr('i'), 1) self.assertEqual(self.c.decr('i'), 0) self.assertEqual(self.c.incrby('i2', 3), 3) self.assertEqual(self.c.incrby('i2', 2), 5) def test_persistence(self): self.c.set('k1', 'v1') self.c.hset('h1', 'k1', 'v1') self.c.sadd('s1', 'v1', 'v2') self.assertTrue(self.c.save('/tmp/simpledb.state')) self.c.flushall() self.assertTrue(self.c.get('k1') is None) self.assertTrue(self.c.hget('h1', 'k1') is None) self.assertTrue(self.c.scard('s1') == 0) self.c.set('k1', 'x1') self.c.set('k2', 'x2') self.assertTrue(self.c.restore('/tmp/simpledb.state')) self.assertEqual(self.c.get('k1'), 'v1') self.assertTrue(self.c.get('k2') is None) self.assertEqual(self.c.hget('h1', 'k1'), 'v1') self.assertEqual(self.c.scard('s1'), 2) self.c.flushall() self.c.set('k1', 'x1') self.c.set('k2', 'x2') self.assertTrue(self.c.merge('/tmp/simpledb.state')) self.assertEqual(self.c.get('k1'), 'x1') self.assertEqual(self.c.get('k2'), 'x2') self.assertEqual(self.c.hget('h1', 'k1'), 'v1') self.assertEqual(self.c.scard('s1'), 2) def test_expiry(self): self.c.mset({'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}) # Make it appear to expire in the past. self.c.expire('k2', -1) self.c.expire('k3', 3) self.assertEqual(self.c.mget('k1', 'k2', 'k3'), ['v1', None, 'v3'])
class SimpleStorage(BaseStorage): def __init__(self, name='huey', host='127.0.0.1', port=31337, **storage_kwargs): super(SimpleStorage, self).__init__(name=name, **storage_kwargs) self.client = Client(host=host, port=port) def enqueue(self, data): self.client.lpush(self.name, data) def dequeue(self): return self.client.rpop(self.name) def unqueue(self, data): return self.client.lrem(self.name) def queue_size(self): return self.client.llen(self.name) def flush_queue(self): return self.client.lflush(self.name) def flush_all(self): return self.client.flushall() def add_to_schedule(self, data, ts): return self.client.add(str(ts), data) def read_schedule(self, ts): return self.client.read(str(ts)) def schedule_size(self): return self.client.length_schedule() def flush_schedule(self): return self.client.flush_schedule() def put_data(self, key, value): return self.client.set(key, value) def peek_data(self, key): return self.client.get(key) def pop_data(self, key): return self.client.pop(key) def has_data_for_key(self, key): return self.client.exists(key) def put_if_empty(self, key, value): return self.client.setnx(key, value) def result_store_size(self, key, value): return self.client.length() def flush_results(self): return self.client.flush()
def setUp(self): self.c = Client(host=TEST_HOST, port=TEST_PORT) self.c.connect()
# /usr/bin/env python3 # list gifts, add gists, remove gifts. from simpledb import Client client = Client() # get db access. import argparse parser = argparse.ArgumentParser(description='Manage the giftlist.') parser.add_argument("--list", "-l", action='store_true', help='List gifts currently in the database.') parser.add_argument("--add", "-a", nargs=2, help='Adds a gift. --add NAME LINK.') parser.add_argument("--delete", help='Removes a gift. --delete NAME.', default=None) args = parser.parse_args() if args.list: giftnames = client.get("gifts") print("List of gifts:{}".format(giftnames)) if giftnames: for gift in [client.get(name) for name in giftnames]: print("{}: link - {}, claimed - {} on the {}".format( gift['name'], gift['link'], gift['claimed'],
class SimpleStorage(BaseStorage): def __init__(self, name='huey', host='127.0.0.1', port=31337, **storage_kwargs): super(SimpleStorage, self).__init__(name=name, **storage_kwargs) self.client = Client(host=host, port=port) self.client.connect() def enqueue(self, data): self.client.lpush(self.name, data) def dequeue(self): return self.client.rpop(self.name) def unqueue(self, data): return self.client.lrem(self.name) def queue_size(self): return self.client.llen(self.name) def flush_queue(self): return self.client.lflush(self.name) def flush_all(self): return self.client.flushall() def add_to_schedule(self, data, ts): return self.client.add(str(ts), data) def read_schedule(self, ts): return self.client.read(str(ts)) def schedule_size(self): return self.client.length_schedule() def flush_schedule(self): return self.client.flush_schedule() def put_data(self, key, value): return self.client.set(key, value) def peek_data(self, key): return self.client.get(key) def pop_data(self, key): return self.client.pop(key) def has_data_for_key(self, key): return self.client.exists(key) def put_if_empty(self, key, value): return self.client.setnx(key, value) def result_store_size(self, key, value): return self.client.length() def flush_results(self): return self.client.flush()
""" Test simpledb with many concurrent connections. """ from gevent import monkey monkey.patch_all() import time import gevent from simpledb import Client client = Client() def get_sleep_set(k, v, n=1): client.set(k, v) time.sleep(n) assert client.get(k) == v client.close() n = 3 t = 256 start = time.time() greenlets = [] for i in range(t): greenlets.append(gevent.spawn(get_sleep_set, 'k%d' % i, 'v%d' % i, n)) for g in greenlets:
from simpledb import Client client = Client() # get db access. from datetime import date today = date.today() client.set('gifts', ["spoons", "fast car", "olive oil"]) client.set( "spoons", { "name": "spoons", "link": "http://the.best.spoons.com", "claimed": False, "claimdate": None }) client.set( "fast car", { "name": "fast car", "link": "http://bmw.co.uk", "claimed": False, "claimdate": None }) client.set( "olive oil", { "name": "olive oil", "link": "http://oil.co.uk", "claimed": True, "claimdate": str(today) })
""" Test simpledb with many concurrent connections. """ from gevent import monkey monkey.patch_all() import time import gevent from simpledb import Client client = Client() def get_sleep_set(k, v, n=1): client.set(k, v) time.sleep(n) assert client.get(k) == v client.close() n = 3 t = 256 start = time.time() greenlets = [] for i in range(t): greenlets.append(gevent.spawn(get_sleep_set, 'k%d' % i, 'v%d' % i, n)) for g in greenlets: g.join()
from flask import Flask, abort, redirect, url_for app = Flask(__name__) from Cheetah.Template import Template import pickle from simpledb import Client from datetime import date client = Client() # get db access. # data model: keys are giftnames. # key gifts has a list of known gifts. # gifts have links, claimed status, and date claimed. # need to schedule dumps. # get page templates and static data. with open("list.templ", "r") as f: listtempl = f.read() @app.route("/") def list(): gifts = [client.get(name) for name in client.get("gifts")] nameSpace = {"giftdb": gifts, "url_for": url_for} body = Template(listtempl, searchList=[nameSpace]) return str(body) @app.route("/claim/<giftname>", methods=["POST"]) def claim(giftname): try: