Class: RDB::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/rdb/reader.rb

Class Method Summary collapse

Class Method Details

.read(rdb, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rdb/reader.rb', line 10

def read(rdb, options = {})
  rdb_version = read_rdb_version(rdb)

  state = ReaderState.new(options[:callbacks])
  callbacks = state.callbacks

  callbacks.start_rdb(rdb_version)

  loop do
    state.key_type_id = rdb.readbyte
    state.info = {}

    case state.key_type_id
    when Opcode::EXPIRETIME_MS
      state.key_expiration = rdb.read(8).unpack('Q').first * 1000
      state.info[:precision] = :millisecond
      state.key_type_id = rdb.readbyte

    when Opcode::EXPIRETIME
      state.key_expiration = rdb.read(4).unpack('L').first * 1000000
      state.info[:precision] = :second
      state.key_type_id = rdb.readbyte

    when Opcode::SELECTDB
      callbacks.end_database(state.database) unless state.database.nil?
      state.database = read_length(rdb).first
      callbacks.start_database(state.database)
      next

    when Opcode::EOF
      callbacks.end_database(state.database) unless state.database.nil?
      callbacks.end_rdb()
      break
    end

    state.key = read_string(rdb)

    if callbacks.accept_key?(state)
      read_object(rdb, state)
      notify_expiration(state) if state.key_expires?
    else
      skip_object(rdb, state)
    end
  end
end

.read_file(rdb_file, options = {}) ⇒ Object



4
5
6
7
8
# File 'lib/rdb/reader.rb', line 4

def read_file(rdb_file, options = {})
  File.open(rdb_file, 'rb') do |rdb|
    read(rdb, options)
  end
end