From ab4c8cd047f7eaafa831674ef501c0545f9be3c3 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Sun, 30 Jan 2022 15:38:45 +0900 Subject: [PATCH] Compress index with GZIP Signed-off-by: Hiroshi Miura --- .../github/eb4j/dsl/DslDictionaryLoader.java | 44 +++++++------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/github/eb4j/dsl/DslDictionaryLoader.java b/src/main/java/io/github/eb4j/dsl/DslDictionaryLoader.java index d7e2e0e..f9b84be 100644 --- a/src/main/java/io/github/eb4j/dsl/DslDictionaryLoader.java +++ b/src/main/java/io/github/eb4j/dsl/DslDictionaryLoader.java @@ -48,6 +48,9 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + /** * Loader class for DSL dictionary. @@ -79,7 +82,8 @@ static DslDictionary load(@NotNull final Path path, @Nullable final Path indexPa DslDictionaryProperty prop = null; DslIndexOuterClass.DslIndex index = getIndexFromFileAndValidate(path, indexPath); if (index != null) { - prop = getPropertyFromIndex(index); + prop = new DslDictionaryProperty( index.getDictionaryName(), index.getIndexLanguage(), + index.getContentLanguage(), Charset.forName(index.getCharset()), index.getEol().toByteArray()); entries = index.getEntriesList(); } // When there is no index or failed to validate @@ -100,11 +104,6 @@ static DslDictionary load(@NotNull final Path path, @Nullable final Path indexPa } } - @SuppressWarnings("AvoidInlineConditionals") - static DslDictionary load(@NotNull final Path path) throws IOException { - return load(path, null); - } - @SuppressWarnings("AvoidInlineConditionals") private static List loadEntriesFromDslFile( final Path path, final boolean isDictzip, final DslDictionaryProperty prop) throws IOException { @@ -174,9 +173,13 @@ private static List loadEntriesFromDslFile( private static DslIndexOuterClass.DslIndex getIndexFromFileAndValidate(final Path path, final Path indexPath) { if (indexPath != null && indexPath.toFile().canRead()) { - try (InputStream is = Files.newInputStream(indexPath)) { + try (InputStream is = new GZIPInputStream(Files.newInputStream(indexPath))) { DslIndexOuterClass.DslIndex index = DslIndexOuterClass.DslIndex.parseFrom(is); - if (validateIndex(path, index)) { + long mtime = Files.getLastModifiedTime(path).toMillis(); + long expectedMTime = index.getFileLastModifiedTime(); + if (path.toString().equals(index.getFilename()) + && (Files.size(path) == index.getFilesize()) + && mtime == expectedMTime) { return index; } } catch (IOException ignored) { @@ -185,23 +188,6 @@ private static DslIndexOuterClass.DslIndex getIndexFromFileAndValidate(final Pat return null; } - private static boolean validateIndex(final Path path, final DslIndexOuterClass.DslIndex index) throws IOException { - long mtime = Files.getLastModifiedTime(path).toMillis(); - long expectedMTime = index.getFileLastModifiedTime(); - return (path.toString().equals(index.getFilename()) - && (Files.size(path) == index.getFilesize()) - && mtime == expectedMTime); - } - - private static DslDictionaryProperty getPropertyFromIndex(final DslIndexOuterClass.DslIndex index) { - return new DslDictionaryProperty( - index.getDictionaryName(), - index.getIndexLanguage(), - index.getContentLanguage(), - Charset.forName(index.getCharset()), - index.getEol().toByteArray()); - } - private static void buildIndexFile(@NotNull final Path path, @Nullable final Path indexPath, @NotNull final List entries, @NotNull final DslDictionaryProperty prop) throws IOException { @@ -209,8 +195,8 @@ private static void buildIndexFile(@NotNull final Path path, @Nullable final Pat // do nothing when indexPath is not specified. return; } - try (OutputStream os = Files.newOutputStream(indexPath, StandardOpenOption.CREATE, - StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { + try (OutputStream os = new GZIPOutputStream(Files.newOutputStream(indexPath, + StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE))) { DslIndexOuterClass.DslIndex index = DslIndexOuterClass.DslIndex.newBuilder() .setFilename(path.toString()) .setFilesize(Files.size(path)) @@ -224,7 +210,9 @@ private static void buildIndexFile(@NotNull final Path path, @Nullable final Pat .build(); index.writeTo(os); os.flush(); - } catch (IOException ignored) { + } catch (IOException e) { + // clean up when error + Files.deleteIfExists(indexPath); } }