| 1 |
package gozstd |
| 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 |
import "C" |
| 28 |
|
| 29 |
import ( |
| 30 |
"fmt" |
| 31 |
"runtime" |
| 32 |
"sync" |
| 33 |
"unsafe" |
| 34 |
) |
| 35 |
|
| 36 |
const minDictLen = C.ZDICT_DICTSIZE_MIN |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
func BuildDict(samples [][]byte, desiredDictLen int) []byte { |
| 44 |
if desiredDictLen < minDictLen { |
| 45 |
desiredDictLen = minDictLen |
| 46 |
} |
| 47 |
dict := make([]byte, desiredDictLen) |
| 48 |
|
| 49 |
|
| 50 |
samplesBufLen := 0 |
| 51 |
for _, sample := range samples { |
| 52 |
if len(sample) == 0 { |
| 53 |
|
| 54 |
continue |
| 55 |
} |
| 56 |
samplesBufLen += len(sample) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
samplesBuf := make([]byte, 0, samplesBufLen) |
| 61 |
samplesSizes := make([]C.size_t, 0, len(samples)) |
| 62 |
for _, sample := range samples { |
| 63 |
samplesBuf = append(samplesBuf, sample...) |
| 64 |
samplesSizes = append(samplesSizes, C.size_t(len(sample))) |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
minSamplesBufLen := int(C.ZDICT_CONTENTSIZE_MIN) |
| 69 |
if minSamplesBufLen < minDictLen { |
| 70 |
minSamplesBufLen = minDictLen |
| 71 |
} |
| 72 |
for samplesBufLen < minSamplesBufLen { |
| 73 |
fakeSample := []byte(fmt.Sprintf("this is a fake sample %d", samplesBufLen)) |
| 74 |
samplesBuf = append(samplesBuf, fakeSample...) |
| 75 |
samplesSizes = append(samplesSizes, C.size_t(len(fakeSample))) |
| 76 |
samplesBufLen += len(fakeSample) |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
buildDictLock.Lock() |
| 84 |
result := C.ZDICT_trainFromBuffer( |
| 85 |
unsafe.Pointer(&dict[0]), |
| 86 |
C.size_t(len(dict)), |
| 87 |
unsafe.Pointer(&samplesBuf[0]), |
| 88 |
&samplesSizes[0], |
| 89 |
C.unsigned(len(samplesSizes))) |
| 90 |
buildDictLock.Unlock() |
| 91 |
if C.ZDICT_isError(result) != 0 { |
| 92 |
|
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
dictLen := int(result) |
| 97 |
return dict[:dictLen] |
| 98 |
} |
| 99 |
|
| 100 |
var buildDictLock sync.Mutex |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
type CDict struct { |
| 106 |
p *C.ZSTD_CDict |
| 107 |
compressionLevel int |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
func NewCDict(dict []byte) (*CDict, error) { |
| 114 |
return NewCDictLevel(dict, DefaultCompressionLevel) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func NewCDictLevel(dict []byte, compressionLevel int) (*CDict, error) { |
| 122 |
if len(dict) == 0 { |
| 123 |
return nil, fmt.Errorf("dict cannot be empty") |
| 124 |
} |
| 125 |
|
| 126 |
cd := &CDict{ |
| 127 |
p: C.ZSTD_createCDict_wrapper( |
| 128 |
C.uintptr_t(uintptr(unsafe.Pointer(&dict[0]))), |
| 129 |
C.size_t(len(dict)), |
| 130 |
C.int(compressionLevel)), |
| 131 |
compressionLevel: compressionLevel, |
| 132 |
} |
| 133 |
|
| 134 |
runtime.KeepAlive(dict) |
| 135 |
runtime.SetFinalizer(cd, freeCDict) |
| 136 |
return cd, nil |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
func (cd *CDict) Release() { |
| 143 |
if cd.p == nil { |
| 144 |
return |
| 145 |
} |
| 146 |
result := C.ZSTD_freeCDict(cd.p) |
| 147 |
ensureNoError("ZSTD_freeCDict", result) |
| 148 |
cd.p = nil |
| 149 |
} |
| 150 |
|
| 151 |
func freeCDict(v interface{}) { |
| 152 |
v.(*CDict).Release() |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
type DDict struct { |
| 159 |
p *C.ZSTD_DDict |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
func NewDDict(dict []byte) (*DDict, error) { |
| 166 |
if len(dict) == 0 { |
| 167 |
return nil, fmt.Errorf("dict cannot be empty") |
| 168 |
} |
| 169 |
|
| 170 |
dd := &DDict{ |
| 171 |
p: C.ZSTD_createDDict_wrapper( |
| 172 |
C.uintptr_t(uintptr(unsafe.Pointer(&dict[0]))), |
| 173 |
C.size_t(len(dict))), |
| 174 |
} |
| 175 |
|
| 176 |
runtime.KeepAlive(dict) |
| 177 |
runtime.SetFinalizer(dd, freeDDict) |
| 178 |
return dd, nil |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
func (dd *DDict) Release() { |
| 185 |
if dd.p == nil { |
| 186 |
return |
| 187 |
} |
| 188 |
|
| 189 |
result := C.ZSTD_freeDDict(dd.p) |
| 190 |
ensureNoError("ZSTD_freeDDict", result) |
| 191 |
dd.p = nil |
| 192 |
} |
| 193 |
|
| 194 |
func freeDDict(v interface{}) { |
| 195 |
v.(*DDict).Release() |
| 196 |
} |