thread_safe 0.2.0-java → 0.3.1-java

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
  SHA1:
3
- metadata.gz: 6ec4092b3c27528c0451830f13a0f1331b3d2073
4
- data.tar.gz: 5e8f84e18a076856ce0d666f24c27060b6173124
3
+ metadata.gz: bd78f6716a19a528d4f244f47944740468398978
4
+ data.tar.gz: 92d44fadb74cb932282e682967b11c532ad7a678
5
5
  SHA512:
6
- metadata.gz: 8d3761a208f3e25afadcf16182b03a296a119c4bf74b5b9db304237c9486c69f4f0df8bd385b3ee30355daf96405a4a6986e099c3e1c5394c7e236428a0a7520
7
- data.tar.gz: aa4cc676aaa4c66290a454aecec9c57c23fac7be246bb1a42210792ba4f30dd81a6c4e2b91d3f547d923f0dd42d470714cd9f7ae73564c5123069028e3013891
6
+ metadata.gz: 50cd42d6dfaa8f703d9fe3034150b3012317d1af47690c88a1c906196760ccd3eafd4afa1f925c946a8842f026cb7ccec00056fdb3bcaff3ed00657fa9db8508
7
+ data.tar.gz: dddfe11774caf6f080292540c11cf795d9e8867d6917da1e2ff9d907fbb9e2eafa3b47ca6e9dcf5e07f2498c53bf238c6771df9e79540f310ae6ac3ef30bbd19
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ test/version_tmp
19
19
  tmp
20
20
  .DS_Store
21
21
  *.swp
22
+ test/package.jar
data/README.md CHANGED
@@ -28,14 +28,22 @@ sh = ThreadSafe::Hash.new # supports standard Hash.new forms
28
28
  ```
29
29
 
30
30
  `ThreadSafe::Cache` also exists, as a hash-like object, and should have
31
- much better performance characteristics under concurrency than
31
+ much better performance characteristics esp. under high concurrency than
32
32
  `ThreadSafe::Hash`. However, `ThreadSafe::Cache` is not strictly semantically
33
- equivalent to ruby Hash -- for instance, it does not neccesarily ordered by
34
- insertion time as Hash is. For most uses it should do fine though, and we
35
- recommend you consider `ThreadSafe::Cache` instead of `ThreadSafe::Hash` for your
36
- concurrency-safe hash needs.
33
+ equivalent to a ruby `Hash` -- for instance, it does not necessarily retain
34
+ ordering by insertion time as `Hash` does. For most uses it should do fine
35
+ though, and we recommend you consider `ThreadSafe::Cache` instead of
36
+ `ThreadSafe::Hash` for your concurrency-safe hash needs. It understands some
37
+ options when created (depending on your ruby platform) that control some of the
38
+ internals - when unsure just leave them out:
37
39
 
38
40
 
41
+ ```ruby
42
+ require 'thread_safe'
43
+
44
+ cache = ThreadSafe::Cache.new
45
+ ```
46
+
39
47
  ## Contributing
40
48
 
41
49
  1. Fork it
@@ -178,11 +178,17 @@ public class JRubyCacheBackendLibrary implements Library {
178
178
  return getRuntime().newBoolean(map.replace(key, oldValue, newValue));
179
179
  }
180
180
 
181
- @JRubyMethod(name = {"key?"}, required = 1)
181
+ @JRubyMethod(name = "key?", required = 1)
182
182
  public RubyBoolean has_key_p(IRubyObject key) {
183
183
  return map.containsKey(key) ? getRuntime().getTrue() : getRuntime().getFalse();
184
184
  }
185
185
 
186
+ @JRubyMethod
187
+ public IRubyObject key(IRubyObject value) {
188
+ final IRubyObject key = map.findKey(value);
189
+ return key == null ? getRuntime().getNil() : key;
190
+ }
191
+
186
192
  @JRubyMethod
187
193
  public IRubyObject replace_if_exists(IRubyObject key, IRubyObject value) {
188
194
  IRubyObject result = map.replace(key, value);
@@ -25,4 +25,7 @@ public interface ConcurrentHashMap<K, V> {
25
25
  public Set<Map.Entry<K,V>> entrySet();
26
26
  public int size();
27
27
  public V getValueOrDefault(Object key, V defaultValue);
28
+
29
+ public boolean containsValue(V value);
30
+ public K findKey(V value);
28
31
  }
@@ -2430,8 +2430,8 @@ public class ConcurrentHashMapV8<K, V>
2430
2430
  @SuppressWarnings("serial") static class Traverser<K,V,R> {
2431
2431
  final ConcurrentHashMapV8<K, V> map;
2432
2432
  Node next; // the next entry to use
2433
- Object nextKey; // cached key field of next
2434
- Object nextVal; // cached val field of next
2433
+ K nextKey; // cached key field of next
2434
+ V nextVal; // cached val field of next
2435
2435
  Node[] tab; // current table; updated if resized
2436
2436
  int index; // index of bin to use next
2437
2437
  int baseIndex; // current index of initial table
@@ -2461,9 +2461,9 @@ public class ConcurrentHashMapV8<K, V>
2461
2461
  * Advances next; returns nextVal or null if terminated.
2462
2462
  * See above for explanation.
2463
2463
  */
2464
- final Object advance() {
2464
+ final V advance() {
2465
2465
  Node e = next;
2466
- Object ev = null;
2466
+ V ev = null;
2467
2467
  outer: do {
2468
2468
  if (e != null) // advance past used/skipped node
2469
2469
  e = e.next;
@@ -2489,8 +2489,8 @@ public class ConcurrentHashMapV8<K, V>
2489
2489
  } // visit upper slots if present
2490
2490
  index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2491
2491
  }
2492
- nextKey = e.key;
2493
- } while ((ev = e.val) == null); // skip deleted or special nodes
2492
+ nextKey = (K) e.key;
2493
+ } while ((ev = (V) e.val) == null); // skip deleted or special nodes
2494
2494
  next = e;
2495
2495
  return nextVal = ev;
2496
2496
  }
@@ -2730,6 +2730,18 @@ public class ConcurrentHashMapV8<K, V>
2730
2730
  return false;
2731
2731
  }
2732
2732
 
2733
+ public K findKey(Object value) {
2734
+ if (value == null)
2735
+ throw new NullPointerException();
2736
+ Object v;
2737
+ Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2738
+ while ((v = it.advance()) != null) {
2739
+ if (v == value || value.equals(v))
2740
+ return it.nextKey;
2741
+ }
2742
+ return null;
2743
+ }
2744
+
2733
2745
  /**
2734
2746
  * Legacy method testing if some key maps into the specified value
2735
2747
  * in this table. This method is identical in functionality to
@@ -2422,8 +2422,8 @@ public class ConcurrentHashMapV8<K, V>
2422
2422
  @SuppressWarnings("serial") static class Traverser<K,V,R> {
2423
2423
  final ConcurrentHashMapV8<K, V> map;
2424
2424
  Node next; // the next entry to use
2425
- Object nextKey; // cached key field of next
2426
- Object nextVal; // cached val field of next
2425
+ K nextKey; // cached key field of next
2426
+ V nextVal; // cached val field of next
2427
2427
  AtomicReferenceArray<Node> tab; // current table; updated if resized
2428
2428
  int index; // index of bin to use next
2429
2429
  int baseIndex; // current index of initial table
@@ -2453,9 +2453,9 @@ public class ConcurrentHashMapV8<K, V>
2453
2453
  * Advances next; returns nextVal or null if terminated.
2454
2454
  * See above for explanation.
2455
2455
  */
2456
- final Object advance() {
2456
+ final V advance() {
2457
2457
  Node e = next;
2458
- Object ev = null;
2458
+ V ev = null;
2459
2459
  outer: do {
2460
2460
  if (e != null) // advance past used/skipped node
2461
2461
  e = e.next;
@@ -2481,8 +2481,8 @@ public class ConcurrentHashMapV8<K, V>
2481
2481
  } // visit upper slots if present
2482
2482
  index = (i += baseSize) < n ? i : (baseIndex = b + 1);
2483
2483
  }
2484
- nextKey = e.key;
2485
- } while ((ev = e.val) == null); // skip deleted or special nodes
2484
+ nextKey = (K) e.key;
2485
+ } while ((ev = (V) e.val) == null); // skip deleted or special nodes
2486
2486
  next = e;
2487
2487
  return nextVal = ev;
2488
2488
  }
@@ -2722,6 +2722,18 @@ public class ConcurrentHashMapV8<K, V>
2722
2722
  return false;
2723
2723
  }
2724
2724
 
2725
+ public K findKey(Object value) {
2726
+ if (value == null)
2727
+ throw new NullPointerException();
2728
+ Object v;
2729
+ Traverser<K,V,Object> it = new Traverser<K,V,Object>(this);
2730
+ while ((v = it.advance()) != null) {
2731
+ if (v == value || value.equals(v))
2732
+ return it.nextKey;
2733
+ }
2734
+ return null;
2735
+ }
2736
+
2725
2737
  /**
2726
2738
  * Legacy method testing if some key maps into the specified value
2727
2739
  * in this table. This method is identical in functionality to
@@ -95,6 +95,12 @@ module ThreadSafe
95
95
  each_pair {|k, v| yield v}
96
96
  end unless method_defined?(:each_value)
97
97
 
98
+ def key(value)
99
+ each_pair {|k, v| return k if v == value}
100
+ nil
101
+ end unless method_defined?(:key)
102
+ alias_method :index, :key if RUBY_VERSION < '1.9'
103
+
98
104
  def empty?
99
105
  each_pair {|k, v| return false}
100
106
  true
@@ -1,3 +1,21 @@
1
+ module ThreadSafe
2
+ VERSION = "0.3.1"
3
+ end
4
+
5
+ # NOTE: <= 0.2.0 used Threadsafe::VERSION
6
+ # @private
1
7
  module Threadsafe
2
- VERSION = "0.2.0"
8
+
9
+ # @private
10
+ def self.const_missing(name)
11
+ name = name.to_sym
12
+ if ThreadSafe.const_defined?(name)
13
+ warn "[DEPRECATION] `Threadsafe::#{name}' is deprecated, use `ThreadSafe::#{name}' instead."
14
+ ThreadSafe.const_get(name)
15
+ else
16
+ warn "[DEPRECATION] the `Threadsafe' module is deprecated, please use `ThreadSafe` instead."
17
+ super
18
+ end
19
+ end
20
+
3
21
  end
@@ -371,6 +371,16 @@ class TestCache < Test::Unit::TestCase
371
371
  end
372
372
 
373
373
  def test_key
374
+ with_or_without_default_proc do
375
+ assert_equal nil, @cache.key(1)
376
+ @cache[:a] = 1
377
+ assert_equal :a, @cache.key(1)
378
+ assert_equal nil, @cache.key(0)
379
+ assert_equal :a, @cache.index(1) if RUBY_VERSION =~ /1\.8/
380
+ end
381
+ end
382
+
383
+ def test_key?
374
384
  with_or_without_default_proc do
375
385
  assert_equal false, @cache.key?(:a)
376
386
  @cache[:a] = 1
@@ -378,7 +388,7 @@ class TestCache < Test::Unit::TestCase
378
388
  end
379
389
  end
380
390
 
381
- def test_value
391
+ def test_value?
382
392
  with_or_without_default_proc do
383
393
  assert_equal false, @cache.value?(1)
384
394
  @cache[:a] = 1
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.name = "thread_safe"
18
18
  gem.require_paths = ["lib"]
19
- gem.version = Threadsafe::VERSION
19
+ gem.version = ThreadSafe::VERSION
20
20
  gem.license = "Apache-2.0"
21
21
 
22
22
  gem.add_dependency 'atomic', ['>= 1.1.7', '< 2']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thread_safe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: java
6
6
  authors:
7
7
  - Charles Oliver Nutter
@@ -9,15 +9,15 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-26 00:00:00.000000000 Z
12
+ date: 2014-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 1.1.7
20
- - - "<"
20
+ - - <
21
21
  - !ruby/object:Gem::Version
22
22
  version: '2'
23
23
  name: atomic
@@ -25,16 +25,16 @@ dependencies:
25
25
  type: :runtime
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
- - - ">="
28
+ - - '>='
29
29
  - !ruby/object:Gem::Version
30
30
  version: 1.1.7
31
- - - "<"
31
+ - - <
32
32
  - !ruby/object:Gem::Version
33
33
  version: '2'
34
34
  - !ruby/object:Gem::Dependency
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ">="
37
+ - - '>='
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  name: rake
@@ -42,7 +42,7 @@ dependencies:
42
42
  type: :development
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  description: Thread-safe collections and utilities for Ruby
@@ -53,8 +53,8 @@ executables: []
53
53
  extensions: []
54
54
  extra_rdoc_files: []
55
55
  files:
56
- - ".gitignore"
57
- - ".travis.yml"
56
+ - .gitignore
57
+ - .travis.yml
58
58
  - Gemfile
59
59
  - LICENSE
60
60
  - README.md
@@ -73,7 +73,6 @@ files:
73
73
  - lib/thread_safe.rb
74
74
  - lib/thread_safe/atomic_reference_cache_backend.rb
75
75
  - lib/thread_safe/cache.rb
76
- - lib/thread_safe/jruby_cache_backend.jar
77
76
  - lib/thread_safe/mri_cache_backend.rb
78
77
  - lib/thread_safe/non_concurrent_cache_backend.rb
79
78
  - lib/thread_safe/synchronized_cache_backend.rb
@@ -96,6 +95,7 @@ files:
96
95
  - test/test_helper.rb
97
96
  - test/test_synchronized_delegator.rb
98
97
  - thread_safe.gemspec
98
+ - lib/thread_safe/jruby_cache_backend.jar
99
99
  homepage: https://github.com/headius/thread_safe
100
100
  licenses:
101
101
  - Apache-2.0
@@ -106,17 +106,17 @@ require_paths:
106
106
  - lib
107
107
  required_ruby_version: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - ">="
109
+ - - '>='
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  requirements:
114
- - - ">="
114
+ - - '>='
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
117
  requirements: []
118
118
  rubyforge_project:
119
- rubygems_version: 2.2.2
119
+ rubygems_version: 2.1.9
120
120
  signing_key:
121
121
  specification_version: 4
122
122
  summary: A collection of data structures and utilities to make thread-safe programming in Ruby easier