1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#!/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 = "<p>Please enter a search term above.</p>";
} elsif (@results == 0) {
$list = "<p>No results found for \"<b>" . escape_html($search_text) . "</b>\".</p>";
} else {
$list = "<ul>";
foreach my $res (@results) {
$list .= "<li><a href=\"/$res->{path}\">$res->{title}</a><br><small>$res->{snippet}</small></li>";
}
$list .= "</ul>";
}
my $safe_search_text = escape_html($search_text);
my $year = (localtime)[5] + 1900;
print <<"HTML";
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Search</title>
<link rel="stylesheet" href="/assets/css/main.css">
<link rel="stylesheet" href="/assets/css/skeleton.css">
</head>
<body>
<div id="nav-container" class="container">
<ul id="navlist" class="left">
<li><a href="/" class="link-decor-none">hme</a></li>
<li><a href="/log/" class="link-decor-none">log</a></li>
<li><a href="/projects/" class="link-decor-none">poc</a></li>
<li><a href="/about/" class="link-decor-none">abt</a></li>
<li class="active"><a href="/cgi-bin/find.cgi" class="link-decor-none">sws</a></li>
<li><a href="/feed.xml" class="link-decor-none">rss</a></li>
</ul>
</div>
<main class="container" id="main">
<div class="container">
<h2>Search</h2>
<form action="" method="GET">
<input id="search-box" type="text" name="q" value="$safe_search_text">
<input id="search-btn" type="submit" value="Search">
</form>
$list
<div style="background: #f4f4f4; padding: 10px; border-radius: 5px; font-family: monospace; font-size: 0.85em; margin-top: 20px; border: 1px solid #ddd;">
<strong>Performance Metrics:</strong><br>
Total Time: @{[ sprintf("%.4f", $elapsed) ]} seconds<br>
User CPU: $user_cpu s<br>
System CPU: $system_cpu s<br>
Peak RAM: $max_rss KB<br>
Files Read: $files_read (IO Activity)
</div>
</div>
</main>
<div class="footer">
<div class="container">
<div class="twelve columns right container-2">
<p id="footer-text">© ASCIIMX - $year</p>
</div>
</div>
</div>
</body>
</html>
HTML
|