This repository has been archived by the owner on Aug 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
db.go
170 lines (153 loc) · 3.37 KB
/
db.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
package wechat_brain
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/boltdb/bolt"
)
var (
memoryDb *bolt.DB
QuestionBucket = "Question"
)
func init() {
var err error
memoryDb, err = bolt.Open("questions.data", 0600, nil)
if err != nil {
log.Fatal(err)
}
memoryDb.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(QuestionBucket))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
}
func StoreQuestion(question *Question) error {
if question.CalData.TrueAnswer != "" {
return memoryDb.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(QuestionBucket))
v := NewQuestionCols(question.CalData.TrueAnswer)
err := b.Put([]byte(question.Data.Quiz), v.GetData())
return err
})
}
return nil
}
func FetchQuestion(question *Question) (str string) {
memoryDb.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(QuestionBucket))
v := b.Get([]byte(question.Data.Quiz))
if len(v) == 0 {
return nil
}
q := DecodeQuestionCols(v, time.Now().Unix())
str = q.Answer
return nil
})
return
}
func FetchQuestionTime(quiz string) (res int64) {
memoryDb.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(QuestionBucket))
v := b.Get([]byte(quiz))
if len(v) == 0 {
res = -1
return nil
}
q := DecodeQuestionCols(v, time.Now().Unix())
res = q.Update
return nil
})
return
}
func ShowAllQuestions() {
var kv = map[string]string{}
memoryDb.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte(QuestionBucket))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
fmt.Printf("key=%s, value=%s\n", k, v)
kv[string(k)] = string(v)
}
return nil
})
}
func CountQuestions() int {
var i int
memoryDb.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte(QuestionBucket))
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
i++
}
return nil
})
return i
}
func MergeQuestions(fs ...string) {
var i int
for _, f := range fs {
thirdDb, err := bolt.Open(f, 0600, nil)
defer thirdDb.Close()
if err != nil {
log.Println("error in merge file db "+f, err.Error())
continue
}
thirdDb.View(func(thirdTx *bolt.Tx) error {
// Assume bucket exists and has keys
b := thirdTx.Bucket([]byte(QuestionBucket))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
memoryDb.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(QuestionBucket))
//三方包的时间
q := DecodeQuestionCols(v, 0)
//数据库中的时间
if q.Update > FetchQuestionTime(string(k)) {
i++
b.Put(k, q.GetData())
}
return nil
})
}
log.Println("merged file", f)
return nil
})
}
log.Println("merged", i, "questions")
}
type QuestionCols struct {
Answer string `json:"a"`
Update int64 `json:"ts"`
}
func NewQuestionCols(answer string) *QuestionCols {
return &QuestionCols{
Answer: answer,
Update: time.Now().Unix(),
}
}
func DecodeQuestionCols(bs []byte, update int64) *QuestionCols {
var q = &QuestionCols{}
err := json.Unmarshal(bs, q)
if err == nil {
return q
} else {
q = NewQuestionCols(string(bs))
q.Update = update
}
return q
}
func (q *QuestionCols) GetData() []byte {
bs, _ := json.Marshal(q)
return bs
}
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}