/[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 45 - (show annotations)
Thu Sep 24 13:37:53 2009 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 7026 byte(s)
turn Sack::Digest into normal perl object with new instead of open

1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 our $VERSION = '0.03';
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 our $digest = Sack::Digest->new( port => $port, clean => 1 );
61 sub digest { $digest->to_int($_[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 print STDERR "END @ $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 :
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 if ( -s $path < 4096 ) {
245 print '$out = ', dump $digest->undigest_out($out);
246 }
247 }
248
249 }
250
251 }
252
253
254 sub info_tabs {
255 "$port\t$offset\t$limit\t$num_records\t$path\t"
256 . join("\t", map {
257 my $b = $_;
258 $b =~ s{^.+/([^/]+)$}{$1};
259 "$b " . -s $_
260 } glob '/dev/shm/sack.*' );
261 }
262
263
264 if ( $port ) {
265 my $sock = IO::Socket::INET->new(
266 Listen => SOMAXCONN,
267 LocalAddr => '127.0.0.1',
268 LocalPort => $port,
269 Proto => 'tcp',
270 Reuse => 1,
271 ) or die $!;
272
273 while (1) {
274
275 warn "[$port] READY path: $path offset: $offset limit: $limit #recs: $num_records\n";
276
277 my $client = $sock->accept();
278
279 warn "[$port] <<<< connect from ", $client->peerhost, $/;
280
281 my @header = split(/\s/, <$client>);
282 warn "[$port] <<<< header ",dump(@header),$/;
283
284 my $size = shift @header;
285
286 my $content;
287 read $client, $content, $size;
288
289 if ( $header[0] eq 'view' ) {
290 run_code $header[1] => $content;
291 send_sock $client => freeze $out;
292 } elsif ( $header[0] eq 'info' ) {
293 my $info = info_tabs;
294 warn "[$port] info $info\n";
295 send_sock $client => $info;
296 } elsif ( $header[0] eq 'exit' ) {
297 warn "[$port] exit";
298 exit;
299 } else {
300 warn "[$port] UNKNOWN $header[0]";
301 }
302
303 }
304 }
305
306 sub info {
307 send_nodes 'info' => $2;
308
309 my @info = (
310 "port\toffset\tlimit\t#recs\tpath",
311 "----\t------\t-----\t-----\t----",
312 info_tabs,
313 );
314
315 push @info, get_node $_ foreach @nodes;
316
317 print "[$port] INFO\n"
318 , join("\n", @info)
319 , "\n\n" ;
320
321 return @info;
322 }
323
324 info;
325 run_views;
326
327 while ( 1 ) {
328
329 print "sack> ";
330 chomp( my $cmd = <STDIN> );
331
332 if ( $cmd =~ m{^(h|\?)} ) {
333 print << "__HELP__"
334 Sacks Lorry v$VERSION - path: $path offset: $offset limit: $limit
335
336 View Run run views
337 VI \\e Output show output of last run
338 Info [\$VERSION] instrospect
339 Quit EXit shutdown
340
341 __HELP__
342 } elsif ( $cmd =~ m{^(vi|\\e|o)}i ) {
343 #system "vi out/*";
344 $digest->sync;
345 system "bin/storableedit.pl", (glob('out/*.storable'))[0];
346 } elsif ( $cmd =~ m{^i(?:nfo)?\s?(.+)?$}i ) {
347 info;
348 } elsif ( $cmd =~ m{^(q|e|x)}i ) {
349 warn "# exit";
350 send_nodes 'exit';
351 exit;
352 } elsif ( $cmd =~ m{^(v|r)}i ) {
353 run_views;
354 } elsif ( $cmd ) {
355 warn "UNKNOWN ", dump $cmd;
356 }
357
358 }
359

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26