haj 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 219dc1783cac1e87594e1cb9c035ebe1a9ece7636a35511d4184aca468e9e3f0
4
- data.tar.gz: b560ac65c1cb1d7b262c9a73801038740edf4b0d3461166fbdfd9b5ae85ce8d0
3
+ metadata.gz: fed2d88ded1291360ede531814dc84cef8159984320b3e9823df64d06c16425d
4
+ data.tar.gz: fb05436a1d0f67219622d53bc0f22bad1d6f9e0f175d4cf90b283bc0d0bd06b3
5
5
  SHA512:
6
- metadata.gz: 509ec3899d10345067f6a54dde60aec8a837ca7fbb91f8a9990c97495fe862d3c7eed1ac3d39d447238090f3bec5abc9ede818cb10833ba405d79c1417b7ceff
7
- data.tar.gz: a42dcddc1b23cf7f0f2417f1cd835b023f9218a9a77a8ed5d824f5764b58a1989ceee4c07730abcf720cf2e7a4e375bd4f8eb0d4bed7f58a206649a915aaa21c
6
+ metadata.gz: 3831d13ef82ca2de322c2343ec1baf5460afa7e83374891bdf0ead1fa07268c6f43f386390a607f0dd6abf679f9eb403d9ad1e4ec86ca32a06b64ded714e0cd1
7
+ data.tar.gz: d1ba78111cae734bb23f41b13400ee864711fe6508bc2823697805fd19c5bbe7a0afe47abfb3483e8a95e3aff9d7419903e379369aa8cbfeadbcdce5fcfd03ee
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ RSpec::Core::RakeTask.new(:spec)
6
6
  require 'jars/installer'
7
7
 
8
8
  desc 'Download and vendor the jars needed'
9
- task :setup do
9
+ task :vendor_jars do
10
10
  Jars::Installer.vendor_jars!
11
11
  end
12
12
 
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'bundler/setup'
4
- # require 'haj'
4
+ require 'haj'
5
5
 
6
6
  require 'faker'
7
7
  require 'benchmark/ips'
@@ -16,7 +16,8 @@ Jdbc::Postgres.load_driver
16
16
  redis = Redis.new(host: 'redis')
17
17
  redis.flushall
18
18
 
19
- pool = JedisRb::Pool.new(host: 'redis')
19
+ jedis = JedisRb::Pool.new(host: 'redis')
20
+ haj = HAJ::Client.new(host: 'redis')
20
21
 
21
22
  conn = Sequel.connect('jdbc:postgresql://pg/hajrb', user: 'hajrb', password: 'hajrb')
22
23
 
@@ -48,16 +49,25 @@ Benchmark.ips do |x|
48
49
  conn.fetch("select v from things where k='#{random_keys[i]}'")
49
50
  end
50
51
  end
52
+
51
53
  x.report('Redis') do
52
54
  n.times do |i|
53
55
  redis.get(random_keys[i])
54
56
  end
55
57
  end
58
+
56
59
  x.report('Jedis') do
57
60
  n.times do |i|
58
- pool.execute(:get, random_keys[i].to_s)
61
+ jedis.execute(:get, random_keys[i].to_s)
59
62
  end
60
63
  end
64
+
65
+ x.report('HAJ') do
66
+ n.times do |i|
67
+ haj.get random_keys[i].to_s
68
+ end
69
+ end
70
+
61
71
  x.report('Ruby hash') do
62
72
  n.times do |i|
63
73
  memory[random_keys[i]]
data/lib/haj.rb CHANGED
@@ -1,10 +1,31 @@
1
+ # Standard library
1
2
  require 'singleton'
2
3
 
4
+ # All the Java jars that need to be loaded
3
5
  require 'haj_jars'
4
6
 
5
7
  require 'haj/version'
6
- require 'haj/pool'
8
+
9
+ # Application files
10
+ require 'haj/client'
11
+ require 'haj/pool/generic'
12
+ require 'haj/pool/config'
7
13
 
8
14
  module HAJ
15
+ # Make the Jedis Java classes a bit more accessible.
16
+ #
17
+ # Normally this might make inspection a bit weird because inspecting a
18
+ # `HAJ::Jedis::Client` object will actually return a
19
+ # `Java::RedisClientsJedis::Jedis` object.
20
+ #
21
+ # However, since we're obviously working with Jedis, this convention will
22
+ # probably look a lot better when reading the code so the previous point
23
+ # will be probably less annoying.
24
+ module Jedis
9
25
 
26
+ Client = Java::RedisClientsJedis::Jedis
27
+ # The Jedis default pool.
28
+ Pool = Java::RedisClientsJedis::JedisPool
29
+ Protocol = Java::RedisClientsJedis::Protocol
30
+ end
10
31
  end
@@ -0,0 +1,15 @@
1
+ module HAJ
2
+ class Client
3
+
4
+
5
+ # @param opts [Hash] Options to connect to the Redis database
6
+ def initialize(opts = {})
7
+ @pool = HAJ::Pool::Generic.new(opts)
8
+ end
9
+
10
+ def execute(method, *args)
11
+ @pool.hold { |conn| conn.send(method, *args) }
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,39 @@
1
+ module HAJ
2
+ module Pool
3
+ class Config < Java::org.apache.commons.pool2.impl.GenericObjectPoolConfig
4
+
5
+ # Default values taken from the Jedis PoolConfig
6
+ DEFAULT_TESTS_WHILE_IDLE = true
7
+ DEFAULT_MIN_EVICTABLE_IDLE_TIME = 60_000
8
+ DEFAULT_TIME_BETWEEN_EVICTIONS_RUNS = 30_000
9
+ DEFAULT_TESTS_PER_EVICTION_RUN = -1
10
+
11
+ # Supplemental default values that allow more fine-tuned configuration
12
+ # of the connection pool
13
+ DEFAULT_MAX_TOTAL = 8
14
+ DEFAULT_MAX_IDLE = 8
15
+
16
+ def initialize(options = {})
17
+ # Make sure that we're not forwarding the arguments to the parent class
18
+ # as there are no constructors defined that accept a Hash/java.util.Map
19
+ # as an argument
20
+ super()
21
+
22
+ self.max_total = options.fetch(:max_total) { DEFAULT_MAX_TOTAL }
23
+ self.max_idle = options.fetch(:max_idle) { DEFAULT_MAX_IDLE }
24
+
25
+ self.test_while_idle =
26
+ options.fetch(:test_while_idle) { DEFAULT_TESTS_WHILE_IDLE }
27
+
28
+ self.min_evictable_idle_time_millis =
29
+ options.fetch(:min_evictable_idle_time) { DEFAULT_MIN_EVICTABLE_IDLE_TIME }
30
+
31
+ self.time_between_eviction_runs_millis =
32
+ options.fetch(:time_between_eviction_runs) { DEFAULT_TIME_BETWEEN_EVICTIONS_RUNS }
33
+
34
+ self.num_tests_per_eviction_run =
35
+ options.fetch(:tests_per_eviction_run) { DEFAULT_TESTS_PER_EVICTION_RUN }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,44 @@
1
+ module HAJ
2
+
3
+ module Pool
4
+ # Class to provide a POC for better pooling.
5
+ class Generic
6
+
7
+ DEFAULT_HOST = 'localhost'.freeze
8
+ DEFAULT_PORT = 6379
9
+ DEFAULT_TIMEOUT = 2000
10
+ DEFAULT_DATABASE = 0
11
+
12
+ def initialize(opts = {})
13
+ config = HAJ::Pool::Config.new(opts)
14
+
15
+ host = opts.fetch(:host) { DEFAULT_HOST }
16
+ port = opts.fetch(:port) { DEFAULT_PORT }
17
+ timeout = opts.fetch(:timeout) { DEFAULT_TIMEOUT }
18
+ password = opts.fetch(:password) { nil }
19
+ database = opts.fetch(:database) { DEFAULT_DATABASE }
20
+ name = opts.fetch(:name) { nil }
21
+
22
+ @inner_pool = HAJ::Jedis::Pool.new(
23
+ config,
24
+ host,
25
+ port,
26
+ timeout,
27
+ password,
28
+ database,
29
+ name
30
+ )
31
+ end
32
+
33
+ def hold
34
+ connection = @inner_pool.resource
35
+
36
+ yield connection
37
+ ensure
38
+ connection.close if connection
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module HAJ
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Maxim
@@ -96,16 +96,17 @@ files:
96
96
  - LICENSE.txt
97
97
  - README.md
98
98
  - Rakefile
99
- - bin/benchmark
99
+ - bin/bench
100
100
  - bin/console
101
101
  - bin/rake
102
102
  - bin/rspec
103
- - bin/setup
104
103
  - data/redis/.gitkeep
105
104
  - docker-compose.yml
106
105
  - haj.gemspec
107
106
  - lib/haj.rb
108
- - lib/haj/pool.rb
107
+ - lib/haj/client.rb
108
+ - lib/haj/pool/config.rb
109
+ - lib/haj/pool/generic.rb
109
110
  - lib/haj/version.rb
110
111
  - lib/haj_jars.rb
111
112
  - lib/org/apache/commons/commons-pool2/2.4.2/commons-pool2-2.4.2.jar
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,6 +0,0 @@
1
- module HAJ
2
- # Class to provide a POC for better pooling.
3
- class Pool
4
-
5
- end
6
- end