MCPcopy
hub / github.com/klauspost/compress / ExampleIndex_Load

Function ExampleIndex_Load

s2/index_test.go:16–103  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14)
15
16func ExampleIndex_Load() {
17 fatalErr := func(err error) {
18 if err != nil {
19 panic(err)
20 }
21 }
22
23 // Create a test corpus
24 tmp := make([]byte, 5<<20)
25 rng := rand.New(rand.NewSource(0xbeefcafe))
26 rng.Read(tmp)
27 // Make it compressible...
28 for i, v := range tmp {
29 tmp[i] = '0' + v&3
30 }
31 // Compress it...
32 var buf bytes.Buffer
33 // We use smaller blocks just for the example...
34 enc := s2.NewWriter(&buf, s2.WriterBlockSize(100<<10))
35 err := enc.EncodeBuffer(tmp)
36 fatalErr(err)
37
38 // Close and get index...
39 idxBytes, err := enc.CloseIndex()
40 fatalErr(err)
41
42 // This is our compressed stream...
43 compressed := buf.Bytes()
44
45 var once sync.Once
46 for wantOffset := int64(0); wantOffset < int64(len(tmp)); wantOffset += 555555 {
47 // Let's assume we want to read from uncompressed offset 'i'
48 // and we cannot seek in input, but we have the index.
49 want := tmp[wantOffset:]
50
51 // Load the index.
52 var index s2.Index
53 _, err = index.Load(idxBytes)
54 fatalErr(err)
55
56 // Find offset in file:
57 compressedOffset, uncompressedOffset, err := index.Find(wantOffset)
58 fatalErr(err)
59
60 // Offset the input to the compressed offset.
61 // Notice how we do not provide any bytes before the offset.
62 input := io.Reader(bytes.NewBuffer(compressed[compressedOffset:]))
63 if _, ok := input.(io.Seeker); !ok {
64 // Notice how the input cannot be seeked...
65 once.Do(func() {
66 fmt.Println("Input does not support seeking...")
67 })
68 } else {
69 panic("did you implement seeking on bytes.Buffer?")
70 }
71
72 // When creating the decoder we must specify that it should not
73 // expect a stream identifier at the beginning og the frame.

Callers

nothing calls this directly

Calls 11

EncodeBufferMethod · 0.95
CloseIndexMethod · 0.95
LoadMethod · 0.95
FindMethod · 0.95
SkipMethod · 0.95
NewWriterFunction · 0.92
WriterBlockSizeFunction · 0.92
NewReaderFunction · 0.92
BytesMethod · 0.65
ReadMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…