Class: Bitcask::HintFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bitcask/hint_file.rb

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ HintFile

Returns a new instance of HintFile.



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

def initialize(filename)
  @file = File.open(filename)
end

Instance Attribute Details

#data_fileObject

Returns the value of attribute data_file.



11
12
13
# File 'lib/bitcask/hint_file.rb', line 11

def data_file
  @data_file
end

Instance Method Details

#[](offset) ⇒ Object

Reads [key, value] from a particular offset. Also advances the cursor.



18
19
20
21
# File 'lib/bitcask/hint_file.rb', line 18

def [](offset)
  seek offset
  read
end

#closeObject



23
24
25
# File 'lib/bitcask/hint_file.rb', line 23

def close
  @file.close
end

#each(opts = {}) ⇒ Object

Iterates over every entry in this file, yielding an Entry. Options:

:rewind (true) - Rewind the file to the beginning, instead of starting
                 right here.
:raise_checksum (false) - Raise Bitcask::ChecksumError on crc failure, 
                 instead of silently continuing.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bitcask/hint_file.rb', line 33

def each(opts = {})
  options = {
    :rewind => true,
    :raise_checksum => false
  }.merge opts

  rewind if options[:rewind]

  loop do
    o = read
    if o
      yield o
    else
      return self
    end
  end
end

#posObject Also known as: tell



51
52
53
# File 'lib/bitcask/hint_file.rb', line 51

def pos
  @file.pos
end

#readObject

Returns [timestamp, key, value_pos, value_size] read from the current offset, and advances to the next.

Can raise Bitcask::ChecksumError



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bitcask/hint_file.rb', line 60

def read
  # Parse header
  header = @file.read(18) or return
  tstamp, ksz, value_sz, value_pos1, value_pos2 = header.unpack "NnNNN"

  # value_pos is an 8 byte big-endian number...
  # For reference, reverse is [value_pos >> 32, value & 0xFFFFFFFF].pack("NN")
  value_pos = (value_pos1 << 32) | value_pos2

  # Read key
  key = @file.read ksz

  Entry.new tstamp, value_sz, value_pos, key
end

#rewindObject

Rewinds the file.



76
77
78
# File 'lib/bitcask/hint_file.rb', line 76

def rewind
  @file.rewind
end

#seek(offset) ⇒ Object

Seek to a given offset.



81
82
83
# File 'lib/bitcask/hint_file.rb', line 81

def seek(offset)
  @file.seek offset
end