freelancing-god-thinking-sphinx 0.9.8 → 0.9.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/README +20 -1
  2. data/lib/thinking_sphinx.rb +30 -2
  3. data/lib/thinking_sphinx/active_record.rb +25 -11
  4. data/lib/thinking_sphinx/active_record/delta.rb +46 -53
  5. data/lib/thinking_sphinx/active_record/has_many_association.rb +1 -1
  6. data/lib/thinking_sphinx/active_record/search.rb +8 -1
  7. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +27 -0
  8. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +9 -0
  9. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +84 -0
  10. data/lib/thinking_sphinx/association.rb +4 -0
  11. data/lib/thinking_sphinx/attribute.rb +4 -2
  12. data/lib/thinking_sphinx/collection.rb +105 -0
  13. data/lib/thinking_sphinx/configuration.rb +112 -75
  14. data/lib/thinking_sphinx/field.rb +11 -3
  15. data/lib/thinking_sphinx/index.rb +119 -26
  16. data/lib/thinking_sphinx/index/builder.rb +30 -22
  17. data/lib/thinking_sphinx/index/faux_column.rb +13 -0
  18. data/lib/thinking_sphinx/rails_additions.rb +13 -1
  19. data/lib/thinking_sphinx/search.rb +40 -81
  20. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +73 -127
  21. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +2 -2
  22. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +26 -0
  23. data/spec/unit/thinking_sphinx/active_record_spec.rb +94 -22
  24. data/spec/unit/thinking_sphinx/attribute_spec.rb +8 -4
  25. data/spec/unit/thinking_sphinx/collection_spec.rb +71 -0
  26. data/spec/unit/thinking_sphinx/configuration_spec.rb +149 -113
  27. data/spec/unit/thinking_sphinx/field_spec.rb +13 -4
  28. data/spec/unit/thinking_sphinx/index/builder_spec.rb +1 -0
  29. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +27 -0
  30. data/spec/unit/thinking_sphinx/index_spec.rb +79 -29
  31. data/spec/unit/thinking_sphinx/search_spec.rb +114 -74
  32. data/spec/unit/thinking_sphinx_spec.rb +21 -0
  33. data/tasks/thinking_sphinx_tasks.rb +24 -10
  34. metadata +21 -8
  35. data/lib/riddle.rb +0 -26
  36. data/lib/riddle/client.rb +0 -639
  37. data/lib/riddle/client/filter.rb +0 -44
  38. data/lib/riddle/client/message.rb +0 -65
  39. data/lib/riddle/client/response.rb +0 -84
  40. data/lib/test.rb +0 -46
@@ -1,44 +0,0 @@
1
- module Riddle
2
- class Client
3
- # Used for querying Sphinx.
4
- class Filter
5
- attr_accessor :attribute, :values, :exclude
6
-
7
- # Attribute name, values (which can be an array or a range), and whether
8
- # the filter should be exclusive.
9
- def initialize(attribute, values, exclude=false)
10
- @attribute, @values, @exclude = attribute, values, exclude
11
- end
12
-
13
- def exclude?
14
- self.exclude
15
- end
16
-
17
- # Returns the message for this filter to send to the Sphinx service
18
- def query_message
19
- message = Message.new
20
-
21
- message.append_string self.attribute.to_s
22
- case self.values
23
- when Range
24
- if self.values.first.is_a?(Float) && self.values.last.is_a?(Float)
25
- message.append_int FilterTypes[:float_range]
26
- message.append_floats self.values.first, self.values.last
27
- else
28
- message.append_int FilterTypes[:range]
29
- message.append_ints self.values.first, self.values.last
30
- end
31
- when Array
32
- message.append_int FilterTypes[:values]
33
- message.append_int self.values.length
34
- # using to_f is a hack from the php client - to workaround 32bit
35
- # signed ints on x32 platforms
36
- message.append_ints *self.values.collect { |val| val.to_f }
37
- end
38
- message.append_int self.exclude? ? 1 : 0
39
-
40
- message.to_s
41
- end
42
- end
43
- end
44
- end
@@ -1,65 +0,0 @@
1
- module Riddle
2
- class Client
3
- # This class takes care of the translation of ints, strings and arrays to
4
- # the format required by the Sphinx service.
5
- class Message
6
- def initialize
7
- @message = ""
8
- @size_method = @message.respond_to?(:bytesize) ? :bytesize : :length
9
- end
10
-
11
- # Append raw data (only use if you know what you're doing)
12
- def append(*args)
13
- return if args.length == 0
14
-
15
- args.each { |arg| @message << arg }
16
- end
17
-
18
- # Append a string's length, then the string itself
19
- def append_string(str)
20
- @message << [str.send(@size_method)].pack('N') + str
21
- end
22
-
23
- # Append an integer
24
- def append_int(int)
25
- @message << [int].pack('N')
26
- end
27
-
28
- def append_64bit_int(int)
29
- @message << [int >> 32, int & 0xFFFFFFFF].pack('NN')
30
- end
31
-
32
- # Append a float
33
- def append_float(float)
34
- @message << [float].pack('f').unpack('L*').pack("N")
35
- end
36
-
37
- # Append multiple integers
38
- def append_ints(*ints)
39
- ints.each { |int| append_int(int) }
40
- end
41
-
42
- def append_64bit_ints(*ints)
43
- ints.each { |int| append_64bit_int(int) }
44
- end
45
-
46
- # Append multiple floats
47
- def append_floats(*floats)
48
- floats.each { |float| append_float(float) }
49
- end
50
-
51
- # Append an array of strings - first appends the length of the array,
52
- # then each item's length and value.
53
- def append_array(array)
54
- append_int(array.length)
55
-
56
- array.each { |item| append_string(item) }
57
- end
58
-
59
- # Returns the entire message
60
- def to_s
61
- @message
62
- end
63
- end
64
- end
65
- end
@@ -1,84 +0,0 @@
1
- module Riddle
2
- class Client
3
- # Used to interrogate responses from the Sphinx daemon. Keep in mind none
4
- # of the methods here check whether the data they're grabbing are what the
5
- # user expects - it just assumes the user knows what the data stream is
6
- # made up of.
7
- class Response
8
- # Create with the data to interpret
9
- def initialize(str)
10
- @str = str
11
- @marker = 0
12
- end
13
-
14
- # Return the next string value in the stream
15
- def next
16
- len = next_int
17
- result = @str[@marker, len]
18
- @marker += len
19
-
20
- return result
21
- end
22
-
23
- # Return the next integer value from the stream
24
- def next_int
25
- int = @str[@marker, 4].unpack('N*').first
26
- @marker += 4
27
-
28
- return int
29
- end
30
-
31
- def next_64bit_int
32
- high, low = @str[@marker, 8].unpack('N*N*')[0..1]
33
- @marker += 8
34
-
35
- return (high << 32) + low
36
- end
37
-
38
- # Return the next float value from the stream
39
- def next_float
40
- float = @str[@marker, 4].unpack('N*').pack('L').unpack('f*').first
41
- @marker += 4
42
-
43
- return float
44
- end
45
-
46
- # Returns an array of string items
47
- def next_array
48
- count = next_int
49
- items = []
50
- for i in 0...count
51
- items << self.next
52
- end
53
-
54
- return items
55
- end
56
-
57
- # Returns an array of int items
58
- def next_int_array
59
- count = next_int
60
- items = []
61
- for i in 0...count
62
- items << self.next_int
63
- end
64
-
65
- return items
66
- end
67
-
68
- def next_float_array
69
- count = next_int
70
- items = []
71
- for i in 0...count
72
- items << self.next_float
73
- end
74
-
75
- return items
76
- end
77
-
78
- # Returns the length of the streamed data
79
- def length
80
- @str.length
81
- end
82
- end
83
- end
84
- end
data/lib/test.rb DELETED
@@ -1,46 +0,0 @@
1
- require 'thinking_sphinx'
2
-
3
- ActiveRecord::Base.establish_connection(
4
- :adapter => 'mysql',
5
- :database => 'nullus_development',
6
- :username => 'nullus',
7
- :password => 'wossname',
8
- :host => 'localhost'
9
- )
10
- ActiveRecord::Base.logger = Logger.new(STDERR)
11
-
12
- class User < ActiveRecord::Base
13
- has_many :posts, :foreign_key => "created_by"
14
- end
15
-
16
- class Post < ActiveRecord::Base
17
- belongs_to :creator, :foreign_key => "created_by", :class_name => "User"
18
- belongs_to :updater, :foreign_key => "updated_by", :class_name => "User"
19
- belongs_to :topic
20
- end
21
-
22
- class Topic < ActiveRecord::Base
23
- belongs_to :creator, :foreign_key => "created_by", :class_name => "User"
24
- belongs_to :forum
25
- has_many :posts
26
- end
27
-
28
- class Forum < ActiveRecord::Base
29
- belongs_to :creator, :foreign_key => "created_by", :class_name => "User"
30
- has_many :topics
31
- end
32
-
33
- def index
34
- @index ||= ThinkingSphinx::Index.new(Topic) do
35
- indexes posts.content, :as => :posts
36
- indexes posts.creator.login, :as => :authors
37
-
38
- has :created_at
39
- has :id, :as => :topic_id
40
- has :forum_id
41
- has posts(:id), :as => :post_ids
42
- has posts.creator(:id), :as => :user_ids
43
-
44
- where "posts.created_at < NOW()"
45
- end
46
- end