hazelcast-client 0.0.1-jruby

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Adrian Madrid
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ = hazelcast-client
2
+
3
+ Connecting to a Hazelcast Cluster has never been easier!
4
+ Hazelcast::Client is a little gem that wraps the Java Hazelcast Client library into a more comfortable JRuby package.
5
+
6
+ Hazelcast is an open source clustering and highly scalable data distribution platform for Java, which is:
7
+
8
+ * Lightning-fast; thousands of operations/sec.
9
+ * Fail-safe; no losing data after crashes.
10
+ * Dynamically scales as new servers added.
11
+ * Super-easy to use; include a single jar.
12
+
13
+ == Getting started
14
+
15
+ Let's get the gem installed and test out the interactive console. Make sure you have a hazelcast cluster running
16
+ in localhost and that is using the default username and password.
17
+
18
+ shell> rvm jruby
19
+ shell> gem install hazelcast-client
20
+ shell> hazelcast-client-console
21
+
22
+ hash = @client.map :test
23
+ hash[:a] = 1
24
+ hash[:b] = 2
25
+
26
+ Let's open another console to check how this distributed hash works:
27
+
28
+ shell> hazelcast-client-console
29
+
30
+ hash = @client.map :test
31
+ hash[:a]
32
+
33
+ >> 1
34
+
35
+ == Note on Patches/Pull Requests
36
+
37
+ * Fork the project.
38
+ * Make your feature addition or bug fix.
39
+ * Add tests for it. This is important so I don't break it in a
40
+ future version unintentionally.
41
+ * Commit, do not mess with rakefile, version, or history.
42
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
43
+ * Send me a pull request. Bonus points for topic branches.
44
+
45
+ == Copyright
46
+
47
+ Copyright (c) 2011 Adrian Madrid. See LICENSE for details.
@@ -0,0 +1,64 @@
1
+ #require 'bundler'
2
+ #Bundler::GemHelper.install_tasks
3
+
4
+ require 'rubygems'
5
+ require 'rake'
6
+ require 'rake/testtask'
7
+ require 'rake/rdoctask'
8
+
9
+ begin
10
+ require 'jeweler'
11
+ Jeweler::Tasks.new do |gem|
12
+ gem.name = "hazelcast-client"
13
+ gem.authors = ["Adrian Madrid"]
14
+ gem.email = ["aemadrid@gmail.com"]
15
+ gem.homepage = ""
16
+ gem.summary = %q{Connecting to a Hazelcast Cluster has never been easier!}
17
+ gem.description = %q{Hazelcast::Client is a little gem that wraps the Java Hazelcast Client library into a more comfortable JRuby package.}
18
+ gem.platform = "jruby"
19
+
20
+ gem.rubyforge_project = "hazelcast-client"
21
+
22
+ gem.files = FileList['bin/*', 'lib/**/*.rb', 'jars/**/*', 'test/**/*.rb', '[A-Z]*'].to_a
23
+ gem.test_files = Dir["test/test*.rb"]
24
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
25
+ gem.require_paths = ["lib"]
26
+
27
+ gem.add_dependency "hazelcast-jars", "1.9.1"
28
+ end
29
+ Jeweler::GemcutterTasks.new
30
+ rescue LoadError
31
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
32
+ end
33
+
34
+ Rake::TestTask.new :test do |t|
35
+ t.libs << "lib"
36
+ t.test_files = FileList["test/**/test_*.rb"]
37
+ end
38
+
39
+ task :test => :check_dependencies
40
+
41
+ task :default => :test
42
+
43
+ begin
44
+ require 'rcov/rcovtask'
45
+ Rcov::RcovTask.new do |spec|
46
+ spec.libs << 'spec'
47
+ spec.pattern = 'spec/**/*_spec.rb'
48
+ spec.verbose = true
49
+ end
50
+ rescue LoadError
51
+ task :rcov do
52
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
53
+ end
54
+ end
55
+
56
+ require 'rake/rdoctask'
57
+ Rake::RDocTask.new do |rdoc|
58
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
59
+
60
+ rdoc.rdoc_dir = 'rdoc'
61
+ rdoc.title = "rubyhaze #{version}"
62
+ rdoc.rdoc_files.include('README*')
63
+ rdoc.rdoc_files.include('lib/**/*.rb')
64
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env jruby
2
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/hazelcast-client.rb")
3
+
4
+ @client = Hazelcast::Client.new ARGV[0], ARGV[1], ARGV[2]
5
+
6
+ puts ">> Starting IRB session..."
7
+ require 'irb'
8
+ ARGV.clear
9
+ IRB.start(__FILE__)
@@ -0,0 +1,108 @@
1
+ raise "Rubyhaze only runs on JRuby. Sorry!" unless (RUBY_PLATFORM =~ /java/)
2
+ require 'java'
3
+ require 'rubygems'
4
+ require 'hazelcast-jars'
5
+
6
+ module Hazelcast
7
+ class Client
8
+
9
+ Hazelcast::Jars.all
10
+ GEM_ROOT = File.expand_path(File.dirname(__FILE__)) unless defined?(GEM_ROOT)
11
+
12
+ java_import 'com.hazelcast.client.HazelcastClient'
13
+ # java_import 'java.util.Map'
14
+
15
+ attr_reader :username, :password, :host
16
+
17
+ def initialize(username = nil, password = nil, host = nil)
18
+ @username = username || "dev"
19
+ @password = password || "dev-pass"
20
+ @host = host || "localhost"
21
+ @client = self.class.connect @username, @password, @host
22
+ end
23
+
24
+ def cluster(name)
25
+ @client.getCluster name.to_s
26
+ end
27
+
28
+ def list(name)
29
+ @client.getList name.to_s
30
+ end
31
+
32
+ def lock(name)
33
+ @client.getLock name.to_s
34
+ end
35
+
36
+ def map(name)
37
+ @client.getMap name.to_s
38
+ end
39
+
40
+ def multi_map(name)
41
+ @client.getMultiMap name.to_s
42
+ end
43
+
44
+ def queue(name)
45
+ @client.getQueue name.to_s
46
+ end
47
+
48
+ def set(name)
49
+ @client.getSet name.to_s
50
+ end
51
+
52
+ def topic(name)
53
+ @client.getTopic name.to_s
54
+ end
55
+
56
+ def transaction
57
+ txn = @client.getTransaction
58
+ txn.begin
59
+ begin
60
+ yield
61
+ txn.commit
62
+ nil
63
+ rescue => e
64
+ txn.rollback
65
+ e
66
+ end
67
+ end
68
+
69
+ def respond_to?(meth)
70
+ super || @client.respond_to?(meth)
71
+ end
72
+
73
+ def method_missing(meth, *args, &blk)
74
+ if @client.respond_to? meth
75
+ @client.send meth, *args, &blk
76
+ else
77
+ super
78
+ end
79
+ end
80
+
81
+ def self.connections
82
+ @connections ||= {}
83
+ end
84
+
85
+ def self.connect(username, password, host)
86
+ conn_id = "#{username}:#{password}:#{host}"
87
+ if connections.key? conn_id
88
+ connections[conn_id]
89
+ else
90
+ puts ">> Connecting to [#{host}] as [#{username}] with [#{password}]..."
91
+ connections[conn_id] ||= HazelcastClient.newHazelcastClient username, password, host
92
+ end
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+
99
+ %w{ map queue topic }.each do |name|
100
+ require Hazelcast::Client::GEM_ROOT + '/hazelcast-client/' + name
101
+ end
102
+
103
+ at_exit do
104
+ Hazelcast::Client.connections.each do |conn_id, client|
105
+ puts ">> Shutting down #{client} before closing shop ..."
106
+ client.shutdown
107
+ end
108
+ end
@@ -0,0 +1,49 @@
1
+ class Java::ComHazelcastClient::MapClientProxy
2
+
3
+ java_import 'com.hazelcast.query.SqlPredicate'
4
+
5
+ def [](key)
6
+ get key.to_s
7
+ end
8
+
9
+ def []=(key, value)
10
+ put key.to_s, value
11
+ end
12
+
13
+ def keys(predicate = nil)
14
+ predicate = prepare_predicate(predicate) unless predicate.is_a?(SqlPredicate)
15
+ key_set(predicate).map
16
+ end
17
+
18
+ alias_method :unlearned_values, :values
19
+
20
+ def values(predicate = nil)
21
+ predicate = prepare_predicate(predicate) unless predicate.is_a?(SqlPredicate)
22
+ unlearned_values(predicate).map
23
+ end
24
+
25
+ alias :find :values
26
+
27
+ def prepare_predicate(predicate)
28
+ return if predicate.nil?
29
+ case predicate
30
+ when String
31
+ SqlPredicate.new predicate
32
+ when Hash
33
+ query = predicate.map do |field, value|
34
+ cmp = '='
35
+ if value.is_a?(String)
36
+ value = "'" + value + "'"
37
+ cmp = 'LIKE' if value.index('%')
38
+ end
39
+ "#{field} #{cmp} #{value}"
40
+ end.join(' AND ')
41
+ SqlPredicate.new query
42
+ else
43
+ raise "Unknown predicate type"
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+
@@ -0,0 +1,9 @@
1
+ class Java::ComHazelcastClient::QueueClientProxy
2
+
3
+ alias_method :unlearned_poll, :poll
4
+
5
+ def poll(timeout = 5, unit = :seconds)
6
+ unlearned_poll timeout, java.util.concurrent.TimeUnit.const_get(unit.to_s.upcase)
7
+ end
8
+
9
+ end
@@ -0,0 +1,12 @@
1
+ class Java::ComHazelcastClient::TopicClientProxy
2
+
3
+ java_import 'com.hazelcast.core.MessageListener'
4
+
5
+ def on_message(&blk)
6
+ klass = Class.new
7
+ klass.send :include, MessageListener
8
+ klass.send :define_method, :on_message, &blk
9
+ add_message_listener klass.new
10
+ end
11
+
12
+ end
@@ -0,0 +1,38 @@
1
+ unless defined?(HELPER_LOADED)
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/hazelcast-client')
4
+ require 'test/unit'
5
+ require 'forwardable'
6
+ require 'date'
7
+
8
+ # Load the Hazelcast cluster
9
+ CLIENT = Hazelcast::Client.new 'dev', 'dev-pass', 'localhost'
10
+
11
+ # Grab notices
12
+ class Notices
13
+ class << self
14
+ extend Forwardable
15
+ def all
16
+ @all ||= []
17
+ end
18
+ def_delegators :all, :size, :<<, :first, :last, :clear, :map
19
+ end
20
+ end
21
+
22
+ # Listen on messages
23
+ class TestListener
24
+ def initialize(name)
25
+ @name = name
26
+ end
27
+ def on_message(msg)
28
+ Notices << "[#{@name}] #{msg}"
29
+ end
30
+ end
31
+
32
+ # Loading the Employee Java class
33
+ $CLASSPATH << File.expand_path(File.dirname(__FILE__)) + '/'
34
+ java_import 'Employee'
35
+
36
+ # Finished loading helpers
37
+ HELPER_LOADED = true
38
+ end
@@ -0,0 +1,49 @@
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 = CLIENT.map :test_same_object
9
+ map = CLIENT.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 = CLIENT.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 = CLIENT.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
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ class TestRubyHazeQueue < Test::Unit::TestCase
4
+
5
+ def test_single_queing
6
+ tasks = CLIENT.queue :test_single
7
+ qty = 50
8
+ qty.times { |idx| tasks.put [idx, Process.pid] }
9
+ found = []
10
+ while !tasks.empty? do
11
+ task = tasks.poll
12
+ found << task
13
+ end
14
+ assert !found.empty?
15
+ assert_equal found.size, qty
16
+ end
17
+
18
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper') unless defined?(HELPER_LOADED)
2
+
3
+ class TestRubyHazeTopic < Test::Unit::TestCase
4
+
5
+ def test_block_listener
6
+ Notices.clear
7
+ topic = CLIENT.topic :test_block
8
+ topic.on_message do |msg|
9
+ Notices << "#{msg}"
10
+ end
11
+ assert_equal Notices.size, 0
12
+ topic.publish "Hola!"
13
+ sleep 0.25
14
+ assert_equal Notices.size, 1
15
+ assert_equal Notices.last, "Hola!"
16
+ end
17
+
18
+ def test_class_listener
19
+ Notices.clear
20
+ topic = CLIENT.topic :test_class
21
+ topic.add_message_listener TestListener.new("test_class")
22
+ assert_equal Notices.size, 0
23
+ topic.publish "Hola!"
24
+ sleep 0.25
25
+ assert_equal Notices.size, 1
26
+ assert_equal Notices.last, "[test_class] Hola!"
27
+ end
28
+
29
+ def test_class2_listener
30
+ Notices.clear
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hazelcast-client
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: jruby
11
+ authors:
12
+ - Adrian Madrid
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-07 00:00:00 -07:00
18
+ default_executable: hazelcast-client-console
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hazelcast-jars
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 9
30
+ - 1
31
+ version: 1.9.1
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Hazelcast::Client is a little gem that wraps the Java Hazelcast Client library into a more comfortable JRuby package.
35
+ email:
36
+ - aemadrid@gmail.com
37
+ executables:
38
+ - hazelcast-client-console
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - VERSION
49
+ - bin/hazelcast-client-console
50
+ - lib/hazelcast-client.rb
51
+ - lib/hazelcast-client/map.rb
52
+ - lib/hazelcast-client/queue.rb
53
+ - lib/hazelcast-client/topic.rb
54
+ - test/helper.rb
55
+ - test/test_map.rb
56
+ - test/test_queue.rb
57
+ - test/test_topic.rb
58
+ has_rdoc: true
59
+ homepage: ""
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: hazelcast-client
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Connecting to a Hazelcast Cluster has never been easier!
88
+ test_files:
89
+ - test/test_map.rb
90
+ - test/test_queue.rb
91
+ - test/test_topic.rb