<s> import shutil <EOL> import pickle <EOL> def save_data ( cfg , filename , data ) : <EOL> logger = cfg . configured_logger ( '<STR_LIT>' ) <EOL> temp = '<STR_LIT>' % filename <EOL> with open ( temp , '<STR_LIT:wb>' ) as file : <EOL> pickle . dump ( data , file , protocol = <NUM_LIT:2> ) <EOL> shutil . move ( temp , filename ) <EOL> logger . info ( '<STR_LIT>' , filename ) <EOL> def sort_command ( store , client , request , value ) : <EOL> sort_type = type ( value ) <EOL> right = <NUM_LIT:0> <EOL> desc = False <EOL> alpha = None <EOL> start = None <EOL> end = None <EOL> storekey = None <EOL> sortby = None <EOL> dontsort = False <EOL> getops = [ ] <EOL> N = len ( request ) <EOL> j = <NUM_LIT:2> <EOL> while j < N : <EOL> val = request [ j ] . lower ( ) <EOL> right = N - j - <NUM_LIT:1> <EOL> if val == b'<STR_LIT>' : <EOL> desc = False <EOL> elif val == b'<STR_LIT>' : <EOL> desc = True <EOL> elif val == b'<STR_LIT>' : <EOL> alpha = True <EOL> elif val == b'<STR_LIT>' and right >= <NUM_LIT:2> : <EOL> try : <EOL> start = max ( <NUM_LIT:0> , int ( request [ j + <NUM_LIT:1> ] ) ) <EOL> count = int ( request [ j + <NUM_LIT:2> ] ) <EOL> except Exception : <EOL> return client . error_reply ( store . SYNTAX_ERROR ) <EOL> end = len ( value ) if count <= <NUM_LIT:0> else start + count <EOL> j += <NUM_LIT:2> <EOL> elif val == b'<STR_LIT:store>' and right >= <NUM_LIT:1> : <EOL> storekey = request [ j + <NUM_LIT:1> ] <EOL> j += <NUM_LIT:1> <EOL> elif val == b'<STR_LIT>' and right >= <NUM_LIT:1> : <EOL> sortby = request [ j + <NUM_LIT:1> ] <EOL> if b'<STR_LIT:*>' not in sortby : <EOL> dontsort = True <EOL> j += <NUM_LIT:1> <EOL> elif val == b'<STR_LIT>' and right >= <NUM_LIT:1> : <EOL> getops . append ( request [ j + <NUM_LIT:1> ] ) <EOL> j += <NUM_LIT:1> <EOL> else : <EOL> return client . error_reply ( store . SYNTAX_ERROR ) <EOL> j += <NUM_LIT:1> <EOL> db = client . db <EOL> if sort_type is store . zset_type and dontsort : <EOL> dontsort = False <EOL> alpha = True <EOL> sortby = None <EOL> vector = [ ] <EOL> sortable = SortableDesc if desc else Sortable <EOL> if not dontsort : <EOL> for val in value : <EOL> if sortby : <EOL> byval = lookup ( store , db , sortby , val ) <EOL> if byval is None : <EOL> vector . append ( ( val , null ) ) <EOL> continue <EOL> else : <EOL> byval = val <EOL> if not alpha : <EOL> try : <EOL> byval = sortable ( float ( byval ) ) <EOL> except Exception : <EOL> byval = null <EOL> else : <EOL> byval = sortable ( byval ) <EOL> vector . append ( ( val , byval ) ) <EOL> vector = sorted ( vector , key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> if start is not None : <EOL> vector = vector [ start : end ] <EOL> vector = [ val for val , _ in vector ] <EOL> else : <EOL> vector = list ( value ) <EOL> if start is not None : <EOL> vector = vector [ start : end ] <EOL> if storekey is None : <EOL> if getops : <EOL> result = [ ] <EOL> for val in vector : <EOL> for getv in getops : <EOL> gval = lookup ( store , db , getv , val ) <EOL> result . append ( gval ) <EOL> vector = result <EOL> client . reply_multi_bulk ( vector ) <EOL> else : <EOL> if getops : <EOL> vals = store . list_type ( ) <EOL> empty = b'<STR_LIT>' <EOL> for val in vector : <EOL> for getv in getops : <EOL> vals . append ( lookup ( store , db , getv , val ) or empty ) <EOL> else : <EOL> vals = store . list_type ( vector ) <EOL> if db . pop ( storekey ) is not None : <EOL> store . _signal ( store . NOTIFY_GENERIC , db , '<STR_LIT>' , storekey ) <EOL> result = len ( vals ) <EOL> if result : <EOL> db . _data [ storekey ] = vals <EOL> store . _signal ( store . NOTIFY_LIST , db , '<STR_LIT>' , storekey , result ) <EOL> client . reply_int ( result ) <EOL> def lookup ( store , db , pattern , repl ) : <EOL> if pattern == b'<STR_LIT:#>' : <EOL> return repl <EOL> key = pattern . replace ( b'<STR_LIT:*>' , repl ) <EOL> bits = key . split ( b'<STR_LIT>' , <NUM_LIT:1> ) <EOL> if len ( bits ) == <NUM_LIT:1> : <EOL> string = db . get ( key ) <EOL> return bytes ( string ) if isinstance ( string , bytearray ) else None <EOL> else : <EOL> key , field = bits <EOL> hash = db . get ( key ) <EOL> return hash . get ( field ) if isinstance ( hash , store . hash_type ) else None <EOL> class Null : <EOL> __slots__ = ( ) <EOL> def __lt__ ( self , other ) : <EOL> return False <EOL> null = Null ( ) <EOL> class Sortable : <EOL> __slots__ = ( '<STR_LIT:value>' , ) <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __lt__ ( self , other ) : <EOL> if other is null : <EOL> return True <EOL> else : <EOL> return self . value < other . value <EOL> class SortableDesc : <EOL> __slots__ = ( '<STR_LIT:value>' , ) <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __lt__ ( self , other ) : <EOL> if other is null : <EOL> return True <EOL> else : <EOL> return self . value > other . value <EOL> def count_bytes ( array ) : <EOL> '''<STR_LIT>''' <EOL> count = <NUM_LIT:0> <EOL> for i in array : <EOL> i = i - ( ( i >> <NUM_LIT:1> ) & <NUM_LIT> ) <EOL> i = ( i & <NUM_LIT> ) + ( ( i >> <NUM_LIT:2> ) & <NUM_LIT> ) <EOL> count += ( ( ( i + ( i >> <NUM_LIT:4> ) ) & <NUM_LIT> ) * <NUM_LIT> ) >> <NUM_LIT> <EOL> return count <EOL> def and_op ( x , y ) : <EOL> return x & y <EOL> def or_op ( x , y ) : <EOL> return x | y <EOL> def xor_op ( x , y ) : <EOL> return x ^ y </s>
