+ my $key = shift;
+ my $ref;
+ my $gotkey = $key;
+ return () if $db->seq($gotkey, $ref, R_CURSOR);
+ return () if $key ne substr $gotkey, 0, length $key;
+
+ return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
+}
+
+#
+# get the next key that matches, this assumes that you have done a 'get' first
+#
+#
+sub next
+{
+ my $key = shift;
+ my $ref;
+ my $gotkey;
+
+ return () if $db->seq($gotkey, $ref, R_NEXT);
+ return () if $key ne substr $gotkey, 0, length $key;
+
+ return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
+}
+
+#
+# put the key LRU incluing the city state info
+#
+
+sub lru_put
+{
+ my ($call, $ref) = @_;
+ my @s = USDB::get($call);
+
+ if (@s) {
+ # this is deep magic, because this is a reference to static data, it
+ # must be copied.
+ my $h = { %{$ref->[1]} };
+ bless $h, ref $ref->[1];
+ $h->{city} = $s[0];
+ $h->{state} = $s[1];
+ $ref->[1] = $h;
+ } else {
+ $ref->[1]->{city} = $ref->[1]->{state} = "" unless exists $ref->[1]->{state};
+ }
+
+ dbg("Prefix::lru_put $call -> ($ref->[1]->{city}, $ref->[1]->{state})") if isdbg('prefix');
+ $lru->put($call, $ref);
+}
+
+#
+# search for the nearest match of a prefix string (starting
+# from the RH end of the string passed)
+#
+
+sub matchprefix
+{
+ my $pref = shift;
+ my @partials;
+
+ for (my $i = length $pref; $i; $i--) {
+ $matchtotal++;
+ my $s = substr($pref, 0, $i);
+ push @partials, $s;
+ my $p = $lru->get($s);
+ if ($p) {
+ $hits++;
+ if (isdbg('prefix')) {
+ my $percent = sprintf "%.1f", $hits * 100 / $misses;
+ dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
+ }
+ lru_put($_, $p) for @partials;
+ return @$p;
+ } else {
+ $misses++;
+ my @out = get($s);
+ if (isdbg('prefix')) {
+ my $part = $out[0] || "*";
+ $part .= '*' unless $part eq '*' || $part eq $s;
+ dbg("Partial prefix: $pref $s $part" );
+ }
+ if (@out && $out[0] eq $s) {
+ return @out;
+ }
+ }
+ }
+ return ();
+}
+
+#
+# extract a 'prefix' from a callsign, in other words the largest entity that will
+# obtain a result from the prefix table.
+#
+# This is done by repeated probing, callsigns of the type VO1/G1TLH or
+# G1TLH/VO1 (should) return VO1
+#
+
+sub extract
+{
+ my $calls = uc shift;
+ my @out;
+ my $p;
+ my @parts;
+ my ($call, $sp, $i);
+
+LM: foreach $call (split /,/, $calls) {
+
+ # first check if the whole thing succeeds either because it is cached
+ # or because it simply is a stored prefix as callsign (or even a prefix)
+ $matchtotal++;
+ $call =~ s/-\d+$//; # ignore SSIDs
+ my $p = $lru->get($call);
+ my @nout;
+ if ($p) {
+ $hits++;
+ if (isdbg('prefix')) {
+ my $percent = sprintf "%.1f", $hits * 100 / $misses;
+ dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
+ }
+ push @out, @$p;
+ next;
+ } else {
+
+ # is it in the USDB, force a matchprefix to match?
+ my @s = USDB::get($call);
+ if (@s) {
+ @nout = get($call);
+ @nout = matchprefix($call) unless @nout;
+ $nout[0] = $call if @nout;
+ } else {
+ @nout = get($call);
+ }
+
+ # now store it
+ if (@nout && $nout[0] eq $call) {
+ $misses++;
+ lru_put($call, \@nout);
+ dbg("got exact prefix: $nout[0]") if isdbg('prefix');
+ push @out, @nout;
+ next;
+ }
+ }
+
+ # now split the call into parts if required
+ @parts = ($call =~ '/') ? split('/', $call) : ($call);
+ dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
+
+ # remove any /0-9 /P /A /M /MM /AM suffixes etc
+ if (@parts > 1) {
+ @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
+
+ # can we resolve them by direct lookup
+ my $s = join('/', @parts);
+ @nout = get($s);
+ if (@nout && $nout[0] eq $s) {
+ dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
+ $misses++;
+ lru_put($call, \@nout);
+ push @out, @nout;
+ next;
+ }
+ }
+ dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');