newEncodedFromRoot returns a jsonEncoded from a fully-encoded JSON document.
(v []byte)
| 59 | |
| 60 | // newEncodedFromRoot returns a jsonEncoded from a fully-encoded JSON document. |
| 61 | func newEncodedFromRoot(v []byte) (*jsonEncoded, error) { |
| 62 | v, typ, err := jsonTypeFromRootBuffer(v) |
| 63 | if err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | |
| 67 | containerLen := -1 |
| 68 | if typ == ArrayJSONType || typ == ObjectJSONType { |
| 69 | containerHeader, err := getUint32At(v, 0) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | containerLen = int(containerHeader & containerHeaderLenMask) |
| 74 | } |
| 75 | |
| 76 | return &jsonEncoded{ |
| 77 | typ: typ, |
| 78 | containerLen: containerLen, |
| 79 | // Manually set the capacity of the new slice to its length, so we properly |
| 80 | // report the memory size of this encoded json object. The original slice |
| 81 | // capacity is very large, since it probably points to the backing byte |
| 82 | // slice of a kv batch. |
| 83 | value: v[:len(v):len(v)], |
| 84 | }, nil |
| 85 | } |
| 86 | |
| 87 | func jsonTypeFromRootBuffer(v []byte) ([]byte, Type, error) { |
| 88 | // Root buffers always have a container header. |
no test coverage detected
searching dependent graphs…