line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Flux::Storage::Memory; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Flux::Storage::Memory::VERSION = '1.03'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: in-memory storage with support for named clients |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
136083
|
use Moo; |
|
2
|
|
|
|
|
53050
|
|
|
2
|
|
|
|
|
13
|
|
9
|
|
|
|
|
|
|
with |
10
|
|
|
|
|
|
|
'Flux::Out::Role::Easy', |
11
|
|
|
|
|
|
|
'Flux::Storage', |
12
|
|
|
|
|
|
|
'Flux::Storage::Role::ClientList'; |
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
4430
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
204
|
|
15
|
2
|
|
|
2
|
|
1200
|
use Flux::Storage::Memory::In; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
90
|
|
16
|
2
|
|
|
2
|
|
25
|
use List::Util qw(sum); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2514
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has 'data' => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
default => sub { [] }, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has 'client_pos' => ( |
24
|
|
|
|
|
|
|
is => 'ro', |
25
|
|
|
|
|
|
|
default => sub { {} }, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has 'client_lock' => ( |
29
|
|
|
|
|
|
|
is => 'ro', |
30
|
|
|
|
|
|
|
default => sub { {} }, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub write { |
34
|
27
|
|
|
27
|
0
|
2645
|
my $self = shift; |
35
|
27
|
|
|
|
|
33
|
my ($item) = @_; |
36
|
|
|
|
|
|
|
|
37
|
27
|
|
|
|
|
33
|
push @{ $self->data }, $item; |
|
27
|
|
|
|
|
111
|
|
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _read { |
41
|
16
|
|
|
16
|
|
98
|
my $self = shift; |
42
|
16
|
|
|
|
|
27
|
my ($pos) = @_; |
43
|
|
|
|
|
|
|
|
44
|
16
|
|
|
|
|
62
|
return $self->data->[$pos]; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _lag { |
48
|
3
|
|
|
3
|
|
16
|
my $self = shift; |
49
|
3
|
|
|
|
|
6
|
my ($pos) = @_; |
50
|
|
|
|
|
|
|
|
51
|
3
|
|
100
|
|
|
6
|
return sum(map { length } @{$self->data}[ $pos .. @{$self->data} - 1 ]) || 0; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _lock_client { |
55
|
8
|
|
|
8
|
|
10
|
my $self = shift; |
56
|
8
|
|
|
|
|
12
|
my ($client) = @_; |
57
|
|
|
|
|
|
|
|
58
|
8
|
|
|
|
|
33
|
my $lock = $self->client_lock->{$client}; |
59
|
8
|
50
|
|
|
|
24
|
if ($lock) { |
60
|
|
|
|
|
|
|
# already locked |
61
|
0
|
|
|
|
|
0
|
return 0; |
62
|
|
|
|
|
|
|
} |
63
|
8
|
|
|
|
|
27
|
$self->client_lock->{$client} = 1; |
64
|
8
|
|
|
|
|
29
|
return 1; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _unlock_client { |
68
|
8
|
|
|
8
|
|
10
|
my $self = shift; |
69
|
8
|
|
|
|
|
17
|
my ($client) = @_; |
70
|
|
|
|
|
|
|
|
71
|
8
|
|
|
|
|
203
|
$self->client_lock->{$client} = 0; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _get_client_pos { |
75
|
7
|
|
|
7
|
|
12
|
my $self = shift; |
76
|
7
|
|
|
|
|
13
|
my ($client) = @_; |
77
|
|
|
|
|
|
|
|
78
|
7
|
|
100
|
|
|
67
|
return $self->client_pos->{$client} || 0; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _set_client_pos { |
82
|
2
|
|
|
2
|
|
14
|
my $self = shift; |
83
|
2
|
|
|
|
|
4
|
my ($client, $pos) = @_; |
84
|
|
|
|
|
|
|
|
85
|
2
|
|
|
|
|
56
|
$self->client_pos->{$client} = $pos; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub client_names { |
89
|
2
|
|
|
2
|
0
|
130
|
my $self = shift; |
90
|
2
|
|
|
|
|
3
|
return keys %{ $self->client_pos }; |
|
2
|
|
|
|
|
38
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub register_client { |
94
|
9
|
|
|
9
|
0
|
502
|
my $self = shift; |
95
|
9
|
|
|
|
|
15
|
my ($name) = @_; |
96
|
|
|
|
|
|
|
|
97
|
9
|
|
|
|
|
40
|
$self->client_pos->{$name} = 0; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub unregister_client { |
101
|
2
|
|
|
2
|
0
|
8
|
my $self = shift; |
102
|
2
|
|
|
|
|
5
|
my ($name) = @_; |
103
|
|
|
|
|
|
|
|
104
|
2
|
|
|
|
|
8
|
delete $self->client_pos->{$name}; |
105
|
2
|
|
|
|
|
10
|
delete $self->client_lock->{$name}; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub in { |
109
|
8
|
|
|
8
|
0
|
316
|
my $self = shift; |
110
|
8
|
|
|
|
|
14
|
my ($client) = @_; |
111
|
|
|
|
|
|
|
|
112
|
8
|
50
|
|
|
|
27
|
unless ($self->_lock_client($client)) { |
113
|
0
|
|
|
|
|
0
|
croak "Constructing two clients with same name '$client' for one MemoryStorage - not implemented yet"; |
114
|
|
|
|
|
|
|
} |
115
|
8
|
|
|
|
|
207
|
return Flux::Storage::Memory::In->new({ |
116
|
|
|
|
|
|
|
storage => $self, |
117
|
|
|
|
|
|
|
client => $client, |
118
|
|
|
|
|
|
|
}); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
__END__ |