Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #30 from eb4j/topic/miurahr/compress-index-file
Browse files Browse the repository at this point in the history
Compress index
  • Loading branch information
miurahr authored Jan 30, 2022
2 parents 4553c4e + ab4c8cd commit 6858e2e
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions src/main/java/io/github/eb4j/dsl/DslDictionaryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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<DslIndexOuterClass.DslIndex.Entry> loadEntriesFromDslFile(
final Path path, final boolean isDictzip, final DslDictionaryProperty prop) throws IOException {
Expand Down Expand Up @@ -174,9 +173,13 @@ private static List<DslIndexOuterClass.DslIndex.Entry> 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) {
Expand All @@ -185,32 +188,15 @@ 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<DslIndexOuterClass.DslIndex.Entry> entries,
@NotNull final DslDictionaryProperty prop) throws IOException {
if (indexPath == null) {
// 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))
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 6858e2e

Please sign in to comment.