model_set 0.10.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +39 -0
  3. data/VERSION.yml +5 -0
  4. data/lib/model_set/conditioned.rb +33 -0
  5. data/lib/model_set/conditions.rb +103 -0
  6. data/lib/model_set/query.rb +132 -0
  7. data/lib/model_set/raw_query.rb +41 -0
  8. data/lib/model_set/raw_sql_query.rb +19 -0
  9. data/lib/model_set/set_query.rb +34 -0
  10. data/lib/model_set/solr_query.rb +70 -0
  11. data/lib/model_set/sphinx_query.rb +206 -0
  12. data/lib/model_set/sql_base_query.rb +52 -0
  13. data/lib/model_set/sql_query.rb +109 -0
  14. data/lib/model_set.rb +743 -0
  15. data/lib/multi_set.rb +67 -0
  16. data/test/model_set_test.rb +329 -0
  17. data/test/multi_set_test.rb +65 -0
  18. data/test/test_helper.rb +23 -0
  19. data/vendor/sphinx_client/README.rdoc +41 -0
  20. data/vendor/sphinx_client/Rakefile +21 -0
  21. data/vendor/sphinx_client/init.rb +1 -0
  22. data/vendor/sphinx_client/install.rb +5 -0
  23. data/vendor/sphinx_client/lib/sphinx/client.rb +1093 -0
  24. data/vendor/sphinx_client/lib/sphinx/request.rb +50 -0
  25. data/vendor/sphinx_client/lib/sphinx/response.rb +69 -0
  26. data/vendor/sphinx_client/lib/sphinx.rb +6 -0
  27. data/vendor/sphinx_client/spec/client_response_spec.rb +112 -0
  28. data/vendor/sphinx_client/spec/client_spec.rb +469 -0
  29. data/vendor/sphinx_client/spec/fixtures/default_search.php +8 -0
  30. data/vendor/sphinx_client/spec/fixtures/default_search_index.php +8 -0
  31. data/vendor/sphinx_client/spec/fixtures/excerpt_custom.php +11 -0
  32. data/vendor/sphinx_client/spec/fixtures/excerpt_default.php +8 -0
  33. data/vendor/sphinx_client/spec/fixtures/excerpt_flags.php +11 -0
  34. data/vendor/sphinx_client/spec/fixtures/field_weights.php +9 -0
  35. data/vendor/sphinx_client/spec/fixtures/filter.php +9 -0
  36. data/vendor/sphinx_client/spec/fixtures/filter_exclude.php +9 -0
  37. data/vendor/sphinx_client/spec/fixtures/filter_float_range.php +9 -0
  38. data/vendor/sphinx_client/spec/fixtures/filter_float_range_exclude.php +9 -0
  39. data/vendor/sphinx_client/spec/fixtures/filter_range.php +9 -0
  40. data/vendor/sphinx_client/spec/fixtures/filter_range_exclude.php +9 -0
  41. data/vendor/sphinx_client/spec/fixtures/filter_range_int64.php +10 -0
  42. data/vendor/sphinx_client/spec/fixtures/filter_ranges.php +10 -0
  43. data/vendor/sphinx_client/spec/fixtures/filters.php +10 -0
  44. data/vendor/sphinx_client/spec/fixtures/filters_different.php +13 -0
  45. data/vendor/sphinx_client/spec/fixtures/geo_anchor.php +9 -0
  46. data/vendor/sphinx_client/spec/fixtures/group_by_attr.php +9 -0
  47. data/vendor/sphinx_client/spec/fixtures/group_by_attrpair.php +9 -0
  48. data/vendor/sphinx_client/spec/fixtures/group_by_day.php +9 -0
  49. data/vendor/sphinx_client/spec/fixtures/group_by_day_sort.php +9 -0
  50. data/vendor/sphinx_client/spec/fixtures/group_by_month.php +9 -0
  51. data/vendor/sphinx_client/spec/fixtures/group_by_week.php +9 -0
  52. data/vendor/sphinx_client/spec/fixtures/group_by_year.php +9 -0
  53. data/vendor/sphinx_client/spec/fixtures/group_distinct.php +10 -0
  54. data/vendor/sphinx_client/spec/fixtures/id_range.php +9 -0
  55. data/vendor/sphinx_client/spec/fixtures/id_range64.php +9 -0
  56. data/vendor/sphinx_client/spec/fixtures/index_weights.php +9 -0
  57. data/vendor/sphinx_client/spec/fixtures/keywords.php +8 -0
  58. data/vendor/sphinx_client/spec/fixtures/limits.php +9 -0
  59. data/vendor/sphinx_client/spec/fixtures/limits_cutoff.php +9 -0
  60. data/vendor/sphinx_client/spec/fixtures/limits_max.php +9 -0
  61. data/vendor/sphinx_client/spec/fixtures/limits_max_cutoff.php +9 -0
  62. data/vendor/sphinx_client/spec/fixtures/match_all.php +9 -0
  63. data/vendor/sphinx_client/spec/fixtures/match_any.php +9 -0
  64. data/vendor/sphinx_client/spec/fixtures/match_boolean.php +9 -0
  65. data/vendor/sphinx_client/spec/fixtures/match_extended.php +9 -0
  66. data/vendor/sphinx_client/spec/fixtures/match_extended2.php +9 -0
  67. data/vendor/sphinx_client/spec/fixtures/match_fullscan.php +9 -0
  68. data/vendor/sphinx_client/spec/fixtures/match_phrase.php +9 -0
  69. data/vendor/sphinx_client/spec/fixtures/max_query_time.php +9 -0
  70. data/vendor/sphinx_client/spec/fixtures/miltiple_queries.php +12 -0
  71. data/vendor/sphinx_client/spec/fixtures/ranking_bm25.php +9 -0
  72. data/vendor/sphinx_client/spec/fixtures/ranking_none.php +9 -0
  73. data/vendor/sphinx_client/spec/fixtures/ranking_proximity.php +9 -0
  74. data/vendor/sphinx_client/spec/fixtures/ranking_proximity_bm25.php +9 -0
  75. data/vendor/sphinx_client/spec/fixtures/ranking_wordcount.php +9 -0
  76. data/vendor/sphinx_client/spec/fixtures/retries.php +9 -0
  77. data/vendor/sphinx_client/spec/fixtures/retries_delay.php +9 -0
  78. data/vendor/sphinx_client/spec/fixtures/select.php +9 -0
  79. data/vendor/sphinx_client/spec/fixtures/set_override.php +11 -0
  80. data/vendor/sphinx_client/spec/fixtures/sort_attr_asc.php +9 -0
  81. data/vendor/sphinx_client/spec/fixtures/sort_attr_desc.php +9 -0
  82. data/vendor/sphinx_client/spec/fixtures/sort_expr.php +9 -0
  83. data/vendor/sphinx_client/spec/fixtures/sort_extended.php +9 -0
  84. data/vendor/sphinx_client/spec/fixtures/sort_relevance.php +9 -0
  85. data/vendor/sphinx_client/spec/fixtures/sort_time_segments.php +9 -0
  86. data/vendor/sphinx_client/spec/fixtures/sphinxapi.php +1269 -0
  87. data/vendor/sphinx_client/spec/fixtures/update_attributes.php +8 -0
  88. data/vendor/sphinx_client/spec/fixtures/update_attributes_mva.php +8 -0
  89. data/vendor/sphinx_client/spec/fixtures/weights.php +9 -0
  90. data/vendor/sphinx_client/spec/sphinx/sphinx-id64.conf +67 -0
  91. data/vendor/sphinx_client/spec/sphinx/sphinx.conf +67 -0
  92. data/vendor/sphinx_client/spec/sphinx/sphinx_test.sql +86 -0
  93. data/vendor/sphinx_client/sphinx.yml.tpl +3 -0
  94. data/vendor/sphinx_client/tasks/sphinx.rake +75 -0
  95. metadata +151 -0
@@ -0,0 +1,50 @@
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
+
10
+ # Put int(s) to request.
11
+ def put_int(*ints)
12
+ ints.each { |i| @request << [i].pack('N') }
13
+ end
14
+
15
+ # Put 64-bit int(s) to request.
16
+ def put_int64(*ints)
17
+ ints.each { |i| @request << [i].pack('q').reverse }#[i >> 32, i & ((1 << 32) - 1)].pack('NN') }
18
+ end
19
+
20
+ # Put string(s) to request (first length, then the string itself).
21
+ def put_string(*strings)
22
+ strings.each { |s| @request << [s.length].pack('N') + s }
23
+ end
24
+
25
+ # Put float(s) to request.
26
+ def put_float(*floats)
27
+ floats.each do |f|
28
+ t1 = [f].pack('f') # machine order
29
+ t2 = t1.unpack('L*').first # int in machine order
30
+ @request << [t2].pack('N')
31
+ end
32
+ end
33
+
34
+ # Put array of ints to request (first length, then the array itself)
35
+ def put_int_array(arr)
36
+ put_int arr.length, *arr
37
+ end
38
+
39
+ # Put array of 64-bit ints to request (first length, then the array itself)
40
+ def put_int64_array(arr)
41
+ put_int arr.length
42
+ put_int64(*arr)
43
+ end
44
+
45
+ # Returns the entire message
46
+ def to_s
47
+ @request
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,69 @@
1
+ module Sphinx
2
+ # Unpack internal Sphinx representation of ints, floats, strings, and arrays.
3
+ # needed by Sphinx search engine.
4
+ class Response
5
+ # Initialize new request.
6
+ def initialize(response)
7
+ @response = response
8
+ @position = 0
9
+ @size = response.length
10
+ end
11
+
12
+ # Gets current stream position.
13
+ def position
14
+ @position
15
+ end
16
+
17
+ # Gets response size.
18
+ def size
19
+ @size
20
+ end
21
+
22
+ # Returns <tt>true</tt> when response stream is out.
23
+ def eof?
24
+ @position >= @size
25
+ end
26
+
27
+ # Get int from stream.
28
+ def get_int
29
+ raise EOFError if @position + 4 > @size
30
+ value = @response[@position, 4].unpack('N*').first
31
+ @position += 4
32
+ return value
33
+ end
34
+
35
+ # Get 64-bit int from stream.
36
+ def get_int64
37
+ raise EOFError if @position + 8 > @size
38
+ hi, lo = @response[@position, 8].unpack('N*N*')
39
+ @position += 8
40
+ return (hi << 32) + lo
41
+ end
42
+
43
+ # Get array of <tt>count</tt> ints from stream.
44
+ def get_ints(count)
45
+ length = 4 * count
46
+ raise EOFError if @position + length > @size
47
+ values = @response[@position, length].unpack('N*' * count)
48
+ @position += length
49
+ return values
50
+ end
51
+
52
+ # Get string from stream.
53
+ def get_string
54
+ length = get_int
55
+ raise EOFError if @position + length > @size
56
+ value = length > 0 ? @response[@position, length] : ''
57
+ @position += length
58
+ return value
59
+ end
60
+
61
+ # Get float from stream.
62
+ def get_float
63
+ raise EOFError if @position + 4 > @size
64
+ uval = @response[@position, 4].unpack('N*').first;
65
+ @position += 4
66
+ return ([uval].pack('L')).unpack('f*').first
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,6 @@
1
+ require File.dirname(__FILE__) + '/sphinx/request'
2
+ require File.dirname(__FILE__) + '/sphinx/response'
3
+ require File.dirname(__FILE__) + '/sphinx/client'
4
+
5
+ module Sphinx
6
+ end
@@ -0,0 +1,112 @@
1
+ require File.dirname(__FILE__) + '/../init'
2
+
3
+ # To execute these tests you need to execute sphinx_test.sql and configure sphinx using sphinx.conf
4
+ # (both files are placed under sphinx directory)
5
+ context 'The SphinxApi connected to Sphinx' do
6
+ setup do
7
+ @sphinx = Sphinx::Client.new
8
+ end
9
+
10
+ specify 'should parse response in Query method' do
11
+ result = @sphinx.Query('wifi', 'test1')
12
+ validate_results_wifi(result)
13
+ end
14
+
15
+ specify 'should process 64-bit keys in Query method' do
16
+ result = @sphinx.Query('wifi', 'test2')
17
+ result['total_found'].should == 3
18
+ result['matches'].length.should == 3
19
+ result['matches'][0]['id'].should == 4294967298
20
+ result['matches'][1]['id'].should == 4294967299
21
+ result['matches'][2]['id'].should == 4294967297
22
+ end
23
+
24
+ specify 'should parse batch-query responce in RunQueries method' do
25
+ @sphinx.AddQuery('wifi', 'test1')
26
+ @sphinx.AddQuery('gprs', 'test1')
27
+ results = @sphinx.RunQueries
28
+ results.should be_an_instance_of(Array)
29
+ results.length.should == 2
30
+ validate_results_wifi(results[0])
31
+ end
32
+
33
+ specify 'should parse response in BuildExcerpts method' do
34
+ result = @sphinx.BuildExcerpts(['what the world', 'London is the capital of Great Britain'], 'test1', 'the')
35
+ result.should == ['what <b>the</b> world', 'London is <b>the</b> capital of Great Britain']
36
+ end
37
+
38
+ specify 'should parse response in BuildKeywords method' do
39
+ result = @sphinx.BuildKeywords('wifi gprs', 'test1', true)
40
+ result.should == [
41
+ { 'normalized' => 'wifi', 'tokenized' => 'wifi', 'hits' => 6, 'docs' => 3 },
42
+ { 'normalized' => 'gprs', 'tokenized' => 'gprs', 'hits' => 1, 'docs' => 1 }
43
+ ]
44
+ end
45
+
46
+ specify 'should parse response in UpdateAttributes method' do
47
+ @sphinx.UpdateAttributes('test1', ['group_id'], { 2 => [1] }).should == 1
48
+ result = @sphinx.Query('wifi', 'test1')
49
+ result['matches'][0]['attrs']['group_id'].should == 1
50
+ @sphinx.UpdateAttributes('test1', ['group_id'], { 2 => [2] }).should == 1
51
+ result = @sphinx.Query('wifi', 'test1')
52
+ result['matches'][0]['attrs']['group_id'].should == 2
53
+ end
54
+
55
+ specify 'should parse response in UpdateAttributes method with MVA' do
56
+ @sphinx.UpdateAttributes('test1', ['tags'], { 2 => [[1, 2, 3, 4, 5, 6, 7, 8, 9]] }, true).should == 1
57
+ result = @sphinx.Query('wifi', 'test1')
58
+ result['matches'][0]['attrs']['tags'].should == [1, 2, 3, 4, 5, 6, 7, 8, 9]
59
+ @sphinx.UpdateAttributes('test1', ['tags'], { 2 => [[5, 6, 7, 8]] }, true).should == 1
60
+ result = @sphinx.Query('wifi', 'test1')
61
+ result['matches'][0]['attrs']['tags'].should == [5, 6, 7, 8]
62
+ end
63
+
64
+ specify 'should process errors in Query method' do
65
+ @sphinx.Query('wifi', 'fakeindex').should be_false
66
+ @sphinx.GetLastError.length.should_not == 0
67
+ end
68
+
69
+ specify 'should process errors in RunQueries method' do
70
+ @sphinx.AddQuery('wifi', 'fakeindex')
71
+ r = @sphinx.RunQueries
72
+ r[0]['error'].length.should_not == 0
73
+ end
74
+
75
+ def validate_results_wifi(result)
76
+ result['total_found'].should == 3
77
+ result['matches'].length.should == 3
78
+ result['time'].should_not be_nil
79
+ result['attrs'].should == {
80
+ 'group_id' => Sphinx::Client::SPH_ATTR_INTEGER,
81
+ 'created_at' => Sphinx::Client::SPH_ATTR_TIMESTAMP,
82
+ 'rating' => Sphinx::Client::SPH_ATTR_FLOAT,
83
+ 'tags' => Sphinx::Client::SPH_ATTR_MULTI | Sphinx::Client::SPH_ATTR_INTEGER
84
+ }
85
+ result['fields'].should == [ 'name', 'description' ]
86
+ result['total'].should == 3
87
+ result['matches'].should be_an_instance_of(Array)
88
+
89
+ result['matches'][0]['id'].should == 2
90
+ result['matches'][0]['weight'].should == 2
91
+ result['matches'][0]['attrs']['group_id'].should == 2
92
+ result['matches'][0]['attrs']['created_at'].should == 1175658555
93
+ result['matches'][0]['attrs']['tags'].should == [5, 6, 7, 8]
94
+ ('%0.2f' % result['matches'][0]['attrs']['rating']).should == '54.85'
95
+
96
+ result['matches'][1]['id'].should == 3
97
+ result['matches'][1]['weight'].should == 2
98
+ result['matches'][1]['attrs']['group_id'].should == 1
99
+ result['matches'][1]['attrs']['created_at'].should == 1175658647
100
+ result['matches'][1]['attrs']['tags'].should == [1, 7, 9, 10]
101
+ ('%0.2f' % result['matches'][1]['attrs']['rating']).should == '16.25'
102
+
103
+ result['matches'][2]['id'].should == 1
104
+ result['matches'][2]['weight'].should == 1
105
+ result['matches'][2]['attrs']['group_id'].should == 1
106
+ result['matches'][2]['attrs']['created_at'].should == 1175658490
107
+ result['matches'][2]['attrs']['tags'].should == [1, 2, 3, 4]
108
+ ('%0.2f' % result['matches'][2]['attrs']['rating']).should == '13.32'
109
+
110
+ result['words'].should == { 'wifi' => { 'hits' => 6, 'docs' => 3 } }
111
+ end
112
+ end