Skip to content

Commit 7debdee

Browse files
committed
RUBY-554 disable caching in java extension
1 parent 387686f commit 7debdee

File tree

4 files changed

+23
-106
lines changed

4 files changed

+23
-106
lines changed

ext/jbson/src/main/jbson/RubyBSONCallback.java

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@ public class RubyBSONCallback implements BSONCallback {
2929
private final LinkedList<RubyObject> _stack = new LinkedList<RubyObject>();
3030
private final LinkedList<String> _nameStack = new LinkedList<String>();
3131
private Ruby _runtime;
32-
private static final HashMap<Ruby, HashMap> _runtimeCache = new HashMap<Ruby, HashMap>();
3332

3433
public RubyBSONCallback(Ruby runtime) {
3534
_runtime = runtime;
36-
_rbclsOrderedHash = _lookupConstant( _runtime, "BSON::OrderedHash" );
37-
_rbclsBinary = _lookupConstant( _runtime, "BSON::Binary" );
38-
_rbclsCode = _lookupConstant( _runtime, "BSON::Code" );
39-
_rbclsMinKey = _lookupConstant( _runtime, "BSON::MinKey" );
40-
_rbclsMaxKey = _lookupConstant( _runtime, "BSON::MaxKey" );
41-
_rbclsTimestamp = _lookupConstant( _runtime, "BSON::Timestamp" );
42-
_rbclsObjectId = _lookupConstant( _runtime, "BSON::ObjectId");
35+
_rbclsOrderedHash = runtime.getClassFromPath( "BSON::OrderedHash" );
36+
_rbclsBinary = runtime.getClassFromPath( "BSON::Binary" );
37+
_rbclsCode = runtime.getClassFromPath( "BSON::Code" );
38+
_rbclsMinKey = runtime.getClassFromPath( "BSON::MinKey" );
39+
_rbclsMaxKey = runtime.getClassFromPath( "BSON::MaxKey" );
40+
_rbclsTimestamp = runtime.getClassFromPath( "BSON::Timestamp" );
41+
_rbclsObjectId = runtime.getClassFromPath( "BSON::ObjectId" );
4342
}
4443

4544
public BSONCallback createBSONCallback(){
@@ -319,33 +318,4 @@ protected void setRoot(RubyHash o) {
319318
protected boolean isStackEmpty() {
320319
return _stack.size() < 1;
321320
}
322-
323-
private static HashMap<String, Object> _getRuntimeCache(final Ruby runtime) {
324-
@SuppressWarnings("unchecked")
325-
HashMap<String, Object> cache = _runtimeCache.get( runtime );
326-
327-
if(cache == null) {
328-
cache = new HashMap<String, Object>();
329-
_runtimeCache.put( runtime, cache );
330-
runtime.addFinalizer( new Finalizable() {
331-
public void finalize() {
332-
_runtimeCache.remove(runtime);
333-
}
334-
});
335-
}
336-
337-
return cache;
338-
}
339-
340-
private static RubyModule _lookupConstant(Ruby runtime, String name)
341-
{
342-
HashMap<String, Object> cache = _getRuntimeCache( runtime );
343-
RubyModule module = (RubyModule) cache.get( name );
344-
345-
if(module == null && !cache.containsKey( name )) {
346-
module = runtime.getClassFromPath( name );
347-
cache.put( (String)name, (Object)module );
348-
}
349-
return module;
350-
}
351321
}

ext/jbson/src/main/jbson/RubyBSONEncoder.java

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
public class RubyBSONEncoder extends BasicBSONEncoder {
3131

3232
private static final boolean DEBUG = false;
33-
private static final Map _runtimeCache = new HashMap();
3433

3534
private Ruby _runtime;
3635

@@ -58,12 +57,12 @@ public RubyBSONEncoder(Ruby runtime, boolean check_keys, boolean move_id, int ma
5857
_check_keys = check_keys;
5958
_move_id = move_id;
6059
_runtime = runtime;
61-
_rbclsByteBuffer = _lookupConstant( _runtime, "BSON::ByteBuffer" );
62-
_rbclsInvalidDocument = _lookupConstant( _runtime, "BSON::InvalidDocument" );
63-
_rbclsInvalidKeyName = _lookupConstant( _runtime, "BSON::InvalidKeyName" );
64-
_rbclsRangeError = _lookupConstant( _runtime, "RangeError" );
65-
_idAsSym = _lookupSymbol( _runtime, "_id" );
66-
_tfAsString = _lookupString( _runtime, "_transientFields" );
60+
_rbclsByteBuffer = runtime.getClassFromPath( "BSON::ByteBuffer" );
61+
_rbclsInvalidDocument = runtime.getClassFromPath( "BSON::InvalidDocument" );
62+
_rbclsInvalidKeyName = runtime.getClassFromPath( "BSON::InvalidKeyName" );
63+
_rbclsRangeError = runtime.getClassFromPath( "RangeError" );
64+
_idAsSym = runtime.newSymbol( "_id" );
65+
_tfAsString = runtime.newString( "_transientFields" );
6766

6867
if(_idAsString == null) {
6968
_idAsString = _runtime.newString( "_id" );
@@ -762,57 +761,4 @@ public void writeCString( String s ){
762761
private CharBuffer _stringC = CharBuffer.wrap( new char[256 + 1] );
763762
private ByteBuffer _stringB = ByteBuffer.wrap( new byte[1024 + 1] );
764763
private CharsetEncoder _encoder = Charset.forName( "UTF-8" ).newEncoder();
765-
766-
private static Map _getRuntimeCache(final Ruby runtime) {
767-
Map cache = (Map) _runtimeCache.get(runtime);
768-
769-
if(cache == null) {
770-
cache = new HashMap();
771-
_runtimeCache.put(runtime, cache);
772-
runtime.addFinalizer( new Finalizable() {
773-
public void finalize() {
774-
_runtimeCache.remove( runtime );
775-
}
776-
});
777-
}
778-
779-
return cache;
780-
}
781-
782-
private static RubyModule _lookupConstant(Ruby runtime, String name)
783-
{
784-
Map cache = _getRuntimeCache( runtime );
785-
RubyModule module = (RubyModule) cache.get( name );
786-
787-
if(module == null && !cache.containsKey( name )) {
788-
module = runtime.getClassFromPath( name );
789-
cache.put( name, module );
790-
}
791-
return module;
792-
}
793-
794-
private static RubySymbol _lookupSymbol(Ruby runtime, String name)
795-
{
796-
Map cache = _getRuntimeCache( runtime );
797-
RubySymbol symbol = (RubySymbol) cache.get( name );
798-
799-
if(symbol == null && !cache.containsKey( name )) {
800-
symbol = runtime.newSymbol( name );
801-
cache.put( name, symbol );
802-
}
803-
return symbol;
804-
}
805-
806-
private static RubyString _lookupString(Ruby runtime, String name)
807-
{
808-
Map cache = _getRuntimeCache( runtime );
809-
RubyString string = (RubyString) cache.get( name );
810-
811-
if(string == null && !cache.containsKey( name )) {
812-
string = runtime.newString( name );
813-
cache.put( name, string );
814-
}
815-
return string;
816-
}
817-
818764
}

ext/jbson/target/jbson.jar

-1.04 KB
Binary file not shown.

test/threading/basic_test.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class TestThreading < Test::Unit::TestCase
55
include Mongo
66

77
def setup
8-
@client = standard_connection(:pool_size => 50, :pool_timeout => 60)
8+
@client = standard_connection(:pool_size => 10, :pool_timeout => 30)
99
@db = @client.db(MONGO_TEST_DB)
1010
@coll = @db.collection('thread-test-collection')
1111
@coll.drop
@@ -58,22 +58,23 @@ def test_safe_insert
5858
threads.each {|thread| thread.join}
5959
end
6060

61-
def test_count
61+
def test_concurrent_find
62+
n_threads = 50
63+
6264
1000.times do |i|
63-
@coll.insert({ "x" => i })
65+
@coll.insert({ "x" => "a" })
6466
end
6567

6668
threads = []
67-
10.times do |i|
69+
n_threads.times do |i|
6870
threads << Thread.new do
6971
sum = 0
70-
@coll.find().each do |document|
71-
sum += document["x"]
72-
end
73-
assert_equal 499500, sum
72+
@coll.find.to_a.size
7473
end
7574
end
7675

77-
threads.each {|thread| thread.join}
76+
thread_values = threads.map(&:value)
77+
assert thread_values.all?{|v| v == 1000}
78+
assert_equal thread_values.size, n_threads
7879
end
7980
end

0 commit comments

Comments
 (0)