rubyhaze 0.0.6-jruby → 0.0.7-jruby

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
data/lib/rubyhaze/map.rb CHANGED
@@ -34,19 +34,23 @@ class RubyHaze::Map
34
34
  end
35
35
 
36
36
  def keys(predicate = nil)
37
- proxy_object.key_set(prepare_predicate(predicate)).map
37
+ predicate = prepare_predicate(predicate) unless predicate.is_a?(SqlPredicate)
38
+ proxy_object.key_set(predicate).map
38
39
  rescue NativeException => x
39
40
  rescue_native_exception x
40
41
  end
41
42
 
42
43
  def values(predicate = nil)
43
- proxy_object.values(prepare_predicate(predicate)).map
44
+ predicate = prepare_predicate(predicate) unless predicate.is_a?(SqlPredicate)
45
+ proxy_object.values(predicate).map
44
46
  rescue NativeException => x
45
47
  rescue_native_exception x
46
48
  end
49
+ alias :find :values
47
50
 
48
51
  def local_keys(predicate = nil)
49
- proxy_object.local_key_set(prepare_predicate(predicate)).map
52
+ predicate = prepare_predicate(predicate) unless predicate.is_a?(SqlPredicate)
53
+ proxy_object.local_key_set(predicate).map
50
54
  rescue NativeException => x
51
55
  rescue_native_exception x
52
56
  end
@@ -57,21 +61,26 @@ class RubyHaze::Map
57
61
  :created => lsm.creation_time, :last_accessed => lsm.last_access_time, }
58
62
  end
59
63
 
60
- private
61
-
62
64
  def prepare_predicate(predicate)
63
65
  return if predicate.nil?
64
66
  case predicate
65
67
  when String
66
68
  SqlPredicate.new predicate
67
69
  when Hash
68
- SqlPredicate.new predicate.map{|k,v| "#{k} = #{v}"}.join(' AND ')
69
- when SqlPredicate
70
- predicate
70
+ query = predicate.map do |field, value|
71
+ cmp = '='
72
+ if value.is_a?(String)
73
+ value = "'" + value + "'"
74
+ cmp = 'LIKE' if value.index('%')
75
+ end
76
+ "#{field} #{cmp} #{value}"
77
+ end.join(' AND ')
78
+ SqlPredicate.new query
71
79
  else
72
80
  raise "Unknown predicate type"
73
81
  end
74
82
  end
83
+
75
84
  end
76
85
 
77
86
  RubyHaze::Hash = RubyHaze::Map unless defined? RubyHaze::Hash
@@ -5,7 +5,7 @@ module RubyHaze
5
5
  def rescue_native_exception(exception)
6
6
  exception = exception.cause while exception.cause
7
7
  exception.print_stack_trace
8
- raise RubyHaze::HazelcastException(exception)
8
+ raise RubyHaze::HazelcastException.new(exception)
9
9
  end
10
10
 
11
11
  end
data/test/helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- unless HELPER_LOADED
1
+ unless defined?(HELPER_LOADED)
2
2
 
3
3
  require File.expand_path(File.dirname(__FILE__) + '/../lib/rubyhaze')
4
4
  require 'test/unit'
@@ -29,6 +29,10 @@ unless HELPER_LOADED
29
29
  end
30
30
  end
31
31
 
32
+ # Loading the Employee Java class
33
+ $CLASSPATH << File.expand_path(File.dirname(__FILE__)) + '/'
34
+ java_import 'Employee'
35
+
32
36
  # Finished loading helpers
33
37
  HELPER_LOADED = true
34
38
 
data/test/test_map.rb ADDED
@@ -0,0 +1,96 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ class TestRubyHazeHash < Test::Unit::TestCase
4
+
5
+ java_import 'com.hazelcast.query.SqlPredicate'
6
+
7
+ def test_same_object
8
+ hash = RubyHaze::Hash[:test_same_object]
9
+ map = RubyHaze::Map[:test_same_object]
10
+ assert_equal hash.name, map.name
11
+ hash[:a] = 1
12
+ assert_equal hash[:a], map[:a]
13
+ end
14
+
15
+ def test_string_keys
16
+ hash = RubyHaze::Map[:test_string_keys]
17
+
18
+ hash[:a] = 1
19
+ assert_equal hash['a'], 1
20
+ assert_equal hash[:a], 1
21
+
22
+ hash['b'] = 2
23
+ assert_equal hash[:b], 2
24
+
25
+ hash[Date.new(2010, 3, 18)] = 3
26
+ assert_equal hash['2010-03-18'], 3
27
+
28
+ hash[4] = 4
29
+ assert_equal hash['4'], 4
30
+ assert_equal hash[4], 4
31
+ end
32
+
33
+ def test_predicates
34
+ map = RubyHaze::Map[:test_predicates]
35
+
36
+ predicate = map.prepare_predicate "active = false AND (age = 45 OR name = 'Joe Mategna')"
37
+ assert_kind_of SqlPredicate, predicate
38
+ assert_equal "(active=false AND (age=45 OR name=Joe Mategna))", predicate.to_s
39
+
40
+ predicate = map.prepare_predicate :quantity => 3
41
+ assert_kind_of SqlPredicate, predicate
42
+ assert_equal 'quantity=3', predicate.to_s
43
+
44
+ predicate = map.prepare_predicate :country => "Unites States of America"
45
+ assert_kind_of SqlPredicate, predicate
46
+ assert_equal "country=Unites States of America", predicate.to_s
47
+ end
48
+
49
+ def test_search
50
+ map = RubyHaze::Map[:test_search]
51
+ map.clear
52
+
53
+ @a = Employee.new "Leonardo", 65, true, 1547.5
54
+ @b = Employee.new "Michelangelo", 45, false, 2835.75
55
+ @c = Employee.new "Marcos", 32, false, 0.0
56
+
57
+ map[:a] = @a
58
+ map[:b] = @b
59
+ map[:c] = @c
60
+
61
+ res = map.find :age => @b.age
62
+ assert_equal res.size, 1
63
+ assert_equal res.first.name, @b.name
64
+
65
+ res = map.find :name => @b.name
66
+ assert_equal res.size, 1
67
+ assert_equal res.first.name, @b.name
68
+
69
+ res = map.find 'salary > 2000.0'
70
+ assert_equal res.size, 1
71
+ assert_equal res.first.name, @b.name
72
+
73
+ res = map.find 'age BETWEEN 40 AND 50'
74
+ assert_equal res.size, 1
75
+ assert_equal res.first.name, @b.name
76
+
77
+ res = map.find "name LIKE 'Leo%'"
78
+ assert_equal res.size, 1
79
+ assert_equal res.first.name, @a.name
80
+
81
+ # Throws an internal exception
82
+ # res = map.find "age IN (32, 65)"
83
+ # assert_equal res.size, 2
84
+ # names = res.map{|x| x.name }.sort
85
+ # assert_equal names.first, @a.name
86
+ # assert_equal names.last, @b.name
87
+
88
+ res = map.find "age < 60 AND name LIKE 'M%'"
89
+ assert_equal res.size, 2
90
+ names = res.map{|x| x.name }.sort
91
+ assert_equal names.first, @c.name
92
+ assert_equal names.last, @b.name
93
+
94
+ end
95
+
96
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 6
9
- version: 0.0.6
8
+ - 7
9
+ version: 0.0.7
10
10
  platform: jruby
11
11
  authors:
12
12
  - Adrian Madrid
@@ -56,7 +56,7 @@ files:
56
56
  - lib/rubyhaze/set.rb
57
57
  - lib/rubyhaze/topic.rb
58
58
  - test/helper.rb
59
- - test/test_hash.rb
59
+ - test/test_map.rb
60
60
  - test/test_queue.rb
61
61
  - test/test_topic.rb
62
62
  has_rdoc: true
@@ -90,6 +90,6 @@ signing_key:
90
90
  specification_version: 3
91
91
  summary: Hazelcast distributed objects in my JRuby
92
92
  test_files:
93
- - test/test_hash.rb
93
+ - test/test_map.rb
94
94
  - test/test_queue.rb
95
95
  - test/test_topic.rb
data/test/test_hash.rb DELETED
@@ -1,31 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/helper')
2
-
3
- class TestRubyHazeHash < Test::Unit::TestCase
4
-
5
- def test_same_object
6
- hash = RubyHaze::Hash[:test_same_object]
7
- map = RubyHaze::Map[:test_same_object]
8
- assert_equal hash.name, map.name
9
- hash[:a] = 1
10
- assert_equal hash[:a], map[:a]
11
- end
12
-
13
- def test_string_keys
14
- hash = RubyHaze::Hash[:test_string_keys]
15
-
16
- hash[:a] = 1
17
- assert_equal hash['a'], 1
18
- assert_equal hash[:a], 1
19
-
20
- hash['b'] = 2
21
- assert_equal hash[:b], 2
22
-
23
- hash[Date.new(2010, 3, 18)] = 3
24
- assert_equal hash['2010-03-18'], 3
25
-
26
- hash[4] = 4
27
- assert_equal hash['4'], 4
28
- assert_equal hash[4], 4
29
- end
30
-
31
- end