Skip to content

Commit

Permalink
push only if queue not exist && push only if queue already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
limen committed Dec 14, 2018
1 parent 82dbebf commit feebfcc
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
60 changes: 60 additions & 0 deletions fastrq/deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ def push_back(self, values):
script = load('deque_push_back')
return self._run_lua_script(script, [self._key], self._makevalues(values))

def push_front_ne(self, values):
script = load('deque_push_front_ne')
rt = self._run_lua_script(script, [self._key], self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_back_ne(self, values):
script = load('deque_push_back_ne')
rt = self._run_lua_script(script, [self._key], self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_front_ae(self, values):
script = load('deque_push_front_ae')
rt = self._run_lua_script(script, [self._key], self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_back_ae(self, values):
script = load('deque_push_back_ae')
rt = self._run_lua_script(script, [self._key], self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_front_ni(self, member):
script = load('deque_push_front_not_in')
r = self._run_lua_script(script, [self._key], [member])
Expand Down Expand Up @@ -71,6 +91,26 @@ def push_front(self, values):
def push_back(self, values):
script = load('capped_deque_push_back')
return self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))

def push_front_ne(self, values):
script = load('capped_deque_push_front_ne')
rt = self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_back_ne(self, values):
script = load('capped_deque_push_back_ne')
rt = self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_front_ae(self, values):
script = load('capped_deque_push_front_ae')
rt = self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_back_ae(self, values):
script = load('capped_deque_push_back_ae')
rt = self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_front_ni(self, member):
script = load('capped_deque_push_front_not_in')
Expand All @@ -92,6 +132,26 @@ def push_back(self, values):
script = load('of_capped_deque_push_back')
return self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))

def push_front_ne(self, values):
script = load('of_capped_deque_push_front_ne')
rt = self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_back_ne(self, values):
script = load('of_capped_deque_push_back_ne')
rt = self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_front_ae(self, values):
script = load('of_capped_deque_push_front_ae')
rt = self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_back_ae(self, values):
script = load('of_capped_deque_push_back_ae')
rt = self._run_lua_script(script, [self._key], (self._cap,) + self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_front_ni(self, member):
script = load('of_capped_deque_push_front_not_in')
r = self._run_lua_script(script, [self._key], (self._cap, member))
Expand Down
2 changes: 2 additions & 0 deletions fastrq/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@
'deque_pop_back': 'deque_pop_back',
'deque_pop_front': 'queue_pop',
'capped_deque_push_front': 'capped_deque_push_front',
'capped_deque_push_front_ne': ['not_exist', 'capped_deque_push_front'],
'capped_deque_push_front_ae': ['exists', 'capped_deque_push_front'],
'capped_deque_push_front_not_in': 'capped_deque_push_front_not_in',
'capped_deque_push_back': 'capped_queue_push',
'capped_deque_push_back_ne': 'capped_queue_push_ne',
Expand Down
30 changes: 30 additions & 0 deletions fastrq/priorityqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def push(self, values):
script = load('priority_queue_push')
return self._run_lua_script(script, (self._key,), self._makevalues(values))

def push_ne(self, values):
script = load('priority_queue_push_ne')
rt = self._run_lua_script(script, (self._key,), self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_ae(self, values):
script = load('priority_queue_push_ae')
rt = self._run_lua_script(script, (self._key,), self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_ni(self, member, score):
""" Push only if the member not inside the queue
"""
Expand Down Expand Up @@ -67,6 +77,16 @@ def push(self, values):
script = load('capped_priority_queue_push')
return self._run_lua_script(script, (self._key,), [self._cap] + self._makevalues(values))

def push_ne(self, values):
script = load('capped_priority_queue_push_ne')
rt = self._run_lua_script(script, (self._key,), [self._cap] + self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_ae(self, values):
script = load('capped_priority_queue_push_ae')
rt = self._run_lua_script(script, (self._key,), [self._cap] + self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_ni(self, member, score):
""" Push only if the member not inside the queue
"""
Expand All @@ -81,6 +101,16 @@ def push(self, values):
p = self._run_lua_script(script, (self._key,), [self._cap] + self._makevalues(values))
return [p[0], self._makereturn(p[1])]

def push_ne(self, values):
script = load('of_capped_priority_queue_push_ne')
p = self._run_lua_script(script, (self._key,), [self._cap] + self._makevalues(values))
return False if p == 'err_ae' else [p[0], self._makereturn(p[1])]

def push_ae(self, values):
script = load('of_capped_priority_queue_push_ae')
p = self._run_lua_script(script, (self._key,), [self._cap] + self._makevalues(values))
return False if p == 'err_ne' else [p[0], self._makereturn(p[1])]

def push_ni(self, member, score):
""" Push only if the member not inside the queue
"""
Expand Down
20 changes: 20 additions & 0 deletions fastrq/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ def __len__(self):
def push(self, values):
script = load('stack_push')
return self._run_lua_script(script, (self._key,), self._makevalues(values))

def push_ne(self, values):
script = load('stack_push_ne')
rt = self._run_lua_script(script, (self._key,), self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_ae(self, values):
script = load('stack_push_ae')
rt = self._run_lua_script(script, (self._key,), self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_ni(self, member):
""" Push only if the member not inside the stack
Expand Down Expand Up @@ -49,6 +59,16 @@ def __init__(self, key, cap):
def push(self, values):
script = load('capped_stack_push')
return self._run_lua_script(script, (self._key,), (self._cap,) + self._makevalues(values))

def push_ne(self, values):
script = load('capped_stack_push_ne')
rt = self._run_lua_script(script, (self._key,), (self._cap,) + self._makevalues(values))
return False if rt == 'err_ae' else rt

def push_ae(self, values):
script = load('capped_stack_push_ae')
rt = self._run_lua_script(script, (self._key,), (self._cap,) + self._makevalues(values))
return False if rt == 'err_ne' else rt

def push_ni(self, member):
""" Push only if the member not inside the stack
Expand Down
24 changes: 24 additions & 0 deletions tests/test_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def test_push_pop(self):
self.assertEqual(self.queue.pop_back(3), ['2', '3', '4'])
self.assertEqual(self.queue.pop_back(), None)

def test_push_e(self):
self.assertEqual(self.queue.push_front_ne(1), 1)
self.assertFalse(self.queue.push_back_ne(1))
self.queue.destruct()
self.assertFalse(self.queue.push_front_ae(1))
self.queue.push_front(1)
self.assertEqual(self.queue.push_front_ae(1), 2)

def test_push_ni(self):
self.assertEqual(self.queue.push_back_ni(1), [1, True])
self.assertEqual(self.queue.push_back_ni(2), [2, True])
Expand Down Expand Up @@ -67,6 +75,14 @@ def test_push(self):
self.assertEqual(self.queue.range(0, -1), ['3', '1', '2'])
self.assertEqual(self.queue.push_back(4), 'err_qf')

def test_push_e(self):
self.assertEqual(self.queue.push_front_ne(1), 1)
self.assertFalse(self.queue.push_back_ne(1))
self.queue.destruct()
self.assertFalse(self.queue.push_front_ae(1))
self.queue.push_front(1)
self.assertEqual(self.queue.push_front_ae(1), 2)

def test_push_ni(self):
self.assertEqual(self.queue.push_back_ni(1), [1, True])
self.assertEqual(self.queue.push_back_ni(1), [1, False])
Expand Down Expand Up @@ -94,6 +110,14 @@ def test_push(self):
self.assertEqual(self.queue.pop_front(), '1')
self.assertEqual(self.queue.pop_back(), '4')

def test_push_e(self):
self.assertEqual(self.queue.push_front_ne(1), [1, []])
self.assertFalse(self.queue.push_back_ne(1))
self.queue.destruct()
self.assertFalse(self.queue.push_front_ae(1))
self.queue.push_front(1)
self.assertEqual(self.queue.push_front_ae(1), [2, []])

def test_push_ni(self):
self.assertEqual(self.queue.push_back_ni('apple'), [1, [], True])
self.assertEqual(self.queue.push_back_ni('banana'), [2, [], True])
Expand Down
22 changes: 22 additions & 0 deletions tests/test_priorityqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def test_push_pop(self):
self.assertEqual(self.queue.pop(), None)
self.assertEqual(self.queue.pop(2), [])

def test_push_e(self):
self.assertFalse(self.queue.push_ae({'alice': 1}))
self.queue.push({'alice': 1})
self.assertEqual(self.queue.push_ae({'bob': 2}), 2)
self.assertFalse(self.queue.push_ne({'alice': 1}))
self.queue.destruct()
self.assertEqual(self.queue.push_ne({'alice': 1}), 1)

def test_push_ni(self):
self.assertEqual(self.queue.push_ni('alice', 1), [1, True]);
self.assertEqual(self.queue.push_ni('bob', 2), [2, True]);
Expand Down Expand Up @@ -70,6 +78,14 @@ def test_push_pop(self):
self.assertEqual(self.queue.push({'cath': 3}), 3)
self.assertEqual(self.queue.push({'cath': 3, 'dylon': 4}), 'err_qf')

def test_push_e(self):
self.assertFalse(self.queue.push_ae({'alice': 1}))
self.queue.push({'alice': 1})
self.assertEqual(self.queue.push_ae({'bob': 2}), 2)
self.assertFalse(self.queue.push_ne({'alice': 1}))
self.queue.destruct()
self.assertEqual(self.queue.push_ne({'alice': 1}), 1)

def test_push_ni(self):
self.assertEqual(self.queue.push_ni('alice', 1), [1, True]);
self.assertEqual(self.queue.push_ni('bob', 2), [2, True]);
Expand All @@ -94,6 +110,12 @@ def test_push_pop(self):
self.assertEqual(self.queue.push({'alice': 1, 'bob': 2}), [3, [('dylon', 4)]])
self.assertEqual(self.queue.push({'cathe': 0}), [3, [('cath', 3)]])

def test_push_e(self):
self.assertFalse(self.queue.push_ae({'alice': 1}))
self.queue.push({'alice': 1})
self.assertEqual(self.queue.push_ae({'bob': 2}), [2, []])
self.assertFalse(self.queue.push_ne({'alice': 1}))

def test_push_ni(self):
self.assertEqual(self.queue.push_ni('alice', 1), [1, [], True]);
self.assertEqual(self.queue.push_ni('bob', 2), [2, [], True]);
Expand Down
16 changes: 16 additions & 0 deletions tests/test_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def test_push_pop(self):
self.assertEqual(self.queue.pop(2), ['2', '1'])
self.assertEqual(self.queue.pop(), None)

def test_push_e(self):
self.assertEqual(self.queue.push_ne(1), 1)
self.assertFalse(self.queue.push_ne(1))
self.queue.destruct()
self.assertFalse(self.queue.push_ae(1))
self.queue.push(1)
self.assertEqual(self.queue.push_ae(1), 2)

def test_push_ni(self):
self.assertEqual(self.queue.push_ni(1), [1, True])
self.assertEqual(self.queue.push_ni('apple'), [2, True])
Expand All @@ -44,6 +52,14 @@ def test_push_pop(self):
self.assertEqual(self.queue.push(3), 3)
self.assertEqual(self.queue.push(4), 'err_qf')

def test_push_e(self):
self.assertEqual(self.queue.push_ne(1), 1)
self.assertFalse(self.queue.push_ne(1))
self.queue.destruct()
self.assertFalse(self.queue.push_ae(1))
self.queue.push(1)
self.assertEqual(self.queue.push_ae(1), 2)

def test_push_ni(self):
self.assertEqual(self.queue.push_ni(1), [1, True])
self.assertEqual(self.queue.push_ni(1), [1, False])
Expand Down

0 comments on commit feebfcc

Please sign in to comment.