| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | package LinkedList::Single::Example::Timer::Array v2.0.0; | ||
| 5 | use v5.40; | ||
| 6 | |||
| 7 | use Carp qw( carp ); | ||
| 8 | |||
| 9 | ######################################################################## | ||
| 10 | # the same queue over a perl array of [ $start, @msg ], in start order. | ||
| 11 | # this is the version a good perl programmer writes, so it uses what | ||
| 12 | # perl is fast at: | ||
| 13 | # | ||
| 14 | # enqueue a message that starts no earlier than the last is pushed. | ||
| 15 | # any other is placed by a binary search and one splice | ||
| 16 | # (a memmove of the elements behind it). | ||
| 17 | # take a binary search finds the cut, then one splice takes the | ||
| 18 | # due messages off the front. perl moves the array's start | ||
| 19 | # pointer for a splice at the front, so this does not move | ||
| 20 | # the messages that stay. | ||
| 21 | # requeue unshift of what is left. perl keeps room before the start | ||
| 22 | # of the array, so this copies only the messages put back. | ||
| 23 | # | ||
| 24 | # so the work is a binary search where the list walks, and a copy of the | ||
| 25 | # pointers of what is moved where the list relinks them. | ||
| 26 | # | ||
| 27 | # 'walked' counts the elements the module's own code steps over, as the | ||
| 28 | # list version's does (the binary search is not counted: it is a log). | ||
| 29 | ######################################################################## | ||
| 30 | |||
| 31 | sub new( $class, %arg ) | ||
| 32 | { | ||
| 33 | bless | ||
| 34 | { | ||
| 35 | clock => $arg{ clock } // sub { time } | ||
| 36 | , window => $arg{ window } // 60 | ||
| 37 | , queue => [] | ||
| 38 | , walked => 0 | ||
| 39 | , merged => 0 | ||
| 40 | } | ||
| 41 | => $class | ||
| 42 | } | ||
| 43 | |||
| 44 | ######################################################################## | ||
| 45 | # public interface | ||
| 46 | ######################################################################## | ||
| 47 | |||
| 48 | sub enqueue( $self, $start, @msg ) | ||
| 49 | { | ||
| 50 | $self->_insert( $start, @msg ); | ||
| 51 | |||
| 52 | $self | ||
| 53 | } | ||
| 54 | |||
| 55 | sub take( $self, $now = $self->{ clock }->() ) | ||
| 56 | { | ||
| 57 | my $queue = $self->{ queue }; | ||
| 58 | |||
| 59 | # the count of messages that start at or before $now: the first | ||
| 60 | # whose start is above it is where the search ends. | ||
| 61 | |||
| 62 | my( $lo, $hi ) = ( 0, scalar @$queue ); | ||
| 63 | |||
| 64 | while( $lo < $hi ) | ||
| 65 | { | ||
| 66 | my $mid = ( $lo + $hi ) >> 1; | ||
| 67 | |||
| 68 | if( $queue->[ $mid ][ 0 ] <= $now ) | ||
| 69 | { | ||
| 70 | $lo = $mid + 1; | ||
| 71 | } | ||
| 72 | else | ||
| 73 | { | ||
| 74 | $hi = $mid; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | $lo | ||
| 79 | or return undef; | ||
| 80 | |||
| 81 | my @item = splice @$queue, 0, $lo; | ||
| 82 | |||
| 83 | return | ||
| 84 | { | ||
| 85 | items => \@item | ||
| 86 | , at => 0 | ||
| 87 | , latest => $item[ -1 ][ 0 ] | ||
| 88 | , left => scalar @item | ||
| 89 | }; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub process( $self, $batch, $handler, $deadline = undef ) | ||
| 93 | { | ||
| 94 | $batch | ||
| 95 | or return 0; | ||
| 96 | |||
| 97 | my $clock = $self->{ clock }; | ||
| 98 | |||
| 99 | $deadline //= $clock->() + $self->{ window }; | ||
| 100 | |||
| 101 | my $items = $batch->{ items }; | ||
| 102 | my $done = 0; | ||
| 103 | |||
| 104 | while( $batch->{ at } < @$items ) | ||
| 105 | { | ||
| 106 | $clock->() >= $deadline | ||
| 107 | and last; | ||
| 108 | |||
| 109 | # an exception stops the batch where it was, as consume does: | ||
| 110 | # quietly for die "\n", carped for any other. | ||
| 111 | |||
| 112 | try | ||
| 113 | { | ||
| 114 | $handler->( $items->[ $batch->{ at } ]->@* ); | ||
| 115 | } | ||
| 116 | catch( $err ) | ||
| 117 | { | ||
| 118 | $err eq "\n" | ||
| 119 | or carp "Failed consume: $err\n"; | ||
| 120 | |||
| 121 | last | ||
| 122 | } | ||
| 123 | |||
| 124 | ++$batch->{ at }; | ||
| 125 | ++$done; | ||
| 126 | } | ||
| 127 | |||
| 128 | $batch->{ left } -= $done; | ||
| 129 | |||
| 130 | $done | ||
| 131 | } | ||
| 132 | |||
| 133 | sub left( $self, $batch ) | ||
| 134 | { | ||
| 135 | $batch | ||
| 136 | ? $batch->{ left } | ||
| 137 | : 0 | ||
| 138 | } | ||
| 139 | |||
| 140 | sub requeue( $self, $batch ) | ||
| 141 | { | ||
| 142 | $batch | ||
| 143 | or return 0; | ||
| 144 | |||
| 145 | my $left = $batch->{ left } | ||
| 146 | or return 0; | ||
| 147 | |||
| 148 | my $queue = $self->{ queue }; | ||
| 149 | my $items = $batch->{ items }; | ||
| 150 | my @rest = splice @$items, $batch->{ at }; | ||
| 151 | |||
| 152 | if( ! @$queue || $batch->{ latest } <= $queue->[ 0 ][ 0 ] ) | ||
| 153 | { | ||
| 154 | unshift @$queue, @rest; | ||
| 155 | } | ||
| 156 | else | ||
| 157 | { | ||
| 158 | $self->_insert( $_->@* ) for @rest; | ||
| 159 | |||
| 160 | $self->{ merged } += $left; | ||
| 161 | } | ||
| 162 | |||
| 163 | $batch->{ left } = 0; | ||
| 164 | |||
| 165 | $left | ||
| 166 | } | ||
| 167 | |||
| 168 | ######################################################################## | ||
| 169 | # utility | ||
| 170 | ######################################################################## | ||
| 171 | |||
| 172 | # put a message after every message that does not start after it. | ||
| 173 | |||
| 174 | sub _insert( $self, $start, @msg ) | ||
| 175 | { | ||
| 176 | my $queue = $self->{ queue }; | ||
| 177 | |||
| 178 | if( ! @$queue || $start >= $queue->[ -1 ][ 0 ] ) | ||
| 179 | { | ||
| 180 | push @$queue, [ $start, @msg ]; | ||
| 181 | |||
| 182 | return $self | ||
| 183 | } | ||
| 184 | |||
| 185 | my( $lo, $hi ) = ( 0, scalar @$queue ); | ||
| 186 | |||
| 187 | while( $lo < $hi ) | ||
| 188 | { | ||
| 189 | my $mid = ( $lo + $hi ) >> 1; | ||
| 190 | |||
| 191 | if( $queue->[ $mid ][ 0 ] <= $start ) | ||
| 192 | { | ||
| 193 | $lo = $mid + 1; | ||
| 194 | } | ||
| 195 | else | ||
| 196 | { | ||
| 197 | $hi = $mid; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | splice @$queue, $lo, 0, [ $start, @msg ]; | ||
| 202 | |||
| 203 | $self | ||
| 204 | } | ||
| 205 | |||
| 206 | ######################################################################## | ||
| 207 | # introspection | ||
| 208 | ######################################################################## | ||
| 209 | |||
| 210 | # arrayref of [ $start, @msg ], earliest first: copies, as the list's is. | ||
| 211 | |||
| 212 | sub contents( $self ) | ||
| 213 | { | ||
| 214 | [ map { [ $_->@* ] } $self->{ queue }->@* ] | ||
| 215 | } | ||
| 216 | |||
| 217 | sub next_start( $self ) | ||
| 218 | { | ||
| 219 | $self->{ queue }[ 0 ] && $self->{ queue }[ 0 ][ 0 ] | ||
| 220 | } | ||
| 221 | |||
| 222 | sub size( $self ) | ||
| 223 | { | ||
| 224 | scalar $self->{ queue }->@* | ||
| 225 | } | ||
| 226 | |||
| 227 | sub window( $self ) | ||
| 228 | { | ||
| 229 | $self->{ window } | ||
| 230 | } | ||
| 231 | |||
| 232 | sub walked( $self ) | ||
| 233 | { | ||
| 234 | $self->{ walked } | ||
| 235 | } | ||
| 236 | |||
| 237 | sub merged( $self ) | ||
| 238 | { | ||
| 239 | $self->{ merged } | ||
| 240 | } | ||
| 241 | |||
| 242 | 1 | ||
| 243 | __END__ |