/usr/share/gocode/src/github.com/retailnext/hllpp/marshal.go is in golang-github-retailnext-hllpp-dev 1.0.0+git20170901.6e8b6d3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | // Copyright (c) 2015, RetailNext, Inc.
// All rights reserved.
package hllpp
import (
"encoding/binary"
"fmt"
)
/*
Here is a diagram of the marshal format:
0 1 2 3
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Marshal Version | Length... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ...Length | Flags |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| p | p' | sparseLength... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ...sparseLength |bitsPerRegister| Data... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
const (
marshalVersion = 1
marshalHeaderSize = 15
marshalFlagSparse = 1
)
// Marshal serializes h into a byte slice that can be deserialized via
// Unmarshal. The data is naturally compressed, so don't bother trying
// to compress it any more.
func (h *HLLPP) Marshal() []byte {
if h.sparse {
h.flushTmpSet()
}
buf := make([]byte, marshalHeaderSize+len(h.data))
offset := 0
binary.BigEndian.PutUint16(buf[offset:], marshalVersion)
offset += 2
binary.BigEndian.PutUint32(buf[offset:], uint32(len(buf)))
offset += 4
var flags uint16
if h.sparse {
flags |= marshalFlagSparse
}
binary.BigEndian.PutUint16(buf[offset:], flags)
offset += 2
buf[offset] = h.p
offset += 1
buf[offset] = h.pp
offset += 1
binary.BigEndian.PutUint32(buf[offset:], h.sparseLength)
offset += 4
buf[offset] = byte(h.bitsPerRegister)
offset += 1
copy(buf[offset:], h.data)
return buf
}
// Unmarshal deserializes a byte slice returned by Marshal back into an
// HLLPP object.
func Unmarshal(data []byte) (*HLLPP, error) {
if len(data) < marshalHeaderSize {
return nil, fmt.Errorf("data too short (%d bytes)", len(data))
}
offset := 0
version := binary.BigEndian.Uint16(data[offset:])
offset += 2
if version != marshalVersion {
return nil, fmt.Errorf("unknown version: %d", version)
}
length := binary.BigEndian.Uint32(data[offset:])
offset += 4
if int(length) != len(data) {
return nil, fmt.Errorf("length mismatch: header says %d, was %d", length, len(data))
}
flags := binary.BigEndian.Uint16(data[offset:])
offset += 2
p := data[offset]
offset++
pp := data[offset]
offset++
h, err := NewWithConfig(Config{
Precision: p,
SparsePrecision: pp,
})
if err != nil {
return nil, err
}
h.sparse = flags&marshalFlagSparse > 0
h.sparseLength = binary.BigEndian.Uint32(data[offset:])
offset += 4
h.bitsPerRegister = uint32(data[offset])
offset++
if len(data) > offset {
h.data = make([]byte, len(data)-offset)
copy(h.data, data[offset:])
}
return h, nil
}
|