Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions internal/storage/v1/cassandra/spanstore/dbmodel/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package dbmodel
import (
"bytes"
"encoding/binary"
"encoding/hex"
"sort"
"strconv"

"github.com/jaegertracing/jaeger-idl/model/v1"
)
Expand Down Expand Up @@ -53,6 +56,136 @@ type KeyValue struct {
ValueBinary []byte `cql:"value_binary"`
}

func (t *KeyValue) compareValues(that *KeyValue) int {
switch t.ValueType {
case stringType:
if t.ValueString != that.ValueString {
if t.ValueString < that.ValueString {
return -1
}
return 1
}
case boolType:
if t.ValueBool != that.ValueBool {
if !t.ValueBool {
return -1
}
return 1
}
case int64Type:
if t.ValueInt64 != that.ValueInt64 {
if t.ValueInt64 < that.ValueInt64 {
return -1
}
return 1
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if t.ValueInt64 != that.ValueInt64 {
if t.ValueInt64 < that.ValueInt64 {
return -1
}
return 1
}
return t.ValueInt64 < that.ValueInt64

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this will only give 0 or 1 and would miss the -1 case

case float64Type:
if t.ValueFloat64 != that.ValueFloat64 {
if t.ValueFloat64 < that.ValueFloat64 {
return -1
}
return 1
}
case binaryType:
if c := bytes.Compare(t.ValueBinary, that.ValueBinary); c != 0 {
return c
}
default:
return -1 // theoretical case, not stating them equal but placing the base pointer before other
}
return 0
}

func (t *KeyValue) Compare(that any) int {
if that == nil {
if t == nil {
return 0
}
return 1
}
that1, ok := that.(*KeyValue)
if !ok {
that2, ok := that.(KeyValue)
if !ok {
return 1
}
that1 = &that2
}
if that1 == nil {
if t == nil {
return 0
}
return 1
} else if t == nil {
return -1
}
if t.Key != that1.Key {
if t.Key < that1.Key {
return -1
}
return 1
}
if t.ValueType != that1.ValueType {
if t.ValueType < that1.ValueType {
return -1
}
return 1
}
return t.compareValues(that1)
}

func (t *KeyValue) Equal(that any) bool {
if that == nil {
return t == nil
}
that1, ok := that.(*KeyValue)
if !ok {
that2, ok := that.(KeyValue)
if !ok {
return false
}
that1 = &that2
}
if that1 == nil {
return t == nil
} else if t == nil {
return false
}
if t.Key != that1.Key {
return false
}
if t.ValueType != that1.ValueType {
return false
}
return t.compareValues(that1) == 0
}

func (t *KeyValue) AsString() string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TagInsertion accepts TagValue only in form of string

switch t.ValueType {
case stringType:
return t.ValueString
case boolType:
if t.ValueBool {
return "true"
}
return "false"
case int64Type:
return strconv.FormatInt(t.ValueInt64, 10)
case float64Type:
return strconv.FormatFloat(t.ValueFloat64, 'g', 10, 64)
case binaryType:
return hex.EncodeToString(t.ValueBinary)
default:
return "unknown type " + t.ValueType
}
}

func SortKVs(kvs []KeyValue) {
sort.Slice(kvs, func(i, j int) bool {
return kvs[i].Compare(kvs[j]) < 0
})
}

// Log is the UDT representation of a Jaeger Log.
type Log struct {
Timestamp int64 `cql:"ts"` // microseconds since epoch
Expand Down
Loading
Loading