-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolutions.go
239 lines (198 loc) · 5.09 KB
/
solutions.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package store
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/boltdb/bolt"
)
// Solution 解题内容
type Solution struct {
QuestionID string `json:"question_id"` // 问题id
Language string `json:"language"` // 语言
CodeHash string `json:"code_hash"` // 实现代码格式化后的hash
SourceDir string `json:"source_dir"` // 代码存放目录
Code string `json:"code"` // 实现的代码
Result string `json:"result"` // 测试返回结果
Times int `json:"times"` // 执行次数
Consumption time.Duration `json:"consumption"` // 消耗
Evaluation string `json:"evaluation"` // 运行评价
Remark string `json:"remark"` // 备注
CreatedAt time.Time `json:"create_at"` // 创建内容时间
}
// NewSolution 创建题解
func NewSolution(questionID, lang, sourceDir, code, result, evaluation, remark string, consumption time.Duration) Solution {
codeHash := md5.Sum([]byte(code))
return Solution{
QuestionID: questionID,
Language: lang,
CodeHash: hex.EncodeToString(codeHash[:]),
SourceDir: sourceDir,
Code: code,
Result: result,
Consumption: consumption,
Evaluation: strings.TrimSpace(evaluation),
Remark: remark,
CreatedAt: time.Now(),
Times: 1,
}
}
// 解题集
type answer []Solution
func (th *answer) Exists(solution Solution) bool {
for idx, s := range *th {
if s.CodeHash == solution.CodeHash {
(*th)[idx].Times++
(*th)[idx].Consumption = solution.Consumption
(*th)[idx].Evaluation = solution.Evaluation
(*th)[idx].Remark = solution.Remark
(*th)[idx].Result = solution.Result
return true
}
}
return false
}
func (th *answer) Append(solution Solution) {
*th = append(*th, solution)
}
func (th answer) Times() (times int) {
for _, solution := range th {
times += solution.Times
}
return
}
func (th answer) FirstTime() time.Time {
return th[0].CreatedAt
}
func (th answer) LastTime() time.Time {
return th[len(th)-1].CreatedAt
}
func (th answer) Bytes() []byte {
b, _ := json.Marshal(th)
return b
}
func (th *answer) Tidy() {
var newAnswer answer
for _, solution := range *th {
tmp := strings.Split(solution.Evaluation, "/")
if len(tmp) == 2 &&
tmp[0] == tmp[1] &&
strings.TrimSpace(tmp[0]) != "0" {
newAnswer = append(newAnswer, solution)
}
}
*th = newAnswer
}
// AddSolution 添加解题答案
func AddSolution(solution Solution) error {
private, err := privateDB()
if err != nil {
return err
}
defer private.Close()
return private.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("solutions"))
if err != nil {
return err
}
key := []byte(fmt.Sprint(solution.QuestionID))
ss := b.Get(key)
var ans answer
if len(ss) != 0 {
err = json.Unmarshal(ss, &ans)
if err != nil {
return err
}
}
if !ans.Exists(solution) {
ans.Append(solution)
}
return b.Put(key, ans.Bytes())
})
}
type SolutionsList struct {
QuestionID string `json:"question_id"`
Version int `json:"version"`
Times int `json:"times"` // 执行次数
FirstTime time.Time `json:"first_time"`
LastTime time.Time `json:"last_time"`
}
func ListSolution() (sl []SolutionsList, err error) {
private, err := privateDB()
if err != nil {
return
}
defer private.Close()
err = private.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("solutions"))
var solutionsList SolutionsList
return b.ForEach(func(k, v []byte) error {
solutionsList.QuestionID = string(k)
var ans answer
err = json.Unmarshal(v, &ans)
if err != nil {
return err
}
solutionsList.Times = ans.Times()
solutionsList.FirstTime = ans.FirstTime()
solutionsList.LastTime = ans.LastTime()
solutionsList.Version = len(ans)
sl = append(sl, solutionsList)
return nil
})
})
return
}
// GetSolution 获取测试过的题解
func GetSolution(questionID string) (ss []Solution, err error) {
private, err := privateDB()
if err != nil {
return
}
defer private.Close()
err = private.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("solutions"))
res := b.Get([]byte(questionID))
if len(res) == 0 {
return fmt.Errorf("question id: %s solutions not found", questionID)
}
return json.Unmarshal(res, &ss)
})
return
}
// RemoveSolution delete solution
func RemoveSolution(questionID string) error {
private, err := privateDB()
if err != nil {
return err
}
defer private.Close()
return private.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("solutions"))
return b.Delete([]byte(questionID))
})
}
func TidySolution(questionID string) error {
private, err := privateDB()
if err != nil {
return err
}
defer private.Close()
return private.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("solutions"))
key := []byte(questionID)
data := b.Get(key)
if len(data) == 0 {
return nil
}
var ans answer
err := json.Unmarshal(data, &ans)
if err != nil {
return err
}
ans.Tidy()
return b.Put(key, ans.Bytes())
})
}