Class: OpenASN::BinaryFormat::PackedBase

Inherits:
Object
  • Object
show all
Defined in:
lib/openasn/binary_format.rb

Overview

Packed mode: binary search directly over the artifact bytes. ~6MB resident for all of IPv4, ~19µs/lookup (measured on the format's validation prototype). Key comparisons happen on raw big-endian address bytes: for unsigned BE values, bytewise String comparison IS numeric comparison, which lets IPv4 and IPv6 share one search.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes, family) ⇒ PackedBase

Returns a new instance of PackedBase.



112
113
114
115
116
117
118
# File 'lib/openasn/binary_format.rb', line 112

def initialize(bytes, family)
  @bytes = bytes.freeze
  @family = family
  @asz = BinaryFormat.addr_size(family)
  @rec = BinaryFormat.base_rec_size(family)
  @count = bytes.bytesize / @rec
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



110
111
112
# File 'lib/openasn/binary_format.rb', line 110

def count
  @count
end

Instance Method Details

#find(ip_int) ⇒ Object

-> [asn, flags] | nil



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/openasn/binary_format.rb', line 121

def find(ip_int)
  key = BinaryFormat.pack_addr(ip_int, @family)
  lo = 0
  hi = @count - 1
  while lo <= hi
    mid = (lo + hi) / 2
    off = mid * @rec
    if key < @bytes[off, @asz]
      hi = mid - 1
    elsif key > @bytes[off + @asz, @asz]
      lo = mid + 1
    else
      return @bytes[off + 2 * @asz, 6].unpack("Nn")
    end
  end
  nil
end