From feebfcca4c369df5d16bacb3ed313102aaa632ba Mon Sep 17 00:00:00 2001 From: LI Mengxiang Date: Fri, 14 Dec 2018 23:04:48 +0800 Subject: [PATCH] push only if queue not exist && push only if queue already exists --- fastrq/deque.py | 60 +++++++++++++++++++++++++++++++++++++ fastrq/loader.py | 2 ++ fastrq/priorityqueue.py | 30 +++++++++++++++++++ fastrq/stack.py | 20 +++++++++++++ tests/test_deque.py | 24 +++++++++++++++ tests/test_priorityqueue.py | 22 ++++++++++++++ tests/test_stack.py | 16 ++++++++++ 7 files changed, 174 insertions(+) diff --git a/fastrq/deque.py b/fastrq/deque.py index ac8f796..80254ee 100755 --- a/fastrq/deque.py +++ b/fastrq/deque.py @@ -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]) @@ -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') @@ -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)) diff --git a/fastrq/loader.py b/fastrq/loader.py index 782cb78..95a7cc8 100755 --- a/fastrq/loader.py +++ b/fastrq/loader.py @@ -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', diff --git a/fastrq/priorityqueue.py b/fastrq/priorityqueue.py index 4a13fad..7e22ba0 100755 --- a/fastrq/priorityqueue.py +++ b/fastrq/priorityqueue.py @@ -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 """ @@ -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 """ @@ -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 """ diff --git a/fastrq/stack.py b/fastrq/stack.py index 156dae6..45c53b1 100755 --- a/fastrq/stack.py +++ b/fastrq/stack.py @@ -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 @@ -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 diff --git a/tests/test_deque.py b/tests/test_deque.py index d653994..82eb981 100755 --- a/tests/test_deque.py +++ b/tests/test_deque.py @@ -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]) @@ -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]) @@ -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]) diff --git a/tests/test_priorityqueue.py b/tests/test_priorityqueue.py index f1a4dfb..02958ce 100755 --- a/tests/test_priorityqueue.py +++ b/tests/test_priorityqueue.py @@ -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]); @@ -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]); @@ -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]); diff --git a/tests/test_stack.py b/tests/test_stack.py index c054c17..dd52460 100755 --- a/tests/test_stack.py +++ b/tests/test_stack.py @@ -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]) @@ -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])