Advertisement
tinwelint

Remove duplicate start entries from logical log

Feb 13th, 2012
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. package org.neo4j;
  2.  
  3. import static java.nio.ByteBuffer.allocate;
  4. import static org.neo4j.kernel.impl.transaction.xaframework.LogIoUtils.readEntry;
  5. import static org.neo4j.kernel.impl.transaction.xaframework.LogIoUtils.readLogHeader;
  6. import static org.neo4j.kernel.impl.transaction.xaframework.LogIoUtils.writeLogEntry;
  7. import static org.neo4j.kernel.impl.transaction.xaframework.LogIoUtils.writeLogHeader;
  8.  
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.io.RandomAccessFile;
  12. import java.lang.reflect.Method;
  13. import java.nio.ByteBuffer;
  14. import java.nio.channels.FileChannel;
  15. import java.nio.channels.ReadableByteChannel;
  16. import java.util.HashSet;
  17. import java.util.Set;
  18.  
  19. import javax.transaction.xa.Xid;
  20.  
  21. import org.neo4j.index.impl.lucene.LuceneDataSource;
  22. import org.neo4j.kernel.impl.nioneo.xa.Command;
  23. import org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource;
  24. import org.neo4j.kernel.impl.transaction.xaframework.DirectMappedLogBuffer;
  25. import org.neo4j.kernel.impl.transaction.xaframework.LogBuffer;
  26. import org.neo4j.kernel.impl.transaction.xaframework.LogEntry;
  27. import org.neo4j.kernel.impl.transaction.xaframework.XaCommand;
  28. import org.neo4j.kernel.impl.transaction.xaframework.XaCommandFactory;
  29.  
  30. public class RemoveDualStartRecords
  31. {
  32.     public static void main( String[] args ) throws Exception
  33.     {
  34.         if ( args.length < 1 ) exit( "Supply store directory to fix" );
  35.         File dir = new File( args[0] );
  36.         if ( !dir.exists() ) exit( "Invalid store directory " + dir.getAbsolutePath() );
  37.        
  38.         CommandFactory neoCommands = new CommandFactory();
  39.         fix( new File( dir, NeoStoreXaDataSource.LOGICAL_LOG_DEFAULT_NAME + ".1" ), neoCommands );
  40.         fix( new File( dir, NeoStoreXaDataSource.LOGICAL_LOG_DEFAULT_NAME + ".2" ), neoCommands );
  41.        
  42.         IndexCommandFactory indexCommands = new IndexCommandFactory();
  43.         fix( new File( new File( dir, "index" ), "lucene.log.1" ), indexCommands );
  44.         fix( new File( new File( dir, "index" ), "lucene.log.2" ), indexCommands );
  45.     }
  46.  
  47.     private static void fix( File file, XaCommandFactory cf ) throws IOException
  48.     {
  49.         if ( !file.exists() ) return;
  50.         FileChannel inChannel = new RandomAccessFile( file, "r" ).getChannel();
  51.         ByteBuffer buffer = allocate( 500 );
  52.         long[] header = readLogHeader( buffer, inChannel, true );
  53.         File fixedFile = new File( file.getAbsolutePath() + ".fixed" );
  54.         writeLogHeader( buffer, header[0], header[1] );
  55.         FileChannel outChannel = new RandomAccessFile( fixedFile, "rw" ).getChannel();
  56.         outChannel.write( buffer ); // header
  57.         LogBuffer logBuffer = new DirectMappedLogBuffer( outChannel );
  58.         LogEntry entry = null;
  59.         Set<Xid> startXids = new HashSet<Xid>();
  60.         boolean success = false;
  61.         int removedCount = 0;
  62.         try
  63.         {
  64.             while ( (entry = readEntry( buffer, inChannel, cf )) != null )
  65.             {
  66.                 if ( entry instanceof LogEntry.Start && !startXids.add( ((LogEntry.Start) entry).getXid() ) )
  67.                 {
  68.                     removedCount++;
  69.                     continue;
  70.                 }
  71.                 writeLogEntry( entry, logBuffer );
  72.             }
  73.             success = true;
  74.         }
  75.         finally
  76.         {
  77.             logBuffer.force();
  78.             inChannel.close();
  79.             outChannel.close();
  80.         }
  81.        
  82.         if ( success )
  83.         {
  84.             file.delete();
  85.             fixedFile.renameTo( file );
  86.             if ( removedCount != 0 ) System.out.println( "Removed " + removedCount + " duplicate entries from " + file.getAbsolutePath() );
  87.         }
  88.     }
  89.  
  90.     private static void exit( String string )
  91.     {
  92.         System.out.println( string );
  93.         System.exit( 1 );
  94.     }
  95.  
  96.     private static class CommandFactory extends XaCommandFactory
  97.     {
  98.         @Override
  99.         public XaCommand readCommand( ReadableByteChannel byteChannel,
  100.                 ByteBuffer buffer ) throws IOException
  101.         {
  102.             return Command.readCommand( null, byteChannel, buffer );
  103.         }
  104.     }
  105.  
  106.     private static class IndexCommandFactory extends XaCommandFactory
  107.     {
  108.         private final Class<?> luceneCommandClass;
  109.         private final Method readCommandMethod;
  110.        
  111.         IndexCommandFactory() throws Exception
  112.         {
  113.             luceneCommandClass = Class.forName( "org.neo4j.index.impl.lucene.LuceneCommand" );
  114.             readCommandMethod = luceneCommandClass.getDeclaredMethod( "readCommand", ReadableByteChannel.class, ByteBuffer.class, LuceneDataSource.class );
  115.             readCommandMethod.setAccessible( true );
  116.         }
  117.        
  118.         @Override
  119.         public XaCommand readCommand( ReadableByteChannel byteChannel, ByteBuffer buffer )
  120.                 throws IOException
  121.         {
  122. //            return LuceneCommand.readCommand( byteChannel, buffer, null );
  123.             try
  124.             {
  125.                 return (XaCommand) readCommandMethod.invoke( null, byteChannel, buffer, null );
  126.             }
  127.             catch ( Exception e )
  128.             {
  129.                 throw new RuntimeException( e );
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement