| 1 | package NSNMP::Mapper; | ||||||||||
| 2 | # Copyright (c) 2003-2004 AirWave Wireless, Inc. | ||||||||||
| 3 | |||||||||||
| 4 | # Redistribution and use in source and binary forms, with or without | ||||||||||
| 5 | # modification, are permitted provided that the following conditions | ||||||||||
| 6 | # are met: | ||||||||||
| 7 | |||||||||||
| 8 | # 1. Redistributions of source code must retain the above | ||||||||||
| 9 | # copyright notice, this list of conditions and the following | ||||||||||
| 10 | # disclaimer. | ||||||||||
| 11 | # 2. Redistributions in binary form must reproduce the above | ||||||||||
| 12 | # copyright notice, this list of conditions and the following | ||||||||||
| 13 | # disclaimer in the documentation and/or other materials provided | ||||||||||
| 14 | # with the distribution. | ||||||||||
| 15 | # 3. The name of the author may not be used to endorse or | ||||||||||
| 16 | # promote products derived from this software without specific | ||||||||||
| 17 | # prior written permission. | ||||||||||
| 18 | |||||||||||
| 19 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | ||||||||||
| 20 | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||||||||||
| 21 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||||||||
| 22 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||||||||||
| 23 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||||||||||
| 24 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||||||||||
| 25 | # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||||||||||
| 26 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||||||||||
| 27 | # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||||||||||
| 28 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||||||||||
| 29 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | # On optimizing: | ||||||||||
| 33 | # I tried encoding the OIDs in BER before comparing them, so as to | ||||||||||
| 34 | # avoid the (?:\.|\z) thingy and the various leading-dot crap, and | ||||||||||
| 35 | # also to look at less bytes. Got it working pretty soon, but the | ||||||||||
| 36 | # time test (matching 500 OIDs against a mapper with 100 similar OIDs | ||||||||||
| 37 | # in it, getting a hit every time) slowed from 35 ms to 47 ms, even | ||||||||||
| 38 | # though the OID lookups were cached. Might be worthwhile if the OIDs | ||||||||||
| 39 | # were already in that format, e.g. for sorting or because they just | ||||||||||
| 40 | # came across the wire (or are about to go back), but I'll cross that | ||||||||||
| 41 | # bridge when I come to it. | ||||||||||
| 42 | |||||||||||
| 43 | 37 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 44 | 37 | my ($class, %args) = @_; | |||||||||
| 45 | 37 | my $self = bless { table => {} }, $class; | |||||||||
| 46 | 37 | ▶ | while (my ($key, $value) = each %args) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 145 | $key =~ s/\A\.//; | |||||||||
| 48 | 145 | $self->{table}{$key} = $value; | |||||||||
| 49 | } | ||||||||||
| 50 | 37 | my $bigre = '\A(' . (join '|', map { "\Q$_\E" } keys %{$self->{table}}) . ')(?:\.|\z)(.*)'; | |||||||||
| 51 | 37 | $self->{bigre} = qr/$bigre/; | |||||||||
| 52 | 37 | return $self; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | # XXX note this interface is really bug-prone in scalar context | ||||||||||
| 56 | |||||||||||
| 57 | 1151 | ▶ | sub map { | ||||||||
|
Subroutine
map: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 58 | 1151 | my ($self, $oid) = @_; | |||||||||
| 59 | 1151 | $oid =~ s/\A\.//; | |||||||||
| 60 | 1151 | ▶ | if ($oid =~ $self->{bigre}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 1144 | my ($key, $instance) = ($1, $2); | |||||||||
| 62 | 1144 | ▶ | return ($self->{table}{$key}, ($instance eq '' ? undef : $instance)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | } | ||||||||||
| 64 | 7 | return (undef, undef); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 3 | 1; | |||||||||