cleanup after revert
This commit is contained in:
@@ -1,127 +0,0 @@
|
||||
package org.lucares.pdbui;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.lucares.pdb.api.Tags;
|
||||
import org.lucares.pdb.api.TagsBuilder;
|
||||
import org.lucares.pdb.datastore.Entries;
|
||||
import org.lucares.pdb.datastore.Entry;
|
||||
import org.lucares.performance.db.PdbExport;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* File format goals: Minimal size/ minimal repetition while also providing a
|
||||
* file format that can be used for "normal" ingestion, not just backup/restore.
|
||||
* It should be easy to implement in any language. It should be easy to debug.
|
||||
* <p>
|
||||
* Note: Line breaks are written as {@code \n}.
|
||||
*
|
||||
* <pre>
|
||||
* # // # is the magic byte for the file format used to detect this format
|
||||
* $123:key1=value1,key2=value2\n // $ marks the beginning of a dictionary entry that says: the following number will be used to refer to the following tags.
|
||||
* // In this case the tags key1=value1,key2=value2 will be identified by 123.
|
||||
* // The newline is used as an end marker.
|
||||
* 1534567890,456,123\n // Defines an entry with timestamp 1534567890, duration 456 and tags key1=value1,key2=value2.
|
||||
* 1,789,123\n // Timestamps are encoded using delta encoding. That means this triple defines
|
||||
* // an entry with timestamp 1534567891, duration 789 and tags key1=value1,key2=value2
|
||||
* -2,135,123\n // Timestamp delta encoding can contain negative numbers. This triple defines an entry
|
||||
* // with timestamp 1534567889, duration 135 and tags key1=value1,key2=value2
|
||||
* </pre>
|
||||
*/
|
||||
|
||||
public class CustomExportFormatToEntryTransformer {
|
||||
|
||||
private static final int ENTRY_BUFFER_SIZE = 100;
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomExportFormatToEntryTransformer.class);
|
||||
|
||||
private final Pattern splitByComma = Pattern.compile(",");
|
||||
|
||||
private final Map<Long, Tags> tagsDictionary = new HashMap<>();
|
||||
|
||||
private long lastEpochMilli;
|
||||
|
||||
public void read(final BufferedReader in, final ArrayBlockingQueue<Entries> queue) throws IOException {
|
||||
|
||||
Entries bufferedEntries = new Entries(ENTRY_BUFFER_SIZE);
|
||||
|
||||
try {
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
try {
|
||||
if (line.startsWith(PdbExport.MARKER_DICT_ENTRY)) {
|
||||
readDictionaryEntry(line);
|
||||
} else {
|
||||
final Entry entry = readEntry(line);
|
||||
if (entry != null) {
|
||||
|
||||
bufferedEntries.add(entry);
|
||||
|
||||
if (bufferedEntries.size() == ENTRY_BUFFER_SIZE) {
|
||||
queue.put(bufferedEntries);
|
||||
bufferedEntries = new Entries(ENTRY_BUFFER_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
LOGGER.error("ignoring line '{}'", line, e);
|
||||
}
|
||||
queue.put(bufferedEntries);
|
||||
bufferedEntries = new Entries(ENTRY_BUFFER_SIZE);
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
LOGGER.info("aborting because of interruption");
|
||||
}
|
||||
}
|
||||
|
||||
private Entry readEntry(final String line) {
|
||||
|
||||
final String[] timeValueTags = splitByComma.split(line);
|
||||
|
||||
final long timeDelta = Long.parseLong(timeValueTags[0]);
|
||||
final long value = Long.parseLong(timeValueTags[1]);
|
||||
final long tagsId = Long.parseLong(timeValueTags[2]);
|
||||
|
||||
lastEpochMilli = lastEpochMilli + timeDelta;
|
||||
|
||||
final Tags tags = tagsDictionary.get(tagsId);
|
||||
if (tags == null) {
|
||||
LOGGER.info("no tags available for tagsId {}. Ignoring line '{}'", tagsId, line);
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Entry(lastEpochMilli, value, tags);
|
||||
}
|
||||
|
||||
private void readDictionaryEntry(final String line) {
|
||||
final String[] tagsIdToSerializedTags = line.split(Pattern.quote(PdbExport.SEPARATOR_TAG_ID));
|
||||
|
||||
final Long tagId = Long.parseLong(tagsIdToSerializedTags[0], 1, tagsIdToSerializedTags[0].length(), 10);
|
||||
final Tags tags = tagsFromCsv(tagsIdToSerializedTags[1]);
|
||||
tagsDictionary.put(tagId, tags);
|
||||
}
|
||||
|
||||
public static Tags tagsFromCsv(final String line) {
|
||||
|
||||
final TagsBuilder tagsBuilder = new TagsBuilder();
|
||||
final String[] tagsAsString = line.split(Pattern.quote(","));
|
||||
|
||||
for (final String tagAsString : tagsAsString) {
|
||||
final String[] keyValue = tagAsString.split(Pattern.quote("="));
|
||||
|
||||
final int key = Tags.STRING_COMPRESSOR.put(keyValue[0]);
|
||||
final int value = Tags.STRING_COMPRESSOR.put(keyValue[1]);
|
||||
tagsBuilder.add(key, value);
|
||||
}
|
||||
|
||||
return tagsBuilder.build();
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ import java.util.zip.GZIPInputStream;
|
||||
import org.lucares.pdb.datastore.Entries;
|
||||
import org.lucares.pdb.datastore.Entry;
|
||||
import org.lucares.pdbui.CsvReaderSettings.ColumnDefinitions;
|
||||
import org.lucares.performance.db.PdbExport;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
|
||||
@@ -58,8 +57,6 @@ public final class IngestionHandler implements Callable<Void> {
|
||||
if (firstByte == '{') {
|
||||
in.reset();
|
||||
readJSON(in);
|
||||
} else if (firstByte == PdbExport.MAGIC_BYTE) {
|
||||
readCustomExportFormat(in);
|
||||
} else if (isGZIP(firstByte)) {
|
||||
in.reset();
|
||||
final GZIPInputStream gzip = new GZIPInputStream(in);
|
||||
@@ -80,15 +77,6 @@ public final class IngestionHandler implements Callable<Void> {
|
||||
return firstByte == 0x1f;
|
||||
}
|
||||
|
||||
private void readCustomExportFormat(final InputStream in) throws IOException {
|
||||
|
||||
final CustomExportFormatToEntryTransformer transformer = new CustomExportFormatToEntryTransformer();
|
||||
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
|
||||
transformer.read(reader, queue);
|
||||
|
||||
}
|
||||
|
||||
private void readJSON(final InputStream in) throws IOException, InterruptedException {
|
||||
final int chunksize = 100;
|
||||
Entries entries = new Entries(chunksize);
|
||||
|
||||
Reference in New Issue
Block a user