<s> """<STR_LIT>""" <EOL> import struct <EOL> import six <EOL> from gcloud . _helpers import _datetime_from_microseconds <EOL> from gcloud . _helpers import _microseconds_from_datetime <EOL> from gcloud . _helpers import _to_bytes <EOL> from gcloud . bigtable . _generated import bigtable_data_pb2 as data_pb2 <EOL> from gcloud . bigtable . _generated import ( <EOL> bigtable_service_messages_pb2 as messages_pb2 ) <EOL> _PACK_I64 = struct . Struct ( '<STR_LIT>' ) . pack <EOL> MAX_MUTATIONS = <NUM_LIT> <EOL> """<STR_LIT>""" <EOL> class Row ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , row_key , table ) : <EOL> self . _row_key = _to_bytes ( row_key ) <EOL> self . _table = table <EOL> class _SetDeleteRow ( Row ) : <EOL> """<STR_LIT>""" <EOL> ALL_COLUMNS = object ( ) <EOL> """<STR_LIT>""" <EOL> def _get_mutations ( self , state ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _set_cell ( self , column_family_id , column , value , timestamp = None , <EOL> state = None ) : <EOL> """<STR_LIT>""" <EOL> column = _to_bytes ( column ) <EOL> if isinstance ( value , six . integer_types ) : <EOL> value = _PACK_I64 ( value ) <EOL> value = _to_bytes ( value ) <EOL> if timestamp is None : <EOL> timestamp_micros = - <NUM_LIT:1> <EOL> else : <EOL> timestamp_micros = _microseconds_from_datetime ( timestamp ) <EOL> timestamp_micros -= ( timestamp_micros % <NUM_LIT:1000> ) <EOL> mutation_val = data_pb2 . Mutation . SetCell ( <EOL> family_name = column_family_id , <EOL> column_qualifier = column , <EOL> timestamp_micros = timestamp_micros , <EOL> value = value , <EOL> ) <EOL> mutation_pb = data_pb2 . Mutation ( set_cell = mutation_val ) <EOL> self . _get_mutations ( state ) . append ( mutation_pb ) <EOL> def _delete ( self , state = None ) : <EOL> """<STR_LIT>""" <EOL> mutation_val = data_pb2 . Mutation . DeleteFromRow ( ) <EOL> mutation_pb = data_pb2 . Mutation ( delete_from_row = mutation_val ) <EOL> self . _get_mutations ( state ) . append ( mutation_pb ) <EOL> def _delete_cells ( self , column_family_id , columns , time_range = None , <EOL> state = None ) : <EOL> """<STR_LIT>""" <EOL> mutations_list = self . _get_mutations ( state ) <EOL> if columns is self . ALL_COLUMNS : <EOL> mutation_val = data_pb2 . Mutation . DeleteFromFamily ( <EOL> family_name = column_family_id , <EOL> ) <EOL> mutation_pb = data_pb2 . Mutation ( delete_from_family = mutation_val ) <EOL> mutations_list . append ( mutation_pb ) <EOL> else : <EOL> delete_kwargs = { } <EOL> if time_range is not None : <EOL> delete_kwargs [ '<STR_LIT>' ] = time_range . to_pb ( ) <EOL> to_append = [ ] <EOL> for column in columns : <EOL> column = _to_bytes ( column ) <EOL> delete_kwargs . update ( <EOL> family_name = column_family_id , <EOL> column_qualifier = column , <EOL> ) <EOL> mutation_val = data_pb2 . Mutation . DeleteFromColumn ( <EOL> ** delete_kwargs ) <EOL> mutation_pb = data_pb2 . Mutation ( <EOL> delete_from_column = mutation_val ) <EOL> to_append . append ( mutation_pb ) <EOL> mutations_list . extend ( to_append ) <EOL> class DirectRow ( _SetDeleteRow ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , row_key , table ) : <EOL> super ( DirectRow , self ) . __init__ ( row_key , table ) <EOL> self . _pb_mutations = [ ] <EOL> def _get_mutations ( self , state ) : <EOL> """<STR_LIT>""" <EOL> return self . _pb_mutations <EOL> def set_cell ( self , column_family_id , column , value , timestamp = None ) : <EOL> """<STR_LIT>""" <EOL> self . _set_cell ( column_family_id , column , value , timestamp = timestamp , <EOL> state = None ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _delete ( state = None ) <EOL> def delete_cell ( self , column_family_id , column , time_range = None ) : <EOL> """<STR_LIT>""" <EOL> self . _delete_cells ( column_family_id , [ column ] , time_range = time_range , <EOL> state = None ) <EOL> def delete_cells ( self , column_family_id , columns , time_range = None ) : <EOL> """<STR_LIT>""" <EOL> self . _delete_cells ( column_family_id , columns , time_range = time_range , <EOL> state = None ) <EOL> def commit ( self ) : <EOL> """<STR_LIT>""" <EOL> mutations_list = self . _get_mutations ( None ) <EOL> num_mutations = len ( mutations_list ) <EOL> if num_mutations == <NUM_LIT:0> : <EOL> return <EOL> if num_mutations > MAX_MUTATIONS : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( num_mutations , MAX_MUTATIONS ) ) <EOL> request_pb = messages_pb2 . MutateRowRequest ( <EOL> table_name = self . _table . name , <EOL> row_key = self . _row_key , <EOL> mutations = mutations_list , <EOL> ) <EOL> client = self . _table . _cluster . _client <EOL> client . _data_stub . MutateRow ( request_pb , client . timeout_seconds ) <EOL> self . clear ( ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . _pb_mutations [ : ] <EOL> class ConditionalRow ( _SetDeleteRow ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , row_key , table , filter_ ) : <EOL> super ( ConditionalRow , self ) . __init__ ( row_key , table ) <EOL> self . _filter = filter_ <EOL> self . _true_pb_mutations = [ ] <EOL> self . _false_pb_mutations = [ ] <EOL> def _get_mutations ( self , state ) : <EOL> """<STR_LIT>""" <EOL> if state : <EOL> return self . _true_pb_mutations <EOL> else : <EOL> return self . _false_pb_mutations <EOL> def commit ( self ) : <EOL> """<STR_LIT>""" <EOL> true_mutations = self . _get_mutations ( state = True ) <EOL> false_mutations = self . _get_mutations ( state = False ) <EOL> num_true_mutations = len ( true_mutations ) <EOL> num_false_mutations = len ( false_mutations ) <EOL> if num_true_mutations == <NUM_LIT:0> and num_false_mutations == <NUM_LIT:0> : <EOL> return <EOL> if ( num_true_mutations > MAX_MUTATIONS or <EOL> num_false_mutations > MAX_MUTATIONS ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> MAX_MUTATIONS , num_true_mutations , num_false_mutations ) ) <EOL> request_pb = messages_pb2 . CheckAndMutateRowRequest ( <EOL> table_name = self . _table . name , <EOL> row_key = self . _row_key , <EOL> predicate_filter = self . _filter . to_pb ( ) , <EOL> true_mutations = true_mutations , <EOL> false_mutations = false_mutations , <EOL> ) <EOL> client = self . _table . _cluster . _client <EOL> resp = client . _data_stub . CheckAndMutateRow ( <EOL> request_pb , client . timeout_seconds ) <EOL> self . clear ( ) <EOL> return resp . predicate_matched <EOL> def set_cell ( self , column_family_id , column , value , timestamp = None , <EOL> state = True ) : <EOL> """<STR_LIT>""" <EOL> self . _set_cell ( column_family_id , column , value , timestamp = timestamp , <EOL> state = state ) <EOL> def delete ( self , state = True ) : <EOL> """<STR_LIT>""" <EOL> self . _delete ( state = state ) <EOL> def delete_cell ( self , column_family_id , column , time_range = None , <EOL> state = True ) : <EOL> """<STR_LIT>""" <EOL> self . _delete_cells ( column_family_id , [ column ] , time_range = time_range , <EOL> state = state ) <EOL> def delete_cells ( self , column_family_id , columns , time_range = None , <EOL> state = True ) : <EOL> """<STR_LIT>""" <EOL> self . _delete_cells ( column_family_id , columns , time_range = time_range , <EOL> state = state ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . _true_pb_mutations [ : ] <EOL> del self . _false_pb_mutations [ : ] <EOL> class AppendRow ( Row ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , row_key , table ) : <EOL> super ( AppendRow , self ) . __init__ ( row_key , table ) <EOL> self . _rule_pb_list = [ ] <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . _rule_pb_list [ : ] <EOL> def append_cell_value ( self , column_family_id , column , value ) : <EOL> """<STR_LIT>""" <EOL> column = _to_bytes ( column ) <EOL> value = _to_bytes ( value ) <EOL> rule_pb = data_pb2 . ReadModifyWriteRule ( family_name = column_family_id , <EOL> column_qualifier = column , <EOL> append_value = value ) <EOL> self . _rule_pb_list . append ( rule_pb ) <EOL> def increment_cell_value ( self , column_family_id , column , int_value ) : <EOL> """<STR_LIT>""" <EOL> column = _to_bytes ( column ) <EOL> rule_pb = data_pb2 . ReadModifyWriteRule ( family_name = column_family_id , <EOL> column_qualifier = column , <EOL> increment_amount = int_value ) <EOL> self . _rule_pb_list . append ( rule_pb ) <EOL> def commit ( self ) : <EOL> """<STR_LIT>""" <EOL> num_mutations = len ( self . _rule_pb_list ) <EOL> if num_mutations == <NUM_LIT:0> : <EOL> return { } <EOL> if num_mutations > MAX_MUTATIONS : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( num_mutations , MAX_MUTATIONS ) ) <EOL> request_pb = messages_pb2 . ReadModifyWriteRowRequest ( <EOL> table_name = self . _table . name , <EOL> row_key = self . _row_key , <EOL> rules = self . _rule_pb_list , <EOL> ) <EOL> client = self . _table . _cluster . _client <EOL> row_response = client . _data_stub . ReadModifyWriteRow ( <EOL> request_pb , client . timeout_seconds ) <EOL> self . clear ( ) <EOL> return _parse_rmw_row_response ( row_response ) <EOL> def _parse_rmw_row_response ( row_response ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for column_family in row_response . families : <EOL> column_family_id , curr_family = _parse_family_pb ( column_family ) <EOL> result [ column_family_id ] = curr_family <EOL> return result <EOL> def _parse_family_pb ( family_pb ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for column in family_pb . columns : <EOL> result [ column . qualifier ] = cells = [ ] <EOL> for cell in column . cells : <EOL> val_pair = ( <EOL> cell . value , <EOL> _datetime_from_microseconds ( cell . timestamp_micros ) , <EOL> ) <EOL> cells . append ( val_pair ) <EOL> return family_pb . name , result </s>
