/usr/share/perl5/AnyEvent/Tools/Buffer.pm is in libanyevent-tools-perl 0.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | use utf8;
use strict;
use warnings;
package AnyEvent::Tools::Buffer;
use AnyEvent::AggressiveIdle qw(aggressive_idle stop_aggressive_idle);
use AnyEvent::Util;
use Carp;
sub new
{
my $class = shift;
croak "usage: buffer(on_flush => sub { ... }, ...)" if @_ % 2;
my (%opts) = @_;
my $self = bless {
queue => [],
exists => {},
timer => undef,
lock => 0,
do_flush => 0,
unique_cb => undef,
} => ref($class) || $class;
$self->on_flush($opts{on_flush});
$self->size($opts{size} || 0);
$self->interval($opts{interval} || 0);
$self->unique_cb($opts{unique_cb});
return $self;
}
sub interval
{
my ($self, $ival) = @_;
return $self->{interval} if @_ == 1;
undef $self->{timer} unless $ival;
return $self->{interval} = $ival;
}
sub on_flush
{
my ($self, $cb) = @_;
croak "callback must be CODEREF" if $cb and 'CODE' ne ref $cb;
return $self->{on_flush} = $cb;
}
sub unique_cb
{
my ($self, $cb) = @_;
# disable unique checking
unless($cb) {
$self->{exists} = {};
return $self->{unique_cb} = $cb;
}
croak "unique_cb must be CODEREF" unless 'CODE' eq ref $cb;
$self->flush;
return $self->{unique_cb} = $cb;
}
sub push :method
{
my ($self, @data) = @_;
if (@data) {
if ($self->{unique_cb}) {
while(@data) {
my $add = shift @data;
my $key = $self->{unique_cb}->($add);
croak "unique_cb must return defined SCALAR"
if ref $key or !defined($key);
next if exists $self->{exists}{$key};
$self->{exists}{$key} = 1;
push @{ $self->{queue} }, $add;
}
} else {
push @{ $self->{queue} }, @data;
}
}
$self->_check_buffer;
return;
}
sub unshift :method
{
my ($self, @data) = @_;
if (@data) {
if ($self->{unique_cb}) {
while(@data) {
my $add = pop @data;
my $key = $self->{unique_cb}->($add);
croak "unique_cb must return defined SCALAR"
if ref $key or !defined($key);
next if exists $self->{exists}{$key};
$_++ for values %{ $self->{exists} };
$self->{exists}{$key} = 1;
unshift @{ $self->{queue} }, $add;
}
} else {
unshift @{ $self->{queue} }, @data;
}
}
$self->_check_buffer;
return;
}
sub unshift_back
{
my ($self, $data) = @_;
croak "Guard has already been destroyed" unless $self->{lock};
unless ($self->{unique_cb}) {
unshift @{ $self->{queue} }, @$data;
return;
}
my @buffer;
$self->{exists} = {};
for (@$data, @{ $self->{queue} }) {
my $key = $self->{unique_cb}->($_);
next if exists $self->{exists}{$key};
$self->{exists}{$key} = 1;
push @buffer, $_;
}
$self->{queue} = \@buffer;
return;
}
sub size
{
my ($self, $value) = @_;
return $self->{size} if @_ == 1;
$self->{size} = $value;
$self->_check_buffer;
return $self->{size};
}
sub flush
{
my ($self) = @_;
return unless @{ $self->{queue} };
return unless $self->{on_flush};
if ($self->{lock}) {
$self->{do_flush} = 1;
return;
}
undef $self->{timer};
my $queue = $self->{queue};
$self->{queue} = [];
$self->{exists} = {};
my $guard = guard sub {
return unless $self; # it can be destroyed
$self->{lock} = 0;
if ($self->{do_flush}) {
$self->{do_flush} = 0;
return unless @{ $self->{queue} };
aggressive_idle sub { # avoid recursion
stop_aggressive_idle $_[0];
$self->flush if $self;
};
return;
}
return unless $self;
return unless @{ $self->{queue} };
aggressive_idle sub { # avoid recursion
stop_aggressive_idle $_[0];
$self->_check_buffer if $self; # can be destroyed again
};
return;
};
$self->{lock} = 1;
$self->{on_flush}->($guard, $queue);
return;
}
sub _check_buffer
{
my ($self) = @_;
return if $self->{lock};
return unless $self->{on_flush};
unless (@{ $self->{queue} }) {
undef $self->{timer};
return;
}
if ($self->size) {
if (@{ $self->{queue} } >= $self->size) {
$self->flush;
return;
}
}
return if $self->{timer};
return unless $self->interval;
$self->{timer} = AE::timer $self->interval, 0 => sub { $self->flush };
return;
}
1;
|