comparison src/goodjava/lucene/logging/LoggingIndexWriter.java @ 1499:22e15cf73040

lucene.backup
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 09 May 2020 23:14:13 -0600
parents af55cfad6e12
children e66e3d50b289
comparison
equal deleted inserted replaced
1498:1b809d2fdf03 1499:22e15cf73040
132 } while( file.exists() ); 132 } while( file.exists() );
133 return new LogFile(file); 133 return new LogFile(file);
134 } 134 }
135 135
136 private void deleteUnusedFiles() throws IOException { 136 private void deleteUnusedFiles() throws IOException {
137 deleteUnusedFiles(logs,index);
138 }
139
140 private static void deleteUnusedFiles(List<LogFile> logs,File index) throws IOException {
137 Set<String> used = new HashSet<String>(); 141 Set<String> used = new HashSet<String>();
138 used.add( index.getName() ); 142 used.add( index.getName() );
139 for( LogFile lf : logs ) { 143 for( LogFile lf : logs ) {
140 used.add( lf.file.getName() ); 144 used.add( lf.file.getName() );
141 } 145 }
142 for( File f : logDir.listFiles() ) { 146 for( File f : index.getParentFile().listFiles() ) {
143 if( !used.contains(f.getName()) ) { 147 if( !used.contains(f.getName()) ) {
144 IoUtils.deleteRecursively(f); 148 IoUtils.deleteRecursively(f);
145 } 149 }
146 } 150 }
147 } 151 }
148 152
149 private void writeIndex() throws IOException { 153 private void writeIndex() throws IOException {
154 writeIndex(logs,index);
155 }
156
157 public static void writeIndex(List<LogFile> logs,File index) throws IOException {
150 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 158 ByteArrayOutputStream baos = new ByteArrayOutputStream();
151 DataOutputStream dos = new DataOutputStream(baos); 159 DataOutputStream dos = new DataOutputStream(baos);
152 dos.writeInt(version); 160 dos.writeInt(version);
153 dos.writeInt(logs.size()); 161 dos.writeInt(logs.size());
154 for( LogFile lf : logs ) { 162 for( LogFile lf : logs ) {
157 } 165 }
158 dos.close(); 166 dos.close();
159 RandomAccessFile raf = new RandomAccessFile( index, "rwd" ); 167 RandomAccessFile raf = new RandomAccessFile( index, "rwd" );
160 raf.write( baos.toByteArray() ); 168 raf.write( baos.toByteArray() );
161 raf.close(); 169 raf.close();
162 deleteUnusedFiles(); 170 deleteUnusedFiles(logs,index);
163 logger.info("writeIndex "+logs.toString()); 171 //logger.info("writeIndex "+logs.toString());
164 } 172 }
165 173
166 private void mergeLogs() throws IOException { 174 private void mergeLogs() throws IOException {
167 logger.info("merge"); 175 logger.info("merge");
168 LogFile first = logs.get(0); 176 LogFile first = logs.get(0);