forked from valyala/gozstd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_example_test.go
58 lines (48 loc) · 1.46 KB
/
dict_example_test.go
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
package gozstd
import (
"fmt"
"log"
)
func ExampleBuildDict() {
// Collect samples for the dictionary.
var samples [][]byte
for i := 0; i < 1000; i++ {
sample := fmt.Sprintf("this is a dict sample number %d", i)
samples = append(samples, []byte(sample))
}
// Build a dictionary with the desired size of 8Kb.
dict := BuildDict(samples, 8*1024)
// Now the dict may be used for compression/decompression.
// Create CDict from the dict.
cd, err := NewCDict(dict)
if err != nil {
log.Fatalf("cannot create CDict: %s", err)
}
defer cd.Release()
// Compress multiple blocks with the same CDict.
var compressedBlocks [][]byte
for i := 0; i < 3; i++ {
plainData := fmt.Sprintf("this is line %d for dict compression", i)
compressedData := CompressDict(nil, []byte(plainData), cd)
compressedBlocks = append(compressedBlocks, compressedData)
}
// The compressedData must be decompressed with the same dict.
// Create DDict from the dict.
dd, err := NewDDict(dict)
if err != nil {
log.Fatalf("cannot create DDict: %s", err)
}
defer dd.Release()
// Decompress multiple blocks with the same DDict.
for _, compressedData := range compressedBlocks {
decompressedData, err := DecompressDict(nil, compressedData, dd)
if err != nil {
log.Fatalf("cannot decompress data: %s", err)
}
fmt.Printf("%s\n", decompressedData)
}
// Output:
// this is line 0 for dict compression
// this is line 1 for dict compression
// this is line 2 for dict compression
}