<s> import ctypes <EOL> from ctypes . util import find_library <EOL> import os <EOL> class RejectRules ( ctypes . Structure ) : <EOL> _fields_ = [ ( "<STR_LIT>" , ctypes . c_uint64 ) , <EOL> ( "<STR_LIT>" , ctypes . c_uint8 , <NUM_LIT:8> ) , <EOL> ( "<STR_LIT>" , ctypes . c_uint8 , <NUM_LIT:8> ) , <EOL> ( "<STR_LIT>" , ctypes . c_uint8 , <NUM_LIT:8> ) , <EOL> ( "<STR_LIT>" , ctypes . c_uint8 , <NUM_LIT:8> ) , <EOL> ] <EOL> def _as_tuple ( self ) : <EOL> return ( self . object_doesnt_exist , self . object_exists , <EOL> self . version_eq_given , self . version_gt_given , <EOL> self . given_version ) <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( self . _as_tuple ( ) , other . _as_tuple ( ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % str ( self . _as_tuple ( ) ) <EOL> @ staticmethod <EOL> def exactly ( want_version ) : <EOL> return RejectRules ( object_doesnt_exist = True , version_gt_given = True , <EOL> given_version = want_version ) <EOL> def get_library_path ( ) : <EOL> path = None <EOL> if '<STR_LIT>' in os . environ : <EOL> for search_dir in os . environ [ '<STR_LIT>' ] . split ( '<STR_LIT::>' ) : <EOL> test_path = os . path . join ( search_dir , '<STR_LIT>' ) <EOL> if os . path . exists ( test_path ) : <EOL> path = test_path <EOL> break <EOL> if not path : <EOL> path = find_library ( '<STR_LIT>' ) <EOL> return path <EOL> def load_so ( ) : <EOL> not_found = ImportError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> path = get_library_path ( ) <EOL> if not path : <EOL> raise not_found <EOL> try : <EOL> so = ctypes . cdll . LoadLibrary ( path ) <EOL> except OSError as e : <EOL> if '<STR_LIT>' in str ( e ) : <EOL> raise not_found <EOL> else : <EOL> raise <EOL> def malloc_errcheck ( result , func , arguments ) : <EOL> if result == <NUM_LIT:0> : <EOL> raise MemoryError ( ) <EOL> return result <EOL> if not hasattr ( ctypes , '<STR_LIT>' ) : <EOL> class c_bool_compat ( ctypes . c_uint8 ) : <EOL> def __init__ ( self , value = None ) : <EOL> if value : <EOL> ctypes . c_uint8 . __init__ ( self , <NUM_LIT:1> ) <EOL> else : <EOL> ctypes . c_uint8 . __init__ ( self , <NUM_LIT:0> ) <EOL> @ staticmethod <EOL> def from_param ( param ) : <EOL> if param : <EOL> return ctypes . c_uint8 ( <NUM_LIT:1> ) <EOL> else : <EOL> return ctypes . c_uint8 ( <NUM_LIT:0> ) <EOL> ctypes . c_bool = c_bool_compat <EOL> from ctypes import POINTER <EOL> address = ctypes . c_char_p <EOL> buf = ctypes . c_void_p <EOL> data = ctypes . c_void_p <EOL> client = ctypes . c_void_p <EOL> enumerationState = ctypes . c_void_p <EOL> enum_key = ctypes . c_void_p <EOL> key = ctypes . c_char_p <EOL> keyLength = ctypes . c_uint16 <EOL> keyLen = ctypes . c_uint32 <EOL> len = ctypes . c_uint32 <EOL> dataLength = ctypes . c_uint32 <EOL> keysOnly = ctypes . c_uint32 <EOL> name = ctypes . c_char_p <EOL> nanoseconds = ctypes . c_uint64 <EOL> rejectRules = POINTER ( RejectRules ) <EOL> serviceLocator = ctypes . c_char_p <EOL> status = ctypes . c_int <EOL> table = ctypes . c_uint64 <EOL> version = ctypes . c_uint64 <EOL> serverId = ctypes . c_uint64 <EOL> so . rc_connect . argtypes = [ address , address , POINTER ( client ) ] <EOL> so . rc_connect . restype = status <EOL> so . rc_disconnect . argtypes = [ client ] <EOL> so . rc_disconnect . restype = None <EOL> so . rc_createTable . argtypes = [ client , name ] <EOL> so . rc_createTable . restype = status <EOL> so . rc_dropTable . argtypes = [ client , name ] <EOL> so . rc_dropTable . restype = status <EOL> so . rc_getStatus . argtypes = [ ] <EOL> so . rc_getStatus . restype = status <EOL> so . rc_getTableId . argtypes = [ client , name , POINTER ( table ) ] <EOL> so . rc_getTableId . restype = status <EOL> so . rc_enumerateTablePrepare . argtypes = [ client , table , keysOnly , <EOL> POINTER ( enumerationState ) ] <EOL> so . rc_enumerateTablePrepare . restype = None <EOL> so . rc_enumerateTableNext . argtypes = [ client , enumerationState , <EOL> POINTER ( keyLen ) , POINTER ( enum_key ) , <EOL> POINTER ( dataLength ) , POINTER ( data ) ] <EOL> so . rc_enumerateTableNextrestype = status <EOL> so . rc_enumerateTableFinalize . argtypes = [ enumerationState ] <EOL> so . rc_enumerateTableFinalize . restype = None <EOL> so . rc_read . argtypes = [ client , table , key , keyLength , rejectRules , <EOL> POINTER ( version ) , buf , len , POINTER ( len ) ] <EOL> so . rc_read . restype = status <EOL> so . rc_remove . argtypes = [ client , table , key , keyLength , rejectRules , <EOL> POINTER ( version ) ] <EOL> so . rc_remove . restype = status <EOL> so . rc_write . argtypes = [ client , table , key , keyLength , buf , len , <EOL> rejectRules , POINTER ( version ) ] <EOL> so . rc_write . restype = status <EOL> so . rc_testing_kill . argtypes = [ client , table , key , keyLength ] <EOL> so . rc_testing_kill . restype = status <EOL> so . rc_testing_fill . argtypes = [ client , table , key , keyLength , <EOL> ctypes . c_uint32 , ctypes . c_uint32 ] <EOL> so . rc_testing_fill . restype = status <EOL> so . rc_testing_get_server_id . argtypes = [ client , table , key , keyLength , <EOL> POINTER ( serverId ) ] <EOL> so . rc_testing_get_server_id . restype = status <EOL> so . rc_testing_get_service_locator . argtypes = [ client , table , key , <EOL> keyLength , serviceLocator , <EOL> ctypes . c_size_t ] <EOL> so . rc_testing_get_service_locator . restype = status <EOL> so . rc_set_runtime_option . argtypes = [ client , <EOL> ctypes . c_char_p , <EOL> ctypes . c_char_p ] <EOL> so . rc_set_runtime_option . restype = status <EOL> so . rc_testing_wait_for_all_tablets_normal . argtypes = [ client , nanoseconds ] <EOL> so . rc_testing_wait_for_all_tablets_normal . restype = None <EOL> so . rc_set_log_file . argtypes = [ ctypes . c_char_p ] <EOL> so . rc_set_log_file . restype = None <EOL> return so <EOL> def _ctype_copy ( addr , var , width ) : <EOL> ctypes . memmove ( addr , ctypes . addressof ( var ) , width ) <EOL> return addr + width <EOL> def get_key ( id ) : <EOL> if type ( id ) is int : <EOL> return str ( id ) <EOL> else : <EOL> return id <EOL> def get_keyLength ( id ) : <EOL> return len ( str ( id ) ) <EOL> class RCException ( Exception ) : <EOL> def __init__ ( self , status ) : <EOL> super ( RCException , self ) . __init__ ( '<STR_LIT>' + str ( status ) ) <EOL> self . status = status <EOL> pass <EOL> class NoObjectError ( Exception ) : <EOL> pass <EOL> class ObjectExistsError ( Exception ) : <EOL> pass <EOL> class VersionError ( Exception ) : <EOL> def __init__ ( self , want_version , got_version ) : <EOL> Exception . __init__ ( self , "<STR_LIT>" % <EOL> ( want_version , got_version ) ) <EOL> self . want_version = want_version <EOL> self . got_version = got_version <EOL> class RAMCloud ( object ) : <EOL> def __init__ ( self ) : <EOL> self . client = ctypes . c_void_p ( ) <EOL> self . hook = lambda : None <EOL> def __del__ ( self ) : <EOL> if self . client . value is not None : <EOL> so . rc_disconnect ( self . client ) <EOL> def handle_error ( self , status , actual_version = <NUM_LIT:0> , given_version = <NUM_LIT:0> ) : <EOL> if status == <NUM_LIT:0> : <EOL> return <EOL> if status == <NUM_LIT:2> : <EOL> raise NoObjectError ( ) <EOL> if status == <NUM_LIT:3> : <EOL> raise ObjectExistsError ( ) <EOL> if status == <NUM_LIT:5> : <EOL> raise VersionError ( given_version , actual_version ) <EOL> raise RCException ( status ) <EOL> def connect ( self , serverLocator = '<STR_LIT>' , <EOL> clusterName = '<STR_LIT>' ) : <EOL> s = so . rc_connect ( serverLocator , clusterName , <EOL> ctypes . byref ( self . client ) ) <EOL> self . handle_error ( s ) <EOL> def enumerate_table_prepare ( self , table_id ) : <EOL> enumeration_state = ctypes . c_void_p ( ) <EOL> so . rc_enumerateTablePrepare ( self . client , table_id , <NUM_LIT:0> , <EOL> ctypes . byref ( enumeration_state ) ) <EOL> return enumeration_state <EOL> def enumerate_table_next ( self , enumeration_state ) : <EOL> key_length = ctypes . c_uint32 ( ) <EOL> data_length = ctypes . c_uint32 ( ) <EOL> data = ctypes . c_void_p ( ) <EOL> key = ctypes . c_void_p ( ) <EOL> s = so . rc_enumerateTableNext ( self . client , enumeration_state , <EOL> ctypes . byref ( key_length ) , <EOL> ctypes . byref ( key ) , <EOL> ctypes . byref ( data_length ) , <EOL> ctypes . byref ( data ) ) <EOL> key_l = key_length . value <EOL> data_l = data_length . value <EOL> dataPtr = ctypes . cast ( data , ctypes . POINTER ( ctypes . c_char ) ) <EOL> keyPtr = ctypes . cast ( key , ctypes . POINTER ( ctypes . c_char ) ) <EOL> data_s = '<STR_LIT>' <EOL> key_s = '<STR_LIT>' <EOL> if ( key_l != <NUM_LIT:0> ) : <EOL> for i in range ( <NUM_LIT:0> , data_l ) : <EOL> data_s = data_s + dataPtr [ i ] <EOL> for i in range ( <NUM_LIT:0> , key_l ) : <EOL> key_s = key_s + keyPtr [ i ] <EOL> self . handle_error ( s ) <EOL> return ( key_s , data_s ) <EOL> def enumerate_table_finalize ( self , enumeration_state ) : <EOL> so . rc_enumerateTableFinalize ( enumeration_state ) <EOL> def create ( self , table_id , id , data ) : <EOL> reject_rules = RejectRules ( object_exists = True ) <EOL> return self . write_rr ( table_id , id , data , reject_rules ) <EOL> def create_table ( self , name , serverSpan = <NUM_LIT:1> ) : <EOL> s = so . rc_createTable ( self . client , name , serverSpan ) <EOL> self . handle_error ( s ) <EOL> def delete ( self , table_id , id , want_version = None ) : <EOL> if want_version : <EOL> reject_rules = RejectRules . exactly ( want_version ) <EOL> else : <EOL> reject_rules = RejectRules ( object_doesnt_exist = True ) <EOL> return self . delete_rr ( table_id , id , reject_rules ) <EOL> def delete_rr ( self , table_id , id , reject_rules ) : <EOL> got_version = ctypes . c_uint64 ( ) <EOL> self . hook ( ) <EOL> s = so . rc_remove ( self . client , table_id , get_key ( id ) , get_keyLength ( id ) , <EOL> ctypes . byref ( reject_rules ) , ctypes . byref ( got_version ) ) <EOL> self . handle_error ( s , got_version . value ) <EOL> return got_version . value <EOL> def drop_table ( self , name ) : <EOL> s = so . rc_dropTable ( self . client , name ) <EOL> self . handle_error ( s ) <EOL> def get_table_id ( self , name ) : <EOL> handle = ctypes . c_uint64 ( ) <EOL> s = so . rc_getTableId ( self . client , name , ctypes . byref ( handle ) ) <EOL> self . handle_error ( s ) <EOL> return handle . value <EOL> def ping ( self , serviceLocator , nonce , nanoseconds ) : <EOL> result = ctypes . c_uint64 ( ) <EOL> s = so . rc_ping ( self . client , serviceLocator , nonce , nanoseconds , <EOL> ctypes . byref ( result ) ) <EOL> self . handle_error ( s ) <EOL> return result <EOL> def read ( self , table_id , id , want_version = None ) : <EOL> if want_version : <EOL> reject_rules = RejectRules . exactly ( want_version ) <EOL> else : <EOL> reject_rules = RejectRules ( object_doesnt_exist = True ) <EOL> return self . read_rr ( table_id , id , reject_rules ) <EOL> def read_rr ( self , table_id , id , reject_rules ) : <EOL> max_length = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:2> <EOL> buf = ctypes . create_string_buffer ( max_length ) <EOL> actual_length = ctypes . c_uint32 ( ) <EOL> got_version = ctypes . c_uint64 ( ) <EOL> reject_rules . object_doesnt_exist = False <EOL> self . hook ( ) <EOL> s = so . rc_read ( self . client , table_id , get_key ( id ) , get_keyLength ( id ) , <EOL> ctypes . byref ( reject_rules ) , <EOL> ctypes . byref ( got_version ) , ctypes . byref ( buf ) , <EOL> max_length , ctypes . byref ( actual_length ) ) <EOL> self . handle_error ( s , got_version . value ) <EOL> return ( buf . raw [ <NUM_LIT:0> : actual_length . value ] , got_version . value ) <EOL> def update ( self , table_id , id , data , want_version = None ) : <EOL> if want_version : <EOL> reject_rules = RejectRules . exactly ( want_version ) <EOL> else : <EOL> reject_rules = RejectRules ( object_doesnt_exist = True ) <EOL> return self . write_rr ( table_id , id , data , reject_rules ) <EOL> def write ( self , table_id , id , data , want_version = None ) : <EOL> if want_version : <EOL> reject_rules = RejectRules ( version_gt_given = True , <EOL> given_version = want_version ) <EOL> else : <EOL> reject_rules = RejectRules ( ) <EOL> return self . write_rr ( table_id , id , data , reject_rules ) <EOL> def write_rr ( self , table_id , id , data , reject_rules ) : <EOL> got_version = ctypes . c_uint64 ( ) <EOL> self . hook ( ) <EOL> s = so . rc_write ( self . client , table_id , get_key ( id ) , get_keyLength ( id ) , <EOL> data , len ( data ) , <EOL> ctypes . byref ( reject_rules ) , ctypes . byref ( got_version ) ) <EOL> self . handle_error ( s , got_version . value ) <EOL> return got_version . value <EOL> def testing_kill ( self , table_id , id ) : <EOL> s = so . rc_testing_kill ( self . client , table_id , <EOL> get_key ( id ) , get_keyLength ( id ) ) <EOL> self . handle_error ( s ) <EOL> def testing_fill ( self , table_id , id , object_count , object_size ) : <EOL> s = so . rc_testing_fill ( self . client , table_id , <EOL> get_key ( id ) , get_keyLength ( id ) , <EOL> object_count , object_size ) <EOL> self . handle_error ( s ) <EOL> def testing_get_server_id ( self , table_id , id ) : <EOL> cserver_id = ctypes . c_uint64 ( ) <EOL> s = so . rc_testing_get_server_id ( self . client , table_id , get_key ( id ) , <EOL> get_keyLength ( id ) , <EOL> ctypes . byref ( cserver_id ) ) <EOL> self . handle_error ( s ) <EOL> return cserver_id . value <EOL> def testing_get_service_locator ( self , table_id , id ) : <EOL> max_len = <NUM_LIT> <EOL> buffer = ctypes . create_string_buffer ( max_len ) <EOL> s = so . rc_testing_get_service_locator ( self . client , <EOL> table_id , get_key ( id ) , <EOL> get_keyLength ( id ) , <EOL> buffer , max_len ) <EOL> self . handle_error ( s ) <EOL> return buffer . value <EOL> def testing_set_runtime_option ( self , option , value ) : <EOL> so . rc_set_runtime_option ( self . client , option , value ) <EOL> def testing_wait_for_all_tablets_normal ( self , timeoutNs = <NUM_LIT:2> ** <NUM_LIT:64> - <NUM_LIT:1> ) : <EOL> so . rc_testing_wait_for_all_tablets_normal ( self . client , timeoutNs ) <EOL> def set_log_file ( self , path ) : <EOL> so . rc_set_log_file ( path ) <EOL> so = load_so ( ) </s>
