-
Notifications
You must be signed in to change notification settings - Fork 1
/
ledis.go
78 lines (62 loc) · 1.24 KB
/
ledis.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
76
77
78
package main
import (
"os"
"path/filepath"
"github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/ledis"
)
type LedisInfo struct {
Name string
Path string
DbFolder string
Conn *ledis.Ledis
Db *ledis.DB
}
func getLedisInfo(dir string) (*LedisInfo, error) {
db := &LedisInfo{Name: "db", DbFolder: "db"}
if err := db.init(dir); err != nil {
return nil, err
}
return db, nil
}
func (l *LedisInfo) init(databasePath string) error {
fullPath := filepath.Join(databasePath, l.DbFolder)
if err := os.MkdirAll(fullPath, 0700); err != nil {
return err
}
l.Path = fullPath
cfg := &config.Config{
DataDir: fullPath,
}
conn, err := ledis.Open(cfg)
if err != nil {
return err
}
l.Conn = conn
db, err := conn.Select(0)
if err != nil {
return err
}
l.Db = db
return nil
}
func (l *LedisInfo) GetKeyList(dataType ledis.DataType) ([]string, error) {
batchSize := 10
var lastKey []byte
result := []string{}
lastKey = nil
for {
keyBatch, err := l.Db.Scan(dataType, lastKey, batchSize, false, "")
if err != nil {
return nil, err
}
for _, k := range keyBatch {
lastKey = k
result = append(result, string(k))
}
if len(keyBatch) < batchSize {
break
}
}
return result, nil
}