Skip to content

Commit

Permalink
minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Nov 23, 2009
1 parent c0576bf commit 3e84520
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/main/com/mongodb/Bytes.java
Expand Up @@ -243,6 +243,7 @@ private static void _warnUnsupported( String flag ) {
}

public static void addEncodingHook( Class c , Transformer t ){
_anyHooks = true;
List<Transformer> l = _encodingHooks.get( c );
if ( l == null ){
l = new Vector<Transformer>();
Expand All @@ -252,6 +253,7 @@ public static void addEncodingHook( Class c , Transformer t ){
}

public static void addDecodingHook( byte type , Transformer t ){
_anyHooks = true;
List<Transformer> l = _decodingHooks.get( type );
if ( l == null ){
l = new Vector<Transformer>();
Expand All @@ -261,6 +263,9 @@ public static void addDecodingHook( byte type , Transformer t ){
}

public static Object applyEncodingHooks( Object o ){
if ( ! _anyHooks )
return o;

if ( _encodingHooks.size() == 0 || o == null )
return o;
List<Transformer> l = _encodingHooks.get( o.getClass() );
Expand All @@ -271,6 +276,9 @@ public static Object applyEncodingHooks( Object o ){
}

public static Object applyDecodingHooks( byte b , Object o ){
if ( ! _anyHooks )
return o;

List<Transformer> l = _decodingHooks.get( b );
if ( l != null )
for ( Transformer t : l )
Expand All @@ -280,10 +288,12 @@ public static Object applyDecodingHooks( byte b , Object o ){


public static void clearAllHooks(){
_anyHooks = false;
_encodingHooks.clear();
_decodingHooks.clear();
}


private static boolean _anyHooks = false;
static Map<Class,List<Transformer>> _encodingHooks = Collections.synchronizedMap( new HashMap<Class,List<Transformer>>() );
static Map<Byte,List<Transformer>> _decodingHooks = Collections.synchronizedMap( new HashMap<Byte,List<Transformer>>() );

Expand Down
8 changes: 6 additions & 2 deletions src/main/com/mongodb/DBApiLayer.java
Expand Up @@ -40,6 +40,7 @@ protected DBApiLayer( String root ){
super( root );

_root = root;
_rootPlusDot = _root + ".";
}

protected abstract void doInsert( ByteBuffer buf , WriteConcern concern ) throws MongoException;
Expand Down Expand Up @@ -69,7 +70,7 @@ protected MyCollection doGetCollection( String name ){
}

String _removeRoot( String ns ){
if ( ! ns.startsWith( _root + "." ) )
if ( ! ns.startsWith( _rootPlusDot ) )
return ns;
return ns.substring( _root.length() + 1 );
}
Expand Down Expand Up @@ -419,6 +420,7 @@ class SingleResult extends QueryHeader {

_bytes = decoder.remaining();
_fullNameSpace = fullNameSpace;
_shortNameSpace = _removeRoot( _fullNameSpace );
skipPastHeader( decoder._buf );

if ( _num == 0 )
Expand All @@ -434,7 +436,7 @@ else if ( _num < 3 )
while( decoder.more() && num < _num ){
final DBObject o = decoder.readObject();

o.put( "_ns" , _removeRoot( _fullNameSpace ) );
o.put( "_ns" , _shortNameSpace );
_lst.add( o );
num++;

Expand All @@ -457,6 +459,7 @@ public String toString(){

final long _bytes;
final String _fullNameSpace;
final String _shortNameSpace;

final List<DBObject> _lst;
}
Expand Down Expand Up @@ -567,6 +570,7 @@ List<Integer> getSizes(){
} // class Result

final String _root;
final String _rootPlusDot;
final Map<String,MyCollection> _collections = Collections.synchronizedMap( new HashMap<String,MyCollection>() );
final Map<String,DBApiLayer> _sisters = Collections.synchronizedMap( new HashMap<String,DBApiLayer>() );
List<Long> _deadCursorIds = new Vector<Long>();
Expand Down

0 comments on commit 3e84520

Please sign in to comment.