Skip to content

Commit 79d46a4

Browse files
committed
Refactoring IOUtil
1 parent d8ac45c commit 79d46a4

5 files changed

Lines changed: 157 additions & 227 deletions

File tree

analysis/src/java/com/github/oeuvres/alix/lucene/analysis/fr/AnalyzerMeta.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

common/src/java/com/github/oeuvres/alix/lucene/terms/FieldStats.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.apache.lucene.util.BytesRef;
1414

1515
import com.github.oeuvres.alix.util.Report;
16-
import com.github.oeuvres.alix.util.SideFiles;
16+
import com.github.oeuvres.alix.util.IOUtil;
1717

1818
import java.io.BufferedInputStream;
1919
import java.io.BufferedOutputStream;
@@ -241,15 +241,15 @@ public static void build(final Path dataDir, final IndexReader reader, final Str
241241
ByTermStats byTerm = byTermStats(reader, field, report);
242242
// write data
243243
final Path statsPath = statsPath(dataDir, field);
244-
SideFiles.ensureAbsent(statsPath);
245-
final Path tmp = SideFiles.tmpPath(statsPath);
246-
SideFiles.ensureAbsent(tmp);
244+
IOUtil.ensureAbsent(statsPath);
245+
final Path tmp = IOUtil.tmpPath(statsPath);
246+
IOUtil.ensureAbsent(tmp);
247247
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmp, StandardOpenOption.CREATE_NEW));
248248
DataOutputStream out = new DataOutputStream(os))
249249
{
250250
out.writeInt(MAGIC);
251251
out.writeInt(VERSION);
252-
SideFiles.writeUtf8(out, field);
252+
IOUtil.writeUtf8(out, field);
253253
// by doc stats
254254
out.writeInt(maxDoc);
255255
for (int docWidth : docWidths) {
@@ -265,9 +265,9 @@ public static void build(final Path dataDir, final IndexReader reader, final Str
265265
for (long termFreq : byTerm.termFreqs) {
266266
out.writeLong(termFreq);
267267
}
268-
SideFiles.moveTemp(tmp, statsPath);
268+
IOUtil.moveTemp(tmp, statsPath);
269269
} catch (IOException | RuntimeException e) {
270-
SideFiles.deleteIfExists(tmp);
270+
IOUtil.deleteIfExists(tmp);
271271
throw e;
272272
}
273273
}
@@ -395,7 +395,7 @@ public static FieldStats open(final Path dataDir, final IndexReader reader, fina
395395
Objects.requireNonNull(field, "field");
396396

397397
final Path path = statsPath(dataDir, field);
398-
SideFiles.ensureRegularFile(path);
398+
IOUtil.ensureRegularFile(path);
399399

400400
try (DataInputStream in = new DataInputStream(
401401
new BufferedInputStream(Files.newInputStream(path, StandardOpenOption.READ))))
@@ -410,7 +410,7 @@ public static FieldStats open(final Path dataDir, final IndexReader reader, fina
410410
throw new IOException("Unsupported stats file version " + version + ": " + path);
411411
}
412412

413-
final String fieldFound = SideFiles.readUtf8(in);
413+
final String fieldFound = IOUtil.readUtf8(in);
414414
if (!field.equals(fieldFound)) {
415415
throw new IOException(
416416
"Field mismatch in stats file: requested '" + field + "', found '" + fieldFound + "'");

common/src/java/com/github/oeuvres/alix/lucene/terms/TermLexicon.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.apache.lucene.util.fst.PositiveIntOutputs;
1515
import org.apache.lucene.util.fst.Util;
1616

17-
import com.github.oeuvres.alix.util.SideFiles;
17+
import com.github.oeuvres.alix.util.IOUtil;
1818

1919
import java.io.BufferedOutputStream;
2020
import java.io.Closeable;
@@ -53,7 +53,7 @@
5353
* </p>
5454
* <p>
5555
* This class implements {@link Closeable}. Closing attempts to release the memory-mapped
56-
* regions immediately via {@link SideFiles#unmap(MappedByteBuffer)}.
56+
* regions immediately via {@link IOUtil#unmap(MappedByteBuffer)}.
5757
* If the reflective call fails (e.g. on a future JDK that
5858
* removes the entry point), the buffers are left for garbage collection — safe on Linux,
5959
* but may hold file locks on Windows until GC runs.
@@ -199,44 +199,44 @@ public static void build(final Path indexDir, final IndexReader reader, final St
199199
final Path datFinal = datPath(indexDir, field);
200200
final Path offFinal = offPath(indexDir, field);
201201

202-
SideFiles.ensureAbsent(fstFinal);
203-
SideFiles.ensureAbsent(datFinal);
204-
SideFiles.ensureAbsent(offFinal);
202+
IOUtil.ensureAbsent(fstFinal);
203+
IOUtil.ensureAbsent(datFinal);
204+
IOUtil.ensureAbsent(offFinal);
205205

206206
final Terms terms = MultiTerms.getTerms(reader, field);
207207
if (terms == null) {
208208
throw new IllegalArgumentException("Field not found or without terms: " + field);
209209
}
210210

211-
final Path fstTmp = SideFiles.tmpPath(fstFinal);
212-
final Path datTmp = SideFiles.tmpPath(datFinal);
213-
final Path offTmp = SideFiles.tmpPath(offFinal);
211+
final Path fstTmp = IOUtil.tmpPath(fstFinal);
212+
final Path datTmp = IOUtil.tmpPath(datFinal);
213+
final Path offTmp = IOUtil.tmpPath(offFinal);
214214

215215
// Clean up stale temps from a previous crash
216-
SideFiles.deleteIfExists(fstTmp);
217-
SideFiles.deleteIfExists(datTmp);
218-
SideFiles.deleteIfExists(offTmp);
216+
IOUtil.deleteIfExists(fstTmp);
217+
IOUtil.deleteIfExists(datTmp);
218+
IOUtil.deleteIfExists(offTmp);
219219

220220
try {
221221
buildFiles(terms, fstTmp, datTmp, offTmp);
222-
SideFiles.moveTemp(datTmp, datFinal);
223-
SideFiles.moveTemp(offTmp, offFinal);
224-
SideFiles.moveTemp(fstTmp, fstFinal);
222+
IOUtil.moveTemp(datTmp, datFinal);
223+
IOUtil.moveTemp(offTmp, offFinal);
224+
IOUtil.moveTemp(fstTmp, fstFinal);
225225
} catch (IOException | RuntimeException e) {
226-
SideFiles.deleteIfExists(fstTmp);
227-
SideFiles.deleteIfExists(datTmp);
228-
SideFiles.deleteIfExists(offTmp);
229-
SideFiles.deleteIfExists(fstFinal);
230-
SideFiles.deleteIfExists(datFinal);
231-
SideFiles.deleteIfExists(offFinal);
226+
IOUtil.deleteIfExists(fstTmp);
227+
IOUtil.deleteIfExists(datTmp);
228+
IOUtil.deleteIfExists(offTmp);
229+
IOUtil.deleteIfExists(fstFinal);
230+
IOUtil.deleteIfExists(datFinal);
231+
IOUtil.deleteIfExists(offFinal);
232232
throw e;
233233
}
234234
}
235235

236236
/**
237237
* Releases the memory-mapped regions.
238238
* <p>
239-
* Attempts immediate unmap via {@link SideFiles#unmap(MappedByteBuffer)}.
239+
* Attempts immediate unmap via {@link IOUtil#unmap(MappedByteBuffer)}.
240240
* If that reflective path is unavailable, the buffers are abandoned to garbage collection.
241241
* </p>
242242
* <p>
@@ -246,8 +246,8 @@ public static void build(final Path indexDir, final IndexReader reader, final St
246246
*/
247247
@Override
248248
public void close() {
249-
SideFiles.unmap(datBuf);
250-
SideFiles.unmap(offBuf);
249+
IOUtil.unmap(datBuf);
250+
IOUtil.unmap(offBuf);
251251
}
252252

253253
/**
@@ -373,13 +373,13 @@ public static TermLexicon open(final Path indexDir, final String field) throws I
373373
final Path datPath = datPath(indexDir, field);
374374
final Path offPath = offPath(indexDir, field);
375375

376-
SideFiles.ensureRegularFile(fstPath);
377-
SideFiles.ensureRegularFile(datPath);
378-
SideFiles.ensureRegularFile(offPath);
379-
SideFiles.checkMtimeCoherence(MTIME_TOLERANCE_MS, fstPath, datPath, offPath);
376+
IOUtil.ensureRegularFile(fstPath);
377+
IOUtil.ensureRegularFile(datPath);
378+
IOUtil.ensureRegularFile(offPath);
379+
IOUtil.checkMtimeCoherence(MTIME_TOLERANCE_MS, fstPath, datPath, offPath);
380380

381-
final MappedByteBuffer datBuf = SideFiles.mapReadOnly(datPath);
382-
final MappedByteBuffer offByteBuf = SideFiles.mapReadOnly(offPath);
381+
final MappedByteBuffer datBuf = IOUtil.mapReadOnly(datPath);
382+
final MappedByteBuffer offByteBuf = IOUtil.mapReadOnly(offPath);
383383
offByteBuf.order(ByteOrder.nativeOrder());
384384

385385
try {
@@ -408,8 +408,8 @@ public static TermLexicon open(final Path indexDir, final String field) throws I
408408
return new TermLexicon(indexDir, field, fst, datBuf, offByteBuf, off);
409409

410410
} catch (IOException | RuntimeException e) {
411-
SideFiles.unmap(datBuf);
412-
SideFiles.unmap(offByteBuf);
411+
IOUtil.unmap(datBuf);
412+
IOUtil.unmap(offByteBuf);
413413
throw e;
414414
}
415415
}

common/src/java/com/github/oeuvres/alix/lucene/terms/TermRail.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import com.github.oeuvres.alix.util.NumWriter;
1414
import com.github.oeuvres.alix.util.Report;
15-
import com.github.oeuvres.alix.util.SideFiles;
15+
import com.github.oeuvres.alix.util.IOUtil;
1616

1717
import java.io.Closeable;
1818
import java.io.IOException;
@@ -92,7 +92,7 @@
9292
* <h2>Lifecycle</h2>
9393
* <p>
9494
* Instances memory-map the two rail files. Call {@link #close()} when finished.
95-
* Close is best-effort and delegates to {@link SideFiles#unmap(MappedByteBuffer)}.
95+
* Close is best-effort and delegates to {@link IOUtil#unmap(MappedByteBuffer)}.
9696
* </p>
9797
*
9898
* @see TermLexicon
@@ -197,9 +197,9 @@ public static void build(
197197
}
198198

199199
final Path offFinal = offPath(dataDir, field);
200-
SideFiles.ensureAbsent(offFinal);
201-
final Path offTmp = SideFiles.tmpPath(offFinal);
202-
SideFiles.deleteIfExists(offTmp);
200+
IOUtil.ensureAbsent(offFinal);
201+
final Path offTmp = IOUtil.tmpPath(offFinal);
202+
IOUtil.deleteIfExists(offTmp);
203203

204204
final int maxDoc = reader.maxDoc();
205205
final BitSet liveDocs = FieldStats.liveDocs(reader);
@@ -225,9 +225,9 @@ public static void build(
225225
}
226226

227227
final Path datFinal = datPath(dataDir, field);
228-
SideFiles.ensureAbsent(datFinal);
229-
final Path datTmp = SideFiles.tmpPath(datFinal);
230-
SideFiles.deleteIfExists(datTmp);
228+
IOUtil.ensureAbsent(datFinal);
229+
final Path datTmp = IOUtil.tmpPath(datFinal);
230+
IOUtil.deleteIfExists(datTmp);
231231

232232
try (NumWriter railWriter = NumWriter.open(datTmp, totalBytes)) {
233233
final int[] rail = new int[widthMax];
@@ -269,13 +269,13 @@ public static void build(
269269
}
270270

271271
try {
272-
SideFiles.moveTemp(datTmp, datFinal);
273-
SideFiles.moveTemp(offTmp, offFinal);
272+
IOUtil.moveTemp(datTmp, datFinal);
273+
IOUtil.moveTemp(offTmp, offFinal);
274274
} catch (IOException | RuntimeException e) {
275-
SideFiles.deleteIfExists(datTmp);
276-
SideFiles.deleteIfExists(offTmp);
277-
SideFiles.deleteIfExists(datFinal);
278-
SideFiles.deleteIfExists(offFinal);
275+
IOUtil.deleteIfExists(datTmp);
276+
IOUtil.deleteIfExists(offTmp);
277+
IOUtil.deleteIfExists(datFinal);
278+
IOUtil.deleteIfExists(offFinal);
279279
throw e;
280280
}
281281
}
@@ -288,8 +288,8 @@ public static void build(
288288
*/
289289
@Override
290290
public void close() {
291-
SideFiles.unmap(datBuf);
292-
SideFiles.unmap(offBuf);
291+
IOUtil.unmap(datBuf);
292+
IOUtil.unmap(offBuf);
293293
}
294294

295295
/**
@@ -428,13 +428,13 @@ public static TermRail open(final Path indexDir, final String field) throws IOEx
428428
final Path datPath = datPath(indexDir, field);
429429
final Path offPath = offPath(indexDir, field);
430430

431-
SideFiles.ensureRegularFile(datPath);
432-
SideFiles.ensureRegularFile(offPath);
433-
SideFiles.checkMtimeCoherence(MTIME_TOLERANCE_MS, datPath, offPath);
431+
IOUtil.ensureRegularFile(datPath);
432+
IOUtil.ensureRegularFile(offPath);
433+
IOUtil.checkMtimeCoherence(MTIME_TOLERANCE_MS, datPath, offPath);
434434

435-
final MappedByteBuffer datMapped = SideFiles.mapReadOnly(datPath);
435+
final MappedByteBuffer datMapped = IOUtil.mapReadOnly(datPath);
436436
datMapped.order(ByteOrder.nativeOrder());
437-
final MappedByteBuffer offMapped = SideFiles.mapReadOnly(offPath);
437+
final MappedByteBuffer offMapped = IOUtil.mapReadOnly(offPath);
438438
offMapped.order(ByteOrder.nativeOrder());
439439

440440
try {
@@ -481,8 +481,8 @@ public static TermRail open(final Path indexDir, final String field) throws IOEx
481481

482482
return new TermRail(indexDir, field, datMapped, dat, offMapped, off, docCount, totalPositions);
483483
} catch (IOException | RuntimeException e) {
484-
SideFiles.unmap(datMapped);
485-
SideFiles.unmap(offMapped);
484+
IOUtil.unmap(datMapped);
485+
IOUtil.unmap(offMapped);
486486
throw e;
487487
}
488488
}

0 commit comments

Comments
 (0)