well on the way to having a working cluster database
[spider.git] / perl / gdx.pl
index 3d311e38264d4d36a5be378c331c1faba986c047..06b21e9488008e479d9f317e0e414bc01a926f45 100755 (executable)
@@ -6,16 +6,16 @@
 use FileHandle;
 use DXUtil;
 use DXDebug;
-use spot;
+use Spot;
 
 # initialise spots file
 STDOUT->autoflush(1);
 
-print "reading in spot data ..";
-$t = time;
-$count = spot->init();
-$t = time - $t;
-print "done ($t secs)\n";
+#print "reading in spot data ..";
+#$t = time;
+#$count = Spot->init();
+#$t = time - $t;
+#print "done ($t secs)\n";
 
 dbgadd('spot');
 
@@ -23,17 +23,12 @@ $field = $ARGV[0];
 $expr = $ARGV[1];
 $time = time;
 
-print "$count database records read in\n";
-
 #loada();
 for (;;) {
-  print "field: ";
-  $field = <STDIN>;
-  last if $field =~ /^q/i;
   print "expr: ";
   $expr = <STDIN>;
+  last if $expr =~ /^q/i;
 
-  chomp $field;
   chomp $expr;
 
   print "doing field $field with /$expr/\n";
@@ -48,9 +43,14 @@ sub b
   my @dx;
   my $ref;
   my $count;
+  my $i;
   
-  @spots = spot->search($field, $expr);
-  
+  my $t = time;
+  @spots = Spot::search($expr);
+  if ($spots[0] eq "error") {
+    print $spots[1];
+       return;
+  }
   foreach $ref (@spots) {
     @dx = @$ref;
        my $t = ztime($dx[2]);
@@ -58,9 +58,59 @@ sub b
        print "$dx[0] $dx[1] $d $t $dx[4] <$dx[3]>\n";
        ++$count;
   }
-  print "$count records found\n";
+  $t = time - $t;
+  print "$count records found, $t secs\n";
+}
+
+sub search
+{
+  my ($expr, $from, $to) = @_;
+  my $eval;
+  my @out;
+  my @spots;
+  my $ref;
+  my $i;
+
+
+  $expr =~ s/\$f(\d)/zzzref->[$1]/g;               # swap the letter n for the correct field name
+  $expr =~ s/[\@\$\%\{\}]//g;                           # remove any other funny characters
+  $expr =~ s/\&\w+\(//g;                           # remove subroutine calls
+  $expr =~ s/eval//g;                              # remove eval words
+  $expr =~ s/zzzref/\$ref/g;                       # put back the $ref
+  
+  print "expr = $expr\n";
+  
+  # build up eval to execute
+  $eval = qq(my \$c;
+    for (\$c = \$#spots; \$c >= 0; \$c--) {
+         \$ref = \$spots[\$c];
+         if ($expr) {
+        push(\@out, \$ref);
+         }
+  });
+
+  my @today = Julian::unixtoj(time);
+  for ($i = 0; $i < 60; ++$i) {
+    my @now = Julian::sub(@today, $i);
+       my @spots;
+       my $fp = Spot->open(@now);
+       if ($fp) {
+         my $fh = $fp->{fh};
+         my $in;
+         foreach $in (<$fh>) {
+           chomp $in;
+        push @spots, [ split('\^', $in) ];
+         }
+         my $ref;
+         eval $eval;
+         return ("error", $@) if $@;
+       }
+  }
+                               # execute it
+  return @out;
 }
 
+
 sub loada
 {
   while (<IN>) {