Class: GoldMine::IndexReader

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#delimObject (readonly)

Returns the value of attribute delim.



26
27
28
# File 'lib/gold_mine/index_reader.rb', line 26

def delim
  @delim
end

#flagsObject (readonly)

Returns the value of attribute flags.



26
27
28
# File 'lib/gold_mine/index_reader.rb', line 26

def flags
  @flags
end

#longlenObject (readonly)

Returns the value of attribute longlen.



26
27
28
# File 'lib/gold_mine/index_reader.rb', line 26

def longlen
  @longlen
end

#numstrObject (readonly)

Returns the value of attribute numstr.



26
27
28
# File 'lib/gold_mine/index_reader.rb', line 26

def numstr
  @numstr
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/gold_mine/index_reader.rb', line 26

def path
  @path
end

#shortlenObject (readonly)

Returns the value of attribute shortlen.



26
27
28
# File 'lib/gold_mine/index_reader.rb', line 26

def shortlen
  @shortlen
end

#versionObject (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_pointersObject

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_fieldsObject

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

#optionsObject

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 options
  {
    version: @version,
    delim: @delim,
    randomized: @flags[0],
    ordered: @flags[1],
    rotated: @flags[2],
    comments: @flags[3]
  }
end

#random_pointerObject



76
77
78
# File 'lib/gold_mine/index_reader.rb', line 76

def random_pointer
  get_pointer_at rand(@numstr)
end