File: dict_example_test.go

package info (click to toggle)
golang-github-valyala-gozstd 1.21.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: makefile: 84
file content (58 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download | duplicates (3)
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
}