/[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 12 - (show annotations)
Tue Sep 22 10:00:22 2009 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 3509 byte(s)
find $prefix of our installation and use it

1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Time::HiRes qw(time);
7 use Data::Dump qw(dump);
8 use File::Slurp;
9 use Getopt::Long;
10 use IO::Socket::INET;
11
12
13 my $path = '/data/isi/full.txt';
14 my $limit = 5000;
15 my $offset = 0;
16 my @views;
17 my $listen;
18 my @nodes;
19
20
21 GetOptions(
22 'path=s' => \$path,
23 'offset=i' => \$offset,
24 'limit=i' => \$limit,
25 'view=s' => \@views,
26 'listen|port=i' => \$listen,
27 'connect=s' => \@nodes,
28 ) or die $!;
29
30 my $t = time;
31
32
33 our $prefix;
34 BEGIN {
35 $prefix = $0;
36 if ( $prefix =~ s{^./}{} ) {
37 chomp( my $pwd = `pwd` );
38 $prefix = "$pwd/$prefix";
39 }
40 $prefix =~ s{^(.+)/srv/Sack/bin.+$}{$1};
41 warn "# prefix $prefix";
42 }
43
44
45 use lib "$prefix/srv/webpac2/lib/";
46 use WebPAC::Input::ISI;
47 my $input = WebPAC::Input::ISI->new(
48 path => "$prefix/$path",
49 offset => $offset,
50 limit => $limit,
51 );
52
53
54 sub report {
55 my $description = shift;
56 my $dt = time - $t;
57 printf "%s in %1.4fs %.2f/s\n", $description, $dt, $input->size / $dt;
58 $t = time;
59 }
60
61
62 report $input->size . ' records loaded';
63
64 mkdir 'out' unless -e 'out';
65
66 our $out;
67
68 our $cache;
69
70 sub send_nodes {
71 my $content = pop @_;
72 my $header = length($content);
73 $header .= ' ' . join(' ', @_) if @_;
74
75 foreach my $node ( @nodes ) {
76
77 my $sock = IO::Socket::INET->new(
78 PeerAddr => $node,
79 Proto => 'tcp',
80 ) or die "can't connect to $node - $!";
81
82 print ">>>> $node $header\n";
83
84 print $sock "$header\n$content" || warn "can't send $header to $node: $!";
85
86 }
87 }
88
89 sub run_code {
90 my ( $view, $code ) = @_;
91
92 warn "\n#### CODE $view START ####\n$code\n#### CODE $view END ####\n";
93
94 send_nodes view => $view => $code;
95
96 undef $out;
97
98 my $affected = 0;
99 $t = time;
100
101 foreach my $pos ( $offset + 1 .. $offset + $input->size ) {
102 my $rec = $cache->{$pos} ||= $input->fetch_rec( $pos );
103 if ( ! $rec ) {
104 warn "END at $pos";
105 last;
106 }
107
108 eval "$code";
109 if ( $@ ) {
110 warn "ERROR [$pos] $@\n";
111 } else {
112 $affected++;
113 }
114 };
115
116 report "$affected affected records $view";
117
118 warn "WARN no \$out defined!" unless defined $out;
119 }
120
121 sub run_views {
122 @views = sort glob 'views/*.pl' unless @views;
123 warn "# views ", dump @views;
124
125 foreach my $view ( @views ) {
126
127 next if system("perl -c $view") != 0;
128
129 my $code = read_file $view;
130
131 run_code $view => $code;
132
133 if ( defined $out ) {
134 my $dump = dump $out;
135 my $len = length $dump;
136
137 my $path = $view;
138 $path =~ s{views?/}{out/} || die "no view in $view";
139 $path =~ s{\.pl}{};
140
141 print "OUT $view $offset/$limit $len bytes $path"
142 , ( $len < 10000 ? " \$out = $dump" : ' SAVED ONLY' )
143 , "\n"
144 ;
145
146 unlink "$path.last" if -e "$path.last";
147 rename $path, "$path.last";
148 write_file $path, $dump;
149 report "SAVE $path";
150 }
151
152 }
153
154 }
155
156 if ( $listen ) {
157 my $sock = IO::Socket::INET->new(
158 Listen => SOMAXCONN,
159 # LocalAddr => '0.0.0.0',
160 LocalPort => $listen,
161 Proto => 'tcp',
162 Reuse => 1,
163 ) or die $!;
164
165 while (1) {
166
167 warn "NODE listen on $listen\n";
168
169 my $client = $sock->accept();
170
171 warn "<<<< connect from ", $client->peerhost, $/;
172
173 my @header = split(/\s/, <$client>);
174 warn "# header ",dump @header;
175
176 my $size = shift @header;
177
178 my $content;
179 read $client, $content, $size;
180
181 if ( $header[0] eq 'view' ) {
182 run_code $header[1] => $content;
183 } else {
184 warn "WARN unknown";
185 }
186
187 }
188 }
189
190 run_views;
191
192 while ( 1 ) {
193
194 print "sack> ";
195 my $cmd = <STDIN>;
196
197 if ( $cmd =~ m{^(vi?|\\e|o(?:ut)?)}i ) {
198 system "vi out/*";
199 } elsif ( $cmd =~ m{^i(nfo)?}i ) {
200 print "nodes: ", dump @nodes, $/;
201 } else {
202 run_views;
203 }
204
205 }
206

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26