Commit d0251cfc authored by Jonathan Mace's avatar Jonathan Mace

Minor changes to previous commit to get tests passing

parent c1fe53eb
......@@ -16,15 +16,17 @@ type Reader struct {
Err error
}
func Read(baggage []atomlayer.Atom) (r Reader) {
func Read(baggage []atomlayer.Atom) *Reader {
var r Reader
r.remaining = baggage
r.level = -1
r.advance()
return
return &r
}
// Reads data from the specified bag, only tracking skipped atoms from this bag.
func Open(baggage []atomlayer.Atom, bagIndex uint64) (r Reader) {
func Open(baggage []atomlayer.Atom, bagIndex uint64) *Reader {
var r Reader
target := MakeIndexedHeader(0, bagIndex)
exists, overflowed, i := find(baggage, 0, target)
......@@ -37,7 +39,7 @@ func Open(baggage []atomlayer.Atom, bagIndex uint64) (r Reader) {
}
r.advance()
return
return &r
}
// Closes the Reader, treating all remaining atoms as skipped
......
......@@ -9,6 +9,23 @@ import (
"github.com/gogo/protobuf/proto"
)
func header(level int, index uint64) atomlayer.Atom {
return MakeIndexedHeader(level, index)
}
func keyed(level int, key string) atomlayer.Atom {
return MakeKeyedHeader(level, []byte(key))
}
func data(bytes ...byte) atomlayer.Atom {
return append([]byte{0}, bytes...)
}
func atoms(atoms ...atomlayer.Atom) []atomlayer.Atom {
return []atomlayer.Atom(atoms)
}
func TestSimpleEnter1(t *testing.T) {
baggage := []byte{}
atoms, err := atomlayer.Deserialize(baggage);
......@@ -214,22 +231,6 @@ func TestSkippedAtomsDropsInitialDataAtoms(t *testing.T) {
assert.Equal(t, []atomlayer.Atom{[]byte{248, 3}, []byte{240, 0}, []byte{0, 6}}, r.Skipped)
}
func header(level int, index uint64) atomlayer.Atom {
return MakeIndexedHeader(level, index)
}
func keyed(level int, key string) atomlayer.Atom {
return MakeKeyedHeader(level, []byte(key))
}
func data(bytes ...byte) atomlayer.Atom {
return append([]byte{0}, bytes...)
}
func atoms(atoms ...atomlayer.Atom) []atomlayer.Atom {
return []atomlayer.Atom(atoms)
}
func TestMultipleSkippedAtoms(t *testing.T) {
baggage := []atomlayer.Atom{
header(0, 3),
......
......@@ -18,17 +18,19 @@ type Writer struct {
var emptyAtom = []byte{}
func NewWriter() (w Writer) {
func NewWriter() *Writer {
var w Writer
w.level = -1
w.prev = emptyAtom
return
return &w
}
// Writes to a specific bag
func WriteBag(bagIndex uint64) (w Writer) {
func WriteBag(bagIndex uint64) *Writer {
var w Writer
w.atoms = append(w.atoms, MakeIndexedHeader(0, bagIndex))
w.prev = emptyAtom
return
return &w
}
func (w *Writer) Enter(bagIndex uint64) {
......
......@@ -6,7 +6,7 @@ import (
)
type Bag interface {
Read(r baggageprotocol.Reader)
Write(w baggageprotocol.Writer)
Read(r *baggageprotocol.Reader)
Write(w *baggageprotocol.Writer)
SetUnprocessedAtoms(atoms []atomlayer.Atom)
}
\ No newline at end of file
......@@ -27,6 +27,27 @@ func WriteLexVarUint64(v uint64) []byte {
return baggageprotocol.EncodeUnsignedLexVarint(v)
}
func ReadLexVarInt32(bytes []byte) *int32 {
value, length := baggageprotocol.DecodeSignedLexVarint(bytes)
if length == 0 || value > math.MaxInt32 || value < math.MinInt32 { return nil }
v := int32(value)
return &v
}
func WriteLexVarInt32(v int32) []byte {
return baggageprotocol.EncodeSignedLexVarint(int64(v))
}
func ReadLexVarInt64(bytes []byte) *int64 {
value, length := baggageprotocol.DecodeSignedLexVarint(bytes)
if length == 0 { return nil }
return &value
}
func WriteLexVarInt64(v int64) []byte {
return baggageprotocol.EncodeSignedLexVarint(v)
}
func ReadUint32Fixed(bytes []byte) *uint32 {
if len(bytes) != 4 { return nil }
value := binary.BigEndian.Uint32(bytes)
......@@ -39,6 +60,16 @@ func WriteUint32Fixed(v uint32) []byte {
return bytes
}
func ReadInt32Fixed(bytes []byte) *int32 {
if len(bytes) != 4 { return nil }
v := int32(binary.BigEndian.Uint32(bytes))
return &v
}
func WriteInt32Fixed(v int32) []byte {
return WriteUint32Fixed(uint32(v))
}
func ReadUint64Fixed(bytes []byte) *uint64 {
if len(bytes) != 8 { return nil }
value := binary.BigEndian.Uint64(bytes)
......@@ -49,4 +80,14 @@ func WriteUint64Fixed(v uint64) []byte {
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, v)
return bytes
}
func ReadInt64Fixed(bytes []byte) *int64 {
if len(bytes) != 8 { return nil }
value := int64(binary.BigEndian.Uint64(bytes))
return &value
}
func WriteInt64Fixed(v int64) []byte {
return WriteUint64Fixed(uint64(v))
}
\ No newline at end of file
......@@ -9,27 +9,27 @@ import (
// An example of a class that would be generated by BDL for XTrace
type XTraceMetadata struct {
TaskID *uint64 // fixed64 taskID = 0
ParentEventIDs map[uint64](struct{}) // set<fixed64> parentEventIDs = 1
TaskID *int64 // fixed64 taskID = 0
ParentEventIDs map[int64](struct{}) // set<fixed64> parentEventIDs = 1
Overflowed bool
Unknown []atomlayer.Atom // Atoms that aren't part of the XTraceMetadata spec, but were present
}
func (xTraceMetadata *XTraceMetadata) Read(r baggageprotocol.Reader) {
func (xTraceMetadata *XTraceMetadata) Read(r *baggageprotocol.Reader) {
// TaskID
if r.EnterIndexed(0) {
xTraceMetadata.TaskID = bdl.ReadUint64Fixed(r.Next());
xTraceMetadata.TaskID = bdl.ReadInt64Fixed(r.Next());
r.Exit()
}
// ParentEventIDs
if r.EnterIndexed(1) {
xTraceMetadata.ParentEventIDs = make(map[uint64](struct{}))
xTraceMetadata.ParentEventIDs = make(map[int64](struct{}))
for {
next := r.Next()
if next == nil { break }
parentEventID := bdl.ReadUint64Fixed(next)
parentEventID := bdl.ReadInt64Fixed(next)
if parentEventID != nil { xTraceMetadata.ParentEventIDs[*parentEventID] = struct{}{} }
}
r.Exit()
......@@ -39,11 +39,11 @@ func (xTraceMetadata *XTraceMetadata) Read(r baggageprotocol.Reader) {
xTraceMetadata.Overflowed = r.Overflowed
}
func (xTraceMetadata *XTraceMetadata) Write(w baggageprotocol.Writer) {
func (xTraceMetadata *XTraceMetadata) Write(w *baggageprotocol.Writer) {
// TaskID
if xTraceMetadata.TaskID != nil {
w.Enter(0)
w.Write(bdl.WriteUint64Fixed(*xTraceMetadata.TaskID))
w.Write(bdl.WriteInt64Fixed(*xTraceMetadata.TaskID))
w.Exit()
}
......@@ -51,7 +51,7 @@ func (xTraceMetadata *XTraceMetadata) Write(w baggageprotocol.Writer) {
if len(xTraceMetadata.ParentEventIDs) > 0 {
var parentEventIDs [][]byte
for parentEventID := range(xTraceMetadata.ParentEventIDs) {
parentEventIDs = append(parentEventIDs, bdl.WriteUint64Fixed(parentEventID))
parentEventIDs = append(parentEventIDs, bdl.WriteInt64Fixed(parentEventID))
}
w.Enter(1)
......
......@@ -8,6 +8,23 @@ import (
"github.com/tracingplane/tracingplane-go/atomlayer"
)
func header(level int, index uint64) atomlayer.Atom {
return baggageprotocol.MakeIndexedHeader(level, index)
}
func keyed(level int, key string) atomlayer.Atom {
return baggageprotocol.MakeKeyedHeader(level, []byte(key))
}
func data(bytes ...byte) atomlayer.Atom {
return append([]byte{0}, bytes...)
}
func atoms(atoms ...atomlayer.Atom) []atomlayer.Atom {
return []atomlayer.Atom(atoms)
}
func TestXTrace(t *testing.T) {
var xtrace XTraceMetadata
......@@ -26,26 +43,28 @@ func TestXTrace(t *testing.T) {
}
func TestXTrace2(t *testing.T) {
bytes := []byte{ 2, 248, 5,
2, 240, 0,
9, 0, 143, 189, 154, 1, 65, 170, 219, 47,
2, 240, 1,
9, 0, 242, 64, 253, 113, 224, 239, 96, 55,
9, 0, 2, 62, 33, 56, 120, 22, 229, 128,
9, 0, 125, 152, 88, 29, 177, 134, 140, 248,
2, 240, 3,
2, 0, 3 }
baggage, err := tracingplane.Deserialize(bytes)
assert.Nil(t, err)
var err error
var baggage tracingplane.BaggageContext
baggage.Atoms = atoms(
header(0, 3),
data(5),
header(0, 5),
header(1, 0),
data(143, 189, 154, 1, 65, 170, 219, 47),
header(1, 1),
data(242, 64, 253, 113, 224, 239, 96, 55),
data(2, 62, 33, 56, 120, 22, 229, 128),
data(125, 152, 88, 29, 177, 134, 140, 248),
header(1, 3),
data(3),
)
var xtrace XTraceMetadata
err = baggage.ReadBag(5, &xtrace)
assert.Nil(t, err)
assert.NotNil(t, xtrace.TaskID)
assert.Equal(t, int64(-8089140025500181713), xtrace.TaskID)
assert.Equal(t, int64(-8089140025500181713), *xtrace.TaskID)
assert.Equal(t, len(xtrace.ParentEventIDs), 3)
expectParentIds := make(map[int64](struct{}))
......
......@@ -15,8 +15,8 @@ import (
// Provides the base declaration of BaggageContext which internally uses the atom layer's atom representation
type BaggageContext struct {
atoms []atomlayer.Atom // The underlying atoms of this baggagecontext
Context context.Context // A golang context carried with this baggagecontext. Propagates through calls
Atoms []atomlayer.Atom // The underlying Atoms of this baggagecontext
Context context.Context // A golang context carried with this baggagecontext. Propagates through calls
// to branch, but not with all merge calls.
componentId **uint32 // A randomly generated ID for this component; only propagates to one side of
// branch calls.
......@@ -27,7 +27,7 @@ type BaggageContext struct {
// The returned BaggageContext will NOT contain anything from B's golang context -- only A's
func (a BaggageContext) MergeWith(bs ...BaggageContext) BaggageContext {
for _, b := range(bs) {
a.atoms = atomlayer.Merge(a.atoms, b.atoms)
a.Atoms = atomlayer.Merge(a.Atoms, b.Atoms)
if !a.hasComponentID() {
a.componentId = b.componentId
// TODO: If multiple baggages have component IDs, keep all of them for later reuse?
......@@ -47,25 +47,25 @@ func (a BaggageContext) MergeWith(bs ...BaggageContext) BaggageContext {
// Derives a new BaggageContext instance that will be passed, for example, to a different goroutine
func (a BaggageContext) Branch() (c BaggageContext) {
a.atoms = atomlayer.Branch(a.atoms)
a.Atoms = atomlayer.Branch(a.Atoms)
a.componentId = nil
return a
}
// Returns the serialized size in bytes of this BaggageContext
func (baggage BaggageContext) SerializedSize() int {
return atomlayer.SerializedSize(baggage.atoms)
return atomlayer.SerializedSize(baggage.Atoms)
}
// Serializes the atoms of the BaggageContext. The serialized representation doesn't include anything from the golang
// Serializes the Atoms of the BaggageContext. The serialized representation doesn't include anything from the golang
// context, or the component ID
func Serialize(baggage BaggageContext) []byte {
return atomlayer.Serialize(baggage.atoms)
return atomlayer.Serialize(baggage.Atoms)
}
// Deserializes a BaggageContext from bytes
func Deserialize(bytes []byte) (baggage BaggageContext, err error) {
baggage.atoms, err = atomlayer.Deserialize(bytes)
baggage.Atoms, err = atomlayer.Deserialize(bytes)
return
}
......@@ -84,9 +84,9 @@ func DecodeBase64(encoded string) (BaggageContext, error) {
}
}
// Drop atoms from the BaggageContext so that it fits into the specified number of bytes
// Drop Atoms from the BaggageContext so that it fits into the specified number of bytes
func Trim(baggage BaggageContext, maxSize int) BaggageContext {
baggage.atoms = atomlayer.Trim(baggage.atoms, maxSize)
baggage.Atoms = atomlayer.Trim(baggage.Atoms, maxSize)
return baggage
}
......
......@@ -10,7 +10,7 @@ import (
// Read the specified bag index into the provided bag object
func (baggage *BaggageContext) ReadBag(bagIndex uint64, bag bdl.Bag) error {
reader := baggageprotocol.Open(baggage.atoms, bagIndex)
reader := baggageprotocol.Open(baggage.Atoms, bagIndex)
bag.Read(reader)
reader.Close()
bag.SetUnprocessedAtoms(reader.Skipped)
......@@ -19,12 +19,12 @@ func (baggage *BaggageContext) ReadBag(bagIndex uint64, bag bdl.Bag) error {
// Drops the specified bag index from the provided baggage object
func (baggage *BaggageContext) Drop(bagIndex uint64) {
baggage.atoms = baggageprotocol.Drop(baggage.atoms, bagIndex, baggageprotocol.PushMarkerDown)
baggage.Atoms = baggageprotocol.Drop(baggage.Atoms, bagIndex, baggageprotocol.PushMarkerDown)
}
func (baggage *BaggageContext) WriteBag(bagIndex uint64, bag bdl.Bag) error {
// Remove the bag from the baggage
baggage.atoms = baggageprotocol.Drop(baggage.atoms, bagIndex, baggageprotocol.DropMarker)
baggage.Atoms = baggageprotocol.Drop(baggage.Atoms, bagIndex, baggageprotocol.DropMarker)
// Write the new bag
writer := baggageprotocol.WriteBag(bagIndex)
......@@ -32,6 +32,6 @@ func (baggage *BaggageContext) WriteBag(bagIndex uint64, bag bdl.Bag) error {
newAtoms, err := writer.Atoms()
// Merge it back in
baggage.atoms = atomlayer.Merge(baggage.atoms, newAtoms)
baggage.Atoms = atomlayer.Merge(baggage.Atoms, newAtoms)
return err
}
\ No newline at end of file
......@@ -9,19 +9,19 @@ import (
func TestEmptyBaggage(t *testing.T) {
var a BaggageContext
assert.Empty(t, a.atoms)
assert.Empty(t, a.Atoms)
assert.Nil(t, a.componentId)
var b BaggageContext
c := a.MergeWith(b)
assert.Empty(t, c.atoms)
assert.Empty(t, c.Atoms)
assert.Nil(t, c.componentId)
d := c.Branch()
assert.Empty(t, d.atoms)
assert.Empty(t, d.Atoms)
assert.Nil(t, d.componentId)
}
......
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