rubyhaze 0.0.3-jruby → 0.0.4-jruby
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.
- data/VERSION +1 -1
- data/lib/rubyhaze.rb +0 -4
- data/test/helper.rb +1 -0
- data/test/test_hash.rb +31 -0
- data/test/test_queue.rb +17 -0
- metadata +7 -8
- data/lib/rubyhaze/core_ext.rb +0 -97
- data/lib/rubyhaze/stored.rb +0 -183
- data/lib/rubyhaze/stores/fake_store.rb +0 -36
- data/test/test_stored.rb +0 -84
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/rubyhaze.rb
CHANGED
@@ -68,8 +68,6 @@ module RubyHaze
|
|
68
68
|
|
69
69
|
end
|
70
70
|
|
71
|
-
require 'rubyhaze/core_ext'
|
72
|
-
|
73
71
|
require 'rubyhaze/mixins/proxy'
|
74
72
|
require 'rubyhaze/mixins/compare'
|
75
73
|
require 'rubyhaze/mixins/native_exception'
|
@@ -89,6 +87,4 @@ require 'rubyhaze/configs/network'
|
|
89
87
|
require 'rubyhaze/configs/queue'
|
90
88
|
require 'rubyhaze/configs/config'
|
91
89
|
|
92
|
-
require 'rubyhaze/stored'
|
93
|
-
|
94
90
|
RH = RubyHaze unless defined? RH
|
data/test/helper.rb
CHANGED
data/test/test_hash.rb
ADDED
@@ -0,0 +1,31 @@
|
|
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
|
data/test/test_queue.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
+
|
3
|
+
class TestRubyHazeQueue < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_single_queing
|
6
|
+
tasks = RubyHaze::Queue[:test_single]
|
7
|
+
50.times { |idx| tasks.put [idx, Process.pid] }
|
8
|
+
found = []
|
9
|
+
while !tasks.empty? do
|
10
|
+
task = tasks.poll 5, java.util.concurrent.TimeUnit::SECONDS
|
11
|
+
found << task
|
12
|
+
end
|
13
|
+
assert !found.empty?
|
14
|
+
assert_equal found.size, 50
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: jruby
|
11
11
|
authors:
|
12
12
|
- Adrian Madrid
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-08-
|
17
|
+
date: 2010-08-30 00:00:00 -06:00
|
18
18
|
default_executable: rubyhaze_console
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -54,7 +54,6 @@ files:
|
|
54
54
|
- lib/rubyhaze/configs/network.rb
|
55
55
|
- lib/rubyhaze/configs/queue.rb
|
56
56
|
- lib/rubyhaze/configs/topic.rb
|
57
|
-
- lib/rubyhaze/core_ext.rb
|
58
57
|
- lib/rubyhaze/list.rb
|
59
58
|
- lib/rubyhaze/lock.rb
|
60
59
|
- lib/rubyhaze/map.rb
|
@@ -66,11 +65,10 @@ files:
|
|
66
65
|
- lib/rubyhaze/node.rb
|
67
66
|
- lib/rubyhaze/queue.rb
|
68
67
|
- lib/rubyhaze/set.rb
|
69
|
-
- lib/rubyhaze/stored.rb
|
70
|
-
- lib/rubyhaze/stores/fake_store.rb
|
71
68
|
- lib/rubyhaze/topic.rb
|
72
69
|
- test/helper.rb
|
73
|
-
- test/
|
70
|
+
- test/test_hash.rb
|
71
|
+
- test/test_queue.rb
|
74
72
|
- test/test_topic.rb
|
75
73
|
has_rdoc: true
|
76
74
|
homepage: http://github.com/aemadrid/rubyhaze
|
@@ -103,5 +101,6 @@ signing_key:
|
|
103
101
|
specification_version: 3
|
104
102
|
summary: JRuby wrapper to play with Hazelcast
|
105
103
|
test_files:
|
106
|
-
- test/
|
104
|
+
- test/test_hash.rb
|
105
|
+
- test/test_queue.rb
|
107
106
|
- test/test_topic.rb
|
data/lib/rubyhaze/core_ext.rb
DELETED
@@ -1,97 +0,0 @@
|
|
1
|
-
# Taken from http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/100177
|
2
|
-
class Exception
|
3
|
-
def errmsg
|
4
|
-
"%s: %s\n%s" % [self.class, message, (backtrace || []).join("\n") << "\n"]
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
# Taken from Active Support
|
9
|
-
class Array
|
10
|
-
# Extracts options from a set of arguments. Removes and returns the last
|
11
|
-
# element in the array if it's a hash, otherwise returns a blank hash.
|
12
|
-
#
|
13
|
-
# def options(*args)
|
14
|
-
# args.extract_options!
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# options(1, 2) # => {}
|
18
|
-
# options(1, 2, :a => :b) # => {:a=>:b}
|
19
|
-
def extract_options!
|
20
|
-
last.is_a?(::Hash) ? pop : {}
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
# Extracted from truthy (http://github.com/ymendel/truthy/tree/master/lib/truthy.rb)
|
25
|
-
# and Rails (http://github.com/rails/rails/commit/823b623fe2de8846c37aa13250010809ac940b57)
|
26
|
-
|
27
|
-
class Object
|
28
|
-
|
29
|
-
# Tricky, tricky! (-:
|
30
|
-
def truthy?
|
31
|
-
!!self
|
32
|
-
end
|
33
|
-
|
34
|
-
# Tries to send the method only if object responds to it. Return +nil+ otherwise.
|
35
|
-
# It will also forward any arguments and/or block like Object#send does.
|
36
|
-
#
|
37
|
-
# ==== Example :
|
38
|
-
#
|
39
|
-
# # Without try
|
40
|
-
# @person ? @person.name : nil
|
41
|
-
#
|
42
|
-
# With try
|
43
|
-
# @person.try(:name)
|
44
|
-
#
|
45
|
-
# # try also accepts arguments/blocks for the method it is trying
|
46
|
-
# Person.try(:find, 1)
|
47
|
-
# @people.try(:map) {|p| p.name}
|
48
|
-
#
|
49
|
-
def try(method, *args, &block)
|
50
|
-
send(method, *args, &block) if respond_to?(method)
|
51
|
-
end
|
52
|
-
|
53
|
-
# List unique local methods
|
54
|
-
# Taken from http://giantrobots.thoughtbot.com/2008/12/23/script-console-tips
|
55
|
-
def local_methods
|
56
|
-
(methods - Object.instance_methods).sort
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
class String
|
62
|
-
def self.random_uuid
|
63
|
-
java.util.UUID.randomUUID.toString
|
64
|
-
end
|
65
|
-
|
66
|
-
def valid_uuid?
|
67
|
-
!!(size == 36 && self =~ %r{^([A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12})$}i)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
# Taken from ActiveSupport
|
72
|
-
class Hash
|
73
|
-
def slice(*keys)
|
74
|
-
keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
|
75
|
-
hash = self.class.new
|
76
|
-
keys.each { |k| hash[k] = self[k] if has_key?(k) }
|
77
|
-
hash
|
78
|
-
end
|
79
|
-
|
80
|
-
def slice!(*keys)
|
81
|
-
keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
|
82
|
-
omit = slice(*self.keys - keys)
|
83
|
-
hash = slice(*keys)
|
84
|
-
replace(hash)
|
85
|
-
omit
|
86
|
-
end
|
87
|
-
|
88
|
-
def except(*keys)
|
89
|
-
dup.except!(*keys)
|
90
|
-
end
|
91
|
-
|
92
|
-
def except!(*keys)
|
93
|
-
keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
|
94
|
-
keys.each { |key| delete(key) }
|
95
|
-
self
|
96
|
-
end
|
97
|
-
end
|
data/lib/rubyhaze/stored.rb
DELETED
@@ -1,183 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../../lib/rubyhaze')
|
2
|
-
require 'bitescript'
|
3
|
-
|
4
|
-
module RubyHaze::Stored
|
5
|
-
|
6
|
-
def self.included(base)
|
7
|
-
base.extend ClassMethods
|
8
|
-
end
|
9
|
-
|
10
|
-
def initialize(*args)
|
11
|
-
return if args.empty?
|
12
|
-
hash = args.extract_options!
|
13
|
-
set hash if hash
|
14
|
-
unless args.empty?
|
15
|
-
args.each_with_index do |value, idx|
|
16
|
-
set self.class.field_names[idx], value
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def set(*args)
|
22
|
-
if args.size == 2
|
23
|
-
instance_variable_set "@#{args.first}", args.last
|
24
|
-
elsif args.size == 1 && args.first.is_a?(Hash)
|
25
|
-
args.first.each { |k,v| set k, v }
|
26
|
-
else
|
27
|
-
raise "Unknown parameters #{args.inspect} for set"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def get(*args)
|
32
|
-
if args.size == 1
|
33
|
-
instance_variable_get "@#{args.first}"
|
34
|
-
else
|
35
|
-
args.map { |k| get k }
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def attributes
|
40
|
-
self.class.field_names.map{ |name| [ name, instance_variable_get("@#{name}") ] }
|
41
|
-
end
|
42
|
-
|
43
|
-
def values
|
44
|
-
self.class.field_names.map{ |name| instance_variable_get("@#{name}") }
|
45
|
-
end
|
46
|
-
|
47
|
-
def to_ary
|
48
|
-
attributes.unshift [ 'class', self.class.name ]
|
49
|
-
end
|
50
|
-
|
51
|
-
def ==(other)
|
52
|
-
return false unless other.respond_to? :to_ary
|
53
|
-
to_ary == other.to_ary
|
54
|
-
end
|
55
|
-
|
56
|
-
def shadow_object
|
57
|
-
self.class.store_java_class.new *values
|
58
|
-
end
|
59
|
-
|
60
|
-
def load_shadow_object(shadow)
|
61
|
-
self.class.field_names.each do |name|
|
62
|
-
instance_variable_set "@#{name}", shadow.send(name)
|
63
|
-
end
|
64
|
-
self
|
65
|
-
end
|
66
|
-
|
67
|
-
attr_accessor :uid
|
68
|
-
|
69
|
-
def save
|
70
|
-
@uid ||= String.random_uuid
|
71
|
-
self.class.store[uid] = shadow_object
|
72
|
-
end
|
73
|
-
|
74
|
-
def load
|
75
|
-
raise "Missing uid for load" if uid.nil?
|
76
|
-
found = self.class.store[uid]
|
77
|
-
raise "Record not found" unless found
|
78
|
-
load_shadow_object(found)
|
79
|
-
self
|
80
|
-
end
|
81
|
-
|
82
|
-
alias :reload :load
|
83
|
-
|
84
|
-
def to_s
|
85
|
-
"<" + self.class.name + " " + attributes[1..-1].inspect + " >"
|
86
|
-
end
|
87
|
-
|
88
|
-
module ClassMethods
|
89
|
-
|
90
|
-
def create(*args)
|
91
|
-
obj = new(*args)
|
92
|
-
obj.save
|
93
|
-
obj
|
94
|
-
end
|
95
|
-
|
96
|
-
def store
|
97
|
-
@store ||= RubyHaze::Map.new store_java_class_name
|
98
|
-
end
|
99
|
-
|
100
|
-
def fields
|
101
|
-
@fields ||= [
|
102
|
-
[ :uid, :string, {} ]
|
103
|
-
]
|
104
|
-
end
|
105
|
-
|
106
|
-
def field_names() fields.map { |ary| ary[0] } end
|
107
|
-
def field_types() fields.map { |ary| ary[1] } end
|
108
|
-
def field_options() fields.map { |ary| ary[2] } end
|
109
|
-
|
110
|
-
def field(name, type, options = {})
|
111
|
-
raise "Field [#{name} already defined" if field_names.include?(name)
|
112
|
-
fields << [ name, type, options ]
|
113
|
-
attr_accessor name
|
114
|
-
end
|
115
|
-
|
116
|
-
def field_loads
|
117
|
-
{
|
118
|
-
:string => :aload,
|
119
|
-
:int => :iload,
|
120
|
-
:boolean => :iload,
|
121
|
-
:double => :dload,
|
122
|
-
}
|
123
|
-
end
|
124
|
-
|
125
|
-
def store_java_class_name
|
126
|
-
'RubyHaze_Shadow__' + name
|
127
|
-
end
|
128
|
-
|
129
|
-
def store_java_class
|
130
|
-
RubyHaze.const_defined?(store_java_class_name) ?
|
131
|
-
RubyHaze.const_get(store_java_class_name) :
|
132
|
-
generate_java_class
|
133
|
-
end
|
134
|
-
|
135
|
-
def generate_java_class
|
136
|
-
builder = BiteScript::FileBuilder.new store_java_class_name + '.class'
|
137
|
-
class_dsl = []
|
138
|
-
class_dsl << %{public_class "#{store_java_class_name}", object, Java::JavaIo::Serializable do}
|
139
|
-
fields.each do |name, type, options|
|
140
|
-
class_dsl << %{ public_field :#{name}, send(:#{type})}
|
141
|
-
end
|
142
|
-
class_dsl << %{ public_constructor [], #{field_types.map{|x| x.to_s}.join(', ')} do}
|
143
|
-
class_dsl << %{ aload 0}
|
144
|
-
class_dsl << %{ invokespecial object, "<init>", [void]}
|
145
|
-
index = 1
|
146
|
-
fields.each do |name, type, options|
|
147
|
-
class_dsl << %{ aload 0}
|
148
|
-
class_dsl << %{ #{field_loads[type]} #{index}}
|
149
|
-
index += 1
|
150
|
-
class_dsl << %{ putfield this, :#{name}, send(:#{type})}
|
151
|
-
end
|
152
|
-
class_dsl << %{ returnvoid}
|
153
|
-
class_dsl << %{ end}
|
154
|
-
class_dsl << %{end}
|
155
|
-
class_dsl = class_dsl.join("\n")
|
156
|
-
if $DEBUG
|
157
|
-
FileUtils.mkdir_p RubyHaze::TMP_PATH
|
158
|
-
filename = RubyHaze::TMP_PATH + '/' + store_java_class_name + '.bc'
|
159
|
-
File.open(filename, 'w') { |file| file.write class_dsl }
|
160
|
-
end
|
161
|
-
builder.instance_eval class_dsl, __FILE__, __LINE__
|
162
|
-
builder.generate do |builder_filename, class_builder|
|
163
|
-
bytes = class_builder.generate
|
164
|
-
klass = JRuby.runtime.jruby_class_loader.define_class store_java_class_name, bytes.to_java_bytes
|
165
|
-
if $DEBUG
|
166
|
-
filename = RubyHaze::TMP_PATH + '/' + builder_filename
|
167
|
-
File.open(filename, 'w') { |file| file.write class_builder.generate }
|
168
|
-
end
|
169
|
-
RubyHaze.const_set store_java_class_name, JavaUtilities.get_proxy_class(klass.name)
|
170
|
-
end
|
171
|
-
RubyHaze.const_get store_java_class_name
|
172
|
-
end
|
173
|
-
|
174
|
-
def find(predicate)
|
175
|
-
store.values(predicate).map { |shadow| new.load_shadow_object shadow }
|
176
|
-
end
|
177
|
-
|
178
|
-
def find_uids(predicate)
|
179
|
-
store.keys(predicate)
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
class RubyHaze::FakeStore
|
2
|
-
|
3
|
-
java_import 'com.hazelcast.core.MapLoader'
|
4
|
-
java_import 'com.hazelcast.core.MapStore'
|
5
|
-
|
6
|
-
include MapStore
|
7
|
-
include MapLoader
|
8
|
-
|
9
|
-
def load(key)
|
10
|
-
puts "[FakeStore] Loading key [#{key}]"
|
11
|
-
end
|
12
|
-
|
13
|
-
def load_all(keys)
|
14
|
-
puts "[FakeStore] Loading #{keys.size} keys"
|
15
|
-
keys.each { |key| load key }
|
16
|
-
end
|
17
|
-
|
18
|
-
def delete(key)
|
19
|
-
puts "[FakeStore] Deleting key [#{key}]"
|
20
|
-
end
|
21
|
-
|
22
|
-
def delete_all(keys)
|
23
|
-
puts "[FakeStore] Deleting #{keys.size} keys"
|
24
|
-
keys.each { |key| delete key }
|
25
|
-
end
|
26
|
-
|
27
|
-
def store(key, value)
|
28
|
-
puts "[FakeStore] Storing key [#{key}] value [#{value}]"
|
29
|
-
end
|
30
|
-
|
31
|
-
def store_all(map)
|
32
|
-
puts "[FakeStore] Storing #{map.size} key/values"
|
33
|
-
map.each { |key, value| store key, value }
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
data/test/test_stored.rb
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
-
|
3
|
-
class Foo
|
4
|
-
include RubyHaze::Stored
|
5
|
-
field :name, :string
|
6
|
-
field :age, :int
|
7
|
-
end unless defined? Foo
|
8
|
-
|
9
|
-
class TestRubyHazeStoredClassMethods < Test::Unit::TestCase
|
10
|
-
|
11
|
-
def test_right_store
|
12
|
-
store = Foo.store
|
13
|
-
assert_equal store.class.name, "RubyHaze::Map"
|
14
|
-
assert_equal store.name, "RubyHaze_Shadow__Foo"
|
15
|
-
assert_equal store.name, RubyHaze::Map.new("RubyHaze_Shadow__Foo").name
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_right_fields
|
19
|
-
fields = Foo.fields
|
20
|
-
assert_equal fields, [[:uid, :string, {}], [:name, :string, {}], [:age, :int, {}]]
|
21
|
-
assert_equal Foo.field_names, [:uid, :name, :age]
|
22
|
-
assert_equal Foo.field_types, [:string, :string, :int]
|
23
|
-
assert_equal Foo.field_options, [{}, {}, {}]
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_right_shadow_class
|
27
|
-
assert_equal Foo.store_java_class_name, "RubyHaze_Shadow__Foo"
|
28
|
-
assert_equal Foo.store_java_class.name, "Java::Default::RubyHaze_Shadow__Foo"
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
class TestRubyHazeStoredStorage < Test::Unit::TestCase
|
35
|
-
|
36
|
-
def test_store_reload_objects
|
37
|
-
Foo.store.clear
|
38
|
-
assert_equal Foo.store.size, 0
|
39
|
-
@a = Foo.create :name => "Leonardo", :age => 65
|
40
|
-
assert_equal Foo.store.size, 1
|
41
|
-
@b = Foo.create :name => "Michelangelo", :age => 45
|
42
|
-
assert_equal Foo.store.size, 2
|
43
|
-
@b.age = 47
|
44
|
-
@b.reload
|
45
|
-
assert_equal Foo.store.size, 2
|
46
|
-
assert_equal @b.age, 45
|
47
|
-
Foo.store.clear
|
48
|
-
assert_equal Foo.store.size, 0
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_find_through_predicates
|
52
|
-
Foo.store.clear
|
53
|
-
@a = Foo.create :name => "Leonardo", :age => 65
|
54
|
-
@b = Foo.create :name => "Michelangelo", :age => 45
|
55
|
-
@c = Foo.create :name => "Raffaello", :age => 32
|
56
|
-
|
57
|
-
res = Foo.find 'age < 40'
|
58
|
-
assert_equal res.size, 1
|
59
|
-
assert_equal res.first, @c
|
60
|
-
assert_equal res.first.name, @c.name
|
61
|
-
|
62
|
-
res = Foo.find 'age BETWEEN 40 AND 50'
|
63
|
-
assert_equal res.size, 1
|
64
|
-
assert_equal res.first, @b
|
65
|
-
assert_equal res.first.name, @b.name
|
66
|
-
|
67
|
-
res = Foo.find "name LIKE 'Leo%'"
|
68
|
-
assert_equal res.size, 1
|
69
|
-
assert_equal res.first, @a
|
70
|
-
assert_equal res.first.name, @a.name
|
71
|
-
|
72
|
-
# res = Foo.find "age IN (32, 65)"
|
73
|
-
# assert_equal res.size, 2
|
74
|
-
# names = res.map{|x| x.name }.sort
|
75
|
-
# assert_equal names.first, @a.name
|
76
|
-
# assert_equal names.last, @b.name
|
77
|
-
|
78
|
-
res = Foo.find "age < 40 AND name LIKE '%el%'"
|
79
|
-
assert_equal res.size, 1
|
80
|
-
assert_equal res.first, @c
|
81
|
-
assert_equal res.first.name, @c.name
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|