Class: Sphinx::Request

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

Overview

Pack ints, floats, strings, and arrays to internal representation needed by Sphinx search engine.

Instance Method Summary collapse

Constructor Details

#initializeRequest

Initialize new empty request.


8
9
10
# File 'lib/sphinx/request.rb', line 8

def initialize
  @request = ''
end

Instance Method Details

#crc32Integer

Returns CRC32 of the serialized request.

Returns:

  • (Integer)

    CRC32 of the serialized request.


117
118
119
# File 'lib/sphinx/request.rb', line 117

def crc32
  Zlib.crc32(@request)
end

#put_float(*floats) ⇒ Request

Put float(s) to request.

Examples:

request.put_float 10
request.put_float 10, 20, 30
request.put_float *[10, 20, 30]

Parameters:

  • floats (Integer, Float)

    one or more floats to put to the request.

Returns:


67
68
69
70
71
72
73
74
# File 'lib/sphinx/request.rb', line 67

def put_float(*floats)
  floats.each do |f|
    t1 = [f.to_f].pack('f') # machine order
    t2 = t1.unpack('L*').first # int in machine order
    @request << [t2].pack('N')
  end
  self
end

#put_int(*ints) ⇒ Request

Put int(s) to request.

Examples:

request.put_int 10
request.put_int 10, 20, 30
request.put_int *[10, 20, 30]

Parameters:

  • ints (Integer)

    one or more integers to put to the request.

Returns:


22
23
24
25
# File 'lib/sphinx/request.rb', line 22

def put_int(*ints)
  ints.each { |i| @request << [i].pack('N') }
  self
end

#put_int64(*ints) ⇒ Request

Put 64-bit int(s) to request.

Examples:

request.put_int64 10
request.put_int64 10, 20, 30
request.put_int64 *[10, 20, 30]

Parameters:

  • ints (Integer)

    one or more 64-bit integers to put to the request.

Returns:


37
38
39
40
# File 'lib/sphinx/request.rb', line 37

def put_int64(*ints)
  ints.each { |i| @request << [i].pack('q').reverse }#[i >> 32, i & ((1 << 32) - 1)].pack('NN') }
  self
end

#put_int64_array(arr) ⇒ Request

Put array of 64-bit ints to request.

Examples:

request.put_int64_array [10]
request.put_int64_array [10, 20, 30]

Parameters:

  • arr (Array<Integer>)

    an array of integers to put to the request.

Returns:


99
100
101
102
103
# File 'lib/sphinx/request.rb', line 99

def put_int64_array(arr)
  put_int(arr.length)
  put_int64(*arr)
  self
end

#put_int_array(arr) ⇒ Request

Put array of ints to request.

Examples:

request.put_int_array [10]
request.put_int_array [10, 20, 30]

Parameters:

  • arr (Array<Integer>)

    an array of integers to put to the request.

Returns:


85
86
87
88
# File 'lib/sphinx/request.rb', line 85

def put_int_array(arr)
  put_int arr.length, *arr
  self
end

#put_string(*strings) ⇒ Request

Put strings to request.

Examples:

request.put_string 'str1'
request.put_string 'str1', 'str2', 'str3'
request.put_string *['str1', 'str2', 'str3']

Parameters:

  • strings (String)

    one or more strings to put to the request.

Returns:


52
53
54
55
# File 'lib/sphinx/request.rb', line 52

def put_string(*strings)
  strings.each { |s| @request << [s.length].pack('N') + s }
  self
end

#to_sString

Returns the serialized request.

Returns:

  • (String)

    serialized request.


109
110
111
# File 'lib/sphinx/request.rb', line 109

def to_s
  @request
end