Class: GoldMine::IndexReader
- Inherits:
-
Object
- Object
- GoldMine::IndexReader
- Defined in:
- lib/gold_mine/index_reader.rb
Overview
Reads an index for fortunes database. The index is a binary file which contains a header and pointers.
The header stores statistical information and instruction specifying how to read the file. Pointer indicates an initial position for the related fortune.
Constant Summary collapse
- HEADER_SIZE =
24
- POINTER_SIZE =
4
Instance Attribute Summary collapse
-
#delim ⇒ Object
readonly
Returns the value of attribute delim.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#longlen ⇒ Object
readonly
Returns the value of attribute longlen.
-
#numstr ⇒ Object
readonly
Returns the value of attribute numstr.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#shortlen ⇒ Object
readonly
Returns the value of attribute shortlen.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#get_pointer_at(index) ⇒ Object
Returns a pointer from a certain position.
-
#get_pointers ⇒ Object
Returns all pointers.
-
#header_fields ⇒ Object
Returns a header.
-
#initialize(path) ⇒ IndexReader
constructor
A new instance of IndexReader.
-
#options ⇒ Object
Returns a hash with selected header fields.
- #random_pointer ⇒ Object
Constructor Details
#initialize(path) ⇒ IndexReader
Returns a new instance of IndexReader.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gold_mine/index_reader.rb', line 14 def initialize(path) @path = path header = header_fields @version = header[0] @numstr = header[1] @longlen = header[2] @shortlen = header[3] @flags = header[4].to_switch @delim = header[5].chr end |
Instance Attribute Details
#delim ⇒ Object (readonly)
Returns the value of attribute delim.
26 27 28 |
# File 'lib/gold_mine/index_reader.rb', line 26 def delim @delim end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
26 27 28 |
# File 'lib/gold_mine/index_reader.rb', line 26 def flags @flags end |
#longlen ⇒ Object (readonly)
Returns the value of attribute longlen.
26 27 28 |
# File 'lib/gold_mine/index_reader.rb', line 26 def longlen @longlen end |
#numstr ⇒ Object (readonly)
Returns the value of attribute numstr.
26 27 28 |
# File 'lib/gold_mine/index_reader.rb', line 26 def numstr @numstr end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
26 27 28 |
# File 'lib/gold_mine/index_reader.rb', line 26 def path @path end |
#shortlen ⇒ Object (readonly)
Returns the value of attribute shortlen.
26 27 28 |
# File 'lib/gold_mine/index_reader.rb', line 26 def shortlen @shortlen end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
26 27 28 |
# File 'lib/gold_mine/index_reader.rb', line 26 def version @version end |
Instance Method Details
#get_pointer_at(index) ⇒ Object
Returns a pointer from a certain position.
72 73 74 |
# File 'lib/gold_mine/index_reader.rb', line 72 def get_pointer_at(index) IO.binread(@path, POINTER_SIZE, HEADER_SIZE + POINTER_SIZE * index).unpack("N").first end |
#get_pointers ⇒ Object
Returns all pointers.
66 67 68 |
# File 'lib/gold_mine/index_reader.rb', line 66 def get_pointers IO.binread(@path, @numstr * POINTER_SIZE, HEADER_SIZE).unpack("N*") end |
#header_fields ⇒ Object
Returns a header.
The header consists of six 32-bit unsigned integers. Integers are stored in big-endian byte order.
The order and meaning of the fields are as follows:
version
-
version number
numstr
-
number of pointers
longlen
-
size of longest fortune
shortlen
-
size of shortest fortune
flags
-
stores multiple booleans (bit-field)
- 1
-
randomize order
- 2
-
sorting in alphabetical order
- 4
-
Caesar encryption
- 8
-
allow comments
delim
-
8-bit unsigned integer packed to 32-bit
which represents a delimeter character
60 61 62 |
# File 'lib/gold_mine/index_reader.rb', line 60 def header_fields IO.binread(@path, HEADER_SIZE, 0).unpack("N5C1") end |
#options ⇒ Object
Returns a hash with selected header fields.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gold_mine/index_reader.rb', line 30 def { version: @version, delim: @delim, randomized: @flags[0], ordered: @flags[1], rotated: @flags[2], comments: @flags[3] } end |
#random_pointer ⇒ Object
76 77 78 |
# File 'lib/gold_mine/index_reader.rb', line 76 def random_pointer get_pointer_at rand(@numstr) end |