Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated based on feedback on pull request.

  • Loading branch information...
commit 1ceff1dce20fce3e04b75964dbf100576d9c255c 1 parent c033bde
@thobe authored
View
71 kernel/src/main/java/org/neo4j/helpers/TimeUtil.java
@@ -0,0 +1,71 @@
+/**
+ * Copyright (c) 2002-2012 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.neo4j.helpers;
+
+import java.util.concurrent.TimeUnit;
+
+public abstract class TimeUtil
+{
+ private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS;
+
+ public static long parseTimeMillis( String timeWithOrWithoutUnit )
+ {
+ int unitIndex = -1;
+ for ( int i = 0; i < timeWithOrWithoutUnit.length(); i++ )
+ {
+ char ch = timeWithOrWithoutUnit.charAt( i );
+ if ( !Character.isDigit( ch ) )
+ {
+ unitIndex = i;
+ break;
+ }
+ }
+ if ( unitIndex == -1 )
+ {
+ return DEFAULT_TIME_UNIT.toMillis( Integer.parseInt( timeWithOrWithoutUnit ) );
+ }
+ else
+ {
+ int amount = Integer.parseInt( timeWithOrWithoutUnit.substring( 0, unitIndex ) );
+ String unit = timeWithOrWithoutUnit.substring( unitIndex ).toLowerCase();
+ TimeUnit timeUnit = null;
+ int multiplyFactor = 1;
+ if ( unit.equals( "ms" ) )
+ {
+ timeUnit = TimeUnit.MILLISECONDS;
+ }
+ else if ( unit.equals( "s" ) )
+ {
+ timeUnit = TimeUnit.SECONDS;
+ }
+ else if ( unit.equals( "m" ) )
+ {
+ // This is only for having to rely on 1.6
+ timeUnit = TimeUnit.SECONDS;
+ multiplyFactor = 60;
+ }
+ else
+ {
+ throw new RuntimeException( "Unrecognized unit " + unit );
+ }
+ return timeUnit.toMillis( amount*multiplyFactor );
+ }
+ }
+}
View
1  kernel/src/main/java/org/neo4j/kernel/Config.java
@@ -168,6 +168,7 @@
/**
* The minimal time that must pass in between logging statistics from the cache (when using the 'array' cache).
+ * Default unit is seconds, suffix with 's', 'm', or 'ms' to have the unit be seconds, minutes or milliseconds respectively.
*/
@Documented
public static final String ARRAY_CACHE_MIN_LOG_INTERVAL = "array_cache_min_log_interval";
View
24 kernel/src/main/java/org/neo4j/kernel/impl/cache/AtomicArrayCache.java
@@ -43,14 +43,14 @@
private long collisions = 0;
private long purgeCount = 0;
- private StringLogger logger;
+ private final StringLogger logger;
- public AtomicArrayCache( long maxSizeInBytes, int arrayHeapFraction )
+ public AtomicArrayCache( long maxSizeInBytes, float arrayHeapFraction )
{
- this( maxSizeInBytes, arrayHeapFraction, 5000, null, null );
+ this( maxSizeInBytes, arrayHeapFraction, 5000, null, StringLogger.SYSTEM );
}
- public AtomicArrayCache( long maxSizeInBytes, int arrayHeapFraction, long minLogInterval, String name, StringLogger logger )
+ public AtomicArrayCache( long maxSizeInBytes, float arrayHeapFraction, long minLogInterval, String name, StringLogger logger )
{
this.minLogInterval = minLogInterval;
if ( arrayHeapFraction < 1 || arrayHeapFraction > 10 )
@@ -59,8 +59,7 @@ public AtomicArrayCache( long maxSizeInBytes, int arrayHeapFraction, long minLog
"The heap fraction used by an array cache must be between 1% and 10%, not "
+ arrayHeapFraction + "%" );
}
- logger = null;
- long memToUse = arrayHeapFraction * Runtime.getRuntime().maxMemory() / 100;
+ long memToUse = (long)(((double)arrayHeapFraction) * Runtime.getRuntime().maxMemory() / 100);
long maxElementCount = (int) ( memToUse / 8 );
if ( memToUse > Integer.MAX_VALUE )
{
@@ -74,7 +73,7 @@ public AtomicArrayCache( long maxSizeInBytes, int arrayHeapFraction, long minLog
this.cache = new AtomicReferenceArray<E>( (int) maxElementCount );
this.maxSize = maxSizeInBytes;
this.name = name == null ? super.toString() : name;
- this.logger = logger;
+ this.logger = logger == null ? StringLogger.SYSTEM : logger;
}
private int getPosition( EntityWithSize obj )
@@ -212,22 +211,15 @@ private synchronized void purgeFrom( int pos )
String missPercentage = ((float) missCount / (float) (hitCount+missCount) * 100.0f) + "%";
String colPercentage = ((float) collisions / (float) totalPuts * 100.0f) + "%";
- logger().logMessage( " purge (nr " + purgeCount + ") " + sizeBeforeStr + " -> " + sizeAfterStr + " (" + diffStr +
+ logger.logMessage( " purge (nr " + purgeCount + ") " + sizeBeforeStr + " -> " + sizeAfterStr + " (" + diffStr +
") " + missPercentage + " misses, " + colPercentage + " collisions.", true );
}
}
}
- private StringLogger logger()
- {
- StringLogger logger = this.logger;
- if ( logger == null ) logger = StringLogger.SYSTEM;
- return logger;
- }
-
public void printStatistics()
{
- logStatistics( logger() );
+ logStatistics( logger );
}
@Override
View
13 kernel/src/main/java/org/neo4j/kernel/impl/core/NodeManager.java
@@ -41,6 +41,7 @@
import org.neo4j.graphdb.event.TransactionData;
import org.neo4j.graphdb.index.Index;
import org.neo4j.helpers.Pair;
+import org.neo4j.helpers.TimeUtil;
import org.neo4j.helpers.Triplet;
import org.neo4j.helpers.collection.PrefetchingIterator;
import org.neo4j.kernel.Config;
@@ -77,8 +78,6 @@
private final GraphDatabaseService graphDbService;
private final Cache<NodeImpl> nodeCache;
private final Cache<RelationshipImpl> relCache;
-// private final AtomicArrayCache<NodeImpl> nodeCache;
-// private final AtomicArrayCache<RelationshipImpl> relCache;
private final CacheType cacheType;
@@ -1322,9 +1321,9 @@ private long minLogInterval( Map<Object, Object> params )
long result = 60000; // Default: a minute
try
{
- if ( interval != null ) result = Long.parseLong( interval );
+ if ( interval != null ) result = TimeUtil.parseTimeMillis( interval );
}
- catch ( NumberFormatException e )
+ catch ( Exception e )
{
throw new IllegalArgumentException( "Invalid configuration value [" + interval + "] for "
+ Config.ARRAY_CACHE_MIN_LOG_INTERVAL, e );
@@ -1376,13 +1375,13 @@ private long memToUse( StringLogger logger, Map<Object, Object> params, boolean
return isNode ? node : rel;
}
- private int fraction( Map<Object, Object> params, String param )
+ private float fraction( Map<Object, Object> params, String param )
{
String fraction = (String)params.get( Config.NODE_ARRAY_CACHE_ARRAY_FRACTION );
- int result = 1;
+ float result = 1;
try
{
- if ( fraction != null ) result = Integer.parseInt( fraction );
+ if ( fraction != null ) result = Float.parseFloat( fraction );
}
catch ( NumberFormatException e )
{
View
10 kernel/src/test/java/org/neo4j/kernel/impl/core/TestCacheTypes.java
@@ -52,8 +52,14 @@ private GraphDatabaseService newDb( String cacheType )
public void testDefaultCache()
{
GraphDatabaseService db = newDb( null );
- assertEquals( CacheType.soft, ((EmbeddedGraphDatabase) db).getConfig().getGraphDbModule().getNodeManager().getCacheType() );
- db.shutdown();
+ try
+ {
+ assertEquals( CacheType.soft, ((EmbeddedGraphDatabase) db).getConfig().getGraphDbModule().getNodeManager().getCacheType() );
+ }
+ finally
+ {
+ db.shutdown();
+ }
}
@Test
Please sign in to comment.
Something went wrong with that request. Please try again.