Class: Rubcask::HintFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rubcask/hint_file.rb

Overview

HintFile stores only keys, and information on where the value of the key is located

Constant Summary collapse

HEADER_FORMAT =
"Q>nNQ>"

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ HintFile

Returns a new instance of HintFile.

Parameters:

  • file (File)

    An already opened file



12
13
14
# File 'lib/rubcask/hint_file.rb', line 12

def initialize(file)
  @file = file
end

Instance Method Details

#append(entry) ⇒ Integer

Appends an entry to the file

Parameters:

Returns:

  • (Integer)

    Number of bytes written



49
50
51
52
53
54
# File 'lib/rubcask/hint_file.rb', line 49

def append(entry)
  @file.write(
    [entry.expire_timestamp, entry.key.bytesize, entry.value_size, entry.value_pos].pack(HEADER_FORMAT),
    entry.key
  )
end

#each {|hint_entry| ... } ⇒ Enumerator

Yields each hint entry from the file

Yield Parameters:

Returns:

  • (Enumerator)

    if no block given



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubcask/hint_file.rb', line 19

def each
  return to_enum(__method__) unless block_given?

  seek(0)

  loop do
    val = read
    break unless val
    yield val
  end
end

#readHintEntry?

Reads hint entry at the current offset

Returns:

  • (HintEntry)
  • (nil)

    If at the end of file

Raises:

  • LoadError if unable to read from the file



35
36
37
38
39
40
41
42
43
44
# File 'lib/rubcask/hint_file.rb', line 35

def read
  header = @file.read(22)

  return nil unless header

  expire_timestamp, key_size, value_size, value_pos = header.unpack(HEADER_FORMAT)
  key = @file.read(key_size)

  HintEntry.new(expire_timestamp, key, value_pos, value_size)
end