fixed basic usdb stuff
[spider.git] / perl / USDB.pm
index 3b62fa5d59c5d995b5371a1e4c4a4e9460533b95..ed519b4b86aa5e2cc03d6222ffcb824586774fbe 100644 (file)
@@ -5,6 +5,8 @@
 #
 # 
 
+package USDB;
+
 use strict;
 
 use DXVars;
@@ -19,9 +21,9 @@ $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0))
 $main::build += $VERSION;
 $main::branch += $BRANCH;
 
-use vars qw(%db $present);
+use vars qw(%db $present $dbfn);
 
-my $dbfn = "$main::data/usdb.v1";
+$dbfn = "$main::data/usdb.v1";
 
 sub init
 {
@@ -64,17 +66,28 @@ sub getcity
 #
 # Note that this removes and overwrites the existing DB file
 # You will need to init again after doing this
-#
+# 
 
 sub load
 {
+       return "Need a filename" unless @_;
+       
        # create the new output file
        my $a = new DB_File::BTREEINFO;
        $a->{psize} = 4096 * 2;
-       my $s;
-       if ($s = -s $dbfn && $s > 1024 * 1024) {
-               $a->{cachesize} = int(($s / (1024*1024)) / 2) * 1024 * 1024;
+       my $s = 0;
+
+       # guess a cache size
+       for (@_) {
+               my $ts = -s;
+               $s = $ts if $ts > $s;
+       }
+       if ($s > 1024 * 1024) {
+               $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
        }
+
+#      print "cache size " . $a->{cachesize} . "\n";
+       
        my %dbn;
        if (-e $dbfn ) {
                syscopy($dbfn, "$dbfn.new") or return "cannot copy $dbfn -> $dbfn.new $!";
@@ -86,9 +99,10 @@ sub load
        for (@_) {
                my $fn = shift;
                my $f = gzopen($fn, "r") or return "Cannot open $fn $!";
-               while ($f->gzreadline) {
-                       chomp;
-                       my ($call, $city, $state) = split /\|/;
+               my $l;
+               while ($f->gzreadline($l)) {
+                       chomp $l;
+                       my ($call, $city, $state) = split /\|/, $l;
                        
                        # lookup the city 
                        my $s = "$city|$state";