def concat(self, key, value, width=None):
     """Concatenates columns of the existing record."""
     # TODO: write better documentation, provide example code
     if width is None:
         wait(self.proto.putcat(key, value))
     else:
         wait(self.proto.putshl(key, value, width))
 def __contains__(self, key):
     try:
         wait(self.proto.vsiz(key))
     except TyrantError:
         return False
     else:
         return True
    def multi_del(self, keys, no_update_log=False):
        """Removes given records from the database."""
        # TODO: write better documentation: why would user need the no_update_log param?
        opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0)
        if not isinstance(keys, (list, tuple)):
            keys = list(keys)

        wait(self.proto.misc("outlist", keys, opts))
 def call_func(self, func, key, value, record_locking=False,
               global_locking=False):
     """Calls specific function."""
     # TODO: write better documentation *OR* move this method to lower level
     opts = ((record_locking and TyrantProtocol.RDBXOLCKREC) |
             (global_locking and TyrantProtocol.RDBXOLCKGLB))
     return wait(self.proto.ext(func, opts, key, value))
 def get_stats(self):
     """Get the status string of the database.
     The return value is the status message of the database.The message 
     format is a dictionary. 
     """ 
     return dict(l.split('\t', 1) \
                     for l in wait(self.proto.stat()).splitlines() if l)
Example #6
0
 def _do_search(self, conditions=None, limit=None, offset=None,
                out=False, count=False, hint=False):
     """
     Returns keys of items that correspond to the Query instance.
     """
     defaults = {
         'out': out,
         'count': count,
         'hint': hint,
         'conditions': conditions or [c.prepare() for c in self._conditions],
         'limit': limit,
         'offset': offset,
     }
     if self._columns:
         defaults.update(columns=self._columns[:])
     if self._ordering:
         defaults.update(
             order_column = self._ordering.name,
             order_type   = self._ordering.type,
         )
     if self._ms_conditions:
         defaults.update(    # FIXME make this more readable
             ms_type = self._ms_type,
             ms_conditions = [[c.prepare() for c in ms_c] for ms_c in self._ms_conditions]
         )
     
     return wait(self._proto.search(**defaults))
 def iterkeys(self):
     """Iterates keys using remote operations."""
     self.proto.iterinit()
     try:
         while True:
             yield wait(self.proto.iternext())
     except TyrantError:
         pass
    def prefix_keys(self, prefix, maxkeys=None):
        """Get forward matching keys in a database.
        The return value is a list object of the corresponding keys.
        """
        # TODO: write better documentation: describe purpose, provide example code
        if maxkeys is None:
            maxkeys = len(self)

        return wait(self.proto.fwmkeys(prefix, maxkeys))
    def multi_set(self, items, no_update_log=False):
        """Stores given records in the database."""
        opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0)
        lst = []
        for k, v in items.iteritems():
            if isinstance(v, (dict)):
                new_v = []
                for kk, vv in v.items():
                    new_v.append(kk)
                    new_v.append(vv)
                v = new_v
            if isinstance(v, (list, tuple)):
                assert self.separator, "Separator is not set"

                v = self.separator.join(v)
            lst.extend((k, v))

        wait(self.proto.misc("putlist", lst, opts))
 def get(self, key, default=None):
     val = wait(self._cache.get(smart_str(key)))
     print val
     if val is None:
         return default
     else:
         val = val[1]
         if isinstance(val, basestring):
             return smart_unicode(val)
         else:
             return val
    def decr(self, key, delta=1):
        try:
            val = wait(self._cache.decrement(key, delta))

        # python-memcache responds to decr on non-existent keys by
        # raising a ValueError. Cmemcache returns None. In both
        # cases, we should raise a ValueError though.
        except ValueError:
            val = None
        if val is None:
            raise ValueError("Key '%s' not found" % key)
        return val
Example #12
0
 def _cursor(self):
     set_tz = False
     settings_dict = self.settings_dict
     if self.connection is None:
         set_tz = True
         if settings_dict['NAME'] == '':
             from django.core.exceptions import ImproperlyConfigured
             raise ImproperlyConfigured("You need to specify NAME in your Django settings file.")
         conn_params = {
             'dbname': settings_dict['NAME'],
         }
         conn_params.update(settings_dict['OPTIONS'])
         if 'autocommit' in conn_params:
             del conn_params['autocommit']
         if settings_dict['USER']:
             conn_params['user'] = settings_dict['USER']
         if settings_dict['PASSWORD']:
             conn_params['password'] = settings_dict['PASSWORD']
         if settings_dict['HOST']:
             conn_params['host'] = settings_dict['HOST']
         if settings_dict['PORT']:
             conn_params['port'] = settings_dict['PORT']
         self.conn_params = conn_params
         #self.connection.set_client_encoding('UTF8')
         #self.connection.set_isolation_level(self.isolation_level)
         connection_created.send(sender=self.__class__)
     self.connection = Database.connect(**self.conn_params)
     cursor = CursorWrapper(wait(self.connection.cursor()))
     cursor.tzinfo_factory = None
     if set_tz:
         #cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']])
         if not hasattr(self, '_version'):
             self.__class__._version = (8,4) #get_version(cursor)
         if self._version[0:2] < (8, 0):
             # No savepoint support for earlier version of PostgreSQL.
             self.features.uses_savepoints = False
         if self.features.uses_autocommit:
             if self._version[0:2] < (8, 2):
                 # FIXME: Needs extra code to do reliable model insert
                 # handling, so we forbid it for now.
                 from django.core.exceptions import ImproperlyConfigured
                 raise ImproperlyConfigured("You cannot use autocommit=True with PostgreSQL prior to 8.2 at the moment.")
             else:
                 # FIXME: Eventually we're enable this by default for
                 # versions that support it, but, right now, that's hard to
                 # do without breaking other things (#10509).
                 self.features.can_return_id_from_insert = True
     cursor.execute('SET client_encoding="UTF8"')
     cursor.commit()
     return cursor
Example #13
0
    def __setitem__(self, key, value):
        if isinstance(value, dict):
            flat = _itertools.chain([key], *value.iteritems())
            wait(self.proto.misc('put', list(flat)))
            
        elif isinstance(value, (list, tuple)):
            assert self.separator, "Separator is not set"

            flat = self.separator.join(value)
            wait(self.proto.put(key, flat))

        else:
            wait(self.proto.put(key, value))
Example #14
0
    def multi_get(self, keys, no_update_log=False):
        """Returns a list of records that match given keys."""
        opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0)
        if not isinstance(keys, (list, tuple)):
            keys = list(keys)

        rval = wait(self.proto.misc("getlist", keys, opts))

        if len(rval) <= len(keys):
            # 1.1.10 protocol, may return invalid results
            if len(rval) < len(keys):
                raise KeyError("Missing a result, unusable response in 1.1.10")

            return rval

        # 1.1.11 protocol returns interleaved key, value list
        d = dict((rval[i], to_python(rval[i + 1], self.dbtype,
                                       self.separator)) \
                    for i in xrange(0, len(rval), 2))
        return d
Example #15
0
 def _commit(self):
     if self.connection is not None:
         return wait(self.connection.commit())
 def get_many(self, keys):
     return wait(self._cache.get_multi(map(smart_str,keys)))
 def delete(self, key):
     wait(self._cache.delete(smart_str(key)))
 def set(self, key, value, timeout=0):
     print key
     if isinstance(value, unicode):
         value = value.encode('utf-8')
     wait(self._cache.set(smart_str(key), value,expireTime=(timeout or self.default_timeout)))
Example #19
0
 def sync(self):
     """Synchronizes updated content with the database."""
     # TODO: write better documentation: when would user need this?
     wait(self.proto.sync())
Example #20
0
 def clear(self):
     """Removes all records from the remote database."""
     wait(self.proto.vanish())
    def __init__(self, server, params):
        BaseCache.__init__(self, params)

        host, port = server.split(':')
        self._cache = wait(protocol.ClientCreator(reactor, MemCacheProtocol).connectTCP(host, int(port)))
Example #22
0
 def _to_python(self, key):
     elem = wait(self._proto.get(key, self.literal))
     elem = to_python(elem, self._dbtype)
     return key, elem
Example #23
0
 def __init__(self, host='127.0.0.1', port=1978, separator=None, literal=False):
     self.proto = wait(protocol.ClientCreator(reactor, TyrantProtocol).connectTCP(host,port))
     self.dbtype = self.get_stats()['type']
     self.separator = separator
     self.literal = literal
def test_proto():
    cc = wait(protocol.ClientCreator(reactor, TyrantProtocol).connectTCP("127.0.0.1",1978))
    try:
        wait(test_fail())
    except Exception, e:
        print "Really! Exception!", e
from tx_tokyo import TyrantProtocol, TyrantError

def pr(res):
    print "Callback:",res

@make_it_green
def test_fail():
    print "kuku!"
    return 0/0

from twisted.web import client
@make_it_green
def test_proto():
    cc = wait(protocol.ClientCreator(reactor, TyrantProtocol).connectTCP("127.0.0.1",1978))
    try:
        wait(test_fail())
    except Exception, e:
        print "Really! Exception!", e

    
    client.getPage("http://www.ru").addBoth(pr)

    print 'Set:',wait(cc.put('test','tutu 123123123123123213'))
    print 'Get:',wait(cc.get('test'))
    #reactor.stop()

if __name__=='__main__':
    test_proto()
    reactor.run()

Example #26
0
 def __delitem__(self, key):
     try:
         return wait(self.proto.out(key))
     except TyrantError:
         raise KeyError(key)
Example #27
0
 def __getitem__(self, key):
     try:
         return _parse_elem(wait(self.proto.get(key, self.literal)), self.dbtype,
                            self.separator)
     except TyrantError:
         raise KeyError(key)
Example #28
0
 def _rollback(self):
     if self.connection is not None:
         return wait(self.connection.rollback())
Example #29
0
 def get_size(self, key):
     """Returns the size of the value for `key`."""
     try:
         return wait(self.proto.vsiz(key))
     except TyrantError:
         raise KeyError(key)
 def add(self, key, value, timeout=0):
     if isinstance(value, unicode):
         value = value.encode('utf-8')
     return wait(self._cache.add(smart_str(key), value,expireTime=(timeout or self.default_timeout)))