@transactional_session
def update_collection_replica(update_request, session=None):
    '\n    Update a collection replica.\n    :param update_request: update request from the upated_col_rep table.\n    '
    if (update_request['rse_id'] is not None):
        ds_length = 0
        old_available_replicas = 0
        ds_bytes = 0
        ds_replica_state = None
        ds_available_bytes = 0
        available_replicas = 0
        try:
            collection_replica = session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name'], rse_id=update_request['rse_id']).one()
            ds_length = collection_replica.length
            old_available_replicas = collection_replica.available_replicas_cnt
            ds_bytes = collection_replica.bytes
        except NoResultFound:
            pass
        try:
            file_replica = session.query(models.RSEFileAssociation, models.DataIdentifierAssociation).filter((models.RSEFileAssociation.scope == models.DataIdentifierAssociation.child_scope), (models.RSEFileAssociation.name == models.DataIdentifierAssociation.child_name), (models.DataIdentifierAssociation.name == update_request['name']), (models.RSEFileAssociation.rse_id == update_request['rse_id']), (models.RSEFileAssociation.state == ReplicaState.AVAILABLE), (update_request['scope'] == models.DataIdentifierAssociation.scope)).with_entities(label('ds_available_bytes', func.sum(models.RSEFileAssociation.bytes)), label('available_replicas', func.count())).one()
            available_replicas = file_replica.available_replicas
            ds_available_bytes = file_replica.ds_available_bytes
        except NoResultFound:
            pass
        if (available_replicas >= ds_length):
            ds_replica_state = ReplicaState.AVAILABLE
        else:
            ds_replica_state = ReplicaState.UNAVAILABLE
        if ((old_available_replicas > 0) and (available_replicas == 0)):
            session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name'], rse_id=update_request['rse_id']).delete()
        else:
            updated_replica = session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name'], rse_id=update_request['rse_id']).one()
            updated_replica.state = ds_replica_state
            updated_replica.available_replicas_cnt = available_replicas
            updated_replica.length = ds_length
            updated_replica.bytes = ds_bytes
            updated_replica.available_bytes = ds_available_bytes
    else:
        association = session.query(models.DataIdentifierAssociation).filter_by(scope=update_request['scope'], name=update_request['name']).with_entities(label('ds_length', func.count()), label('ds_bytes', func.sum(models.DataIdentifierAssociation.bytes))).one()
        ds_length = association.ds_length
        ds_bytes = association.ds_bytes
        ds_replica_state = None
        collection_replicas = session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name']).all()
        for collection_replica in collection_replicas:
            if ds_length:
                collection_replica.length = ds_length
            else:
                collection_replica.length = 0
            if ds_bytes:
                collection_replica.bytes = ds_bytes
            else:
                collection_replica.bytes = 0
        file_replicas = session.query(models.RSEFileAssociation, models.DataIdentifierAssociation).filter((models.RSEFileAssociation.scope == models.DataIdentifierAssociation.child_scope), (models.RSEFileAssociation.name == models.DataIdentifierAssociation.child_name), (models.DataIdentifierAssociation.name == update_request['name']), (models.RSEFileAssociation.state == ReplicaState.AVAILABLE), (update_request['scope'] == models.DataIdentifierAssociation.scope)).with_entities(models.RSEFileAssociation.rse_id, label('ds_available_bytes', func.sum(models.RSEFileAssociation.bytes)), label('available_replicas', func.count())).group_by(models.RSEFileAssociation.rse_id).all()
        for file_replica in file_replicas:
            if (file_replica.available_replicas >= ds_length):
                ds_replica_state = ReplicaState.AVAILABLE
            else:
                ds_replica_state = ReplicaState.UNAVAILABLE
            collection_replica = session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name'], rse_id=file_replica.rse_id).first()
            if collection_replica:
                collection_replica.state = ds_replica_state
                collection_replica.available_replicas_cnt = file_replica.available_replicas
                collection_replica.available_bytes = file_replica.ds_available_bytes
    session.query(models.UpdatedCollectionReplica).filter_by(id=update_request['id']).delete()
Update a collection replica.
:param update_request: update request from the upated_col_rep table.
@transactional_session
def update_collection_replica(update_request, session=None):
    '\n    Update a collection replica.\n    :param update_request: update request from the upated_col_rep table.\n    '
    if (update_request['rse_id'] is not None):
        ds_length = 0
        old_available_replicas = 0
        ds_bytes = 0
        ds_replica_state = None
        ds_available_bytes = 0
        available_replicas = 0
        try:
            collection_replica = session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name'], rse_id=update_request['rse_id']).one()
            ds_length = collection_replica.length
            old_available_replicas = collection_replica.available_replicas_cnt
            ds_bytes = collection_replica.bytes
        except NoResultFound:
            pass
        try:
            file_replica = session.query(models.RSEFileAssociation, models.DataIdentifierAssociation).filter((models.RSEFileAssociation.scope == models.DataIdentifierAssociation.child_scope), (models.RSEFileAssociation.name == models.DataIdentifierAssociation.child_name), (models.DataIdentifierAssociation.name == update_request['name']), (models.RSEFileAssociation.rse_id == update_request['rse_id']), (models.RSEFileAssociation.state == ReplicaState.AVAILABLE), (update_request['scope'] == models.DataIdentifierAssociation.scope)).with_entities(label('ds_available_bytes', func.sum(models.RSEFileAssociation.bytes)), label('available_replicas', func.count())).one()
            available_replicas = file_replica.available_replicas
            ds_available_bytes = file_replica.ds_available_bytes
        except NoResultFound:
            pass
        if (available_replicas >= ds_length):
            ds_replica_state = ReplicaState.AVAILABLE
        else:
            ds_replica_state = ReplicaState.UNAVAILABLE
        if ((old_available_replicas > 0) and (available_replicas == 0)):
            session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name'], rse_id=update_request['rse_id']).delete()
        else:
            updated_replica = session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name'], rse_id=update_request['rse_id']).one()
            updated_replica.state = ds_replica_state
            updated_replica.available_replicas_cnt = available_replicas
            updated_replica.length = ds_length
            updated_replica.bytes = ds_bytes
            updated_replica.available_bytes = ds_available_bytes
    else:
        association = session.query(models.DataIdentifierAssociation).filter_by(scope=update_request['scope'], name=update_request['name']).with_entities(label('ds_length', func.count()), label('ds_bytes', func.sum(models.DataIdentifierAssociation.bytes))).one()
        ds_length = association.ds_length
        ds_bytes = association.ds_bytes
        ds_replica_state = None
        collection_replicas = session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name']).all()
        for collection_replica in collection_replicas:
            if ds_length:
                collection_replica.length = ds_length
            else:
                collection_replica.length = 0
            if ds_bytes:
                collection_replica.bytes = ds_bytes
            else:
                collection_replica.bytes = 0
        file_replicas = session.query(models.RSEFileAssociation, models.DataIdentifierAssociation).filter((models.RSEFileAssociation.scope == models.DataIdentifierAssociation.child_scope), (models.RSEFileAssociation.name == models.DataIdentifierAssociation.child_name), (models.DataIdentifierAssociation.name == update_request['name']), (models.RSEFileAssociation.state == ReplicaState.AVAILABLE), (update_request['scope'] == models.DataIdentifierAssociation.scope)).with_entities(models.RSEFileAssociation.rse_id, label('ds_available_bytes', func.sum(models.RSEFileAssociation.bytes)), label('available_replicas', func.count())).group_by(models.RSEFileAssociation.rse_id).all()
        for file_replica in file_replicas:
            if (file_replica.available_replicas >= ds_length):
                ds_replica_state = ReplicaState.AVAILABLE
            else:
                ds_replica_state = ReplicaState.UNAVAILABLE
            collection_replica = session.query(models.CollectionReplica).filter_by(scope=update_request['scope'], name=update_request['name'], rse_id=file_replica.rse_id).first()
            if collection_replica:
                collection_replica.state = ds_replica_state
                collection_replica.available_replicas_cnt = file_replica.available_replicas
                collection_replica.available_bytes = file_replica.ds_available_bytes
    session.query(models.UpdatedCollectionReplica).filter_by(id=update_request['id']).delete()