Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added set_multi() and tests.

  • Loading branch information...
commit 6ff216aaf4fc4bb3b6883851402389b97e66963c 1 parent 14671bf
Toru Maesaka authored
View
36 kyototycoon/kt_http.py
@@ -76,6 +76,38 @@ def get(self, key, db=None):
return self.unpack(body)
+ def set_bulk(self, kv_dict, expire, atomic, db):
+ if not isinstance(kv_dict, dict):
+ return False
+ if len(kv_dict) < 1:
+ return False
+
+ path = '/rpc/set_bulk'
+ if db:
+ db = urllib.quote(db)
+ path += '?DB=' + db
+
+ request_body = ''
+
+ if atomic:
+ request_body = 'atomic\t\n'
+
+ for k, v in kv_dict.items():
+ k = urllib.quote(k)
+ v = urllib.quote(self.pack(v))
+ request_body += '_' + k + '\t' + v + '\n'
+
+ self.conn.request('POST', path, body=request_body,
+ headers=KT_HTTP_HEADER)
+
+ res = self.conn.getresponse()
+ body = res.read()
+
+ if res.status != 200:
+ return False
+
+ return int(self._tsv_to_dict(body)['num'])
+
def get_bulk(self, keys, atomic, db):
if not isinstance(keys, list):
return None
@@ -94,7 +126,7 @@ def get_bulk(self, keys, atomic, db):
request_body = 'atomic\t\n'
for key in keys:
- request_body += '_' + key + '\t\n'
+ request_body += '_' + urllib.quote(key) + '\t\n'
self.conn.request('POST', path, body=request_body,
headers=KT_HTTP_HEADER)
@@ -114,7 +146,7 @@ def get_bulk(self, keys, atomic, db):
for k, v in res_dict.items():
if v is not None:
- rv[k[1:]] = self.unpack(urllib.unquote(v))
+ rv[urllib.unquote(k[1:])] = self.unpack(urllib.unquote(v))
return rv
def get_int(self, key, db=None):
View
3  kyototycoon/kyototycoon.py
@@ -70,6 +70,9 @@ def get(self, key, db=None):
def get_int(self, key, db=None):
return self.core.get_int(key, db)
+ def set_bulk(self, kv_dict, expire=None, atomic=True, db=None):
+ return self.core.set_bulk(kv_dict, expire, atomic, db)
+
def get_bulk(self, keys, atomic=True, db=None):
return self.core.get_bulk(keys, atomic, db)
View
29 tests/t_simple.py
@@ -94,6 +94,35 @@ def test_add(self):
self.assertTrue(self.kt_handle.add('number', 111))
self.assertEqual(self.kt_handle.get('number'), 111)
+ def test_set_bulk(self):
+ self.assertTrue(self.kt_handle.clear())
+
+ dict = {
+ 'k1': 'one',
+ 'k2': 'two',
+ 'k3': 'three',
+ 'k4': 'four',
+ 'k\n5': 'five',
+ 'k\t6': 'six',
+ 'k7': 111
+ }
+
+ n = self.kt_handle.set_bulk(dict)
+ self.assertEqual(len(dict), n)
+ self.assertEqual(self.kt_handle.get('k1'), 'one')
+ self.assertEqual(self.kt_handle.get('k2'), 'two')
+ self.assertEqual(self.kt_handle.get('k3'), 'three')
+ self.assertEqual(self.kt_handle.get('k4'), 'four')
+ self.assertEqual(self.kt_handle.get('k\n5'), 'five')
+ self.assertEqual(self.kt_handle.get('k\t6'), 'six')
+ self.assertEqual(self.kt_handle.get('k7'), 111)
+
+ d = self.kt_handle.get_bulk(['k1', 'k2', 'k3', 'k4',
+ 'k\n5', 'k\t6', 'k7'])
+
+ self.assertEqual(len(d), len(dict))
+ self.assertEqual(d, dict)
+
def test_get_bulk(self):
self.assertTrue(self.kt_handle.clear())
self.assertTrue(self.kt_handle.set('a', 'one'))
Please sign in to comment.
Something went wrong with that request. Please try again.