use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.
the class ConverterTest method test.
@Test
public void test() {
SampleNode testSample = testSample();
final List<StackSampleElement> samples = new ArrayList<>();
Converters.convert(Methods.ROOT, testSample, -1, 0, samples::add);
SampleNode back = Converter.convert(samples.iterator());
Assert.assertEquals(testSample, back);
}
use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.
the class Converter method saveLabeledDumps.
public static void saveLabeledDumps(final File file, final Map<String, SampleNode> pcollected) throws IOException {
try (OutputStream bos = newOutputStream(file)) {
final SpecificDatumWriter<StackSampleElement> writer = new SpecificDatumWriter<>(StackSampleElement.SCHEMA$);
final BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
encoder.writeMapStart();
final Map<String, SampleNode> collected = pcollected.entrySet().stream().filter((e) -> e.getValue() != null).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()));
encoder.setItemCount(collected.size());
for (Map.Entry<String, SampleNode> entry : collected.entrySet()) {
encoder.startItem();
encoder.writeString(entry.getKey());
encoder.writeArrayStart();
Converters.convert(Methods.ROOT, entry.getValue(), -1, 0, (final StackSampleElement object) -> {
try {
encoder.setItemCount(1L);
encoder.startItem();
writer.write(object, encoder);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
});
encoder.writeArrayEnd();
}
encoder.writeMapEnd();
encoder.flush();
}
}
use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.
the class Converter method save.
public static void save(final File file, final SampleNode collected) throws IOException {
try (OutputStream bos = newOutputStream(file)) {
final SpecificDatumWriter<StackSampleElement> writer = new SpecificDatumWriter<>(StackSampleElement.getClassSchema());
final BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
Converters.convert(Methods.ROOT, collected, -1, 0, (StackSampleElement object) -> {
try {
writer.write(object, encoder);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
});
encoder.flush();
}
}
use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.
the class Converter method load.
@Nullable
public static SampleNode load(@WillNotClose final InputStream fis) throws IOException {
try (MemorizingBufferedInputStream bis = new MemorizingBufferedInputStream(fis)) {
final PushbackInputStream pis = new PushbackInputStream(bis);
final SpecificDatumReader<StackSampleElement> reader = new SpecificDatumReader<>(StackSampleElement.getClassSchema());
final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(pis, null);
return convert(new Iterator<StackSampleElement>() {
@Override
public boolean hasNext() {
try {
int read = pis.read();
pis.unread(read);
return read >= 0;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
@Override
@SuppressFBWarnings
public StackSampleElement next() {
try {
return reader.read(null, decoder);
} catch (IOException ex) {
NoSuchElementException e = new NoSuchElementException();
e.addSuppressed(ex);
throw e;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
});
}
}
use of org.spf4j.base.avro.StackSampleElement in project spf4j by zolyfarkas.
the class Converter method loadLabels.
/**
* Load the labels from a ssdump3 file.
* @param file the ssdump3 file.
* @throws IOException
*/
public static void loadLabels(final File file, final Consumer<String> labalsConsumer) throws IOException {
try (InputStream bis = newInputStream(file)) {
final SpecificDatumReader<StackSampleElement> reader = new SpecificDatumReader<>(StackSampleElement.SCHEMA$);
final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(bis, null);
long nrItems = decoder.readMapStart();
StackSampleElement asmp = new StackSampleElement();
while (nrItems > 0) {
for (int i = 0; i < nrItems; i++) {
String key = decoder.readString();
labalsConsumer.accept(key);
skipDump(decoder, reader, asmp);
}
nrItems = decoder.mapNext();
}
}
}
Aggregations