Skip to content

Commit

Permalink
expand testing of Writer and WriterMT
Browse files Browse the repository at this point in the history
  • Loading branch information
carltimmer committed Dec 17, 2024
1 parent a8ea448 commit d8141d9
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 46 deletions.
104 changes: 79 additions & 25 deletions java/org/jlab/coda/jevio/test/HipoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jlab.coda.hipo.*;
import org.jlab.coda.jevio.*;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand All @@ -27,56 +28,90 @@ void testCompactEventCreation(int tag, int num) throws EvioException, IOExceptio
// Create ByteBuffer with EvioEvent in it
buffer = createCompactEventBuffer(tag, num, ByteOrder.nativeOrder(), 200000, null);

Utilities.printBytes(buffer, 0, 100, "BUFFER BYTES");

Utilities.printBytes(buffer, 0, buffer.limit(), "BUFFER BYTES");
System.out.println("\nBuffer -> \n" + buffer.toString());


//
// Write file
//
Writer writer = new Writer(HeaderType.EVIO_FILE, ByteOrder.nativeOrder(),
0, 0, null, null,
CompressionType.RECORD_UNCOMPRESSED, false);

writer.open(writeFileName1);
//WriterMT writer = new WriterMT();

// WriterMT writer = new WriterMT(order, 1000, 1000000,
// CompressionType.RECORD_COMPRESSION_LZ4_BEST,
// 3, 4);

WriterMT writer = new WriterMT(HeaderType.EVIO_FILE, order, 1000, 1000000,
dictionary, buffer.array(), buffer.limit(),
CompressionType.RECORD_COMPRESSION_LZ4_BEST,
3, false, 4);

//
// Writer writer = new Writer(HeaderType.EVIO_FILE, ByteOrder.nativeOrder(),
// 0, 0, dictionary, buffer.array(), buffer.limit(),
// CompressionType.RECORD_COMPRESSION_LZ4_BEST, false);

writer.open(writeFileName1, null, true);
writer.addEvent(buffer);
writer.close();
File f = new File (writeFileName1);
System.out.println("File size of " + writeFileName1 + " is " + f.length());
Utilities.printBytes(writeFileName1, 0, 200, "WRITTEN FILE BYTES");


Utilities.printBytes(writeFileName1, 0, 100, "WRITTEN FILE BYTES");
writer.open(writeFileName1, null, true);
System.out.println("\nCall open again, rewrite 3 events to file");
writer.addEvent(buffer);
writer.addEvent(buffer);
writer.addEvent(buffer);
writer.close();

System.out.println("\n\nAfter writer closed ... ");
f = new File (writeFileName1);
System.out.println("File size of " + writeFileName1 + " is now " + f.length());

Utilities.printBytes(writeFileName1, 0, 200, "WRITTEN FILE BYTES 2");

System.out.println("\n\nRead file ...");

// Read event back out of file
Reader reader = new Reader(writeFileName1);

System.out.println("createCompactEvents: have dictionary? " + reader.hasDictionary());
System.out.println("have dictionary? " + reader.hasDictionary());
String xmlDict = reader.getDictionary();
System.out.println("createCompactEvents: read dictionary ->\n\n" + xmlDict);
System.out.println("\ncreateCompactEvents: try getting getNextEvent");
if (reader.hasDictionary()) {
System.out.println("dictionary ->\n\n" + xmlDict + "\n");
}

System.out.println("have first event? " + reader.hasFirstEvent());
if (reader.hasFirstEvent()) {
byte[] firstEvt = reader.getFirstEvent();
if (firstEvt != null) {
System.out.println("first event len = " + firstEvt.length);
}
}

System.out.println("\ntry getting getNextEvent");
if (reader.getEventCount() < 1) {
System.out.println("createCompactEvents: no data events in file");
System.out.println("no data events in file");
return;
}

int len;
byte[] bytes = reader.getNextEvent();
EvioEvent ev = EvioReader.getEvent(bytes, 0, reader.getByteOrder());
System.out.println("createCompactEvents: next evio event ->\n" + ev.treeToString(""));
System.out.println("next evio event ->\n" + ev.treeToString(""));

byte[] bytes2 = reader.getEvent(0);
System.out.println("createCompactEvents: get event(0), size = " + bytes2.length);
System.out.println("get event(0), size = " + bytes2.length);

ByteBuffer bb1 = ByteBuffer.allocate(20000);
reader.getEvent(bb1, 0);
System.out.println("createCompactEvents: event 1, bb1 limit ->\n" + bb1.limit());
bytes2 = reader.getEvent(1);
System.out.println("get event(1), size = " + bytes2.length);

bytes2 = reader.getEvent(2);
System.out.println("get event(2), size = " + bytes2.length);

ByteBuffer bb2 = ByteBuffer.allocate(20000);
reader.getEvent(bb2, 0);
System.out.println("createCompactEvents: event 1, bb2 limit ->\n" + bb2.limit());
ByteBuffer bb1 = ByteBuffer.allocate(20000);
reader.getEvent(bb1, 0);
System.out.println("event 1, ByteBuffer limit = " + bb1.limit());
}
catch (EvioException e) {
e.printStackTrace();
Expand All @@ -88,9 +123,20 @@ void testCompactEventCreation(int tag, int num) throws EvioException, IOExceptio
void testTreeEventCreation(int tag, int num) throws IOException {

try {
// Build event (bank of banks) with EventBuilder object
// Build an event using original evio tree interface.
// NOTE: doing things this way means that event.getRawBytes() returns null! */
EvioEvent event = createTreeEvent(tag, num);

// Create node from this event
int size = event.getTotalBytes();
ByteBuffer evioDataBuf = ByteBuffer.allocate(size);
evioDataBuf.order(ByteOrder.nativeOrder());
event.write(evioDataBuf);
evioDataBuf.flip();

EvioNode node = EvioNode.extractEventNode(evioDataBuf, null, 0, 0, 0);


System.out.println("Event:\n" + event.treeToString(""));
System.out.println("Event Header:\n" + event.getHeader().toString());

Expand All @@ -102,6 +148,10 @@ void testTreeEventCreation(int tag, int num) throws IOException {

writer.setFirstEvent(event);
writer.writeEvent(event);

// Test double sync
writer.writeEvent(node, false, false);

System.out.println(" createObjectEvents: call writer.close()");
writer.close();

Expand All @@ -120,6 +170,10 @@ void testTreeEventCreation(int tag, int num) throws IOException {
System.out.println("\n createObjectEvents: try getting ev #1");
EvioEvent ev = reader.parseEvent(1);
System.out.println(" createObjectEvents: event ->\n" + ev.treeToString(""));

System.out.println("\n createObjectEvents: try getting ev #2");
ev = reader.parseEvent(2);
System.out.println(" createObjectEvents: event ->\n" + ev.treeToString(""));
}
catch (EvioException e) {
e.printStackTrace();
Expand All @@ -131,8 +185,8 @@ void testTreeEventCreation(int tag, int num) throws IOException {
public static void main(String args[]) {
try {
HipoTest tester = new HipoTest();
//tester.testCompactEventCreation(1,1);
tester.testTreeEventCreation(1,1);
tester.testCompactEventCreation(1,1);
//tester.testTreeEventCreation(1,1);
}
catch (Exception e) {
e.printStackTrace();
Expand Down
83 changes: 62 additions & 21 deletions src/test/Hipo_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// (757)-269-7100


#include <iostream>
#include <fstream>

#include "TestBase.h"
#include "eviocc.h"
Expand All @@ -27,65 +29,104 @@ namespace evio {
public:



std::streamsize getFileSize(const std::string& filePath) {
std::ifstream file(filePath, std::ios::binary | std::ios::ate); // Open file in binary mode at the end
if (!file) {
std::cerr << "Error opening file: " << filePath << std::endl;
return -1;
}
return file.tellg(); // Get the position, which is the file size
}



/** Writing to a buffer using CompactEventBuilder interface. */
void testCompactEventCreation(uint16_t tag, uint8_t num) {

try {
// Create ByteBuffer with EvioEvent in it
buffer = createCompactEventBuffer(tag, num);


Util::printBytes(buffer, 0, 100, "BUFFER BYTES");

Util::printBytes(buffer, 0, buffer->limit(), "BUFFER BYTES");
std::cout << "\nBuffer -> \n" << buffer->toString() << "\n";


//
// Write file
//
Writer writer(HeaderType::EVIO_FILE, ByteOrder::ENDIAN_LOCAL,
0, 0, "", nullptr, 0,
Compressor::UNCOMPRESSED, false);
// Writer writer(HeaderType::EVIO_FILE, ByteOrder::ENDIAN_LOCAL,
// 0, 0, "", nullptr, 0,
// Compressor::UNCOMPRESSED, false);

WriterMT writer(HeaderType::EVIO_FILE, ByteOrder::ENDIAN_LOCAL,
0, 0, dictionary, buffer->array(), buffer->limit(),
Compressor::UNCOMPRESSED, 1);

writer.open(writeFileName1);
// WriterMT writer(HeaderType::EVIO_FILE, ByteOrder::ENDIAN_LOCAL,
// 0, 0, dictionary, buffer->array(), buffer->limit(),
// Compressor::LZ4_BEST, 3);

writer.open(writeFileName1, nullptr, true);
writer.addEvent(buffer);
writer.close();
std::cout << "File size of " << writeFileName1 << " is " << getFileSize(writeFileName1) << std::endl;
Util::printBytes(writeFileName1, 0, 200, "WRITTEN FILE BYTES");

writer.open(writeFileName1, nullptr, true);
std::cout << std::endl<< "Call open again, rewrite 3 events to file" << std::endl;
writer.addEvent(buffer);
writer.addEvent(buffer);
writer.addEvent(buffer);
writer.close();
std::cout << "File size of " << writeFileName1 << " is now " << getFileSize(writeFileName1) << std::endl;

Util::printBytes(writeFileName1, 0, 100, "WRITTEN FILE BYTES");
Util::printBytes(writeFileName1, 0, 200, "WRITTEN FILE BYTES 2");

std::cout << "\n\nAfter writer closed ... \n";
std::cout << "\n\nRead file ...\n";

// Read event back out of file
Reader reader(writeFileName1);

std::cout << "createCompactEvents: have dictionary? " << reader.hasDictionary() << std::endl;
std::cout << "have dictionary? " << reader.hasDictionary() << std::endl;
std::string xmlDict = reader.getDictionary();
std::cout << "createCompactEvents: read dictionary ->\n\n" << xmlDict << std::endl << std::endl;
if (reader.hasDictionary()) {
std::cout << "dictionary ->\n\n" << xmlDict << std::endl << std::endl;
}

std::cout << "createCompactEvents: try getting getNextEvent" << std::endl;
std::cout << "have first event? " << reader.hasFirstEvent() << std::endl;
if (reader.hasFirstEvent()) {
uint32_t feSize = 0;
std::shared_ptr<uint8_t> fe = reader.getFirstEvent(&feSize);
std::cout << "first event len = " << feSize << std::endl;
}

std::cout << "\ntry getting getNextEvent" << std::endl;
if (reader.getEventCount() < 1) {
std::cout << "createCompactEvents: no data events in file" << std::endl;
std::cout << "no data events in file" << std::endl;
return;
}

uint32_t len;
std::shared_ptr<uint8_t> bytes = reader.getNextEvent(&len);
auto ev = EvioReader::getEvent(bytes.get(), len, reader.getByteOrder());
std::cout << "createCompactEvents: next evio event ->\n" << ev->treeToString("") << std::endl;
std::cout << "next evio event ->\n" << ev->treeToString("") << std::endl;

std::shared_ptr<uint8_t> bytes2 = reader.getEvent(0, &len);
std::cout << "createCompactEvents: get event(0), size = " << std::to_string(len) << std::endl << std::endl;
std::cout << "get event(0), size = " << std::to_string(len) << std::endl;

reader.getEvent(1, &len);
std::cout << "get event(1), size = " << std::to_string(len) << std::endl;

reader.getEvent(1, &len);
std::cout << "get event(2), size = " << std::to_string(len) << std::endl;

ByteBuffer bb1(20000);
reader.getEvent(bb1, 0);
std::cout << "createCompactEvents: event 1, bb1 limit ->\n" << bb1.limit() << std::endl;

std::cout << "event 1, ByteBuffer limit = " << bb1.limit() << std::endl;

auto bb2 = std::make_shared<ByteBuffer>(20000);
reader.getEvent(bb2, 0);
std::cout << "createCompactEvents: event 1, bb2 limit ->\n" << bb2->limit() << std::endl;
std::cout << "event 1, std::shared_ptr<ByteBuffer> limit = " << bb2->limit() << std::endl;
}
catch (EvioException &e) {
std::cout << e.what() << std::endl;
Expand Down Expand Up @@ -146,8 +187,8 @@ namespace evio {

int main(int argc, char **argv) {
auto tester = evio::HipoTester();
//tester.testCompactEventCreation(1,1);
tester.testTreeEventCreation(1,1);
tester.testCompactEventCreation(1,1);
//tester.testTreeEventCreation(1,1);
return 0;
}

Expand Down

0 comments on commit d8141d9

Please sign in to comment.