Permalink
Browse files

addint, addouble

  • Loading branch information...
1 parent 32618ec commit bd58edd787159e195bd7c908bf8c3a957cb282dd @etrepum etrepum committed Apr 8, 2009
Showing with 25 additions and 0 deletions.
  1. +25 −0 pytyrant.py
View
@@ -23,6 +23,7 @@
>>> del t['__test_key__']
"""
+import math
import socket
import struct
import UserDict
@@ -144,6 +145,13 @@ def _t3F(code, func, opts, key, value):
]
+def _tDouble(code, key, integ, fract):
+ return [
+ struct.pack('>BBIQQ', MAGIC, code, integ, fract),
+ key,
+ ]
+
+
def socksend(sock, lst):
for chunk in lst:
sock.sendall(chunk)
@@ -174,6 +182,11 @@ def sockstr(sock):
return sockrecv(sock, socklen(sock))
+def sockdouble(sock):
+ intpart, fracpart = struct.unpack('>QQ', sockrecv(sock, 16))
+ return intpart + (fracpart * 1e-12)
+
+
def sockstrpair(sock):
klen = socklen(sock)
vlen = socklen(sock)
@@ -429,6 +442,18 @@ def fwmkeys(self, prefix, maxkeys):
"""
return list(self._fwmkeys(prefix, maxkeys))
+ def addint(self, key, num):
+ socksend(self.sock, _t1M(C.addint, key, num))
+ socksuccess(self.sock)
+ return socklen(self.sock)
+
+ def adddouble(self, key, num):
+ fracpart, intpart = math.modf(num)
+ fracpart, intpart = int(fracpart * 1e12), int(intpart)
+ socksend(self.sock, _tDouble(C.adddouble, key, fracpart, intpart))
+ socksuccess(self.sock)
+ return sockdouble(self.sock)
+
def ext(self, func, opts, key, value):
# tcrdbext opts are RDBXOLCKREC, RDBXOLCKGLB
"""Call func(key, value) with opts

0 comments on commit bd58edd

Please sign in to comment.