Skip to content
Browse files

Merge remote-tracking branch 'neo4j/master' into cookbook

  • Loading branch information...
2 parents 6c87de5 + 4cdecf6 commit 6e14f9b7f65b5c4ddd909aff7c6f90787c422e38 @peterneubauer peterneubauer committed Feb 17, 2012
View
62 kernel/src/main/java/org/neo4j/helpers/Format.java
@@ -22,39 +22,70 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
+import java.util.TimeZone;
public class Format
{
public static String date()
{
- return date( new Date() );
+ return date( DEFAULT_TIME_ZONE );
}
+ public static String date( TimeZone timeZone )
+ {
+ return date( new Date(), timeZone );
+ }
+
public static String date( long millis )
{
- return date( new Date( millis ) );
+ return date( millis, DEFAULT_TIME_ZONE );
}
+ public static String date( long millis, TimeZone timeZone )
+ {
+ return date( new Date( millis ), timeZone );
+ }
+
public static String date( Date date )
{
- return DATE.format( date );
+ return date( date, DEFAULT_TIME_ZONE );
}
+ public static String date( Date date, TimeZone timeZone )
+ {
+ return DATE.format( date, timeZone );
+ }
+
public static String time()
{
- return time( new Date() );
+ return time( DEFAULT_TIME_ZONE );
}
+ public static String time( TimeZone timeZone )
+ {
+ return time( new Date() );
+ }
+
public static String time( long millis )
{
- return time( new Date( millis ) );
+ return time( millis, DEFAULT_TIME_ZONE );
}
+ public static String time( long millis, TimeZone timeZone )
+ {
+ return time( new Date( millis ), timeZone );
+ }
+
public static String time( Date date )
{
- return TIME.format( date );
+ return time( date, DEFAULT_TIME_ZONE );
}
+ public static String time( Date date, TimeZone timeZone )
+ {
+ return TIME.format( date, timeZone );
+ }
+
public static String bytes( long bytes )
{
double size = bytes;
@@ -73,8 +104,17 @@ private Format()
private static final String[] BYTE_SIZES = { "B", "kB", "MB", "GB" };
- private static final ThreadLocalFormat DATE = new ThreadLocalFormat( "yyyy-MM-dd HH:mm:ss.SSSZ" ),
- TIME = new ThreadLocalFormat( "HH:mm:ss.SSS" );
+ public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSSZ";
+ public static final String TIME_FORMAT = "HH:mm:ss.SSS";
+
+ /**
+ * Default time zone is UTC (+00:00) so that comparing timestamped logs from different
+ * sources is an easier task.
+ */
+ public static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
+
+ private static final ThreadLocalFormat DATE = new ThreadLocalFormat( DATE_FORMAT ),
+ TIME = new ThreadLocalFormat( TIME_FORMAT );
private static class ThreadLocalFormat extends ThreadLocal<DateFormat>
{
@@ -85,9 +125,11 @@ private Format()
this.format = format;
}
- String format( Date date )
+ String format( Date date, TimeZone timeZone )
{
- return get().format( date );
+ DateFormat dateFormat = get();
+ dateFormat.setTimeZone( timeZone );
+ return dateFormat.format( date );
}
@Override
View
69 kernel/src/main/java/org/neo4j/kernel/impl/transaction/xaframework/LogEntry.java
@@ -19,6 +19,8 @@
*/
package org.neo4j.kernel.impl.transaction.xaframework;
+import java.util.TimeZone;
+
import javax.transaction.xa.Xid;
import org.neo4j.helpers.Format;
@@ -49,6 +51,11 @@ public int getIdentifier()
{
return identifier;
}
+
+ public String toString( TimeZone timeZone )
+ {
+ return toString();
+ }
public static class Start extends LogEntry
{
@@ -112,7 +119,13 @@ public long getChecksum()
@Override
public String toString()
{
- return "Start[" + getIdentifier() + ",xid=" + xid + ",master=" + masterId + ",me=" + myId + ",time=" + timestamp( timeWritten ) + "]";
+ return toString( Format.DEFAULT_TIME_ZONE );
+ }
+
+ @Override
+ public String toString( TimeZone timeZone )
+ {
+ return "Start[" + getIdentifier() + ",xid=" + xid + ",master=" + masterId + ",me=" + myId + ",time=" + timestamp( timeWritten, timeZone ) + "]";
}
}
@@ -134,20 +147,28 @@ public long getTimeWritten()
@Override
public String toString()
{
- return "Prepare[" + getIdentifier() + ", " + timestamp( timeWritten ) + "]";
+ return toString( Format.DEFAULT_TIME_ZONE );
+ }
+
+ @Override
+ public String toString( TimeZone timeZone )
+ {
+ return "Prepare[" + getIdentifier() + ", " + timestamp( timeWritten, timeZone ) + "]";
}
}
public static abstract class Commit extends LogEntry
{
private final long txId;
private final long timeWritten;
+ protected final String name;
- Commit( int identifier, long txId, long timeWritten )
+ Commit( int identifier, long txId, long timeWritten, String name )
{
super( identifier );
this.txId = txId;
this.timeWritten = timeWritten;
+ this.name = name;
}
public long getTxId()
@@ -159,19 +180,33 @@ public long getTimeWritten()
{
return timeWritten;
}
+
+ @Override
+ public String toString()
+ {
+ return toString( Format.DEFAULT_TIME_ZONE );
+ }
+
+ @Override
+ public String toString( TimeZone timeZone )
+ {
+ return name + "[" + getIdentifier() + ", txId=" + getTxId() + ", " + timestamp( getTimeWritten(), timeZone ) + "]";
+ }
}
public static class OnePhaseCommit extends Commit
{
OnePhaseCommit( int identifier, long txId, long timeWritten )
{
- super( identifier, txId, timeWritten );
+ super( identifier, txId, timeWritten, "1PC" );
}
+ }
- @Override
- public String toString()
+ public static class TwoPhaseCommit extends Commit
+ {
+ TwoPhaseCommit( int identifier, long txId, long timeWritten )
{
- return "1PC[" + getIdentifier() + ", txId=" + getTxId() + ", " + timestamp( getTimeWritten() ) + "]";
+ super( identifier, txId, timeWritten, "2PC" );
}
}
@@ -188,21 +223,7 @@ public String toString()
return "Done[" + getIdentifier() + "]";
}
}
-
- public static class TwoPhaseCommit extends Commit
- {
- TwoPhaseCommit( int identifier, long txId, long timeWritten )
- {
- super( identifier, txId, timeWritten );
- }
-
- @Override
- public String toString()
- {
- return "2PC[" + getIdentifier() + ", txId=" + getTxId() + ", " + timestamp( getTimeWritten() ) + "]";
- }
- }
-
+
public static class Command extends LogEntry
{
private final XaCommand command;
@@ -230,8 +251,8 @@ public void setIdentifier( int newXidIdentifier )
identifier = newXidIdentifier;
}
- public String timestamp( long timeWritten )
+ public String timestamp( long timeWritten, TimeZone timeZone )
{
- return Format.date( timeWritten ) + "/" + timeWritten;
+ return Format.date( timeWritten, timeZone ) + "/" + timeWritten;
}
}
View
27 kernel/src/main/java/org/neo4j/kernel/impl/util/DumpLogicalLog.java
@@ -28,10 +28,14 @@
import java.nio.channels.ReadableByteChannel;
import java.util.Collection;
import java.util.Comparator;
+import java.util.TimeZone;
import java.util.TreeSet;
import javax.transaction.xa.Xid;
+import org.neo4j.helpers.Args;
+import org.neo4j.helpers.Format;
+import org.neo4j.helpers.Pair;
import org.neo4j.kernel.impl.nioneo.store.NeoStore;
import org.neo4j.kernel.impl.nioneo.xa.Command;
import org.neo4j.kernel.impl.transaction.xaframework.LogEntry;
@@ -41,7 +45,7 @@
public class DumpLogicalLog
{
- public int dump( String filenameOrDirectory ) throws IOException
+ public int dump( String filenameOrDirectory, TimeZone timeZone ) throws IOException
{
int logsFound = 0;
for ( String fileName : filenamesOf( filenameOrDirectory, getLogPrefix() ) )
@@ -70,7 +74,7 @@ public int dump( String filenameOrDirectory ) throws IOException
prevLastCommittedTx + "]" );
long logEntriesFound = 0;
XaCommandFactory cf = instantiateCommandFactory();
- while ( readAndPrintEntry( fileChannel, buffer, cf ) )
+ while ( readAndPrintEntry( fileChannel, buffer, cf, timeZone ) )
{
logEntriesFound++;
}
@@ -85,13 +89,13 @@ protected static boolean isAGraphDatabaseDirectory( String fileName )
return file.isDirectory() && new File( file, NeoStore.DEFAULT_NAME ).exists();
}
- protected boolean readAndPrintEntry( FileChannel fileChannel, ByteBuffer buffer, XaCommandFactory cf )
+ protected boolean readAndPrintEntry( FileChannel fileChannel, ByteBuffer buffer, XaCommandFactory cf, TimeZone timeZone )
throws IOException
{
LogEntry entry = LogIoUtils.readEntry( buffer, fileChannel, cf );
if ( entry != null )
{
- System.out.println( entry.toString() );
+ System.out.println( entry.toString( timeZone ) );
return true;
}
return false;
@@ -109,10 +113,17 @@ protected String getLogPrefix()
public static void main( String args[] ) throws IOException
{
- for ( String arg : args )
- {
- new DumpLogicalLog().dump( arg );
- }
+ Pair<Iterable<String>, TimeZone> config = parseConfig( args );
+ for ( String file : config.first() ) new DumpLogicalLog().dump( file, config.other() );
+ }
+
+ public static Pair<Iterable<String>, TimeZone> parseConfig( String[] args )
+ {
+ Args arguments = new Args( args );
+ TimeZone timeZone = Format.DEFAULT_TIME_ZONE;
+ String timeZoneString = arguments.get( "timezone", null );
+ if ( timeZoneString != null ) timeZone = TimeZone.getTimeZone( timeZoneString );
+ return Pair.<Iterable<String>,TimeZone>of( arguments.orphans(), timeZone );
}
protected static String[] filenamesOf( String filenameOrDirectory, final String prefix )
View
95 kernel/src/test/java/org/neo4j/helpers/TestFormat.java
@@ -0,0 +1,95 @@
+/**
+ * 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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.junit.Test;
+
+public class TestFormat
+{
+ @Test
+ public void dateTime() throws Exception
+ {
+ // Date
+ long timeWithDate = System.currentTimeMillis();
+ String dateAsString = Format.date( timeWithDate );
+ assertEquals( timeWithDate, new SimpleDateFormat( Format.DATE_FORMAT ).parse( dateAsString ).getTime() );
+
+ // Time
+ String timeAsString = Format.time( timeWithDate );
+ assertEquals( timeWithDate, translateToDate( timeWithDate,
+ new SimpleDateFormat( Format.TIME_FORMAT ).parse( timeAsString ).getTime(), Format.DEFAULT_TIME_ZONE ) );
+ }
+
+ @Test
+ public void dateTimeWithTimeZone() throws Exception
+ {
+ String zoneOffset = "+03:00";
+ TimeZone zone = TimeZone.getTimeZone( "GMT" + zoneOffset );
+
+ // Date
+ String asString = Format.date( zone );
+ assertTrue( asString.endsWith( withoutColon( zoneOffset ) ) );
+ long timeWithDate = new SimpleDateFormat( Format.DATE_FORMAT ).parse( asString ).getTime();
+
+ asString = Format.date( timeWithDate, zone );
+ assertTrue( asString.endsWith( withoutColon( zoneOffset ) ) );
+ assertEquals( timeWithDate, new SimpleDateFormat( Format.DATE_FORMAT ).parse( asString ).getTime() );
+
+ asString = Format.date( new Date( timeWithDate ), zone );
+ assertTrue( asString.endsWith( withoutColon( zoneOffset ) ) );
+ assertEquals( timeWithDate, new SimpleDateFormat( Format.DATE_FORMAT ).parse( asString ).getTime() );
+
+ // Time
+ asString = Format.time( timeWithDate, zone );
+ assertEquals( timeWithDate, translateToDate( timeWithDate,
+ new SimpleDateFormat( Format.TIME_FORMAT ).parse( asString ).getTime(), zone ) );
+
+ asString = Format.time( new Date( timeWithDate ), zone );
+ assertEquals( timeWithDate, translateToDate( timeWithDate,
+ new SimpleDateFormat( Format.TIME_FORMAT ).parse( asString ).getTime(), zone ) );
+ }
+
+ private long translateToDate( long timeWithDate, long time, TimeZone timeIsGivenInThisTimeZone )
+ {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTimeInMillis( timeWithDate );
+
+ Calendar timeCalendar = Calendar.getInstance();
+ timeCalendar.setTimeInMillis( time );
+ timeCalendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) );
+ timeCalendar.set( Calendar.MONTH, calendar.get( Calendar.MONTH ) );
+ timeCalendar.set( Calendar.DAY_OF_MONTH, calendar.get( Calendar.DAY_OF_MONTH ) );
+ int timeZoneOffset = TimeZone.getDefault().getOffset( timeWithDate )-timeIsGivenInThisTimeZone.getOffset( timeWithDate );
+ return timeCalendar.getTimeInMillis()+timeZoneOffset;
+ }
+
+ private String withoutColon( String zoneOffset )
+ {
+ return zoneOffset.replaceAll( ":", "" );
+ }
+}
View
11 lucene-index/src/main/java/org/neo4j/index/impl/lucene/DumpLogicalLog.java
@@ -23,21 +23,24 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
+import java.util.TimeZone;
+import org.neo4j.helpers.Pair;
import org.neo4j.kernel.impl.transaction.xaframework.XaCommand;
import org.neo4j.kernel.impl.transaction.xaframework.XaCommandFactory;
public class DumpLogicalLog extends org.neo4j.kernel.impl.util.DumpLogicalLog
{
public static void main( String[] args ) throws IOException
{
- for ( String arg : args )
+ Pair<Iterable<String>, TimeZone> config = parseConfig( args );
+ for ( String file : config.first() )
{
- int dumped = new DumpLogicalLog().dump( arg );
- if ( dumped == 0 && isAGraphDatabaseDirectory( arg ) )
+ int dumped = new DumpLogicalLog().dump( file, config.other() );
+ if ( dumped == 0 && isAGraphDatabaseDirectory( file ) )
{ // If none were found and we really pointed to a neodb directory
// then go to its index folder and try there.
- new DumpLogicalLog().dump( new File( arg, "index" ).getAbsolutePath() );
+ new DumpLogicalLog().dump( new File( file, "index" ).getAbsolutePath(), config.other() );
}
}
}
View
2 server/src/main/java/org/neo4j/server/webadmin/rest/ShellSession.java
@@ -43,7 +43,7 @@
private final ShellClient client;
private final CollectingOutput output;
- private ShellServer fallbackServer = null;
+ private static volatile ShellServer fallbackServer = null;
public ShellSession( AbstractGraphDatabase graph )
{

0 comments on commit 6e14f9b

Please sign in to comment.
Something went wrong with that request. Please try again.