Skip to content

Commit

Permalink
objectionary#1326: Use csv instead of serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Apr 27, 2023
1 parent b916d11 commit c823748
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
5 changes: 5 additions & 0 deletions eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ SOFTWARE.
<version>0.7.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.10.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.cactoos.list.ListOf;
import org.eolang.maven.footprint.FtDefault;
import org.eolang.maven.util.Home;
Expand Down Expand Up @@ -98,14 +104,26 @@ public String name(final String code, final String dependencies) {
*/
public void save() throws IOException {
Files.createDirectories(this.dest.getParent());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this.all);
oos.flush();
new Home(this.dest.getParent()).save(
new String(Base64.getEncoder().encode(baos.toByteArray())),
this.dest.getFileName()
);
StringWriter sw = new StringWriter();

CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setHeader(new String[]{"code", "name"})
.build();

try (final CSVPrinter printer = new CSVPrinter(sw, csvFormat)) {
this.all.forEach((code, name) -> {
try {
printer.printRecord(code, name);
} catch (IOException e) {
e.printStackTrace();
}
});
new Home(this.dest.getParent()).save(
sw.toString(),
this.dest.getFileName()
);
}

}

/**
Expand All @@ -119,19 +137,18 @@ private static ConcurrentHashMap<String, String> load(final Path src) throws IOE
src.getFileName().toString(),
""
);
final ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(Base64.getDecoder().decode(content))
);
try {
return (ConcurrentHashMap<String, String>) ois.readObject();
} catch (final ClassNotFoundException exc) {
throw new IllegalArgumentException(
String.format(
"File %s contains invalid data",
src
),
exc
);
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setHeader("code", "name")
.setSkipHeaderRecord(true)
.build();

Iterable<CSVRecord> records = csvFormat
.parse(new StringReader(content));

ConcurrentHashMap<String, String> res = new ConcurrentHashMap<>();
for (CSVRecord record : records) {
res.put(record.get("code"), record.get("name"));
}
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void solvesSameHashes(@TempDir final Path temp) throws IOException {

@Test
void recoversNames(@TempDir final Path temp) throws IOException {
final List<String> codes = IntStream.range(0, 1000)
final List<String> codes = IntStream.range(0, 1000000)
.mapToObj(String::valueOf)
.collect(Collectors.toList());
final String dependency = "dependency";
Expand All @@ -75,7 +75,10 @@ void recoversNames(@TempDir final Path temp) throws IOException {
Matchers.equalTo(functions.size())
);
before.save();
final double start = System.currentTimeMillis();
final Names after = new Names(temp);
final double finish = System.currentTimeMillis();
System.out.printf("time elapsed %f\n", finish - start);
final ListIterator<String> iterator = codes.listIterator(codes.size());
while (iterator.hasPrevious()) {
final String code = iterator.previous();
Expand Down

0 comments on commit c823748

Please sign in to comment.