def add_to_delta_list(self,delta_to_add_to,current_internal_val,action_event): ''' @param {varStoreDeltas.SingleMapDelta} delta_to_add_to --- We log all operations on this variable ''' changes_made = False # for all keys that were not changed directly, must check # whether any potentially nested elements changed. keys_affected = {} # FIXME: only need to keep track of change log for peered # variables. May be expensive to otherwise. for partner_change in self.partner_change_log: changes_made = True # FIXME: no need to transmit overwrites. but am doing # that currently. map_action = delta_to_add_to.map_actions.add() if is_delete_key_tuple(partner_change): map_action.container_action = VarStoreDeltas.ContainerAction.DELETE_KEY delete_action = map_action.deleted_key key = partner_change[1] keys_affected[key] = True if isinstance(key,numbers.Number): delete_action.deleted_key_num = key elif util.is_string(key): delete_action.deleted_key_text = key elif isinstance(key,bool): delete_action.deleted_key_tf = key #### DEBUG else: util.logger_assert('Unknown map key type when serializing') #### END DEBUG elif is_add_key_tuple(partner_change): key = partner_change[1] keys_affected[key] = True if key in current_internal_val: # note, key may not be in internal val, for # instance if we had deleted it after adding. # in this case, can ignore the add here. map_action.container_action = VarStoreDeltas.ContainerAction.ADD_KEY add_action = map_action.added_key add_action.parent_type = VarStoreDeltas.CONTAINER_ADDED if isinstance(key,numbers.Number): add_action.added_key_num = key elif util.is_string(key): add_action.added_key_text = key elif isinstance(key,bool): add_action.added_key_tf = key #### DEBUG else: util.logger_assert('Unknown map key type when serializing') #### END DEBUG # now actually add the value to the map map_val = current_internal_val[key] if isinstance(map_val,numbers.Number): add_action.added_what_num = map_val elif util.is_string(map_val): add_action.added_what_text = map_val elif isinstance(map_val,bool): add_action.added_what_tf = map_val elif isinstance(map_val,WaldoObj): map_val.serializable_var_tuple_for_network( add_action,'',action_event, # true here because if anything is written # or added, then we must force the entire # copy of it. True) #### DEBUG else: util.logger_assert('Unknown map value type when serializing') #### END DEBUG elif is_write_key_tuple(partner_change): key = partner_change[1] keys_affected[key] = True if key in current_internal_val: # note, key may not be in internal val, for # instance if we had deleted it after adding. # in this case, can ignore the add here. map_action.container_action = VarStoreDeltas.ContainerAction.WRITE_VALUE write_action = map_action.write_key write_action.parent_type = VarStoreDeltas.CONTAINER_WRITTEN if isinstance(key,numbers.Number): write_action.write_key_num = key elif util.is_string(key): write_action.write_key_text = key elif isinstance(key,bool): write_action.write_key_tf = key #### DEBUG else: util.logger_assert('Unknown map key type when serializing') #### END DEBUG # now actually add the value to the map map_val = current_internal_val[key] if isinstance(map_val,numbers.Number): write_action.what_written_num = map_val elif util.is_string(map_val): write_action.what_written_text = map_val elif isinstance(map_val,bool): write_action.what_written_tf = map_val elif isinstance(map_val,WaldoObj): map_val.serializable_var_tuple_for_network( write_action,'',action_event, # true here because if anything is written # or added, then we must force the entire # copy of it. True) #### DEBUG else: util.logger_assert('Unknown map value type when serializing') #### END DEBUG #### DEBUG else: util.logger_assert('Unknown container operation') #### END DEBUG for map_key in current_internal_val: map_val = current_internal_val[map_key] if not isinstance(map_val,WaldoObj): break if map_key not in keys_affected: # create action sub_element_action = delta_to_add_to.sub_element_update_actions.add() sub_element_action.parent_type = VarStoreDeltas.SUB_ELEMENT_ACTION if isinstance(map_key,numbers.Number): sub_element_action.key_num = map_key elif util.is_string(map_key): sub_element_action.key_text = map_key else: sub_element_action.key_tf = map_key if map_val.serializable_var_tuple_for_network(sub_element_action,'',action_event,False): changes_made = True else: # no change made to subtree: go ahead and delete added subaction del delta_to_add_to.sub_element_update_actions[-1] # clean out change log: do not need to re-send updates for # these changes to partner, so can just reset after sending # once. self.partner_change_log = [] return changes_made
def add_to_delta_list(self,delta_to_add_to,current_internal_val,action_event): ''' @param {varStoreDeltas.SingleListDelta} delta_to_add_to --- @param {list} current_internal_val --- The internal val of the action event. @param {_InvalidationListener} action_event @returns {bool} --- Returns true if have any changes to add false otherwise. ''' modified_indices = {} changes_made = False # FIXME: only need to keep track of change log for peered # variables. May be expensive to otherwise. for partner_change in self.partner_change_log: changes_made = True # FIXME: no need to transmit overwrites. but am doing # that currently. list_action = delta_to_add_to.list_actions.add() if is_delete_key_tuple(partner_change): list_action.container_action = VarStoreDeltas.ContainerAction.DELETE_KEY delete_action = list_action.deleted_key key = partner_change[1] modified_indices[key] = True delete_action.deleted_key_num = key elif is_add_key_tuple(partner_change): key = partner_change[1] modified_indices[key] = True if key < len(current_internal_val): # note, key may not be in internal val, for # instance if we had deleted it after adding. # in this case, can ignore the add here. list_action.container_action = VarStoreDeltas.ContainerAction.ADD_KEY add_action = list_action.added_key add_action.parent_type = VarStoreDeltas.CONTAINER_ADDED add_action.added_key_num = key # now actually add the value to the map list_val = current_internal_val[key] if isinstance(list_val,numbers.Number): add_action.added_what_num = list_val elif util.is_string(list_val): add_action.added_what_text = list_val elif isinstance(list_val,bool): add_action.added_what_tf = list_val elif isinstance(list_val,WaldoObj): list_val.serializable_var_tuple_for_network( add_action,'',action_event, # true here because if anything is written # or added, then we must force the entire # copy of it. True) #### DEBUG else: util.logger_assert('Unknown list value type when serializing') #### END DEBUG elif is_write_key_tuple(partner_change): key = partner_change[1] modified_indices[key] = True if key < len(current_internal_val): list_action.container_action = VarStoreDeltas.ContainerAction.WRITE_VALUE write_action = list_action.write_key write_action.parent_type = VarStoreDeltas.CONTAINER_WRITTEN write_action.write_key_num = key list_val = current_internal_val[key] if isinstance(list_val,numbers.Number): write_action.what_written_num = list_val elif util.is_string(list_val): write_action.what_written_text = list_val elif isinstance(list_val,bool): write_action.what_written_tf = list_val elif isinstance(list_val,WaldoObj): list_val.serializable_var_tuple_for_network( write_action,'',action_event, # true here because if anything is written # or added, then we must force the entire # copy of it. True) #### DEBUG else: util.logger_assert('Unknown list type') #### END DEBUG #### DEBUG else: util.logger_assert('Unknown container operation') #### END DEBUG for index in range(0,len(current_internal_val)): list_val = current_internal_val[index] if not isinstance(list_val,WaldoObj): break if index not in modified_indices: # create action sub_element_action = delta_to_add_to.sub_element_update_actions.add() sub_element_action.parent_type = VarStoreDeltas.SUB_ELEMENT_ACTION sub_element_action.key_num = index if map_val.serializable_var_tuple_for_network(sub_element_action,'',action_event,False): changes_made = True else: # no change made to subtree: go ahead and delete added subaction del delta_to_add_to.sub_element_update_actions[-1] # clean out change log: do not need to re-send updates for # these changes to partner, so can just reset after sending # once. self.partner_change_log = [] return changes_made