Class: Bitcask::HintFile
- Inherits:
-
Object
- Object
- Bitcask::HintFile
- Includes:
- Enumerable
- Defined in:
- lib/bitcask/hint_file.rb
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#data_file ⇒ Object
Returns the value of attribute data_file.
Instance Method Summary collapse
-
#[](offset) ⇒ Object
Reads [key, value] from a particular offset.
- #close ⇒ Object
-
#each(opts = {}) ⇒ Object
Iterates over every entry in this file, yielding an Entry.
-
#initialize(filename) ⇒ HintFile
constructor
A new instance of HintFile.
- #pos ⇒ Object (also: #tell)
-
#read ⇒ Object
Returns [timestamp, key, value_pos, value_size] read from the current offset, and advances to the next.
-
#rewind ⇒ Object
Rewinds the file.
-
#seek(offset) ⇒ Object
Seek to a given offset.
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_file ⇒ Object
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 |
#close ⇒ Object
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 = {}) = { :rewind => true, :raise_checksum => false }.merge opts rewind if [:rewind] loop do o = read if o yield o else return self end end end |
#pos ⇒ Object Also known as: tell
51 52 53 |
# File 'lib/bitcask/hint_file.rb', line 51 def pos @file.pos end |
#read ⇒ Object
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 |
#rewind ⇒ Object
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 |