-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
75 lines (63 loc) · 1.26 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package extsort_test
import (
"bytes"
"fmt"
"github.com/bsm/extsort"
)
func Example_map() {
// Init with de-duplication.
sorter := extsort.New(&extsort.Options{
Dedupe: bytes.Equal,
})
defer sorter.Close()
// Put key/value data.
_ = sorter.Put([]byte("foo"), []byte("v1"))
_ = sorter.Put([]byte("bar"), []byte("v2"))
_ = sorter.Put([]byte("baz"), []byte("v3"))
_ = sorter.Put([]byte("bar"), []byte("v4")) // duplicate
_ = sorter.Put([]byte("dau"), []byte("v5"))
// Sort and iterate.
iter, err := sorter.Sort()
if err != nil {
panic(err)
}
defer iter.Close()
for iter.Next() {
fmt.Println(string(iter.Key()), string(iter.Value()))
}
if err := iter.Err(); err != nil {
panic(err)
}
// Output:
// bar v4
// baz v3
// dau v5
// foo v1
}
func Example_plain() {
// Init sorter.
sorter := extsort.New(nil)
defer sorter.Close()
// Append plain data.
_ = sorter.Append([]byte("foo"))
_ = sorter.Append([]byte("bar"))
_ = sorter.Append([]byte("baz"))
_ = sorter.Append([]byte("dau"))
// Sort and iterate.
iter, err := sorter.Sort()
if err != nil {
panic(err)
}
defer iter.Close()
for iter.Next() {
fmt.Println(string(iter.Data()))
}
if err := iter.Err(); err != nil {
panic(err)
}
// Output:
// bar
// baz
// dau
// foo
}