Commit 694bde71 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Use iterative related-station fetch instead of recursive approach

parent dc92c7f2
Loading
Loading
Loading
Loading
+42 −52
Original line number Diff line number Diff line
@@ -160,11 +160,16 @@ sub get_station {
	my ( $self, %opt ) = @_;

	my @ret;
	my $recursion_depth = $opt{recursion_depth} // 0;
	my @queue = ( $opt{name} );
	my @seen;

	while (@queue) {
		my $station = shift(@queue);
		push( @seen, $station );

		my ( $raw, $err )
		  = $self->get_with_cache( $self->{main_cache},
		$self->{iris_base} . '/station/' . $opt{name} );
			$self->{iris_base} . '/station/' . $station );
		if ($err) {
			$self->{errstr} = "Failed to fetch station data: $err";
			return;
@@ -178,14 +183,13 @@ sub get_station {
			if ( $opt{root} ) {
				$self->{errstr}
				  = "The station '$opt{name}' has no associated timetable";
		}
				return;
			}
	if ( $recursion_depth > 5 ) {
		cluck("Reached recursion depth $recursion_depth while tracking IDs");
		return;
			next;
		}

		push( @seen, $station_node->getAttribute('eva') );

		push(
			@ret,
			{
@@ -196,29 +200,15 @@ sub get_station {
		);

		if ( $self->{developer_mode} ) {
		printf( " -> %s (%s / %s)\n", @{ $ret[0] }{qw{name uic ds100}} );
			printf( " -> %s (%s / %s)\n", @{ $ret[-1] }{qw{name uic ds100}} );
		}

	# TODO this approach is flawed, iterative is probably better

	if ( $opt{recursive} and $station_node->hasAttribute('meta') ) {
		my @recursion_blacklist = @{ $opt{recursion_blacklist} // [] };
		my @refs = uniq(split( m{ \| }x, $station_node->getAttribute('meta') ));

		push( @recursion_blacklist, map { $_->{uic} } @ret );

		for my $ref (@refs) {
			if ( not( $ref ~~ \@recursion_blacklist ) ) {
				push(
					@ret,
					$self->get_station(
						name                => $ref,
						recursive           => 1,
						recursion_depth     => $recursion_depth + 1,
						recursion_blacklist => \@recursion_blacklist,
					)
				);
			}
		if ( $opt{recursive} ) {
			my @refs
			  = uniq( split( m{ \| }x, $station_node->getAttribute('meta') ) );
			@refs = grep { not( $_ ~~ \@seen or $_ ~~ \@queue ) } @refs;
			push( @queue, @refs );
			$opt{root} = 0;
		}
	}