use custom csv writer for performance

This commit is contained in:
2016-12-13 18:41:19 +01:00
parent 876520eb4c
commit fa4921fcc9
5 changed files with 43 additions and 63 deletions

View File

@@ -9,10 +9,13 @@ class PdbFile {
private final File file;
private final long offsetInEpochMilli;
public PdbFile(final Day day, final File file, final Tags tags) {
this.day = day;
this.file = file;
this.tags = tags;
offsetInEpochMilli = day.getOffsetInEpochMilli();
}
public static PdbFile today(final File file, final Tags tags) {
@@ -38,7 +41,7 @@ class PdbFile {
}
public long getOffsetInEpochMilli() {
return getTimeRange().getFrom().toInstant().toEpochMilli();
return offsetInEpochMilli;
}
@Override

View File

@@ -34,9 +34,9 @@ public class PdbFileIterator implements Iterator<Entry>, AutoCloseable {
if (reader == null) {
return null;
}
final Optional<Entry> optionalEntry = reader.readEntry(currentPdbFile.getTags());
final Entry entry = reader.readNullableEntry(currentPdbFile.getTags());
return optionalEntry.orElseGet(() -> {
if (entry == null) {
nextFile();
if (reader == null) {
return null;
@@ -44,7 +44,9 @@ public class PdbFileIterator implements Iterator<Entry>, AutoCloseable {
final Tags tags = currentPdbFile.getTags();
return reader.readEntry(tags).orElse(null);
}
});
}
return entry;
}

View File

@@ -30,8 +30,6 @@ class PdbReader implements AutoCloseable {
* @return the value or -1 if end of stream has been reached
*/
public long readValue() {
assertPositionIsAValuePosition();
return read();
}
@@ -42,13 +40,11 @@ class PdbReader implements AutoCloseable {
* @throws IOException
*/
public long readEpochMilli() {
assertPositionIsADatePosition();
final long value = read();
if (value < 0) {
return -1;
}
return pdbFile.getDay().getOffsetInEpochMilli() + value;
return pdbFile.getOffsetInEpochMilli() + value;
}
public OffsetDateTime readDate() {
@@ -65,11 +61,12 @@ class PdbReader implements AutoCloseable {
try {
final int read = data.read(buffer);
if (read < 0) {
return -1;
}
if (read != BYTES_PER_VALUE) {
throw new IllegalStateException("invalid file");
if (read < 0) {
return -1;
} else {
throw new IllegalStateException("invalid file");
}
}
return BitFiddling.makeLong(buffer[0], buffer[1], buffer[2], buffer[3]);
} catch (final IOException e) {
@@ -77,40 +74,6 @@ class PdbReader implements AutoCloseable {
}
}
private void assertPositionIsADatePosition() {
try {
assertPositionIsValid();
if ((data.getFilePointer() / BYTES_PER_VALUE) % 2 != 0) {
throw new IllegalStateException("file pointer is not at a date position: " + data.getFilePointer());
}
} catch (final IOException e) {
throw new ReadRuntimeException(e);
}
}
private void assertPositionIsAValuePosition() {
assertPositionIsValid();
try {
if ((data.getFilePointer() / BYTES_PER_VALUE) % 2 != 1) {
throw new IllegalStateException("file pointer is not at a value position: " + data.getFilePointer());
}
} catch (final IOException e) {
throw new ReadRuntimeException(e);
}
}
private void assertPositionIsValid() {
try {
if (data.getFilePointer() % BYTES_PER_VALUE != 0) {
throw new IllegalStateException("file pointer is at an illegal position. It is at "
+ data.getFilePointer() + " which is not divisible by " + BYTES_PER_VALUE);
}
} catch (final IOException e) {
throw new ReadRuntimeException(e);
}
}
/**
* Seek to the n-th value.
*
@@ -183,17 +146,22 @@ class PdbReader implements AutoCloseable {
}
}
public Optional<Entry> readEntry(final Tags tags) throws ReadRuntimeException {
Entry readNullableEntry(final Tags tags) throws ReadRuntimeException {
final long epochMilli = readEpochMilli();
if (epochMilli < 0) {
return Optional.empty();
return null;
}
final long value = readValue();
if (value < 0) {
return Optional.empty();
return null;
}
return Optional.of(new Entry(epochMilli, value, tags));
return new Entry(epochMilli, value, tags);
}
public Optional<Entry> readEntry(final Tags tags) throws ReadRuntimeException {
return Optional.ofNullable(readNullableEntry(tags));
}
}