-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
190 lines (161 loc) · 3.74 KB
/
queue.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package queue
import (
"fmt"
"log"
"sync"
)
type queue struct {
size uint
list *doubleLinkedList
}
//DBSet 数据库设置
type DBSet struct {
mu sync.Mutex
db *map[string]*queue
}
//Instance 实例
type Instance struct {
dbName string
debug bool
}
var pool *DBSet
//GetDBInstance 获取实例
func GetDBInstance(dbName string) *Instance {
if len(dbName) == 0 {
dbName = "db0"
}
pool.mu.Lock()
defer pool.mu.Unlock()
_, ok := (*pool.db)[dbName]
if !ok {
//当前库不存在,先创建
(*pool.db)[dbName] = &queue{}
}
return &Instance{dbName: dbName, debug: true}
}
//SetDebugMode 设置调试模式
func (instance *Instance) SetDebugMode(isDebug bool) *Instance {
instance.debug = isDebug
return instance
}
//LPush 向队列头部添加节点
func (instance *Instance) LPush(val interface{}) error {
pool.mu.Lock()
defer pool.mu.Unlock()
list, err := (*pool.db)[instance.dbName].list.lPush(val)
if err != nil {
if instance.debug {
log.Printf("db {%s} [LPush] error: %#v\n", instance.dbName, err)
}
return err
}
//当前队列长度加1
(*pool.db)[instance.dbName].size++
(*pool.db)[instance.dbName].list = list
return nil
}
//LPop 从队列头部返回节点值
func (instance *Instance) LPop() (interface{}, error) {
pool.mu.Lock()
defer pool.mu.Unlock()
list, val, err := (*pool.db)[instance.dbName].list.lPop()
if err != nil {
if instance.debug {
log.Printf("db {%s} [LPop] error: %#v\n", instance.dbName, err)
}
return nil, err
}
//当前队列长度减1
(*pool.db)[instance.dbName].size--
(*pool.db)[instance.dbName].list = list
return val, nil
}
//RPush 向队列尾部追加节点
func (instance *Instance) RPush(val interface{}) error {
pool.mu.Lock()
defer pool.mu.Unlock()
list, err := (*pool.db)[instance.dbName].list.rPush(val)
if err != nil {
if instance.debug {
log.Printf("db {%s} [RPush] error: %#v\n", instance.dbName, err)
}
return err
}
//当前队列长度加1
(*pool.db)[instance.dbName].size++
(*pool.db)[instance.dbName].list = list
return nil
}
//RPop 从队列尾部弹出节点值
func (instance *Instance) RPop() (interface{}, error) {
pool.mu.Lock()
defer pool.mu.Unlock()
list, val, err := (*pool.db)[instance.dbName].list.rPop()
if err != nil {
if instance.debug {
log.Printf("db {%s} [RPop] error: %#v\n", instance.dbName, err)
}
return nil, err
}
//当前队列长度减1
(*pool.db)[instance.dbName].size--
(*pool.db)[instance.dbName].list = list
return val, nil
}
//GetDBList 获取当前数据库
func (instance *Instance) GetDBList() []string {
var result []string
pool.mu.Lock()
defer pool.mu.Unlock()
if len(*pool.db) == 0 {
return result
}
for dbName := range *pool.db {
result = append(result, dbName)
}
return result
}
//GetSize 获取当前队列长度
func (instance *Instance) GetSize() uint {
var length uint
pool.mu.Lock()
defer pool.mu.Unlock()
if list, ok := (*pool.db)[instance.dbName]; ok {
length = list.size
}
return length
}
//DisplayQueue 打印队列信息
func (instance *Instance) DisplayQueue() {
pool.mu.Lock()
defer pool.mu.Unlock()
(*pool.db)[instance.dbName].list.displayQueue()
return
}
//FlushDB 删除数据库
//
//警告:此操作后,当前实例将被销毁,若继续调用,将会panic
//
//如:
//
//instance := queue.GetDBInstance("db1")
//
//instance.FlushDB()
//
//instance.LPush(1) # <- 此处将会panic
func (instance *Instance) FlushDB() error {
pool.mu.Lock()
defer func() {
pool.mu.Unlock()
//销毁实例
instance = (*Instance)(nil)
}()
if _, ok := (*pool.db)[instance.dbName]; ok {
if instance.debug {
log.Printf("db {%s} will be delete\n", instance.dbName)
}
delete(*pool.db, instance.dbName)
return nil
}
return fmt.Errorf("db {%s} not exist", instance.dbName)
}