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

重构数据结构,并使用 sqlite 进行查询 #8

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
63cf15e
remove gorm, instead of xorm
zxhd863943427 Jul 16, 2024
cc1c4bc
finish riff init interface and test
zxhd863943427 Jul 19, 2024
fbcc124
test db.sql to improve speed
zxhd863943427 Jul 20, 2024
1c13084
use tx improve speed
zxhd863943427 Jul 20, 2024
3a953db
add BlockIDs init
zxhd863943427 Jul 20, 2024
9f634ef
add load
zxhd863943427 Jul 21, 2024
54540e3
finish card interface
zxhd863943427 Jul 21, 2024
c0509b9
finish history interface
zxhd863943427 Jul 21, 2024
902f5b0
add load lock
zxhd863943427 Jul 21, 2024
de8e347
fix bug of dup return
zxhd863943427 Jul 21, 2024
131b3b4
add BaseCard tag
zxhd863943427 Jul 21, 2024
2ca70a0
fix history
zxhd863943427 Jul 21, 2024
fc5f48c
improve test
zxhd863943427 Jul 21, 2024
7948a9f
change due return
zxhd863943427 Jul 21, 2024
5870d86
clean deugger stmt
zxhd863943427 Jul 22, 2024
a3b2022
add target
zxhd863943427 Jul 22, 2024
45cb814
add TestPerformance
zxhd863943427 Jul 22, 2024
4588c80
Extract batch insert func
zxhd863943427 Jul 22, 2024
8dbf4a4
Refactor to add lock
zxhd863943427 Jul 22, 2024
28047a4
add xorm Find lock warp
zxhd863943427 Jul 22, 2024
e7e3e48
Refactor load histroy
zxhd863943427 Jul 22, 2024
f7d34a5
improve riff_test
zxhd863943427 Jul 22, 2024
716fc28
clean code
zxhd863943427 Jul 25, 2024
093f6f6
Adjust the review interface to make it closer to the old version and …
zxhd863943427 Jul 25, 2024
592f034
add GetCardsByBlockIDs
zxhd863943427 Jul 27, 2024
dd0c4bb
The map cache is added to greatly improve query and save performance
zxhd863943427 Jul 30, 2024
2b2b7e2
Allow load to execute async and add functions that wait for completion
zxhd863943427 Jul 30, 2024
94a1875
add deck GetDID
zxhd863943427 Jul 30, 2024
1a5ad6c
clean unused code
zxhd863943427 Jul 30, 2024
84688e8
Update card status during review.
zxhd863943427 Jul 31, 2024
17326dd
Retrieve csid using cache in byblockIDs.
zxhd863943427 Aug 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 225 additions & 17 deletions card.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,97 @@

package riff

import "time"
import (
"encoding/json"
"time"

"github.com/open-spaced-repetition/go-fsrs"
"github.com/siyuan-note/logging"
)

// Card 描述了闪卡。
type Card interface {
// ID 返回闪卡 ID。
ID() string

// BlockID 返回闪卡关联的内容块 ID。
BlockID() string
// BlockID() string

// CSID 获取卡片的 CSID
GetCSID() string

// NextDues 返回每种评分对应的下次到期时间。
NextDues() map[Rating]time.Time

// SetNextDues 设置每种评分对应的下次到期时间。
SetNextDues(map[Rating]time.Time)

// SetDue 设置到期时间。
SetDue(time.Time)
// GetUpdate 返回闪卡的更新时间。
GetUpdate() time.Time

// SetUpdate 设置闪卡的更新时间。
SetUpdate(time.Time)

// GetState 返回闪卡状态。
GetState() State

// SetState 设置闪卡状态。
SetState(State)

// GetLapses 返回闪卡的遗忘次数。
GetLapses() int

// SetLapses 设置闪卡的遗忘次数。
SetLapses(int)

// GetReps 返回闪卡的复习次数。
GetReps() int

// GetState 返回闪卡状态。
GetState() State
// SetReps 设置闪卡的复习次数。
SetReps(int)

// GetSuspend 返回闪卡是否被暂停。
GetSuspend() bool

// SetSuspend 设置闪卡是否被暂停。
SetSuspend(bool)

// GetTag 返回闪卡的标签。
GetTag() string

// SetTag 设置闪卡的标签。
SetTag(string)

// GetFlag 返回闪卡的标志。
GetFlag() string

// SetFlag 设置闪卡的标志。
SetFlag(string)

// GetPriority 返回闪卡的优先级。
GetPriority() float64

// SetPriority 设置闪卡的优先级。
SetPriority(float64)

// GetDue 返回闪卡的到期时间。
GetDue() time.Time

// SetDue 设置闪卡的到期时间。
SetDue(time.Time)

// 返回 Algo
GetAlgo() Algo

UseAlgo(algo Algo)

// GetLastReview 返回闪卡的最后复习时间。
GetLastReview() time.Time
// 返回 MarshalImpl
GetMarshalImpl() []uint8

// Clone 返回闪卡的克隆。
Clone() Card
// 对 Impl 进行 Marshal
MarshalImpl()

UnmarshalImpl()

// Impl 返回具体的闪卡实现。
Impl() interface{}
Expand All @@ -57,11 +115,129 @@ type Card interface {
SetImpl(c interface{})
}

func UnmarshalImpl(card Card) {
switch card.GetAlgo() {
case AlgoFSRS:
impl := fsrs.Card{}
json.Unmarshal(card.GetMarshalImpl(), &impl)
card.SetImpl(impl)
default:
return
}
}

// BaseCard 描述了基础的闪卡实现。
type BaseCard struct {
CID string
BID string
NDues map[Rating]time.Time
CID string `xorm:"pk index"`
CSID string
Update time.Time
State State //State 返回闪卡状态。
Lapses int //Lapses 返回闪卡的遗忘次数。
Reps int //Reps 返回闪卡的复习次数。
Suspend bool `xorm:"index"`
Tag string
Flag string
Priority float64
Due time.Time `xorm:"index"`
NDues map[Rating]time.Time `xorm:"-"`
Algo Algo
AlgoImpl interface{} `xorm:"-"`
AlgoImplData []uint8 `json:"-"`
}

func NewBaseCard(cs CardSource) (card *BaseCard) {
CSID := cs.GetCSID()
card = &BaseCard{
CSID: CSID,
CID: newID(),
State: New,
Update: time.Now(),
Due: time.Now(),
NDues: map[Rating]time.Time{},
Priority: 0.5,
AlgoImplData: []uint8{},
}
return
}

func (card *BaseCard) ID() string {
return card.CID
}

func (card *BaseCard) GetCSID() string {
return card.CSID
}

func (c *BaseCard) GetUpdate() time.Time {
return c.Update
}

func (c *BaseCard) SetUpdate(update time.Time) {
c.Update = update
}

func (c *BaseCard) GetState() State {
return c.State
}

func (c *BaseCard) SetState(state State) {
c.State = state
}

func (c *BaseCard) GetLapses() int {
return c.Lapses
}

func (c *BaseCard) SetLapses(lapses int) {
c.Lapses = lapses
}

func (c *BaseCard) GetReps() int {
return c.Reps
}

func (c *BaseCard) SetReps(reps int) {
c.Reps = reps
}

func (c *BaseCard) GetSuspend() bool {
return c.Suspend
}

func (c *BaseCard) SetSuspend(suspend bool) {
c.Suspend = suspend
}

func (c *BaseCard) GetTag() string {
return c.Tag
}

func (c *BaseCard) SetTag(tag string) {
c.Tag = tag
}

func (c *BaseCard) GetFlag() string {
return c.Flag
}

func (c *BaseCard) SetFlag(flag string) {
c.Flag = flag
}

func (c *BaseCard) GetPriority() float64 {
return c.Priority
}

func (c *BaseCard) SetPriority(priority float64) {
c.Priority = priority
}

func (c *BaseCard) GetDue() time.Time {
return c.Due
}

func (c *BaseCard) SetDue(due time.Time) {
c.Due = due
}

func (card *BaseCard) NextDues() map[Rating]time.Time {
Expand All @@ -72,10 +248,42 @@ func (card *BaseCard) SetNextDues(dues map[Rating]time.Time) {
card.NDues = dues
}

func (card *BaseCard) ID() string {
return card.CID
func (card *BaseCard) Impl() interface{} {
return card.AlgoImpl
}
func (card *BaseCard) SetImpl(c interface{}) {
card.AlgoImpl = c
}

func (card *BaseCard) UseAlgo(algo Algo) {
switch algo {
case AlgoFSRS:
AlgoImpl := fsrs.NewCard()
card.AlgoImpl = AlgoImpl
card.Algo = AlgoFSRS
// card.Due = AlgoImpl.Due
default:
logging.LogErrorf("unsupported Algo: %s", algo)
}
}

func (card *BaseCard) GetMarshalImpl() []uint8 {
if card.AlgoImplData == nil || len(card.AlgoImplData) == 0 {
card.MarshalImpl()
}

return card.AlgoImplData
}

func (card *BaseCard) MarshalImpl() {
data, _ := json.Marshal(card.AlgoImpl)
card.AlgoImplData = data
}

func (card *BaseCard) UnmarshalImpl() {
UnmarshalImpl(card)
}

func (card *BaseCard) BlockID() string {
return card.BID
func (card *BaseCard) GetAlgo() Algo {
return card.Algo
}
108 changes: 108 additions & 0 deletions card_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package riff

import (
"encoding/json"
"errors"
"fmt"
"time"

"github.com/open-spaced-repetition/go-fsrs"
)

type History interface {
ID() string

// 对 Impl 进行 Marshal
MarshalImpl()

UnmarshalImpl() (err error)
}

type BaseHistory struct {
HID string `xorm:"pk index"`
CID string
Update time.Time `xorm:"index"`
UpdateResult int
State State
Tag string
Flag string
Suspend bool
Priority float64
Due time.Time
Algo Algo
AlgoImpl interface{} `xorm:"-"`
AlgoImplData []uint8 `json:"-"`
}

type ReviewLog struct {
HID string
Rate Rating
Review time.Time
}

type ReviewHistory struct {
BaseHistory `xorm:"extends"`
ReviewLog `xorm:"extends"`
}

func NewBaseHistory(c Card) (history *BaseHistory) {
return &BaseHistory{
HID: newID(),
CID: c.ID(),
Update: time.Now(),
UpdateResult: CreateHistory, // 这里可以根据需要设置具体的更新结果
State: c.GetState(),
Tag: c.GetTag(),
Flag: c.GetFlag(),
Suspend: c.GetSuspend(),
Priority: c.GetPriority(),
Due: c.GetDue(),
Algo: c.GetAlgo(),
AlgoImpl: c.Impl(), // 假设Card接口有Impl方法
AlgoImplData: c.GetMarshalImpl(), // 假设Card接口有GetMarshalImpl方法
}
}

func (b *BaseHistory) ID() string {
return b.HID
}

func (b *BaseHistory) UnmarshalImpl() (err error) {
if b.AlgoImplData == nil || len(b.AlgoImplData) == 0 {
err = errors.New("dont have AlgoImplData")
return
}
switch b.Algo {
case AlgoFSRS:
impl := fsrs.Card{}
json.Unmarshal(b.AlgoImplData, &impl)
b.AlgoImpl = impl
default:
err = fmt.Errorf("un support Algo Type : %s", string(b.Algo))
return
}
return
}

func (b *BaseHistory) MarshalImpl() {
if len(b.AlgoImplData) != 0 && b.AlgoImpl == nil {
return
}
data, _ := json.Marshal(b.AlgoImpl)
b.AlgoImplData = data
}

func NewReviewLog(h History, rate Rating) (log *ReviewLog) {
log = &ReviewLog{
HID: h.ID(),
Rate: rate,
Review: time.Now(),
}
return
}

const (
EditUpdate int = -2
CreateHistory int = -1
ReviewUpdate int = 1
)
Loading