unify name of CsvOutptPlugin impl

This commit is contained in:
Jonathan Shook 2021-09-14 12:05:10 -05:00
parent 668f9f35ea
commit 9d96131bbf
4 changed files with 24 additions and 23 deletions

View File

@ -1,4 +0,0 @@
package io.nosqlbench.engine.extensions.csvoutput;
public class CsvOutput {
}

View File

@ -2,7 +2,7 @@ package io.nosqlbench.engine.extensions.csvoutput;
public class CsvOutputPluginInstance { public class CsvOutputPluginInstance {
public CsvOutput open(String filename, String... headers) { public CsvOutputPluginWriter open(String filename, String... headers) {
return new CsvOutputWriter(filename, headers); return new CsvOutputPluginWriter(filename, headers);
} }
} }

View File

@ -8,23 +8,28 @@ import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.*; import java.util.*;
public class CsvOutputWriter extends CsvOutput { public class CsvOutputPluginWriter extends CsvOutput {
private final CSVPrinter printer; private final CSVPrinter printer;
private final FileWriter filewriter; private final FileWriter filewriter;
private final LinkedHashSet<String> headerKeys; private final LinkedHashSet<String> headerKeys;
private final String filename; private final String filename;
public CsvOutputWriter(String filename, String... headers) { public CsvOutputPluginWriter(String filename, String... headers) {
this.filename = filename;
CSVFormat fmt = CSVFormat.DEFAULT;
this.headerKeys = new LinkedHashSet<>(Arrays.asList(headers));
try { try {
this.filewriter = new FileWriter(filename); this.filename = filename;
this.printer = new CSVPrinter(filewriter,fmt); Path filepath = Path.of(filename);
if (Files.size(Path.of(filename))==0) { Files.createDirectories(filepath.getParent(), PosixFilePermissions.asFileAttribute(
PosixFilePermissions.fromString("rwxr-x---")
));
CSVFormat fmt = CSVFormat.DEFAULT;
this.headerKeys = new LinkedHashSet<>(Arrays.asList(headers));
this.filewriter = new FileWriter(filepath.toString());
this.printer = new CSVPrinter(filewriter, fmt);
if (Files.size(Path.of(filename)) == 0) {
printer.printRecord(headerKeys); printer.printRecord(headerKeys);
printer.flush(); printer.flush();
} }
@ -33,21 +38,21 @@ public class CsvOutputWriter extends CsvOutput {
} }
} }
public CsvOutputWriter write(Value value) { public CsvOutputPluginWriter write(Value value) {
List<String> lineout = new ArrayList<>(); List<String> lineout = new ArrayList<>();
Map<String,String> provided = new HashMap<>(); Map<String, String> provided = new HashMap<>();
if (value.isHostObject()) { if (value.isHostObject()) {
Object o = value.asHostObject(); Object o = value.asHostObject();
if (o instanceof Map) { if (o instanceof Map) {
((Map<?, ?>) o).forEach((k,v) -> { ((Map<?, ?>) o).forEach((k, v) -> {
provided.put(k.toString(),v.toString()); provided.put(k.toString(), v.toString());
}); });
} else { } else {
throw new RuntimeException("host object provided as '" + o.getClass().getCanonicalName()+ ", but only Maps are supported."); throw new RuntimeException("host object provided as '" + o.getClass().getCanonicalName() + ", but only Maps are supported.");
} }
} else if (value.hasMembers()) { } else if (value.hasMembers()) {
for (String vkey : value.getMemberKeys()) { for (String vkey : value.getMemberKeys()) {
provided.put(vkey,value.getMember(vkey).toString()); provided.put(vkey, value.getMember(vkey).toString());
} }
} else { } else {
throw new RuntimeException("Value was not a Map host object nor a type with members."); throw new RuntimeException("Value was not a Map host object nor a type with members.");
@ -60,7 +65,7 @@ public class CsvOutputWriter extends CsvOutput {
lineout.add(""); lineout.add("");
} }
} }
if (provided.size()>0) { if (provided.size() > 0) {
throw new RuntimeException("Unqualified column was emitted for file '" + filename); throw new RuntimeException("Unqualified column was emitted for file '" + filename);
} }

View File

@ -7,14 +7,14 @@ import org.junit.jupiter.api.Test;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
public class CsvOutputWriterTest { public class CsvOutputPluginWriterTest {
@Test @Test
public void testCsvOutputWriter() { public void testCsvOutputWriter() {
File tmpfile = Files.newTemporaryFile(); File tmpfile = Files.newTemporaryFile();
tmpfile.deleteOnExit(); tmpfile.deleteOnExit();
System.out.println("tmpfile="+ tmpfile.getPath()); System.out.println("tmpfile="+ tmpfile.getPath());
CsvOutputWriter out = new CsvOutputWriter(tmpfile.getPath(), "one", "two"); CsvOutputPluginWriter out = new CsvOutputPluginWriter(tmpfile.getPath(), "one", "two");
out.write(Value.asValue(Map.of("one","one_","two","two_"))); out.write(Value.asValue(Map.of("one","one_","two","two_")));
} }