|
VolD 0.1
|
00001 00002 package de.zib.vold.backend; 00003 00004 import de.zib.vold.common.VoldException; 00005 00006 import java.util.List; 00007 import java.util.Map; 00008 00009 import java.io.FileWriter; 00010 import java.io.BufferedWriter; 00011 import java.io.IOException; 00012 00013 import javax.annotation.PostConstruct; 00014 import javax.annotation.PreDestroy; 00015 00016 import org.slf4j.Logger; 00017 import org.slf4j.LoggerFactory; 00018 00028 public class WriteLogger implements PartitionedDirectoryBackend 00029 { 00030 protected final Logger log = LoggerFactory.getLogger( this.getClass() ); 00031 00032 private String logfilename; 00033 private FileWriter logfile; 00034 private BufferedWriter out; 00035 00044 public WriteLogger( String logfilename ) 00045 { 00046 this.logfilename = logfilename; 00047 00048 this.logfile = null; 00049 this.out = null; 00050 } 00051 00055 public WriteLogger( ) 00056 { 00057 this.logfilename = null; 00058 00059 this.logfile = null; 00060 this.out = null; 00061 } 00062 00072 public void setLogfile( String logfilename ) 00073 { 00074 // guard 00075 { 00076 if( this.isopen() ) 00077 { 00078 log.warn( "Changing logfilename while logfile has already been opened." ); 00079 } 00080 } 00081 00082 this.logfilename = logfilename; 00083 } 00084 00088 public void checkState( ) 00089 { 00090 if( null == this.logfilename ) 00091 { 00092 throw new IllegalStateException( "Tried to operate on WriteLogger while it had not been initialized yet. You first need to set the logfilename!!" ); 00093 } 00094 } 00095 00103 @Override 00104 @PostConstruct 00105 public void open( ) 00106 { 00107 // guard 00108 { 00109 checkState(); 00110 00111 if( this.isopen() ) 00112 { 00113 log.warn( "Tried to open WriteLogger while it had already been opened!" ); 00114 return; 00115 } 00116 } 00117 00118 try 00119 { 00120 this.logfile = new FileWriter( this.logfilename, true ); 00121 this.out = new BufferedWriter( this.logfile ); 00122 } 00123 catch( IOException e ) 00124 { 00125 this.logfile = null; 00126 this.out = null; 00127 throw new VoldException( e ); 00128 } 00129 00130 log.info( "Backend opened." ); 00131 } 00132 00140 @Override 00141 @PreDestroy 00142 public void close( ) 00143 { 00144 // guard 00145 { 00146 checkState(); 00147 00148 if( ! this.isopen() ) 00149 { 00150 log.warn( "Tried to close WriteLogger while it wasn't open!" ); 00151 return; 00152 } 00153 } 00154 00155 try 00156 { 00157 if( isopen() ) 00158 { 00159 out.flush(); 00160 out.close(); 00161 logfile.flush(); 00162 logfile.close(); 00163 } 00164 } 00165 catch( IOException e ) 00166 { 00167 throw new VoldException( e ); 00168 } 00169 00170 out = null; 00171 logfile = null; 00172 00173 log.info( "Backend closed." ); 00174 } 00175 00181 @Override 00182 public boolean isopen( ) 00183 { 00184 return logfile != null && out != null; 00185 } 00186 00196 @Override 00197 public void insert( int partition, List< String > key, List< String > value ) 00198 { 00199 // guard 00200 { 00201 log.trace( "Insert: " + partition + ":'" + key.toString() + "' -> '" + value.toString() + "'" ); 00202 00203 checkState(); 00204 00205 if( ! this.isopen() ) 00206 { 00207 throw new VoldException( "Tried to operate on WriteLogger while it had not been initialized yet. Open it first!" ); 00208 } 00209 } 00210 00211 try 00212 { 00213 out.write( "INSERT: " + key.toString() + " |--> " + value.toString() ); 00214 out.newLine(); 00215 } 00216 catch( IOException e ) 00217 { 00218 throw new VoldException( e ); 00219 } 00220 } 00221 00230 @Override 00231 public void delete( int partition, List< String > key ) 00232 { 00233 // guard 00234 { 00235 log.trace( "Delete: " + key.toString() + "'" ); 00236 00237 checkState(); 00238 00239 if( ! this.isopen() ) 00240 { 00241 throw new VoldException( "Tried to operate on WriteLogger while it had not been initialized yet. Open it first!" ); 00242 } 00243 } 00244 00245 try 00246 { 00247 out.write( "DELETE: " + key.toString() ); 00248 out.newLine(); 00249 } 00250 catch( IOException e ) 00251 { 00252 throw new VoldException( e ); 00253 } 00254 } 00255 00259 @Override 00260 public List< String > lookup( int partition, List< String > key ) 00261 { 00262 // guard 00263 { 00264 log.trace( "Lookup: " + partition + ":'" + key.toString() + "'" ); 00265 } 00266 00267 throw new NotSupportedException( "WriteLogger does not have the ability to lookup. It's a write-only backend!" ); 00268 } 00269 00273 @Override 00274 public Map< List< String >, List< String > > prefixlookup( int partition, List< String > key ) 00275 { 00276 // guard 00277 { 00278 log.trace( "PrefixLookup: " + partition + ":'" + key.toString() + "'" ); 00279 } 00280 00281 throw new NotSupportedException( "WriteLogger does not have the ability to lookup. It's a write-only backend!" ); 00282 } 00283 }