MCPcopy
hub / github.com/pingcap/tidb / appendBinaryObject

Function appendBinaryObject

pkg/types/json_binary.go:860–897  ·  view source on GitHub ↗
(buf []byte, x map[string]any)

Source from the content-addressed store, hash-verified

858}
859
860func appendBinaryObject(buf []byte, x map[string]any) ([]byte, error) {
861 docOff := len(buf)
862 buf = appendUint32(buf, uint32(len(x)))
863 buf = appendZero(buf, dataSizeOff)
864 keyEntryBegin := len(buf)
865 buf = appendZero(buf, len(x)*keyEntrySize)
866 valEntryBegin := len(buf)
867 buf = appendZero(buf, len(x)*valEntrySize)
868
869 fields := make([]field, 0, len(x))
870 for key, val := range x {
871 fields = append(fields, field{key: key, val: val})
872 }
873 slices.SortFunc(fields, func(i, j field) int {
874 return cmp.Compare(i.key, j.key)
875 })
876 for i, field := range fields {
877 keyEntryOff := keyEntryBegin + i*keyEntrySize
878 keyOff := len(buf) - docOff
879 keyLen := uint32(len(field.key))
880 if keyLen > math.MaxUint16 {
881 return nil, ErrJSONObjectKeyTooLong
882 }
883 jsonEndian.PutUint32(buf[keyEntryOff:], uint32(keyOff))
884 jsonEndian.PutUint16(buf[keyEntryOff+keyLenOff:], uint16(keyLen))
885 buf = append(buf, field.key...)
886 }
887 for i, field := range fields {
888 var err error
889 buf, err = appendBinaryValElem(buf, docOff, valEntryBegin+i*valEntrySize, field.val)
890 if err != nil {
891 return nil, errors.Trace(err)
892 }
893 }
894 docSize := len(buf) - docOff
895 jsonEndian.PutUint32(buf[docOff+dataSizeOff:], uint32(docSize))
896 return buf, nil
897}

Callers 1

appendBinaryJSONFunction · 0.85

Calls 4

appendUint32Function · 0.85
appendZeroFunction · 0.85
appendBinaryValElemFunction · 0.85
CompareMethod · 0.65

Tested by

no test coverage detected