Skip to content

Commit

Permalink
Extract vector matching encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jeschkies committed Nov 13, 2023
1 parent a4cb461 commit ce67404
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions pkg/logql/syntax/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,7 @@ func (v *JSONSerializer) VisitBinOp(e *BinOpExpr) {
if e.Opts.VectorMatching != nil {
v.WriteMore()
v.WriteObjectField("vector_matching")
v.WriteObjectStart()

v.WriteObjectField("include")
v.WriteArrayStart()
for i, l := range e.Opts.VectorMatching.Include {
if i > 0 {
v.WriteMore()
}
v.WriteString(l)
}
v.WriteArrayEnd()

v.WriteMore()
v.WriteObjectField("on")
v.WriteBool(e.Opts.VectorMatching.On)

v.WriteMore()
v.WriteObjectField("card")
v.WriteInt(int(e.Opts.VectorMatching.Card))

v.WriteMore()
v.WriteObjectField("matching_labels")
v.WriteArrayStart()
for i, l := range e.Opts.VectorMatching.MatchingLabels {
if i > 0 {
v.WriteMore()
}
v.WriteString(l)
}
v.WriteArrayEnd()

v.WriteObjectEnd()
encodeVectorMatching(v.Stream, e.Opts.VectorMatching)
}

v.WriteObjectEnd()
Expand Down Expand Up @@ -463,6 +432,42 @@ func decodeBinOpOptions(iter *jsoniter.Iterator) *BinOpOptions {
return opts
}

func encodeVectorMatching(s *jsoniter.Stream, vm *VectorMatching) {
s.WriteObjectStart()

s.WriteObjectField("include")
s.WriteArrayStart()
for i, l := range vm.Include {
if i > 0 {
s.WriteMore()
}
s.WriteString(l)
}
s.WriteArrayEnd()

s.WriteMore()
s.WriteObjectField("on")
s.WriteBool(vm.On)

s.WriteMore()
s.WriteObjectField("card")
s.WriteInt(int(vm.Card))

s.WriteMore()
s.WriteObjectField("matching_labels")
s.WriteArrayStart()
for i, l := range vm.MatchingLabels {
if i > 0 {
s.WriteMore()
}
s.WriteString(l)
}
s.WriteArrayEnd()

s.WriteObjectEnd()
}


func decodeVectorMatching(iter *jsoniter.Iterator) *VectorMatching {
vm := &VectorMatching{}

Expand Down

0 comments on commit ce67404

Please sign in to comment.