-
Notifications
You must be signed in to change notification settings - Fork 10
/
deck.go
223 lines (185 loc) · 5.26 KB
/
deck.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
// Riff - Spaced repetition.
// Copyright (c) 2022-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package riff
import (
"errors"
"path/filepath"
"sync"
"time"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/vmihailenco/msgpack/v5"
)
// Deck 描述了一套闪卡包。
type Deck struct {
ID string // ID
Name string // 名称
Algo Algo // 间隔重复算法
Desc string // 描述
Created int64 // 创建时间
Updated int64 // 更新时间
store Store // 底层存储
lock *sync.Mutex
}
// LoadDeck 从文件夹 saveDir 路径上加载 id 闪卡包。
func LoadDeck(saveDir, id string, requestRetention float64, maximumInterval int, weights string) (deck *Deck, err error) {
created := time.Now().UnixMilli()
deck = &Deck{
ID: id,
Name: id,
Algo: AlgoFSRS,
Created: created,
Updated: created,
lock: &sync.Mutex{},
}
dataPath := getDeckMsgpackPath(saveDir, id)
if filelock.IsExist(dataPath) {
var data []byte
data, err = filelock.ReadFile(dataPath)
if nil != err {
logging.LogErrorf("load deck [%s] failed: %s", deck.Name, err)
return
}
err = msgpack.Unmarshal(data, deck)
if nil != err {
logging.LogErrorf("load deck [%s] failed: %s", deck.Name, err)
return
}
}
var store Store
switch deck.Algo {
case AlgoFSRS:
store = NewFSRSStore(deck.ID, saveDir, requestRetention, maximumInterval, weights)
err = store.Load()
default:
err = errors.New("not supported yet")
return
}
if nil != err {
return
}
deck.store = store
return
}
// AddCard 新建一张闪卡。
func (deck *Deck) AddCard(cardID, blockID string) {
deck.lock.Lock()
defer deck.lock.Unlock()
card := deck.store.GetCard(cardID)
if nil != card {
return
}
deck.store.AddCard(cardID, blockID)
deck.Updated = time.Now().UnixMilli()
}
// RemoveCard 删除一张闪卡。
func (deck *Deck) RemoveCard(cardID string) {
deck.lock.Lock()
defer deck.lock.Unlock()
deck.store.RemoveCard(cardID)
deck.Updated = time.Now().UnixMilli()
}
// SetCard 设置一张闪卡。
func (deck *Deck) SetCard(card Card) {
deck.lock.Lock()
defer deck.lock.Unlock()
deck.store.SetCard(card)
}
// GetCard 根据闪卡 ID 获取对应的闪卡。
func (deck *Deck) GetCard(cardID string) Card {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.GetCard(cardID)
}
func (deck *Deck) GetCardsByBlockID(blockID string) (ret []Card) {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.GetCardsByBlockID(blockID)
}
// GetCardsByBlockIDs 获取指定内容块的所有卡片。
func (deck *Deck) GetCardsByBlockIDs(blockIDs []string) (ret []Card) {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.GetCardsByBlockIDs(blockIDs)
}
func (deck *Deck) GetNewCardsByBlockIDs(blockIDs []string) (ret []Card) {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.GetNewCardsByBlockIDs(blockIDs)
}
func (deck *Deck) GetDueCardsByBlockIDs(blockIDs []string) (ret []Card) {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.GetDueCardsByBlockIDs(blockIDs)
}
// GetBlockIDs 获取所有内容块 ID。
func (deck *Deck) GetBlockIDs() (ret []string) {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.GetBlockIDs()
}
// CountCards 获取卡包中的闪卡数量。
func (deck *Deck) CountCards() int {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.CountCards()
}
// Save 保存闪卡包。
func (deck *Deck) Save() (err error) {
deck.lock.Lock()
defer deck.lock.Unlock()
deck.Updated = time.Now().UnixMilli()
err = deck.store.Save()
if nil != err {
logging.LogErrorf("save deck [%s] failed: %s", deck.Name, err)
return
}
saveDir := deck.store.GetSaveDir()
dataPath := getDeckMsgpackPath(saveDir, deck.ID)
data, err := msgpack.Marshal(deck)
if nil != err {
logging.LogErrorf("save deck failed: %s", err)
return
}
if err = filelock.WriteFile(dataPath, data); nil != err {
logging.LogErrorf("save deck failed: %s", err)
return
}
return
}
// SaveLog 保存闪卡包的复习日志。
func (deck *Deck) SaveLog(log *Log) (err error) {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.SaveLog(log)
}
// Review 复习一张闪卡,rating 为复习评分结果。
func (deck *Deck) Review(cardID string, rating Rating) (ret *Log) {
deck.lock.Lock()
defer deck.lock.Unlock()
ret = deck.store.Review(cardID, rating)
deck.Updated = time.Now().UnixMilli()
return
}
// Dues 返回所有到期的闪卡。
func (deck *Deck) Dues() (ret []Card) {
deck.lock.Lock()
defer deck.lock.Unlock()
return deck.store.Dues()
}
func getDeckMsgpackPath(saveDir, id string) string {
return filepath.Join(saveDir, id+".deck")
}