| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package compression |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import "C" |
| 25 |
|
| 26 |
import ( |
| 27 |
"fmt" |
| 28 |
"unsafe" |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
type GzipZinfo struct { |
| 33 |
cZinfo *C.struct_gzip_zinfo |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func newGzipZinfo(zinfoBytes []byte) (*GzipZinfo, error) { |
| 38 |
if len(zinfoBytes) == 0 { |
| 39 |
return nil, fmt.Errorf("empty checkpoints") |
| 40 |
} |
| 41 |
cZinfo := C.blob_to_zinfo(unsafe.Pointer(&zinfoBytes[0]), C.off_t(len(zinfoBytes))) |
| 42 |
if cZinfo == nil { |
| 43 |
return nil, fmt.Errorf("cannot convert blob to gzip_zinfo") |
| 44 |
} |
| 45 |
return &GzipZinfo{ |
| 46 |
cZinfo: cZinfo, |
| 47 |
}, nil |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
func newGzipZinfoFromFile(gzipFile string, spanSize int64) (*GzipZinfo, error) { |
| 52 |
cstr := C.CString(gzipFile) |
| 53 |
defer C.free(unsafe.Pointer(cstr)) |
| 54 |
|
| 55 |
var cZinfo *C.struct_gzip_zinfo |
| 56 |
ret := C.generate_zinfo_from_file(cstr, C.off_t(spanSize), &cZinfo) |
| 57 |
if int(ret) < 0 { |
| 58 |
return nil, fmt.Errorf("could not generate gzip zinfo. gzip error: %v", ret) |
| 59 |
} |
| 60 |
|
| 61 |
return &GzipZinfo{ |
| 62 |
cZinfo: cZinfo, |
| 63 |
}, nil |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func (i *GzipZinfo) Close() { |
| 68 |
if i.cZinfo != nil { |
| 69 |
C.free(unsafe.Pointer(i.cZinfo)) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func (i *GzipZinfo) Bytes() ([]byte, error) { |
| 75 |
blobSize := C.get_blob_size(i.cZinfo) |
| 76 |
bytes := make([]byte, uint64(blobSize)) |
| 77 |
if len(bytes) == 0 { |
| 78 |
return nil, fmt.Errorf("could not allocate byte array of size %d", blobSize) |
| 79 |
} |
| 80 |
|
| 81 |
ret := C.zinfo_to_blob(i.cZinfo, unsafe.Pointer(&bytes[0])) |
| 82 |
if int(ret) <= 0 { |
| 83 |
return nil, fmt.Errorf("could not serialize gzip zinfo to byte array; gzip error: %v", ret) |
| 84 |
} |
| 85 |
return bytes, nil |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func (i *GzipZinfo) MaxSpanID() SpanID { |
| 90 |
return SpanID(C.get_max_span_id(i.cZinfo)) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
func (i *GzipZinfo) SpanSize() Offset { |
| 95 |
return Offset(i.cZinfo.span_size) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func (i *GzipZinfo) UncompressedOffsetToSpanID(offset Offset) SpanID { |
| 100 |
return SpanID(C.pt_index_from_ucmp_offset(i.cZinfo, C.long(offset))) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
func (i *GzipZinfo) ExtractDataFromBuffer(compressedBuf []byte, uncompressedSize, uncompressedOffset Offset, spanID SpanID) ([]byte, error) { |
| 106 |
if len(compressedBuf) == 0 { |
| 107 |
return nil, fmt.Errorf("empty compressed buffer") |
| 108 |
} |
| 109 |
if uncompressedSize < 0 { |
| 110 |
return nil, fmt.Errorf("invalid uncompressed size: %d", uncompressedSize) |
| 111 |
} |
| 112 |
if uncompressedSize == 0 { |
| 113 |
return []byte{}, nil |
| 114 |
} |
| 115 |
bytes := make([]byte, uncompressedSize) |
| 116 |
ret := C.extract_data_from_buffer( |
| 117 |
unsafe.Pointer(&compressedBuf[0]), |
| 118 |
C.off_t(len(compressedBuf)), |
| 119 |
i.cZinfo, |
| 120 |
C.off_t(uncompressedOffset), |
| 121 |
unsafe.Pointer(&bytes[0]), |
| 122 |
C.off_t(uncompressedSize), |
| 123 |
C.int(spanID), |
| 124 |
) |
| 125 |
if ret <= 0 { |
| 126 |
return bytes, fmt.Errorf("error extracting data; return code: %v", ret) |
| 127 |
} |
| 128 |
|
| 129 |
return bytes, nil |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
func (i *GzipZinfo) ExtractDataFromFile(fileName string, uncompressedSize, uncompressedOffset Offset) ([]byte, error) { |
| 135 |
cstr := C.CString(fileName) |
| 136 |
defer C.free(unsafe.Pointer(cstr)) |
| 137 |
if uncompressedSize < 0 { |
| 138 |
return nil, fmt.Errorf("invalid uncompressed size: %d", uncompressedSize) |
| 139 |
} |
| 140 |
if uncompressedSize == 0 { |
| 141 |
return []byte{}, nil |
| 142 |
} |
| 143 |
bytes := make([]byte, uncompressedSize) |
| 144 |
ret := C.extract_data_from_file(cstr, i.cZinfo, C.off_t(uncompressedOffset), unsafe.Pointer(&bytes[0]), C.int(uncompressedSize)) |
| 145 |
if ret <= 0 { |
| 146 |
return nil, fmt.Errorf("unable to extract data; return code = %v", ret) |
| 147 |
} |
| 148 |
|
| 149 |
return bytes, nil |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
func (i *GzipZinfo) StartCompressedOffset(spanID SpanID) Offset { |
| 154 |
start := i.getCompressedOffset(spanID) |
| 155 |
if i.hasBits(spanID) { |
| 156 |
start-- |
| 157 |
} |
| 158 |
return start |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
func (i *GzipZinfo) EndCompressedOffset(spanID SpanID, fileSize Offset) Offset { |
| 164 |
if spanID == i.MaxSpanID() { |
| 165 |
return fileSize |
| 166 |
} |
| 167 |
return i.getCompressedOffset(spanID + 1) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
func (i *GzipZinfo) StartUncompressedOffset(spanID SpanID) Offset { |
| 172 |
return i.getUncompressedOffset(spanID) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
func (i *GzipZinfo) EndUncompressedOffset(spanID SpanID, fileSize Offset) Offset { |
| 178 |
if spanID == i.MaxSpanID() { |
| 179 |
return fileSize |
| 180 |
} |
| 181 |
return i.getUncompressedOffset(spanID + 1) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
func (i *GzipZinfo) getCompressedOffset(spanID SpanID) Offset { |
| 186 |
return Offset(C.get_comp_off(i.cZinfo, C.int(spanID))) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
func (i *GzipZinfo) hasBits(spanID SpanID) bool { |
| 191 |
return C.has_bits(i.cZinfo, C.int(spanID)) != 0 |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
func (i *GzipZinfo) getUncompressedOffset(spanID SpanID) Offset { |
| 196 |
return Offset(C.get_ucomp_off(i.cZinfo, C.int(spanID))) |
| 197 |
} |