Skip to content
Browse files

Added match_regex(). More tests to come.

  • Loading branch information...
1 parent 6c59934 commit f3b1a4ba4a94a61ddc527dac7ce7a8fcd631d255 Toru Maesaka committed May 26, 2011
Showing with 60 additions and 2 deletions.
  1. +35 −2 kyototycoon/kt_http.py
  2. +3 −0 kyototycoon/kyototycoon.py
  3. +9 −0 tests/t_multi.py
  4. +13 −0 tests/t_simple.py
View
37 kyototycoon/kt_http.py
@@ -213,7 +213,7 @@ def vacuum(self, db):
def match_prefix(self, prefix, max, db):
if prefix is None:
- return False
+ return None
rv = []
request_dict = {}
@@ -245,7 +245,40 @@ def match_prefix(self, prefix, max, db):
return rv
- def set(self, key, value, expire, db=None):
+ def match_regex(self, regex, max, db):
+ if regex is None:
+ return None
+
+ path = '/rpc/match_regex'
+ if db:
+ path += '?DB=' + db
+
+ request_dict = { 'regex': regex }
+ if max:
+ request_dict['max'] = max
+
+ request_body = self._dict_to_tsv(request_dict)
+ self.conn.request('POST', path, body=request_body,
+ headers=KT_HTTP_HEADER)
+
+ res = self.conn.getresponse()
+ body = res.read()
+
+ if res.status != 200:
+ return None
+
+ rv = []
+ res_dict = self._tsv_to_dict(body)
+
+ if res_dict.pop('num') < 1:
+ return []
+
+ for k in res_dict.keys():
+ rv.append(k[1:])
+
+ return rv
+
+ def set(self, key, value, expire, db):
if key is None:
return False
View
3 kyototycoon/kyototycoon.py
@@ -87,3 +87,6 @@ def vacuum(self, db=None):
def match_prefix(self, prefix, max=None, db=None):
return self.core.match_prefix(prefix, max, db)
+
+ def match_regex(self, regex, max=None, db=None):
+ return self.core.match_regex(regex, max, db)
View
9 tests/t_multi.py
@@ -117,6 +117,15 @@ def test_replace(self):
self.assertEqual(self.kt_handle.get('key2'), 'bbb')
self.assertEqual(self.kt_handle.get('key1'), 'zzz')
+ def test_cas(self):
+ self.assertTrue(self.clear_all())
+ self.assertTrue(self.kt_handle.set('key', 'xxx'))
+ self.assertFalse(self.kt_handle.cas('key', old_val='xxx',
+ new_val='yyy', db=DB_2))
+ self.assertTrue(self.kt_handle.cas('key', old_val='xxx',
+ new_val='yyy', db=DB_1))
+ self.assertTrue(self.kt_handle.cas('key', new_val='xxx', db=DB_2))
+
def test_remove(self):
self.assertTrue(self.clear_all())
self.assertTrue(self.kt_handle.add('key', 'value', db=DB_1))
View
13 tests/t_simple.py
@@ -213,5 +213,18 @@ def test_match_prefix(self):
list = self.kt_handle.match_prefix('abc', 1)
self.assertEqual(list[0], 'abc')
+ def test_match_regex(self):
+ self.assertTrue(self.kt_handle.clear())
+ self.assertTrue(self.kt_handle.set('abc', 'val'))
+ self.assertTrue(self.kt_handle.set('abcd', 'val'))
+ self.assertTrue(self.kt_handle.set('abcde', 'val'))
+
+ list = self.kt_handle.match_regex('^abc')
+ self.assertEqual(len(list), 3)
+ list = self.kt_handle.match_regex('^abcd')
+ self.assertEqual(len(list), 2)
+ list = self.kt_handle.match_regex('e$')
+ self.assertEqual(len(list), 1)
+
if __name__ == '__main__':
unittest.main()

0 comments on commit f3b1a4b

Please sign in to comment.
Something went wrong with that request. Please try again.