Changeset 332


Ignore:
Timestamp:
01/23/11 10:55:48 (13 years ago)
Author:
nanardon
Message:
  • many fixes and optimisations
Location:
server/trunk/web
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • server/trunk/web/bin/sophie_scan

    r42 r332  
    33use strict; 
    44use warnings; 
    5 use Sophie::Base; 
    6 use Sophie::Base::RpmsPath; 
    75use Linux::Inotify2; 
    86use POSIX ":sys_wait_h"; 
    97use Getopt::Long; 
     8 
     9$ENV{LC_ALL} = 'C'; 
     10require Sophie::Base; 
     11require Sophie::Base::RpmsPath; 
    1012 
    1113GetOptions( 
     
    1820my %modified_paths; 
    1921my $inotify = undef; 
    20 $updated_inotify ||= 300; 
     22$updated_inotify ||= 500; 
    2123 
    2224if ($daemon) { 
     
    2931while (1) { 
    3032    local $SIG{ALRM} = sub { 
    31         $inotify = undef; 
    32         warn "reparse paths"; 
    3333        alarm($updated_inotify); 
    3434    }; 
     
    3636        %modified_paths = (); 
    3737    } 
    38     $inotify ||= inotify_path(); 
     38    warn "$$ Inotify"; 
     39    $inotify = inotify_path(); 
    3940    $inotify->poll; 
    4041} 
     
    6869        return; 
    6970    } 
    70     warn "updating base"; 
    71  
    72     my @pkey; 
    73     { 
    74         my $sophie = Sophie::Base->connect or do { 
    75             die "cannot read config file\n"; 
    76         }; 
    77         @pkey = $sophie->resultset('Paths')->search( 
    78             path => [ @path ], 
    79         )->get_column('d_path_key')->all; 
    80         push(@pkey, $sophie->resultset('Paths')->search({ 
    81             updated => [ undef,  
    82                 \[ " < now() - '6 hours'::interval"], 
    83             ], 
    84         })->get_column('d_path_key')->all); 
    85         my %uniq = map { $_ => 1 } @pkey; 
    86         @pkey = keys %uniq; 
    87     } 
    88  
    89     return 1 if (!@pkey); 
    90  
     71    warn "$$ updating base"; 
    9172    if (my $pid = fork()) { 
    9273    } else { 
    9374 
     75        alarm 0; 
     76        my @pkey; 
     77        { 
     78            my $sophie = Sophie::Base->connect or do { 
     79                die "cannot read config file\n"; 
     80            }; 
     81            @pkey = $sophie->resultset('Paths')->search( 
     82                path => [ @path ], 
     83            )->get_column('d_path_key')->all; 
     84            push(@pkey, $sophie->resultset('Paths')->search({ 
     85                updated => [ undef,  
     86                    \[ " < now() - '24 hours'::interval"], 
     87                ], 
     88            })->get_column('d_path_key')->all); 
     89            my %uniq = map { $_ => 1 } @pkey; 
     90            @pkey = keys %uniq; 
     91        } 
    9492 
    95         my $NB_PAR = 2; 
     93        exit(0) if (!@pkey); 
     94 
     95 
     96 
     97        my $NB_PAR = 1; 
    9698        my @split; 
    9799        my $div = @pkey / $NB_PAR; 
     
    108110 
    109111                foreach my $pathkey (@{ $job }) { 
    110                     my @delta = Sophie::Base::RpmsPath->new($pathkey) 
     112                    my $time = time; 
     113                    my @delta = Sophie::Base::RpmsPath->new($pathkey, Sophie::Base->connect) 
    111114                        ->find_delta; 
    112115                    while (my @d = splice(@delta, 0, 25)) { 
    113                         my $path = Sophie::Base::RpmsPath->new($pathkey); 
     116                        my $path = Sophie::Base::RpmsPath->new($pathkey, Sophie::Base->connect); 
    114117                        $path->update_content(@d); 
    115                         $path->db->disconnect; 
     118                        #$path->db->disconnect; 
     119                        # If it take too long time, next path 
     120                        last if (time > $time + 15 * 60); 
    116121                    } 
    117                     Sophie::Base::RpmsPath->new($pathkey)->set_updated; 
     122                    Sophie::Base::RpmsPath->new($pathkey, Sophie::Base->connect)->set_updated 
     123                        if (!@delta); # update only if we finished 
    118124                } 
    119125                exit(0); 
  • server/trunk/web/lib/Sophie/Base.pm

    r42 r332  
    2929        $connect_info->{user} = $config->{dbuser}; 
    3030        $connect_info->{password} = $config->{dbpassword}; 
     31        $connect_info->{unsafe} = 1; 
    3132    } 
     33    $connect_info->{PrintError} = 0; 
     34    $connect_info->{RaiseError} = 0; 
    3235    exists($connect_info->{AutoCommit}) or $connect_info->{AutoCommit} = 0; 
    3336    $class->SUPER::connection( 
  • server/trunk/web/lib/Sophie/Base/Header.pm

    r42 r332  
    1111 
    1212sub new { 
    13     my ($class, $pkgid) = @_; 
     13    my ($class, $pkgid, $db) = @_; 
    1414 
    15     bless({ key => $pkgid }, $class); 
     15    bless({ key => $pkgid, db => $db }, $class); 
    1616} 
    1717 
    1818sub key { $_[0]->{key} } 
     19sub db  { 
     20    $_[0]->{db}->storage->dbh 
     21} 
    1922 
    2023sub rpm_path { 
     
    5962    my $add_content = $self->db->prepare_cached( 
    6063        q{ 
    61         UPDATE allfiles set contents = ? where pkgid = ? and count = ? 
     64        UPDATE allfiles set contents = ?, has_content = ? where pkgid = ? and count = ? 
    6265        } 
    6366    ); 
     
    6972            sub { 
    7073                my ($file) = @_; 
    71                 my ($dirname, $basename) = $file->name =~ /^(?:\.(.*\/))?([^\/]+)$/; 
    72                 $dirname ||= ''; 
    73                 my $entry = $files->{$dirname}{$basename}; 
     74                my $fname = $file->name; 
     75                $fname =~ s/^\.\///; 
     76                my ($dirname, $basename) = $fname =~ /^(.*\/)?([^\/]+)$/; 
     77                $dirname = $rpm =~ /src.rpm$/ 
     78                    ? '' 
     79                    : $dirname ? "/$dirname" : '' ; 
     80                my $entry = $files->{$dirname}{$basename} or do { 
     81                    warn "unknown $dirname, $basename"; 
     82                    return 1; 
     83                }; 
    7484                for (1) { 
    75                     $entry->{size} > 1024 * 1024 and return 1; 
     85                    $entry->{flags} == 32 and last; 
     86                    my $maxsize = $dirname eq '' ? 2 * 1024 : 50; 
     87                    $entry->{size} > $maxsize * 1024 and return 1; 
     88                    $basename =~ /\.gz$/ and return; 
    7689                    # Spec files and patch 
    77                     $entry->{flags} == 32 and last; 
    7890                    $dirname eq '' and last; 
    7991                    # Doc files 
     
    8698                    return 1; 
    8799                } 
    88                 my $content = $file->get_content; 
    89                 my $enc = guess_encoding($content, qw/latin1/); 
     100                my $rawcontent = $file->get_content; 
     101                my $enc = guess_encoding($rawcontent, qw/latin1/); 
    90102                if ($enc && ref $enc) { 
    91                     $content = $enc->decode($content); 
     103                    my $content = $enc->decode($rawcontent); 
    92104 
    93105                    $self->db->pg_savepoint('FILECONTENT'); 
    94106                    $add_content->execute( 
    95                         $enc && ref $enc ? encode('utf8', $enc->decode($content)) : $content, 
     107                        $enc && ref $enc ? encode('utf8', $content) : $rawcontent, 
     108                        1, 
    96109                        $self->key, 
    97110                        $entry->{count}) or do { 
    98                         $self->db->pg_rollback_to('FILECONTENT'); 
    99                     }; 
    100                 } else { 
    101                 } 
     111                            $self->db->pg_rollback_to('FILECONTENT'); 
     112                        }; 
     113                    } 
    102114                1; 
    103115            } 
  • server/trunk/web/lib/Sophie/Base/RpmsPath.pm

    r42 r332  
    1111use Encode::Guess; 
    1212use Encode; 
     13use Time::HiRes; 
    1314 
    1415sub new { 
    15     my ($class, $pathkey) = @_; 
    16  
    17     bless({ key => $pathkey }, $class); 
    18 } 
    19  
    20 sub key { $_[0]->{key} } 
     16    my ($class, $pathkey, $db) = @_; 
     17 
     18    bless({ key => $pathkey, db => $db }, $class); 
     19} 
     20 
     21sub key { $_[0]->{key} }  
     22sub db {  
     23    $_[0]->{db}->storage->dbh 
     24}  
    2125 
    2226sub path { 
     
    6266    my ($self) = @_; 
    6367 
    64     warn $self->path; 
    65  
    66     my $localrpms = $self->local_ls_rpms || {}; 
     68    warn "$$ " . $self->path; 
     69 
     70    my @delta; 
     71    my $localrpms = $self->local_ls_rpms; 
    6772    my $baserpms  = $self->ls_rpms; 
    6873 
     74    if ($localrpms) { 
     75        push(@delta, { delta => 'DE' }); 
     76    } else { 
     77        push(@delta, { delta => 'DM' }); 
     78    } 
     79 
     80 
    6981    my %list; 
    70     foreach (keys %{ $localrpms }, keys %{ $baserpms }) { 
     82    foreach (keys %{ $localrpms || {} }, keys %{ $baserpms }) { 
    7183        $list{$_} = 1; 
    7284    } 
    73     my @delta; 
    7485 
    7586    foreach my $rpm (sort { $b cmp $a } keys %list) { 
     
    91102        elsif ($_->{delta} eq 'A') { 
    92103            $self->add_rpm($_->{rpm}); 
     104            #sleep(1); 
     105            #Time::HiRes::usleep(750); 
    93106        } 
    94107        elsif ($_->{delta} eq 'R') { 
    95108            $self->remove_rpm($_->{rpm}); 
    96         } 
    97     } 
     109        } elsif ($_->{delta} eq 'DM') { 
     110            $self->set_exists(0); 
     111        } elsif ($_->{delta} eq 'DE') { 
     112            $self->set_exists(1); 
     113        } 
     114    } 
     115} 
     116 
     117sub set_exists { 
     118    my ($self, $exists) = @_; 
     119    $self->db->prepare_cached(q{ 
     120        update d_path set exists = ? where d_path_key = ? 
     121        })->execute(($exists ? 1 : 0), $self->key); 
     122    $self->db->commit; 
    98123} 
    99124 
    100125sub set_updated { 
    101126    my ($self) = @_; 
    102     warn "UPD"; 
     127    warn "$$ UPD"; 
    103128    $self->db->prepare_cached(q{ 
    104129        update d_path set updated = now() where d_path_key = ? 
     
    110135sub remove_rpm { 
    111136    my ($self, $rpm) = @_; 
     137    warn "$$ deleting $rpm"; 
    112138    my $remove = $self->db->prepare_cached( 
    113139        q{ 
     
    117143    for (1 .. 3) { 
    118144        if ($remove->execute($self->key, $rpm)) {  
    119             warn "deleting $rpm"; 
    120145            $self->db->commit; 
    121146            return 1; 
     
    128153    my ($self, $rpm) = @_; 
    129154 
     155    warn "$$ adding $rpm"; 
    130156    for (1 .. 3) { 
    131157        if (defined(my $pkgid = $self->_add_header($rpm))) { 
     
    137163                } 
    138164            ); 
    139             if ($register->execute($self->key, $rpm, $pkgid)) { 
    140                 warn "adding $rpm"; 
     165            $register->execute($self->key, $rpm, $pkgid) and do { 
    141166                $self->db->commit; 
    142                 return; 
     167                return 1; 
    143168            } 
    144169 
     
    153178    my $header; 
    154179    eval { 
    155         $header = RPM4::Header->new($self->path . '/' . $rpm)  
     180        $header = RPM4::Header->new($self->path . '/' . $rpm, $self->{db})  
    156181    }; 
    157182    $header or do { 
    158         warn "Cannot read " . $self->path . '/' . $rpm; 
     183        warn "$$ Cannot read " . $self->path . '/' . $rpm; 
    159184        return ""; 
    160185    }; 
     
    168193        $find->finish; 
    169194        if ($rows) { 
    170             warn "Find"; 
     195            warn "$$ Find"; 
    171196            return $header->queryformat('%{PKGID}'); 
    172197        } 
     
    181206    my $add_header = $self->db->prepare_cached( 
    182207        q{ 
    183         INSERT into rpms (pkgid, name, header, evr, issrc, description, summary) 
    184         values (?,?,rpmheader_in(decode(?, 'hex')::bytea),?,?,?,?) 
     208        INSERT into rpms (pkgid, name, header, evr, arch, issrc, description, summary) 
     209        values (?,?,rpmheader_in(decode(?, 'hex')::bytea),?,?,?,?,?) 
    185210        } 
    186211    ); 
     
    201226        unpack('H*', $string), 
    202227        $header->queryformat('%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}'), 
     228        $header->queryformat('%{ARCH}'), 
    203229        $header->hastag('SOURCERPM') ? 'f' : 't', 
    204230        $description, 
    205231        $summary, 
    206     ); 
     232    ) or return; 
    207233    my $index_tag = $self->db->prepare_cached( 
    208234        q{ 
     
    212238    $index_tag->execute($header->queryformat('%{PKGID}')) or return; 
    213239    $index_tag->finish; 
    214     Sophie::Base::Header->new($header->queryformat('%{PKGID}')) 
    215         ->addfiles_content({ path => $self->path, filename => $rpm}) or return; 
     240    if (!$header->hastag('SOURCERPM')) { 
     241        Sophie::Base::Header->new($header->queryformat('%{PKGID}'), $self->{db}) 
     242            ->addfiles_content({ path => $self->path, filename => $rpm}) or return; 
     243    } 
    216244 
    217245    $header->queryformat('%{PKGID}'); 
Note: See TracChangeset for help on using the changeset viewer.