Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor flashcard #6

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
完成 FSRSStore 中的 CardSourceStore 基本实现
zxhd863943427 committed May 3, 2024
commit a7c1f66eb18f3c6a68a43eb148f3f16f6a6cf069
6 changes: 4 additions & 2 deletions card_source_store.go
Original file line number Diff line number Diff line change
@@ -11,11 +11,13 @@ type CardSourceStore interface {
// 通过 CardSourceID 获得 CardSource
GetCardSourceByID(id string) CardSource

// 通过 Card获取
// 通过 Card 获取 cardSource
GetCardSourceByCard(card Card) CardSource

SetCardSource(cardSource CardSource)
// 设置 store 内相同 cardSourceID 的 cardsource 为传入的cardSource
SetCardSource(cardSource CardSource) (err error)

// 通过 id 删除 cardSource
RemoveCardSource(id string)

Load()
98 changes: 98 additions & 0 deletions fsrs_store.go
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
package riff

import (
"errors"
"os"
"path/filepath"
"sort"
@@ -115,6 +116,7 @@ func (store *FSRSStore) RemoveCard(id string) Card {
return card
}

// 获取 cardsource 关联的 blockIDs
func getCardSourceRelatedBlockID(cardSource CardSource) (ret []string) {
blockIDsStr := cardSource.GetContext()[builtInContext]
blockIDsStr = strings.Replace(blockIDsStr, " ", "", -1)
@@ -400,6 +402,102 @@ func (store *FSRSStore) AddCardSource(id string, CType CardType, cardIDMap map[s
return cardSource
}

func (store *FSRSStore) setNewCardSOurceRelatedCard(cardID, cardSourceID, key string) (err error) {
cardSource, ok := store.cardSources[cardSourceID]
if !ok {
err = errors.New("cardsource not found")
return
}

c := fsrs.NewCard()
card := &FSRSCard{BaseCard: &BaseCard{CID: cardID, SID: cardSourceID}, C: &c}
store.cards[cardID] = card
cardSource.SetCardIDMap(key, cardID)
return
}

func (store *FSRSStore) UpdateCardSource(id string, cardIDMap map[string]string) (err error) {
store.lock.Lock()
defer store.lock.Unlock()
cardSource, ok := store.cardSources[id]
if !ok {
err = errors.New("cardsource not found")
return
}
originCardIDMap := cardSource.GetCardIDMap()
var deleteCardIDs []string
for key := range originCardIDMap {
if cardID, ok := cardIDMap[key]; !ok {
deleteCardIDs = append(deleteCardIDs, cardID)
}
}
for _, cardID := range deleteCardIDs {
store.RemoveCard(cardID)
}
for key, cardID := range cardIDMap {
if originCardID, ok := originCardIDMap[key]; !ok {
err = store.setNewCardSOurceRelatedCard(cardID, id, key)
if nil != err {
return
}
} else {
if originCardID == cardID {
continue
}
store.RemoveCard(originCardID)
err = store.setNewCardSOurceRelatedCard(cardID, id, key)
if nil != err {
return
}
}
}
return
}

func (store *FSRSStore) GetCardSourceByID(id string) CardSource {
store.lock.Lock()
defer store.lock.Unlock()
ret := store.cardSources[id]
if nil == ret {
return nil
}
return ret
}

func (store *FSRSStore) GetCardSourceByCard(card Card) CardSource {
store.lock.Lock()
defer store.lock.Unlock()
id := card.CardSourceID()
ret := store.cardSources[id]
if nil == ret {
return nil
}
return ret
}

func (store *FSRSStore) RemoveCardSource(id string) {
cardSource := store.cardSources[id]
if nil == cardSource {
return
}
cardIDs := cardSource.GetCardIDs()

for _, cardID := range cardIDs {
store.RemoveCard(cardID)
}
}

func (store *FSRSStore) SetCardSource(cardSource CardSource) (err error) {
cardIDMap := cardSource.GetCardIDMap()
id := cardSource.SourceID()
err = store.UpdateCardSource(id, cardIDMap)
if nil != err {
return
}
store.cardSources[id] = cardSource.(*BaseCardSource)
return
}

type FSRSCard struct {
*BaseCard
C *fsrs.Card