#!/usr/bin/perl use warnings; use strict; $|=1; my $opt_s = 0; #( 33495226 + 10000 ) * 48 / 4; # max slabsize my $opt_S = 4; # number of slabs my $opt_b = 3; # number of banks my $opt_f = "rgb4x4"; # base filename my $opt_u = 1; # update mode #use DBI; #my $dbh = DBI->connect("DBI:mysql:database=loot;mysql_socket=/tmp/myloot.sock", "loot", undef) or die "no dbh"; use lib "/home/tma"; use TMA::LootDb; my $dbh = TMA::LootDb::get_dbh(); use Fcntl ':flock'; my $lf = "/tmp/nbid.$opt_f.lock"; open LF, ">>", $lf or die "open($lf): $!"; my $lr = flock(LF,LOCK_EX|LOCK_NB); die "lock($lf) undef" unless defined $lr; die "lock($lf) failed" unless $lr; my ($tmax, $thash,) = (0, {},); my @sl; my $mode; if ($opt_u) { @sl = ($opt_S,); $mode = ">>"; my $cf = $opt_f.".cur"; die "no $cf" unless -e $cf; my $c = do $cf or die "do(cf): $!"; ($tmax, $thash,) = @$c; # die "what now"; } else { @sl = (0..($opt_S-1)); $mode = ">"; } die "no sl" unless @sl; printf "Opening '%s', %i slabs x %i banks\n", $opt_f, scalar @sl, $opt_b; my @slabs = (); for my $c (@sl) { $slabs[$c] = [[],[],]; my $fn = sprintf "%s.slab%s", $opt_f, $c; my $fh = undef; open $fh, $mode, $fn.".ids" or die "cant open ids: $!"; $slabs[$c][0] = $fh; for my $b (0..($opt_b-1)) { my $fh = undef; open $fh, $mode, $fn.".bank".$b or die "cant open bank$b: $!"; $slabs[$c][1][$b] = $fh; } #open IF, ">", "rgb4x4.ids" or die "cant open i: $!"; } my $tcur; unless ($tmax) { my $sth = $dbh->prepare_cached(qq{ select unix_timestamp(min(tstamp)) from v_rgb4x4 }) or die $dbh->errstr; $sth->execute() or die $sth->errstr; my $ar = $sth->fetchrow_arrayref(); $sth->finish(); die "no ar" unless defined $ar; die "bad ar $ar" unless ref $ar eq "ARRAY"; die "empty ar" unless @$ar; $tmax = $ar->[0]; #$tmax = 1318000000; } die "no tmax" unless $tmax; printf "starting at tstamp %s\n", $tmax; my $stepsize = 7*24*60*60; my $sth = $dbh->prepare_cached(qq{ select lid, vec, unix_timestamp(tstamp) from v_rgb4x4 where tstamp between from_unixtime(?) and from_unixtime(?) order by tstamp }) or die $dbh->errstr; $tcur ||= $tmax; DOLOOP: my $ocur = $tcur; printf "selecting from %s ...", $ocur; $sth->execute($ocur, $ocur+$stepsize) or die $sth->errstr; printf " executed "; my $c = 0; my ($l, $v, $t,); $sth->bind_columns(\$l, \$v, \$t) or die $sth->errstr; while ($sth->fetch()) { if ($t == $tmax) { if (exists $thash->{$l}) { printf "-"; #printf "SKIP(%s)", $l; #delete $thash->{$l}; next; } } my $s; if ($opt_u) { $s = $opt_S; } else { $s = $c % $opt_S; } die "no slab" unless defined $s; my $f = $slabs[$s]; my $fh = $f->[0]; die "no i fh for slab$s" unless $fh; print $fh pack("N",$l); my @b = unpack("a16 a16 a16", $v); # TODO dynamic format handling die "mismatch" unless scalar @b == scalar @{$f->[1]}; for (0..$#b) { my $fh = $f->[1]->[$_]; die "no v fh for slab$s bank$_" unless $fh; die "bad value" unless length $b[$_] == 16; print $fh $b[$_]; } $c++; if ($t > $tmax) { $tcur = $tmax = $t; $thash = {$l=>1,}; } elsif ($t == $tmax) { $thash->{$l} = 1; } else { die "time reversal!"; } print "+" unless $c % 100000; } $sth->finish(); printf " %i rows", $c; if (($ocur < (time-$stepsize)) || ($c && ((time-10) > $tcur))) { if ($tcur == $ocur) { printf " FORCEBUMP"; $tcur = $ocur + $stepsize; } printf "\n"; goto DOLOOP; } printf "\n"; use Data::Dumper; open CF, ">", $opt_f.".cur" or die $!; print CF Dumper([$tmax,$thash,]); close CF; close LF;