#!/usr/bin/perl use strict; use warnings; use Storable qw(retrieve); use Encode qw(decode_utf8); use HTML::Escape qw(escape_html); use Time::HiRes qw(gettimeofday tv_interval); use BSD::Resource; # 1. Start Benchmark Timer my $start_time = [gettimeofday]; my $files_read = 0; # Track IO Activity # Decode search text my $search_text = ''; if (($ENV{QUERY_STRING} || '') =~ /^q=([^&]*)/) { $search_text = decode_utf8($1 // ""); $search_text =~ s/\P{Print}//g; $search_text = substr($search_text, 0, 64); $search_text =~ s/^\s+|\s+$//g; } # We search using lowercase for the case-insensitive index my $query = lc($search_text); my $query_len = length($query); my @results; if ($query_len >= 3 && -f 'sa.bin' && -f 'corpus.bin') { open(my $fh_sa, '<', 'sa.bin') or die $!; open(my $fh_cp, '<', 'corpus.bin') or die $!; my $file_map = retrieve('file_map.dat'); $files_read += 3; my $total_suffixes = (-s 'sa.bin') / 4; # Helper for binary search comparisons sub compare_at { my ($idx, $fh_sa, $fh_cp, $q, $len) = @_; seek($fh_sa, $idx * 4, 0); read($fh_sa, my $bin_off, 4); my $off = unpack("L", $bin_off); seek($fh_cp, $off, 0); read($fh_cp, my $text, $len); return ($text cmp $q, $off); } # Range Search: Find Left and Right boundaries my ($low, $high) = (0, $total_suffixes - 1); my $first_hit = -1; while ($low <= $high) { my $mid = int(($low + $high) / 2); my ($cmp) = compare_at($mid, $fh_sa, $fh_cp, $query, $query_len); if ($cmp >= 0) { $first_hit = $mid if $cmp == 0; $high = $mid - 1; } else { $low = $mid + 1; } } if ($first_hit != -1) { ($low, $high) = ($first_hit, $total_suffixes - 1); my $last_hit = $first_hit; while ($low <= $high) { my $mid = int(($low + $high) / 2); my ($cmp) = compare_at($mid, $fh_sa, $fh_cp, $query, $query_len); if ($cmp <= 0) { $last_hit = $mid if $cmp == 0; $low = $mid + 1; } else { $high = $mid - 1; } } # Collect unique file results my %seen; for my $i ($first_hit .. $last_hit) { my (undef, $offset) = compare_at($i, $fh_sa, $fh_cp, $query, $query_len); foreach my $m (@$file_map) { if ($offset >= $m->{start} && $offset < $m->{end}) { if (!$seen{$m->{path}}++) { my $snip_start = ($offset - 30 < $m->{start}) ? $m->{start} : $offset - 30; seek($fh_cp, $snip_start, 0); read($fh_cp, my $raw_snip, 80); push @results, { path => $m->{path}, title => "Result: " . (split('/', $m->{path}))[-2], snippet => "..." . escape_html($raw_snip) . "..." }; } last; } } last if @results >= 20; } } close($fh_sa); close($fh_cp); } # 2. Calculate Metrics my $end_time = [gettimeofday]; my $elapsed = tv_interval($start_time, $end_time); my $rusage = getrusage(); my $user_cpu = $rusage->utime; my $system_cpu = $rusage->stime; my $max_rss = $rusage->maxrss; # 3. Output print "Content-Type: text/html\n\n"; my $list; if ($search_text eq '') { $list = "

Please enter a search term above.

"; } elsif (@results == 0) { $list = "

No results found for \"" . escape_html($search_text) . "\".

"; } else { $list = ""; } my $safe_search_text = escape_html($search_text); my $year = (localtime)[5] + 1900; print <<"HTML"; Search

Search

$list
Performance Metrics:
Total Time: @{[ sprintf("%.4f", $elapsed) ]} seconds
User CPU: $user_cpu s
System CPU: $system_cpu s
Peak RAM: $max_rss KB
Files Read: $files_read (IO Activity)
HTML