forked from colinrgodsey/go-tokyocabinet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
adb_helpers.go
47 lines (40 loc) · 856 Bytes
/
adb_helpers.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
package tokyocabinet
import (
"sort"
)
// IterKeysString is an IterKeys() helper method that returns a slice of iterated strings.
//
// NOTE: returned []string can be in arbitrary order, see IterKeysStringSorted()
func (db *ADB) IterKeysString() ([]string, error) {
var s []string
c, e := db.IterKeys()
for {
if c == nil && e == nil {
break
}
select {
case err, ok := <-e:
if ok == false {
e = nil
} else {
return nil, err
}
case val, ok := <-c:
if ok == false {
c = nil
} else {
s = append(s, string(val))
}
}
}
return s, nil
}
// IterKeysStringSorted is an IterKeys() helper method that returns a sorted slice of iterated strings.
func (db *ADB) IterKeysStringSorted() ([]string, error) {
s, err := db.IterKeysString()
if err != nil {
return nil, err
}
sort.Strings(s)
return s, nil
}