torquebox-cache 2.0.3-java → 2.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,146 +0,0 @@
1
- # Copyright 2008-2012 Red Hat, Inc, and individual contributors.
2
- #
3
- # This is free software; you can redistribute it and/or modify it
4
- # under the terms of the GNU Lesser General Public License as
5
- # published by the Free Software Foundation; either version 2.1 of
6
- # the License, or (at your option) any later version.
7
- #
8
- # This software is distributed in the hope that it will be useful,
9
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
- # Lesser General Public License for more details.
12
- #
13
- # You should have received a copy of the GNU Lesser General Public
14
- # License along with this software; if not, write to the Free
15
- # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
- # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
-
18
- module Infinispan
19
-
20
- class Search
21
-
22
- def initialize(cache, deserializer)
23
- @cache = cache
24
- @deserializer = deserializer
25
- begin
26
- @search_manager = cache.search_manager
27
- rescue Exception => e
28
- cache.log( "Infinispan SearchManager not available for cache: #{cache.name}", 'ERROR' )
29
- cache.log( e.message, 'ERROR' )
30
- end
31
- end
32
-
33
- def search( query )
34
- if @search_manager
35
- cache_query = search_manager.get_query( build_query( query ), query.model.java_class )
36
- cache_query.list.collect { |record| deserialize(record) }
37
- else
38
- cache.all.select do |r|
39
- record = deserialize(r)
40
- record.class == query.model
41
- end
42
- end
43
- end
44
-
45
- def search_manager
46
- @search_manager
47
- end
48
-
49
- private
50
- def build_query( query )
51
- builder = search_manager.build_query_builder_for_class( query.model.java_class ).get
52
- query = query.conditions.nil? ? builder.all.create_query : handle_condition( builder, query.conditions.first )
53
- #puts "LUCENE QUERY: #{query.to_s}"
54
- query
55
- end
56
-
57
- def handle_condition( builder, condition )
58
- #puts "CONDITION: #{condition.inspect} <<<>>> #{condition}"
59
- #puts "CONDITION CLASS: #{condition.class}"
60
- #puts "CONDITION OPERANDS: #{condition.operands.inspect}" if condition.respond_to? :operands
61
- #puts "CONDITION VALUE: #{condition.value}"
62
- #puts "CONDITION SUBJECT: #{condition.subject.inspect}"
63
- if condition.class == DataMapper::Query::Conditions::OrOperation
64
- terms = condition.operands.each do |op|
65
- builder.bool.should( handle_condition( builder, op ) )
66
- end
67
- builder.all.create_query
68
- elsif condition.class == DataMapper::Query::Conditions::NotOperation
69
- handle_not_operation( builder, condition )
70
- elsif condition.class == DataMapper::Query::Conditions::EqualToComparison
71
- handle_equal_to( builder, condition )
72
- elsif condition.class == DataMapper::Query::Conditions::InclusionComparison
73
- handle_inclusion( builder, condition )
74
- elsif condition.class == DataMapper::Query::Conditions::RegexpComparison
75
- handle_regex( builder, condition )
76
- else
77
- builder.all.create_query
78
- end
79
- end
80
-
81
- def handle_regex( builder, condition )
82
- field = condition.subject.name
83
- # TODO Figure out how hibernate search/lucene deal with regexp
84
- value = condition.value.nil? ? "?*" : "*" + condition.value.source.gsub('/','') + "*"
85
- builder.keyword.wildcard.on_field(field).matching(value).create_query
86
- end
87
-
88
- def handle_inclusion( builder, condition )
89
- #puts "RANGE: #{condition.value.class} #{condition.value}"
90
- if condition.value.is_a? Range
91
- # TODO: Deal with Time
92
- if ((condition.subject.class == DataMapper::Property::DateTime) ||
93
- (condition.subject.class == DataMapper::Property::Date))
94
- rng = builder.range.on_field(condition.subject.name).from(convert_date(condition.value.begin)).to(convert_date(condition.value.end))
95
- else
96
- rng = builder.range.on_field(condition.subject.name).from(condition.value.begin).to(condition.value.end)
97
- end
98
- condition.value.exclude_end? ? rng.exclude_limit.create_query : rng.create_query
99
- else # an Array
100
- match = condition.value.collect { |v| v }.join(' ')
101
- if match.empty?
102
- # we should find nothing
103
- builder.bool.must( builder.all.create_query ).not.create_query
104
- else
105
- builder.keyword.on_field( condition.subject.name ).matching( match ).create_query
106
- end
107
- end
108
- end
109
-
110
- def convert_date(date)
111
- java.util.Date.new(Time.mktime(date.year, date.month, date.day, date.hour, date.min, date.sec, 0).to_i*1000) if date
112
- end
113
-
114
- def handle_equal_to( builder, condition )
115
- field = condition.subject.name
116
- value = condition.value.nil? ? "?*" : condition.value.to_s
117
- if !value.nil? && (value.include?( '?' ) || value.include?( '*' ))
118
- builder.keyword.wildcard.on_field(field).matching(value).create_query
119
- else
120
- builder.keyword.on_field(field).matching(value).create_query
121
- end
122
- end
123
-
124
- def handle_not_operation( builder, operation )
125
- condition = operation.operands.first
126
- if (condition.class == DataMapper::Query::Conditions::EqualToComparison && condition.value.nil?)
127
- # not nil means everything
128
- everything = DataMapper::Query::Conditions::EqualToComparison.new( condition.subject, '*' )
129
- handle_condition( builder, everything )
130
- else
131
- builder.bool.must( handle_condition( builder, condition ) ).not.create_query
132
- end
133
- end
134
-
135
- def cache
136
- @cache
137
- end
138
-
139
- def deserialize(value)
140
- @deserializer.call(value)
141
- end
142
-
143
- end
144
- end
145
-
146
-
@@ -1,375 +0,0 @@
1
- #
2
- # Copyright 2011 Red Hat, Inc.
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
-
17
- require File.dirname(__FILE__) + '/spec_helper'
18
- require 'dm-core'
19
- require 'datamapper/model'
20
-
21
- describe DataMapper::Adapters::InfinispanAdapter do
22
-
23
- before :all do
24
- @adapter = DataMapper.setup(:default, :adapter => 'infinispan')
25
- @heffalump_index = File.join( File.dirname(__FILE__), '..', 'rubyobj.Heffalump' )
26
- class ::Heffalump
27
- include DataMapper::Resource
28
-
29
- property :id, Serial
30
- property :color, String
31
- property :num_spots, Integer
32
- property :striped, Boolean
33
- end
34
-
35
- DataMapper.finalize
36
- Heffalump.auto_migrate!
37
- end
38
-
39
- it "should use the infinispan search manager" do
40
- @adapter.search_manager.should_not be_nil
41
- end
42
-
43
- after :all do
44
- @adapter.stop
45
- FileUtils.rm_rf @heffalump_index
46
- end
47
-
48
- describe '#auto_migrate!' do
49
- it 'should clear the cache' do
50
- Heffalump.create(:color => 'magenta')
51
- Heffalump.all.size.should == 1
52
- Heffalump.auto_migrate!
53
- Heffalump.all.size.should == 0
54
- end
55
- end
56
-
57
- describe '#auto_upgrade!' do
58
- it 'should not clear the cache' do
59
- Heffalump.create(:color => 'magenta')
60
- Heffalump.all.size.should == 1
61
- Heffalump.auto_upgrade!
62
- Heffalump.all.size.should == 1
63
- end
64
- end
65
-
66
- describe '#create' do
67
- it 'should not raise any errors' do
68
- lambda {
69
- Heffalump.create(:color => 'peach')
70
- }.should_not raise_error
71
- end
72
-
73
- it 'should set the identity field for the resource' do
74
- heffalump = Heffalump.new(:color => 'peach')
75
- heffalump.id.should be_nil
76
- heffalump.save
77
- heffalump.id.should_not be_nil
78
- end
79
- end
80
-
81
- describe '#read' do
82
- before :all do
83
- @heffalump = Heffalump.create(:color => 'brownish hue')
84
- end
85
-
86
- it 'should not raise any errors' do
87
- lambda {
88
- Heffalump.all()
89
- }.should_not raise_error
90
- end
91
-
92
- it 'should return stuff' do
93
- Heffalump.all.should be_include(@heffalump)
94
- end
95
- end
96
-
97
- describe '#update' do
98
- before do
99
- @heffalump = Heffalump.create(:color => 'indigo')
100
- end
101
-
102
- it 'should not raise any errors' do
103
- lambda {
104
- @heffalump.color = 'violet'
105
- @heffalump.save
106
- }.should_not raise_error
107
- end
108
-
109
- it 'should not alter the identity field' do
110
- id = @heffalump.id
111
- @heffalump.color = 'violet'
112
- @heffalump.save
113
- @heffalump.id.should == id
114
- end
115
-
116
- it 'should update altered fields' do
117
- @heffalump.color = 'violet'
118
- @heffalump.save
119
- Heffalump.get(*@heffalump.key).color.should == 'violet'
120
- end
121
-
122
- it 'should not alter other fields' do
123
- color = @heffalump.color
124
- @heffalump.num_spots = 3
125
- @heffalump.save
126
- Heffalump.get(*@heffalump.key).color.should == color
127
- end
128
- end
129
-
130
- describe '#delete' do
131
- before do
132
- @heffalump = Heffalump.create(:color => 'forest green')
133
- end
134
-
135
- it 'should not raise any errors' do
136
- lambda {
137
- @heffalump.destroy
138
- }.should_not raise_error
139
- end
140
-
141
- it 'should delete the requested resource' do
142
- id = @heffalump.id
143
- @heffalump.destroy
144
- Heffalump.get(id).should be_nil
145
- end
146
- end
147
-
148
- describe 'query matching' do
149
- before :all do
150
- Heffalump.auto_migrate!
151
- @red = Heffalump.create(:color => 'red')
152
- @two = Heffalump.create(:num_spots => 2)
153
- @five = Heffalump.create(:num_spots => 5)
154
- end
155
-
156
- describe 'conditions' do
157
- describe 'eql' do
158
- it 'should be able to search for objects included in an inclusive range of values' do
159
- Heffalump.all(:num_spots => 1..5).should be_include(@five)
160
- end
161
-
162
- it 'should be able to search for objects included in an exclusive range of values' do
163
- Heffalump.all(:num_spots => 1...6).should be_include(@five)
164
- end
165
-
166
- it 'should not be able to search for values not included in an inclusive range of values' do
167
- Heffalump.all(:num_spots => 1..4).should_not be_include(@five)
168
- end
169
-
170
- it 'should not be able to search for values not included in an exclusive range of values' do
171
- Heffalump.all(:num_spots => 1...5).should_not be_include(@five)
172
- end
173
- end
174
-
175
- describe 'not' do
176
- it 'should be able to search for objects with not equal value' do
177
- Heffalump.all(:color.not => 'red').should_not be_include(@red)
178
- end
179
-
180
- it 'should include objects that are not like the value' do
181
- Heffalump.all(:color.not => 'black').should be_include(@red)
182
- end
183
-
184
- it 'should be able to search for objects with not nil value' do
185
- Heffalump.all(:color.not => nil).should be_include(@red)
186
- end
187
-
188
- it 'should not include objects with a nil value' do
189
- Heffalump.all(:color.not => nil).should_not be_include(@two)
190
- end
191
-
192
- it 'should be able to search for object with a nil value using required properties' do
193
- Heffalump.all(:id.not => nil).should == [ @red, @two, @five ]
194
- end
195
-
196
- it 'should be able to search for objects not in an empty list (match all)' do
197
- Heffalump.all(:color.not => []).should == [ @red, @two, @five ]
198
- end
199
-
200
- it 'should be able to search for objects in an empty list and another OR condition (match none on the empty list)' do
201
- Heffalump.all(
202
- :conditions => DataMapper::Query::Conditions::Operation.new(
203
- :or,
204
- DataMapper::Query::Conditions::Comparison.new(:in, Heffalump.properties[:color], []),
205
- DataMapper::Query::Conditions::Comparison.new(:in, Heffalump.properties[:num_spots], [5])
206
- )
207
- ).should == [ @five ]
208
- end
209
-
210
- it 'should be able to search for objects not included in an array of values' do
211
- Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should be_include(@two)
212
- end
213
-
214
- it 'should be able to search for objects not included in an array of values' do
215
- Heffalump.all(:num_spots.not => [ 1, 3, 5, 7 ]).should_not be_include(@five)
216
- end
217
-
218
- it 'should be able to search for objects not included in an inclusive range of values' do
219
- Heffalump.all(:num_spots.not => 1..4).should be_include(@five)
220
- end
221
-
222
- it 'should be able to search for objects not included in an exclusive range of values' do
223
- Heffalump.all(:num_spots.not => 1...5).should be_include(@five)
224
- end
225
-
226
- it 'should not be able to search for values not included in an inclusive range of values' do
227
- Heffalump.all(:num_spots.not => 1..5).should_not be_include(@five)
228
- end
229
-
230
- it 'should not be able to search for values not included in an exclusive range of values' do
231
- Heffalump.all(:num_spots.not => 1...6).should_not be_include(@five)
232
- end
233
- end
234
-
235
- describe 'like' do
236
- it 'should be able to search for objects that match value' do
237
- Heffalump.all(:color.like => '%ed').should be_include(@red)
238
- end
239
-
240
- it 'should not search for objects that do not match the value' do
241
- Heffalump.all(:color.like => '%blak%').should_not be_include(@red)
242
- end
243
- end
244
-
245
- describe 'regexp' do
246
- before do
247
- if (defined?(DataMapper::Adapters::SqliteAdapter) && @adapter.kind_of?(DataMapper::Adapters::SqliteAdapter) ||
248
- defined?(DataMapper::Adapters::SqlserverAdapter) && @adapter.kind_of?(DataMapper::Adapters::SqlserverAdapter))
249
- pending 'delegate regexp matches to same system that the InMemory and YAML adapters use'
250
- end
251
- end
252
-
253
- it 'should be able to search for objects that match value' do
254
- Heffalump.all(:color => /ed/).should be_include(@red)
255
- end
256
-
257
- it 'should not be able to search for objects that do not match the value' do
258
- Heffalump.all(:color => /blak/).should_not be_include(@red)
259
- end
260
-
261
- it 'should be able to do a negated search for objects that match value' do
262
- Heffalump.all(:color.not => /blak/).should be_include(@red)
263
- end
264
-
265
- it 'should not be able to do a negated search for objects that do not match value' do
266
- Heffalump.all(:color.not => /ed/).should_not be_include(@red)
267
- end
268
-
269
- end
270
-
271
- describe 'gt' do
272
- it 'should be able to search for objects with value greater than' do
273
- Heffalump.all(:num_spots.gt => 1).should be_include(@two)
274
- end
275
-
276
- it 'should not find objects with a value less than' do
277
- Heffalump.all(:num_spots.gt => 3).should_not be_include(@two)
278
- end
279
- end
280
-
281
- describe 'gte' do
282
- it 'should be able to search for objects with value greater than' do
283
- Heffalump.all(:num_spots.gte => 1).should be_include(@two)
284
- end
285
-
286
- it 'should be able to search for objects with values equal to' do
287
- Heffalump.all(:num_spots.gte => 2).should be_include(@two)
288
- end
289
-
290
- it 'should not find objects with a value less than' do
291
- Heffalump.all(:num_spots.gte => 3).should_not be_include(@two)
292
- end
293
- end
294
-
295
- describe 'lt' do
296
- it 'should be able to search for objects with value less than' do
297
- Heffalump.all(:num_spots.lt => 3).should be_include(@two)
298
- end
299
-
300
- it 'should not find objects with a value less than' do
301
- Heffalump.all(:num_spots.gt => 2).should_not be_include(@two)
302
- end
303
- end
304
-
305
- describe 'lte' do
306
- it 'should be able to search for objects with value less than' do
307
- Heffalump.all(:num_spots.lte => 3).should be_include(@two)
308
- end
309
-
310
- it 'should be able to search for objects with values equal to' do
311
- Heffalump.all(:num_spots.lte => 2).should be_include(@two)
312
- end
313
-
314
- it 'should not find objects with a value less than' do
315
- Heffalump.all(:num_spots.lte => 1).should_not be_include(@two)
316
- end
317
- end
318
- end
319
-
320
- describe 'limits' do
321
- it 'should be able to limit the objects' do
322
- Heffalump.all(:limit => 2).length.should == 2
323
- end
324
- end
325
- end
326
-
327
- describe "with persistence" do
328
- before :all do
329
- @configured_dir = File.join( File.dirname(__FILE__), '..', random_string + "-dm-infinispan-adapter.cache" )
330
- @default_dir = File.join(File.dirname(__FILE__), '..', 'Infinispan-FileCacheStore')
331
- @snuffy = File.join( File.dirname(__FILE__), '..', 'rubyobj.Snuffleupagus' )
332
- FileUtils.mkdir( @configured_dir )
333
- class Snuffleupagus
334
- include DataMapper::Resource
335
- property :id, Serial
336
- property :birthday, Date
337
- end
338
- Snuffleupagus.configure_index!
339
- end
340
-
341
- after :all do
342
- FileUtils.rm_rf( @configured_dir )
343
- FileUtils.rm_rf( @default_dir )
344
- FileUtils.rm_rf( @snuffy )
345
- end
346
-
347
- it "should store data in a configured directory" do
348
- adapter = DataMapper.setup(:dminfinispanadapterconfigured, :adapter => 'infinispan', :persist => @configured_dir.to_s)
349
- snuffy = Snuffleupagus.create(:birthday=>Date.today)
350
- File.exist?("#{@configured_dir.to_s}/dminfinispanadapterconfigured").should be_true
351
- snuffy.should_not be_nil
352
- adapter.stop
353
- end
354
-
355
- it "should store data in a default directory" do
356
- adapter = DataMapper.setup(:dminfinispanadapterdefault, :adapter => 'infinispan', :persist=>true)
357
- snuffy = Snuffleupagus.create(:birthday=>Date.today)
358
- File.exist?( @default_dir ).should be_true
359
- snuffy.should_not be_nil
360
- snuffy.id.should_not be_nil
361
- adapter.stop
362
- end
363
-
364
- it "should store dates" do
365
- adapter = DataMapper.setup(:default, :adapter => 'infinispan', :persist=>true)
366
- snuffy = Snuffleupagus.create(:birthday => Date.today)
367
- snuffy.should_not be_nil
368
- snuffy.getBirthday.should_not be_nil
369
- adapter.stop
370
- end
371
-
372
- end
373
- end
374
-
375
-