/[Sack]/trunk/bin/sack.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/bin/sack.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations)
Wed Sep 23 20:28:21 2009 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 6840 byte(s)
extract Sack::Digest into own package and test it

1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 our $VERSION = '0.02';
7
8 use Time::HiRes qw(time);
9 use Data::Dump qw(dump);
10 use File::Slurp;
11 use Getopt::Long;
12 use IO::Socket::INET;
13 use Storable qw/freeze thaw store/;
14
15
16 my $debug = 0;
17 my $path = '/data/isi/full.txt';
18 my $limit = 5000;
19 my $offset = 0;
20 my @views;
21 my $port = 0; # interactive
22 my @nodes;
23
24
25 GetOptions(
26 'path=s' => \$path,
27 'offset=i' => \$offset,
28 'limit=i' => \$limit,
29 'view=s' => \@views,
30 'listen|port=i' => \$port,
31 'connect=s' => \@nodes,
32 'debug!' => \$debug,
33 ) or die $!;
34
35 my $t = time;
36
37
38 sub send_nodes;
39
40 our $prefix;
41 sub BEGIN {
42 $prefix = $0;
43 if ( $prefix !~ m{^/} ) {
44 chomp( my $pwd = `pwd` );
45 $prefix = "$pwd/$prefix";
46 }
47 $prefix =~ s{^(.*)/srv/Sack/[\./]+bin.+$}{$1};
48 warn "# prefix $prefix";
49
50 $SIG{INT} = sub {
51 my $signame = shift;
52 send_nodes 'exit';
53 #clean if $clean; # FIXME
54 die "SIG$signame";
55 };
56 }
57
58 use lib "$prefix/srv/Sack/lib/";
59 use Sack::Digest;
60 Sack::Digest->open( $port );
61 sub digest { Sack::Digest->digest($_[0]) }
62
63 use lib "$prefix/srv/webpac2/lib/";
64 use WebPAC::Input::ISI;
65
66 $WebPAC::Input::ISI::subfields = undef; # disable parsing of subfields
67
68 my $input = WebPAC::Input::ISI->new(
69 path => "$prefix/$path",
70 offset => $offset,
71 limit => $limit,
72 );
73
74 our $num_records = $input->size;
75
76 sub report {
77 my $description = join(' ',@_);
78 my $dt = time - $t;
79 printf "%s in %1.4fs %.2f/s\n", $description, $dt, $input->size / $dt;
80 $t = time;
81 }
82
83
84 report $input->size . ' records loaded';
85
86 mkdir 'out' unless -e 'out';
87
88 our $out;
89
90 our $cache;
91
92 our $connected;
93
94 sub send_nodes {
95 my $content = $#_ > 0 ? pop @_ : ''; # no content with just one argument!
96 my $header = defined $content ? length($content) : 0;
97 $header .= ' ' . join(' ', @_) if @_;
98
99 foreach my $node ( @nodes ) {
100
101 my $sock = IO::Socket::INET->new(
102 PeerAddr => $node,
103 Proto => 'tcp',
104 );
105
106 if ( ! $sock ) {
107 warn "can't connect to $node - $!"; # FIXME die?
108 next;
109 }
110
111 warn "[$port] >>>> $node $header\n";
112 print $sock "$header\n$content" || warn "can't send $header to $node: $!";
113
114 $connected->{$node} = $sock;
115 }
116 }
117
118 sub get_node {
119 my $node = shift;
120
121 my $sock = $connected->{$node};
122 if ( ! $sock ) {
123 warn "[$port] ERROR lost connection to $node";
124 delete $connected->{$node};
125 return;
126 }
127 chomp( my $size = <$sock> );
128 warn "[$port] <<<< $node $size bytes\n";
129 my $data;
130 read $sock, $data, $size;
131 return $data;
132 }
133
134 sub send_sock {
135 my ( $sock, $data ) = @_;
136 my $size = length $data;
137 warn "[$port] >>>> ", $sock->peerhost, " $size bytes\n";
138 print $sock "$size\n$data" || warn "can't send $size bytes to ", $sock->peerhost;
139 }
140
141 sub merge_out {
142 my $new = shift;
143
144 foreach my $k1 ( keys %$new ) {
145
146 foreach my $k2 ( keys %{ $new->{$k1} } ) {
147
148 my $n = delete $new->{$k1}->{$k2};
149 my $ref = ref $out->{$k1}->{$k2};
150
151 if ( ! defined $out->{$k1}->{$k2} ) {
152 $out->{$k1}->{$k2} = $n;
153 } elsif ( $k1 =~ m{\+} ) {
154 # warn "## agregate $k1 $k2";
155 $out->{$k1}->{$k2} += $n;
156 } elsif ( $ref eq 'ARRAY' ) {
157 if ( ref $n eq 'ARRAY' ) {
158 push @{ $out->{$k1}->{$k2} }, $_ foreach @$n;
159 } else {
160 push @{ $out->{$k1}->{$k2} }, $n;
161 }
162 } elsif ( $ref eq '' ) {
163 $out->{$k1}->{$k2} = [ $out->{$k1}->{$k2}, $n ];
164 } else {
165 die "can't merge $k2 [$ref] from ",dump($n), " into ", dump($out->{$k1}->{$k2});
166 }
167 }
168 }
169
170 warn "## merge out ", dump $out if $debug;
171 }
172
173 sub run_code {
174 my ( $view, $code ) = @_;
175
176 warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n" if $debug;
177
178 send_nodes view => $view => $code;
179
180 undef $out;
181
182 my $affected = 0;
183 $t = time;
184
185 foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
186 my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
187 if ( ! $rec ) {
188 warn "END at $pos";
189 last;
190 }
191
192 eval "$code";
193 if ( $@ ) {
194 warn "ABORT [$pos] $@\n";
195 last;
196 } else {
197 $affected++;
198 }
199
200 $pos % 10000 == 0 ? print STDERR $pos - $offset :
201 $pos % 1000 == 0 ? print STDERR "." : 0 ;
202 };
203
204 report "\n[$port] RECS $affected $view";
205
206 warn "WARN no \$out defined!" unless defined $out;
207
208 if ( $connected ) {
209 foreach my $node ( keys %$connected ) {
210 warn "[$port] get_node $node\n";
211 my $o = get_node $node;
212 my $s = length $o;
213 $o = thaw $o;
214 warn "[$port] merge $node $s bytes\n";
215 merge_out $o;
216 }
217 }
218 }
219
220 sub run_views {
221 @views = sort glob 'views/*.pl' unless @views;
222 warn "# views ", dump @views;
223
224 foreach my $view ( @views ) {
225
226 next if system("perl -c $view") != 0;
227
228 my $code = read_file $view;
229
230 run_code $view => $code;
231
232 if ( defined $out ) {
233
234 my $path = $view;
235 $path =~ s{views?/}{out/} || die "no view in $view";
236 $path =~ s{\.pl}{.storable};
237
238 unlink "$path.last" if -e "$path.last";
239 rename $path, "$path.last";
240
241 store $out => $path;
242 report "[$port] SAVE $path $offset-$limit", -s $path, "bytes";
243 }
244
245 }
246
247 }
248
249 if ( $port ) {
250 my $sock = IO::Socket::INET->new(
251 Listen => SOMAXCONN,
252 LocalAddr => '127.0.0.1',
253 LocalPort => $port,
254 Proto => 'tcp',
255 Reuse => 1,
256 ) or die $!;
257
258 while (1) {
259
260 warn "[$port] READY path: $path offset: $offset limit: $limit #recs: $num_records\n";
261
262 my $client = $sock->accept();
263
264 warn "[$port] <<<< connect from ", $client->peerhost, $/;
265
266 my @header = split(/\s/, <$client>);
267 warn "[$port] <<<< header ",dump(@header),$/;
268
269 my $size = shift @header;
270
271 my $content;
272 read $client, $content, $size;
273
274 if ( $header[0] eq 'view' ) {
275 run_code $header[1] => $content;
276 send_sock $client => freeze $out;
277 } elsif ( $header[0] eq 'info' ) {
278 my $info = "$port\t$offset\t$limit\t$num_records\t$path";
279 $info .= "\t" . eval $header[1] if $header[1];
280 warn "[$port] info $info\n";
281 send_sock $client => $info;
282 } elsif ( $header[0] eq 'exit' ) {
283 warn "[$port] exit";
284 exit;
285 } else {
286 warn "[$port] UNKNOWN $header[0]";
287 }
288
289 }
290 }
291
292 sub info {
293 send_nodes 'info' => $2;
294
295 my @info = (
296 "port\toffset\tlimit\t#recs\tpath",
297 "----\t------\t-----\t-----\t----",
298 "$port\t$offset\t$limit\t$num_records\t$path",
299 );
300
301 push @info, get_node $_ foreach @nodes;
302
303 print "[$port] INFO\n"
304 , join("\n", @info)
305 , "\n\n" ;
306
307 return @info;
308 }
309
310 info;
311 run_views;
312
313 while ( 1 ) {
314
315 print "sack> ";
316 chomp( my $cmd = <STDIN> );
317
318 if ( $cmd =~ m{^(h|\?)} ) {
319 print << "__HELP__"
320 Sacks Lorry v$VERSION - path: $path offset: $offset limit: $limit
321
322 View Run run views
323 VI \\e Output show output of last run
324 Info [\$VERSION] instrospect
325 Quit EXit shutdown
326
327 __HELP__
328 } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {
329 #system "vi out/*";
330 system "bin/storableedit.pl", (glob('out/*.storable'))[0];
331 } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {
332 info;
333 } elsif ( $cmd =~ m{^(q|e|x)}i ) {
334 warn "# exit";
335 send_nodes 'exit';
336 exit;
337 } elsif ( $cmd =~ m{^(v|r)}i ) {
338 run_views;
339 } elsif ( $cmd ) {
340 warn "UNKNOWN ", dump $cmd;
341 }
342
343 }
344

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26