IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 16765


Ignore:
Timestamp:
Feb 29, 2008, 4:20:09 PM (18 years ago)
Author:
bills
Message:

Implement fileset delete. Get data store location from config. Other enhancements.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DataStoreServer/scripts/dsreg

    r16761 r16765  
    1616use PS::IPP::Metadata::Stats;
    1717use PS::IPP::Metadata::List qw( parse_md_list );
     18use File::Copy;
    1819
    1920use PS::IPP::Config qw($PS_EXIT_SUCCESS
     
    3233my $del;
    3334
    34 #my $ds_dir = './dsroot';
    35 my $ds_dir = '/var/www/html/ds/dsroot';
     35my $ds_dir;
    3636my $product;
    3737my $fileset;
     
    7777    unless $add xor $del;
    7878
    79 $err .= "Datastore not found at '$ds_dir'.\n"
    80     if stat("$ds_dir/index.txt") eq undef;
     79my $ipprc =  PS::IPP::Config->new(); # IPP Configuration
     80my $siteConfig = $ipprc->{_siteConfig};
     81if (!$ds_dir) {
     82    $ds_dir = metadataLookupStr($ipprc->{_siteConfig}, 'PSTAMP_DATA_STORE_ROOT');
     83    if (!$ds_dir) {
     84        die("data store root not defined");
     85    }
     86}
     87
     88
     89if (!stat("$ds_dir/index.txt")) {
     90    $err .= "Datastore not found at '$ds_dir'.\n"
     91}
    8192
    8293# bail if any of the above args were improper
    8394show_usage($err)
    84     if defined($err);
     95    if ($err);
    8596
    8697$product = shift;
     
    91102$fileset = shift;
    92103
     104
    93105#
    94106# run
    95107#
    96 
    97 # allow write for the mockup
    98 umask 0011;
    99 
    100 my $ipprc =  PS::IPP::Config->new(); # IPP Configuration
    101 my $siteConfig = $ipprc->{_siteConfig};
    102108
    103109my $dbserver = metadataLookupStr($siteConfig, 'DBSERVER');
     
    111117my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Cannot connect to server\n";
    112118
    113 
    114 #
    115 # delete fileset
    116 #
     119my $index_script_name = "$ds_dir/$product/$fileset/index.txt";
     120
    117121if ($del) {
    118 
    119     die("delete not implemented yet");
    120 
    121     if (stat("$ds_dir/$product/$fileset/index.txt") == undef) {
    122         print "Fileset '$fileset' doesn't exist under $product.\n";
    123         exit(3);
    124     }
    125 
    126     # When iterating through the product index below, we rememeber the
    127     # fileset and time of the newest (assumes chrono order) fileset that is
    128     # not the fileset we're deleting, just in case we have to update the
    129     # root index.
    130     my $last_fileset;
    131     my $last_time;
    132 
    133 
    134     # Read the product index into a buffer, minus $fileset's entry
    135 
    136     open(IO, "$ds_dir/$product/index.lst")
    137         or die("Failed to open '$ds_dir/$product/index.lst' for reading\n");
    138 
    139     my $buf;
    140 
    141     while (<IO>) {
    142         my ($fs, $time, my $tmp) = split(/\|/, $_, 3);
    143         $fs =~ s/^\s+|\s+$//;
    144        
    145         # skip the one we're removing
    146         next if $fs eq $fileset;
    147 
    148         # record the last one that wasn't the one we're removing
    149         $last_fileset = $fs;
    150         $last_time = $time;
    151 
    152         $buf .= $_;
    153     }
    154 
    155     close(IO);
    156 
    157 
    158     # Write the new index
    159 
    160     open(IO, ">", "$ds_dir/$product/index.lst")
    161         or die("Failed to open '$ds_dir/$product/index.lst' for writing\n");
    162        
    163     print IO $buf;
    164     close(IO);
    165    
    166 
    167     # Remove the fileset index file
    168 
    169     unlink("$ds_dir/$product/$fileset/index.txt")
    170         or die("Failed to delete '$ds_dir/$product/$fileset/index.txt'\n");
    171 
    172     # Check the root index; if the fileset we just removed was the
    173     # 'most_recent' one, then replace it.
    174 
    175     my $doreplace = 0;
    176    
    177     open(IO, "$ds_dir/index.txt")
    178         or die("Failed to open '$ds_dir/index.txt' for reading\n");
    179 
    180     $buf = '';
    181 
    182     while (<IO>) {
    183         my ($prod, $oldrecent, $oldtime, $extra) = split(/\|/, $_, 4);
    184         $prod =~ s/^\s+|\s+$//;
    185        
    186         if ($prod ne $product) {
    187             $buf .= $_;
    188             next;
    189         }
    190 
    191         if ($oldrecent ne $fileset) {
    192             $buf .= $_;
    193             next;
    194         }
    195        
    196         $buf .= "$prod|$last_fileset|$last_time|$extra";
    197         $doreplace = 1;
    198     }
    199 
    200     close(IO);
    201 
    202     # Write the new root index if needed
    203     if ($doreplace) {
    204         open(IO, ">", "$ds_dir/index.txt")
    205             or die("Failed to open '$ds_dir/index.txt' for writing\n");
    206        
    207         print IO $buf;
    208         close(IO);
    209     }
    210  
    211     print "Removed $fileset from $product.\n";
    212 }
    213 
    214 
    215 #
    216 # addding a new fileset
    217 #
    218 else {
     122    #
     123    # delete fileset
     124    #
     125    my $stmt = $dbh->prepare("SELECT prod_id, last_fs FROM dsProduct WHERE prod_name = \'$product\'");
     126    $stmt->execute();
     127    my $prod_row = $stmt->fetchrow_hashref();
     128    my $prod_id = $prod_row->{prod_id};
     129    my $old_last_fs = $prod_row->{last_fs};
     130
     131    if (!$prod_id) {
     132        die("product $product not found");
     133    }
     134    $stmt = $dbh->prepare("SELECT fileset_id,fileset_name FROM dsFileset WHERE fileset_name = '$fileset'" .
     135                        " AND prod_id = $prod_id");
     136
     137    $stmt->execute();
     138    my $fs_row = $stmt->fetchrow_hashref();
     139
     140    if (!$fs_row) {
     141        print "Fileset '$fileset' not found under $product.\n";
     142        exit 1;
     143    }
     144
     145    # zap the index script
     146    unlink("$index_script_name");
     147
     148    my $fileset_id = $fs_row->{fileset_id};
     149
     150    $dbh->do("DELETE from dsFile where fileset_id = $fileset_id");
     151    $dbh->do("DELETE from dsFileset where fileset_id = $fileset_id");
     152
     153    if ($old_last_fs eq $fs_row->{fileset_name}) {
     154        # print STDERR "deleting most recent fileset in $product\n";
     155
     156        $stmt = $dbh->prepare("SELECT fileset_name from dsFileset " .
     157                    "WHERE prod_id = $prod_id ORDER BY reg_time DESC LIMIT 1");
     158        $stmt->execute();
     159        my $new_last_fs;
     160        my $new_newest = $stmt->fetchrow_hashref();
     161        if ($new_newest) {
     162            $new_last_fs = $new_newest->{fileset_name};
     163        } else {
     164            $new_last_fs = "none";
     165        }
     166        $dbh->do("UPDATE dsProduct SET last_fs = \'$new_last_fs\' WHERE prod_id = $prod_id");
     167    }
     168    exit 0;
     169
     170} else {
     171    #
     172    # adding a new fileset
     173    #
    219174    my $stmt = $dbh->prepare("SELECT prod_id FROM dsProduct WHERE prod_name = \'$product\'");
    220175    $stmt->execute();
     
    283238    }
    284239
     240    # create the cgi script index file by making a copy of its parent's
     241    if (!copy("$ds_dir/$product/index.txt", $index_script_name)) {
     242        die("failed to copy index script to fileset");
     243    }
     244
    285245    eval {
    286246        $dbh->{RaiseError} = 1; # raise exception if error occurs
     
    332292        print STDERR "transactionfailed, rolling back error was:\n$@\n";
    333293        eval {$dbh->rollback();};
     294        unlink($index_script_name);
    334295        exit 1;
    335296    }
    336        
    337     print "Added $fileset to $product.\n";
     297
     298    # print "Added $fileset to $product.\n";
    338299
    339300    exit 0;
     
    356317    --add        Add a new fileset with the given metadata options.
    357318                File information will be read per-line from STDIN, in the form:
    358                     fileID type chipname
     319                    fileID type [chipname]
    359320                These entries are read until EOF is sent.
    360321Options:
    361322   
    362     --dsdir      Specify the path to the Datastore (defaults to CWD).
     323    --dsdir      Specify the path to the Datastore
    363324   
    364  (The following metadata is optional because this is just a mock-up.)
     325$str",
     326        -exitval => 2
     327    );
     328
     329    if (0) {
     330     my $saveString = "(The following metadata is not used yet)
    365331
    366332    --date       ISO8601 date string. (File timestamp used if not specified.)
     
    371337    --etime      Exposure time.
    372338    --filter     Filter used.
    373     --airmass    Air mass.
    374    
    375 $str",
    376         -exitval => 2
    377     );
     339    --airmass    Air mass. ";   
     340    }
    378341}
    379342
Note: See TracChangeset for help on using the changeset viewer.