Class: Sphinx::Response

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

Overview

Unpack internal Sphinx representation of ints, floats, strings, and arrays. needed by Sphinx search engine.

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Initialize new request.


8
9
10
11
12
# File 'lib/sphinx/response.rb', line 8

def initialize(response)
  @response = response
  @position = 0
  @size = response.length
end

Instance Method Details

#eof?Boolean

Returns true when response stream is out.

Returns:

  • (Boolean)

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

def eof?
  @position >= @size
end

#get_floatObject

Get float from stream.

Raises:

  • (EOFError)

64
65
66
67
68
69
# File 'lib/sphinx/response.rb', line 64

def get_float
  raise EOFError if @position + 4 > @size
  uval = @response[@position, 4].unpack('N*').first;
  @position += 4
  return ([uval].pack('L')).unpack('f*').first
end

#get_intObject

Get int from stream.

Raises:

  • (EOFError)

30
31
32
33
34
35
# File 'lib/sphinx/response.rb', line 30

def get_int
  raise EOFError if @position + 4 > @size
  value = @response[@position, 4].unpack('N*').first
  @position += 4
  return value
end

#get_int64Object

Get 64-bit int from stream.

Raises:

  • (EOFError)

38
39
40
41
42
43
# File 'lib/sphinx/response.rb', line 38

def get_int64
  raise EOFError if @position + 8 > @size
  hi, lo = @response[@position, 8].unpack('N*N*')
  @position += 8
  return (hi << 32) + lo
end

#get_ints(count) ⇒ Object

Get array of count ints from stream.

Raises:

  • (EOFError)

46
47
48
49
50
51
52
# File 'lib/sphinx/response.rb', line 46

def get_ints(count)
  length = 4 * count
  raise EOFError if @position + length > @size
  values = @response[@position, length].unpack('N*' * count)
  @position += length
  return values
end

#get_stringObject

Get string from stream.

Raises:

  • (EOFError)

55
56
57
58
59
60
61
# File 'lib/sphinx/response.rb', line 55

def get_string
  length = get_int
  raise EOFError if @position + length > @size
  value = length > 0 ? @response[@position, length] : ''
  @position += length
  return value
end

#positionObject

Gets current stream position.


15
16
17
# File 'lib/sphinx/response.rb', line 15

def position
  @position
end

#sizeObject

Gets response size.


20
21
22
# File 'lib/sphinx/response.rb', line 20

def size
  @size
end