-
Notifications
You must be signed in to change notification settings - Fork 58
/
boltdb.go
168 lines (147 loc) · 3.12 KB
/
boltdb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package hltool
import (
"fmt"
"os"
"path"
"github.com/boltdb/bolt"
)
var (
db *bolt.DB
)
// BoltDB DB操作
type BoltDB struct {
DBPath string // 数据库路径名称
TableName string // 表名
}
// NewBoltDB 初始化数据库对象
func NewBoltDB(dbPath, tableName string) (*BoltDB, error) {
if dbPath == "" {
return nil, fmt.Errorf("database path required")
}
dirname := path.Dir(dbPath)
if !IsExist(dirname) {
err := os.MkdirAll(dirname, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("create dir(%s) error: %s", dirname, err)
}
}
return &BoltDB{DBPath: dbPath, TableName: tableName}, nil
}
// table 获取表 表不存在则创建
func (btb *BoltDB) table() error {
var err error
db, err = bolt.Open(btb.DBPath, 0600, nil)
if err != nil {
return fmt.Errorf("open db error: %s", err)
}
return db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(btb.TableName))
if err != nil {
return fmt.Errorf("create table error: %s", err)
}
return nil
})
}
// Set 设置值
// kv 键值对
func (btb *BoltDB) Set(kv map[string][]byte) error {
err := btb.table()
if err != nil {
return err
}
defer db.Close()
return db.Batch(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(btb.TableName))
var err error
for k, v := range kv {
err = b.Put([]byte(k), v)
if err != nil {
return err
}
}
return err
})
}
// Get 根据键名数组获取各自的值
// keys 键名数组
func (btb *BoltDB) Get(keys []string) (map[string][]byte, error) {
err := btb.table()
if err != nil {
return nil, err
}
defer db.Close()
values := make(map[string][]byte)
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(btb.TableName))
for _, k := range keys {
result := b.Get([]byte(k))
if result == nil {
continue
}
tmp := make([]byte, len(result))
copy(tmp, result)
values[k] = tmp
}
return nil
})
return values, err
}
// GetAll 获取全部
func (btb *BoltDB) GetAll() (map[string][]byte, error) {
err := btb.table()
if err != nil {
return nil, err
}
defer db.Close()
values := make(map[string][]byte)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(btb.TableName))
b.ForEach(func(k, v []byte) error {
tmpV := make([]byte, len(v))
copy(tmpV, v)
tmpK := make([]byte, len(k))
copy(tmpK, k)
values[string(tmpK)] = tmpV
return nil
})
return nil
})
return values, nil
}
// Delete 删除键值
func (btb *BoltDB) Delete(keys []string) error {
err := btb.table()
if err != nil {
return err
}
defer db.Close()
return db.Batch(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(btb.TableName))
var err error
for _, key := range keys {
err = b.Delete([]byte(key))
if err != nil {
return err
}
}
return err
})
}
// Backup 备份数据库文件
func (btb *BoltDB) Backup(filepath string) error {
db, err := bolt.Open(btb.DBPath, 0600, nil)
if err != nil {
return fmt.Errorf("open db error: %s", err)
}
err = db.View(func(tx *bolt.Tx) error {
err := tx.CopyFile(filepath, 0644)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return db.Close()
}