request.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. module Sphinx
  2. # Pack ints, floats, strings, and arrays to internal representation
  3. # needed by Sphinx search engine.
  4. class Request
  5. # Initialize new request.
  6. def initialize
  7. @request = ''
  8. end
  9. # Put int(s) to request.
  10. def put_int(*ints)
  11. ints.each { |i| @request << [i].pack('N') }
  12. end
  13. # Put 64-bit int(s) to request.
  14. def put_int64(*ints)
  15. ints.each { |i| @request << [i].pack('q').reverse }#[i >> 32, i & ((1 << 32) - 1)].pack('NN') }
  16. end
  17. # Put string(s) to request (first length, then the string itself).
  18. def put_string(*strings)
  19. strings.each { |s| @request << [s.length].pack('N') + s }
  20. end
  21. # Put float(s) to request.
  22. def put_float(*floats)
  23. floats.each do |f|
  24. t1 = [f].pack('f') # machine order
  25. t2 = t1.unpack('L*').first # int in machine order
  26. @request << [t2].pack('N')
  27. end
  28. end
  29. # Put array of ints to request (first length, then the array itself)
  30. def put_int_array(arr)
  31. put_int arr.length, *arr
  32. end
  33. # Put array of 64-bit ints to request (first length, then the array itself)
  34. def put_int64_array(arr)
  35. put_int arr.length
  36. put_int64(*arr)
  37. end
  38. # Returns the entire message
  39. def to_s
  40. @request
  41. end
  42. end
  43. end