Commit 24bd2136 authored by Jonathan Mace's avatar Jonathan Mace

Add a simple baggagecontext impl for the baggage layer. Not intended to be...

Add a simple baggagecontext impl for the baggage layer.  Not intended to be used, more as a demonstration, so it's left untested...
parent 3d326087
package brown.tracingplane.impl;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import brown.tracingplane.BaggageContext;
import brown.tracingplane.atomlayer.ByteBuffers;
import brown.tracingplane.atomlayer.Lexicographic;
import brown.tracingplane.baggageprotocol.BagKey;
import brown.tracingplane.baggageprotocol.BaggageReader;
import brown.tracingplane.baggageprotocol.BaggageWriter;
/**
* <p>
* An implementation of {@link BaggageContext} based on the baggage protocol. It essentially implements nested bags.
* This {@link BaggageContext} is here mostly for demonstration... in practice, either use an atom context, or a BDL
* client context.
* </p>
*/
public class NestedBaggageContext implements BaggageContext {
public boolean didOverflow = false;
public List<ByteBuffer> dataAtoms = new ArrayList<>();
public Map<BagKey, NestedBaggageContext> children = new TreeMap<>();
static NestedBaggageContext parse(BaggageReader reader) {
if (!reader.hasNext()) {
return null;
}
NestedBaggageContext bag = new NestedBaggageContext();
while (reader.hasData()) {
bag.dataAtoms.add(reader.nextData());
}
if (reader.didOverflow()) {
bag.didOverflow = true;
}
while (reader.hasChild()) {
BagKey childKey = reader.enter();
NestedBaggageContext childBag = parse(reader);
if (childBag != null) {
bag.children.put(childKey, childBag);
}
reader.exit();
}
return bag;
}
void serialize(BaggageWriter writer) {
writer.didOverflowHere(didOverflow);
for (ByteBuffer atom : dataAtoms) {
ByteBuffer newAtom = writer.newDataAtom(atom.remaining());
ByteBuffers.copyTo(atom, newAtom);
}
for (BagKey childKey : children.keySet()) {
writer.enter(childKey);
children.get(childKey).serialize(writer);
writer.exit();
}
}
public NestedBaggageContext branch() {
NestedBaggageContext copy = new NestedBaggageContext();
copy.didOverflow = didOverflow;
copy.dataAtoms.addAll(dataAtoms);
for (BagKey childKey : children.keySet()) {
copy.children.put(childKey, children.get(childKey).branch());
}
return copy;
}
public void mergeWith(NestedBaggageContext right) {
if (right == null) {
return;
}
this.didOverflow |= right.didOverflow;
if (this.dataAtoms == null) {
this.dataAtoms = right.dataAtoms;
} else if (right.dataAtoms != null) {
this.dataAtoms = Lexicographic.merge(this.dataAtoms, right.dataAtoms);
}
if (this.children == null) {
this.children = right.children;
} else if (right.children != null) {
for (BagKey childKey : right.children.keySet()) {
NestedBaggageContext leftChild = this.children.get(childKey);
NestedBaggageContext rightChild = right.children.get(childKey);
if (leftChild != null) {
leftChild.mergeWith(rightChild);
} else {
this.children.put(childKey, rightChild);
}
}
}
}
}
package brown.tracingplane.impl;
import java.nio.ByteBuffer;
import java.util.List;
import brown.tracingplane.BaggageContext;
import brown.tracingplane.BaggageProvider;
import brown.tracingplane.atomlayer.AtomLayerSerialization;
import brown.tracingplane.baggageprotocol.BaggageReader;
import brown.tracingplane.baggageprotocol.BaggageWriter;
/**
* <p>
* An implementation of {@link BaggageContext} based on atoms and lexicographic merge. {@link AtomContext} represents
* the minimal logic necessary to propagate {@link BaggageContext}s and participate in the tracing plane.
* </p>
*/
public class NestedBaggageContextProvider implements BaggageProvider<NestedBaggageContext> {
@Override
public boolean isValid(BaggageContext baggage) {
return baggage == null || baggage instanceof NestedBaggageContext;
}
@Override
public NestedBaggageContext newInstance() {
return null;
}
@Override
public void discard(NestedBaggageContext baggage) {}
@Override
public NestedBaggageContext branch(NestedBaggageContext from) {
return from == null ? null : from.branch();
}
@Override
public NestedBaggageContext join(NestedBaggageContext left, NestedBaggageContext right) {
if (left == null) {
return right;
} else {
left.mergeWith(right);
return left;
}
}
@Override
public NestedBaggageContext deserialize(byte[] serialized, int offset, int length) {
List<ByteBuffer> atoms = AtomLayerSerialization.deserialize(serialized, offset, length);
return NestedBaggageContext.parse(BaggageReader.create(atoms));
}
@Override
public NestedBaggageContext deserialize(ByteBuffer buf) {
List<ByteBuffer> atoms = AtomLayerSerialization.deserialize(buf);
return NestedBaggageContext.parse(BaggageReader.create(atoms));
}
@Override
public byte[] serialize(NestedBaggageContext baggage) {
if (baggage == null) {
return null;
}
BaggageWriter writer = BaggageWriter.create();
baggage.serialize(writer);
List<ByteBuffer> atoms = writer.atoms();
return AtomLayerSerialization.serialize(atoms);
}
@Override
public byte[] serialize(NestedBaggageContext baggage, int maximumSerializedSize) {
if (baggage == null) {
return null;
}
BaggageWriter writer = BaggageWriter.create();
baggage.serialize(writer);
List<ByteBuffer> atoms = writer.atoms();
return AtomLayerSerialization.serialize(atoms, maximumSerializedSize);
}
}
package brown.tracingplane.impl;
import brown.tracingplane.BaggageContext;
import brown.tracingplane.BaggageProvider;
import brown.tracingplane.BaggageProviderFactory;
/**
* {@link BaggageProviderFactory} for {@link NestedBaggageContextProvider}
*/
public class NestedBaggageContextProviderFactory implements BaggageProviderFactory {
@Override
public BaggageProvider<? extends BaggageContext> provider() {
return new NestedBaggageContextProvider();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment