<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import pylibmc <EOL> from itertools import izip <EOL> from nydus . db . backends import BaseConnection , BasePipeline <EOL> from nydus . db . promise import EventualCommand <EOL> from nydus . utils import peek <EOL> class Memcache ( BaseConnection ) : <EOL> retryable_exceptions = frozenset ( [ pylibmc . Error ] ) <EOL> supports_pipelines = True <EOL> def __init__ ( self , num , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , binary = True , <EOL> behaviors = None , ** options ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . binary = binary <EOL> self . behaviors = behaviors <EOL> super ( Memcache , self ) . __init__ ( num ) <EOL> @ property <EOL> def identifier ( self ) : <EOL> mapping = vars ( self ) <EOL> return "<STR_LIT>" % mapping <EOL> def connect ( self ) : <EOL> host = "<STR_LIT>" % ( self . host , self . port ) <EOL> return pylibmc . Client ( [ host ] , binary = self . binary , behaviors = self . behaviors ) <EOL> def disconnect ( self ) : <EOL> self . connection . disconnect_all ( ) <EOL> def get_pipeline ( self , * args , ** kwargs ) : <EOL> return MemcachePipeline ( self ) <EOL> class MemcachePipeline ( BasePipeline ) : <EOL> def execute ( self ) : <EOL> grouped = regroup_commands ( self . pending ) <EOL> results = resolve_grouped_commands ( grouped , self . connection ) <EOL> return results <EOL> def grouped_args_for_command ( command ) : <EOL> """<STR_LIT>""" <EOL> if command . get_name ( ) == '<STR_LIT>' : <EOL> return command . get_args ( ) [ <NUM_LIT:2> : ] <EOL> return command . get_args ( ) [ <NUM_LIT:1> : ] <EOL> def grouped_command ( commands ) : <EOL> """<STR_LIT>""" <EOL> base = commands [ <NUM_LIT:0> ] <EOL> name = base . get_name ( ) <EOL> multi_command = EventualCommand ( '<STR_LIT>' % name ) <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> args = [ c . get_args ( ) [ <NUM_LIT:0> ] for c in commands ] <EOL> elif base . get_name ( ) == '<STR_LIT>' : <EOL> args = dict ( c . get_args ( ) [ <NUM_LIT:0> : <NUM_LIT:2> ] for c in commands ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( base . get_name ( ) , ) ) <EOL> multi_command ( args , * grouped_args_for_command ( base ) , ** base . get_kwargs ( ) ) <EOL> return multi_command <EOL> def can_group_commands ( command , next_command ) : <EOL> """<STR_LIT>""" <EOL> multi_capable_commands = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if next_command is None : <EOL> return False <EOL> name = command . get_name ( ) <EOL> if name not in multi_capable_commands : <EOL> return False <EOL> if name != next_command . get_name ( ) : <EOL> return False <EOL> if grouped_args_for_command ( command ) != grouped_args_for_command ( next_command ) : <EOL> return False <EOL> if command . get_kwargs ( ) != next_command . get_kwargs ( ) : <EOL> return False <EOL> return True <EOL> def regroup_commands ( commands ) : <EOL> """<STR_LIT>""" <EOL> grouped = [ ] <EOL> pending = [ ] <EOL> def group_pending ( ) : <EOL> if not pending : <EOL> return <EOL> new_command = grouped_command ( pending ) <EOL> result = [ ] <EOL> while pending : <EOL> result . append ( pending . pop ( <NUM_LIT:0> ) ) <EOL> grouped . append ( ( new_command , result ) ) <EOL> for command , next_command in peek ( commands ) : <EOL> if can_group_commands ( command , next_command ) : <EOL> if pending and not can_group_commands ( pending [ <NUM_LIT:0> ] , command ) : <EOL> group_pending ( ) <EOL> pending . append ( command ) <EOL> else : <EOL> if pending and can_group_commands ( pending [ <NUM_LIT:0> ] , command ) : <EOL> pending . append ( command ) <EOL> else : <EOL> grouped . append ( ( command . clone ( ) , [ command ] ) ) <EOL> group_pending ( ) <EOL> group_pending ( ) <EOL> return grouped <EOL> def resolve_grouped_commands ( grouped , connection ) : <EOL> results = { } <EOL> for master_command , grouped_commands in grouped : <EOL> result = master_command . resolve ( connection ) <EOL> if len ( grouped_commands ) == <NUM_LIT:1> : <EOL> results [ grouped_commands [ <NUM_LIT:0> ] ] = result <EOL> else : <EOL> if isinstance ( result , dict ) : <EOL> for command in grouped_commands : <EOL> results [ command ] = result . get ( command . get_args ( ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> for command , value in izip ( grouped_commands , result ) : <EOL> results [ command ] = value <EOL> return results </s>
