StemmerPatchTrieBinaryIO.java

1
/*******************************************************************************
2
 * Copyright (C) 2026, Leo Galambos
3
 * All rights reserved.
4
 * 
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 * 
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 
11
 * 2. Redistributions in binary form must reproduce the above copyright notice,
12
 *    this list of conditions and the following disclaimer in the documentation
13
 *    and/or other materials provided with the distribution.
14
 * 
15
 * 3. Neither the name of the copyright holder nor the names of its contributors
16
 *    may be used to endorse or promote products derived from this software
17
 *    without specific prior written permission.
18
 * 
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 ******************************************************************************/
31
package org.egothor.stemmer;
32
33
import java.io.BufferedInputStream;
34
import java.io.BufferedOutputStream;
35
import java.io.DataInputStream;
36
import java.io.DataOutputStream;
37
import java.io.IOException;
38
import java.io.InputStream;
39
import java.io.OutputStream;
40
import java.nio.file.Files;
41
import java.nio.file.Path;
42
import java.util.Objects;
43
import java.util.logging.Level;
44
import java.util.logging.Logger;
45
import java.util.zip.GZIPInputStream;
46
import java.util.zip.GZIPOutputStream;
47
48
/**
49
 * Binary persistence helper for patch-command stemmer tries.
50
 *
51
 * <p>
52
 * This class persists {@link FrequencyTrie} instances whose values are compact
53
 * patch commands represented as {@link String}. The serialized trie payload is
54
 * the native binary format of {@link FrequencyTrie}, wrapped in GZip
55
 * compression.
56
 *
57
 * <p>
58
 * The helper centralizes the codec and compression details so that higher-level
59
 * loader APIs can remain focused on source selection rather than stream
60
 * mechanics.
61
 */
62
public final class StemmerPatchTrieBinaryIO {
63
64
    /**
65
     * Logger of this class.
66
     */
67
    private static final Logger LOGGER = Logger.getLogger(StemmerPatchTrieBinaryIO.class.getName());
68
69
    /**
70
     * Value codec for persisted patch-command strings.
71
     */
72
    private static final FrequencyTrie.ValueStreamCodec<String> STRING_CODEC = new StringValueStreamCodec();
73
74
    /**
75
     * Utility class.
76
     */
77
    private StemmerPatchTrieBinaryIO() {
78
        throw new AssertionError("No instances.");
79
    }
80
81
    /**
82
     * Reads a GZip-compressed binary patch-command trie from a filesystem path.
83
     *
84
     * @param path source file
85
     * @return deserialized trie
86
     * @throws NullPointerException if {@code path} is {@code null}
87
     * @throws IOException          if reading or decompression fails
88
     */
89
    public static FrequencyTrie<String> read(final Path path) throws IOException {
90
        Objects.requireNonNull(path, "path");
91
92
        try (InputStream fileInputStream = Files.newInputStream(path)) {
93 1 1. read : replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED
            return read(fileInputStream);
94
        }
95
    }
96
97
    /**
98
     * Reads a GZip-compressed binary patch-command trie from a filesystem path
99
     * string.
100
     *
101
     * @param fileName source file name or path string
102
     * @return deserialized trie
103
     * @throws NullPointerException if {@code fileName} is {@code null}
104
     * @throws IOException          if reading or decompression fails
105
     */
106
    public static FrequencyTrie<String> read(final String fileName) throws IOException {
107
        Objects.requireNonNull(fileName, "fileName");
108 1 1. read : replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED
        return read(Path.of(fileName));
109
    }
110
111
    /**
112
     * Reads a GZip-compressed binary patch-command trie from an input stream.
113
     *
114
     * <p>
115
     * The supplied stream is consumed but not interpreted as plain trie bytes; it
116
     * is first decompressed using {@link GZIPInputStream}.
117
     *
118
     * @param inputStream source stream
119
     * @return deserialized trie
120
     * @throws NullPointerException if {@code inputStream} is {@code null}
121
     * @throws IOException          if reading or decompression fails
122
     */
123
    public static FrequencyTrie<String> read(final InputStream inputStream) throws IOException {
124
        Objects.requireNonNull(inputStream, "inputStream");
125
126
        try (GZIPInputStream gzipInputStream = new GZIPInputStream(new BufferedInputStream(inputStream));
127
                DataInputStream dataInputStream = new DataInputStream(gzipInputStream)) {
128 1 1. lambda$read$0 : replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::lambda$read$0 → KILLED
            final FrequencyTrie<String> trie = FrequencyTrie.readFrom(dataInputStream, String[]::new, STRING_CODEC);
129
130
            LOGGER.log(Level.FINE, "Read compressed binary stemmer trie.");
131 1 1. read : replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED
            return trie;
132
        }
133
    }
134
135
    /**
136
     * Writes a GZip-compressed binary patch-command trie to a filesystem path.
137
     *
138
     * @param trie trie to persist
139
     * @param path target file
140
     * @throws NullPointerException if any argument is {@code null}
141
     * @throws IOException          if writing fails
142
     */
143
    public static void write(final FrequencyTrie<String> trie, final Path path) throws IOException {
144
        Objects.requireNonNull(trie, "trie");
145
        Objects.requireNonNull(path, "path");
146
147
        final Path parent = path.toAbsolutePath().getParent();
148 1 1. write : negated conditional → KILLED
        if (parent != null) {
149
            Files.createDirectories(parent);
150
        }
151
152
        try (OutputStream fileOutputStream = Files.newOutputStream(path)) {
153 1 1. write : removed call to org/egothor/stemmer/StemmerPatchTrieBinaryIO::write → KILLED
            write(trie, fileOutputStream);
154
        }
155
    }
156
157
    /**
158
     * Writes a GZip-compressed binary patch-command trie to a filesystem path
159
     * string.
160
     *
161
     * @param trie     trie to persist
162
     * @param fileName target file name or path string
163
     * @throws NullPointerException if any argument is {@code null}
164
     * @throws IOException          if writing fails
165
     */
166
    public static void write(final FrequencyTrie<String> trie, final String fileName) throws IOException {
167
        Objects.requireNonNull(fileName, "fileName");
168 1 1. write : removed call to org/egothor/stemmer/StemmerPatchTrieBinaryIO::write → KILLED
        write(trie, Path.of(fileName));
169
    }
170
171
    /**
172
     * Writes a GZip-compressed binary patch-command trie to an output stream.
173
     *
174
     * @param trie         trie to persist
175
     * @param outputStream target stream
176
     * @throws NullPointerException if any argument is {@code null}
177
     * @throws IOException          if writing fails
178
     */
179
    public static void write(final FrequencyTrie<String> trie, final OutputStream outputStream) throws IOException {
180
        Objects.requireNonNull(trie, "trie");
181
        Objects.requireNonNull(outputStream, "outputStream");
182
183
        try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new BufferedOutputStream(outputStream));
184
                DataOutputStream dataOutputStream = new DataOutputStream(gzipOutputStream)) {
185 1 1. write : removed call to org/egothor/stemmer/FrequencyTrie::writeTo → KILLED
            trie.writeTo(dataOutputStream, STRING_CODEC);
186
        }
187
188
        LOGGER.log(Level.FINE, "Wrote compressed binary stemmer trie.");
189
    }
190
191
    /**
192
     * Binary stream codec for persisted patch-command strings.
193
     */
194
    private static final class StringValueStreamCodec implements FrequencyTrie.ValueStreamCodec<String> {
195
196
        /**
197
         * Creates a codec instance.
198
         */
199
        private StringValueStreamCodec() {
200
        }
201
202
        @Override
203
        public void write(final DataOutputStream dataOutput, final String value) throws IOException {
204 1 1. write : removed call to java/io/DataOutputStream::writeUTF → KILLED
            dataOutput.writeUTF(value);
205
        }
206
207
        @Override
208
        public String read(final DataInputStream dataInput) throws IOException {
209 1 1. read : replaced return value with "" for org/egothor/stemmer/StemmerPatchTrieBinaryIO$StringValueStreamCodec::read → KILLED
            return dataInput.readUTF();
210
        }
211
    }
212
}

Mutations

93

1.1
Location : read
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:ReadTests]/[method:shouldReadGzipPayloadFromPath()]
replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED

108

1.1
Location : read
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:ReadTests]/[method:shouldReadGzipPayloadFromFileNameString()]
replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED

128

1.1
Location : lambda$read$0
Killed by : org.egothor.stemmer.StemmerPatchTrieLoaderTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieLoaderTest]/[nested-class:FilesystemAndParserTests]/[method:shouldRoundTripBinaryTrieAcrossAllBinaryOverloads()]
replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::lambda$read$0 → KILLED

131

1.1
Location : read
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:ReadTests]/[method:shouldReadGzipPayloadFromPath()]
replaced return value with null for org/egothor/stemmer/StemmerPatchTrieBinaryIO::read → KILLED

148

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:WriteTests]/[method:shouldCreateParentDirectoriesAndWriteGzipFile()]
negated conditional → KILLED

153

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:WriteTests]/[method:shouldWriteToFilesystemWhenFileNameStringIsUsed()]
removed call to org/egothor/stemmer/StemmerPatchTrieBinaryIO::write → KILLED

168

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:WriteTests]/[method:shouldWriteToFilesystemWhenFileNameStringIsUsed()]
removed call to org/egothor/stemmer/StemmerPatchTrieBinaryIO::write → KILLED

185

1.1
Location : write
Killed by : org.egothor.stemmer.StemmerPatchTrieBinaryIOTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieBinaryIOTest]/[nested-class:WriteTests]/[method:shouldPropagateWriteFailureFromTrieSerialization()]
removed call to org/egothor/stemmer/FrequencyTrie::writeTo → KILLED

204

1.1
Location : write
Killed by : org.egothor.stemmer.CompiledTrieArtifactRegressionTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.CompiledTrieArtifactRegressionTest]/[test-template:shouldMatchGoldenArtifactAndExpectedHash(org.egothor.stemmer.CompiledTrieArtifactRegressionTest$ArtifactCase)]/[test-template-invocation:#2]
removed call to java/io/DataOutputStream::writeUTF → KILLED

209

1.1
Location : read
Killed by : org.egothor.stemmer.StemmerPatchTrieLoaderTest.[engine:junit-jupiter]/[class:org.egothor.stemmer.StemmerPatchTrieLoaderTest]/[nested-class:FilesystemAndParserTests]/[method:shouldRoundTripBinaryTrieAcrossAllBinaryOverloads()]
replaced return value with "" for org/egothor/stemmer/StemmerPatchTrieBinaryIO$StringValueStreamCodec::read → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1