Class: MiniMime::Db::RandomAccessDb
- Inherits:
-
Object
- Object
- MiniMime::Db::RandomAccessDb
- Defined in:
- lib/mini_mime.rb
Constant Summary collapse
- MAX_CACHED =
100
Instance Method Summary collapse
-
#initialize(path, sort_order) ⇒ RandomAccessDb
constructor
A new instance of RandomAccessDb.
- #lookup(val) ⇒ Object
-
#lookup_uncached(val) ⇒ Object
lifted from marcandre/backports.
- #resolve(row) ⇒ Object
Constructor Details
#initialize(path, sort_order) ⇒ RandomAccessDb
Returns a new instance of RandomAccessDb.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mini_mime.rb', line 117 def initialize(path, sort_order) @path = path @file = PReadFile.new(@path) @row_length = @file.readline("\n").length @file_length = File.size(@path) @rows = @file_length / @row_length @hit_cache = Cache.new(MAX_CACHED) @miss_cache = Cache.new(MAX_CACHED) @sort_order = sort_order end |
Instance Method Details
#lookup(val) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mini_mime.rb', line 131 def lookup(val) @hit_cache.fetch(val) do @miss_cache.fetch(val) do data = lookup_uncached(val) if data @hit_cache[val] = data else @miss_cache[val] = nil end data end end end |
#lookup_uncached(val) ⇒ Object
lifted from marcandre/backports
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/mini_mime.rb', line 147 def lookup_uncached(val) from = 0 to = @rows - 1 result = nil while from <= to do midpoint = from + (to - from).div(2) current = resolve(midpoint) data = current[@sort_order] if data > val to = midpoint - 1 elsif data < val from = midpoint + 1 else result = current break end end result end |