Class: Ignite::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ignite/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
# File 'lib/ignite/response.rb', line 5

def initialize(client)
  @client = client

  # use buffer so errors don't leave unread data on socket
  len = client.read(4).unpack1("i!<")
  @buffer = StringIO.new(client.read(len))
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/ignite/response.rb', line 3

def client
  @client
end

Instance Method Details

#read(len) ⇒ Object



13
14
15
# File 'lib/ignite/response.rb', line 13

def read(len)
  @buffer.read(len)
end

#read_boolObject



45
46
47
# File 'lib/ignite/response.rb', line 45

def read_bool
  read_byte != 0
end

#read_byteObject



17
18
19
# File 'lib/ignite/response.rb', line 17

def read_byte
  read(1).unpack1("C")
end

#read_charObject



41
42
43
# File 'lib/ignite/response.rb', line 41

def read_char
  read(1).unpack1("c")
end

#read_data_objectObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ignite/response.rb', line 93

def read_data_object
  type_code = read_byte
  case type_code
  when 1
    read_byte
  when 2
    read_short
  when 3
    read_int
  when 4
    read_long
  when 5
    read_float
  when 6
    read_double
  when 7
    read_char
  when 8
    read_bool
  when 9
    read_string
  when 11
    read_date
  when 30
    read_decimal
  when 33
    read_timestamp
  when 101
    nil
  else
    raise Error, "Type not supported yet: #{type_code}. Please create an issue."
  end
end

#read_dateObject



60
61
62
63
64
# File 'lib/ignite/response.rb', line 60

def read_date
  msecs_since_epoch = read_long
  sec = msecs_since_epoch / 1000
  Time.at(sec).to_date
end

#read_decimalObject

same as Python



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ignite/response.rb', line 67

def read_decimal
  scale = read_int
  length = read_int
  data = read(length).unpack("C*")

  sign = (data[0] & 0x80) != 0
  data[0] = data[0] & 0x7f

  result = 0
  data.reverse.each_with_index do |v, i|
    result += v * 0x100 ** i
  end

  result = result / BigDecimal("10") ** BigDecimal(scale)
  result = -result if sign
  result
end

#read_doubleObject



37
38
39
# File 'lib/ignite/response.rb', line 37

def read_double
  read(8).unpack1("E")
end

#read_floatObject



33
34
35
# File 'lib/ignite/response.rb', line 33

def read_float
  read(4).unpack1("e")
end

#read_intObject



25
26
27
# File 'lib/ignite/response.rb', line 25

def read_int
  read(4).unpack1("i!<")
end

#read_longObject



29
30
31
# File 'lib/ignite/response.rb', line 29

def read_long
  read(8).unpack1("l!<")
end

#read_shortObject



21
22
23
# File 'lib/ignite/response.rb', line 21

def read_short
  read(2).unpack1("s!<")
end

#read_stringObject



49
50
51
52
# File 'lib/ignite/response.rb', line 49

def read_string
  len = read_int
  read(len)
end

#read_string_objectObject



54
55
56
57
58
# File 'lib/ignite/response.rb', line 54

def read_string_object
  type = read_byte
  raise "Expected string, not type #{type}" unless type == 9
  read_string
end

#read_timestampObject



85
86
87
88
89
90
91
# File 'lib/ignite/response.rb', line 85

def read_timestamp
  msecs_since_epoch = read_long
  msec_fraction_in_nsecs = read_int
  sec = msecs_since_epoch / 1000
  nsec = (msecs_since_epoch % 1000) * 1000000 + msec_fraction_in_nsecs
  Time.at(sec, nsec, :nanosecond)
end