Skip to content

Instantly share code, notes, and snippets.

(Avro) -> Size 43 Bytes
{"tradeId": 454442738626075203, "customerId": 1924973958993118808, "qty": 100, "tradeType": "Buy", "symbol": "GOOGL", "exchange": "NYSE"}
(Avro) -> Size 43 Bytes
{"tradeId": 1984810212692753661, "customerId": -7397692262047989958, "qty": 100, "tradeType": "Sell", "symbol": "AAPL", "exchange": "NYSE"}
(chronicle) -> Size 35 Bytes
CVCNX†©ãá¶NYSEBuyGOOGLd
(chronicle) -> Size 35 Bytes
ý|Z_v‹:?ùV™NYSESellAAPLd
Object[][] data = new Object[][]{
{new Random().nextLong(), new Random().nextLong(), "NYSE", "Buy", "GOOGL", 100},
{new Random().nextLong(), new Random().nextLong(), "NYSE", "Sell", "AAPL", 100},
};
process("Avro", Arrays.stream(data), AvroTradeRecordBuilder::newTrade, AvroTradeRecordBuilder::toBytes, AvroTradeRecordBuilder::fromBytes);
process("chronicle", Arrays.stream(data), ChronicleTradeRecordBuilder::newTrade, ChronicleTradeRecordBuilder::toBytes, ChronicleTradeRecordBuilder::fromBytes);
process("sbe", Arrays.stream(data), SBETradeRecordBuilder::newTrade, SBETradeRecordBuilder::toBytes, SBETradeRecordBuilder::fromBytes);
process("csv", Arrays.stream(data), CSVTradeRecordBuilder::newTrade, CSVTradeRecordBuilder::toBytes, CSVTradeRecordBuilder::fromBytes);
long tradeId , customerId;
int qty;
TradeType tradeType;
String symbol,exchange;
List functions = asList(
SerCode.f((Integer x) -> x * x),
SerCode.f((String x) -> x.toUpperCase()),
SerCode.p((String x) -> x.length() > 5)
);
byte[] code = saveFunction(functions);
ObjectInputStream fStream = codeStream(code);
List rFunctions = (List) fStream.readObject();
@FunctionalInterface
public interface SerFunction<T, R> extends Function<T, R>, Serializable {
}
@FunctionalInterface
public interface SerPredicate<T> extends Predicate<T>, Serializable {
}
....
@Test()
public void save_function_works() throws Exception {
// Addtional casting allow to mark as serilized
Function<String, String> upper = (Function<String, String> & Serializable) x -> x.toUpperCase();
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bos)) {
os.writeObject(upper);
@Test
public void save_function() throws Exception {
Function<String, String> upper = x -> x.toUpperCase();
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bos)) {
os.writeObject(upper);
}
}
@Test
public void square_number() {
Function<Integer, Integer> sqr = x -> x * x;
assertEquals(4, sqr.apply(2));
assertEquals(9, sqr.apply(3));
assertEquals(16, sqr.apply(4));
}
public class RingBuffer<T> {
public RingBuffer(int size) {
this.capacity = Bits.powOf2(size);
this.mask = capacity - 1;
buffer = new Object[this.capacity];
}
private int offset(int index) {return index & mask;
//return index % capacity;
}
public void restoreDisk() {
RaidDisk disk1 = new RaidDisk(2);
disk1.set(0, MoreInts.toByte("01101101"));
disk1.set(1, MoreInts.toByte("00101101"));
RaidDisk disk2 = new RaidDisk(1);
disk2.set(0, MoreInts.toByte("11010100"));
RaidDisk raidDisk = disk1.xor(disk2); // This xor allow to keep data for both disk in RaidDisk