blob: 674b6923b0f78ea3c3c23c28966fcea8d554423e (
plain) (
blame)
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
|
package cache
import (
"sync"
"github.com/MichaelMure/git-bug/entity/dag"
"github.com/MichaelMure/git-bug/repository"
)
var _ dag.Interface[dag.Snapshot, dag.OperationWithApply[dag.Snapshot]] = &withSnapshot[dag.Snapshot, dag.OperationWithApply[dag.Snapshot]]{}
// withSnapshot encapsulate an entity and maintain a snapshot efficiently.
type withSnapshot[SnapT dag.Snapshot, OpT dag.OperationWithApply[SnapT]] struct {
dag.Interface[SnapT, OpT]
mu sync.Mutex
snap *SnapT
}
func (ws *withSnapshot[SnapT, OpT]) Compile() SnapT {
ws.mu.Lock()
defer ws.mu.Unlock()
if ws.snap == nil {
snap := ws.Interface.Compile()
ws.snap = &snap
}
return *ws.snap
}
// Append intercept Bug.Append() to update the snapshot efficiently
func (ws *withSnapshot[SnapT, OpT]) Append(op OpT) {
ws.mu.Lock()
defer ws.mu.Unlock()
ws.Interface.Append(op)
if ws.snap == nil {
return
}
op.Apply(*ws.snap)
(*ws.snap).AppendOperation(op)
}
// Commit intercept Bug.Commit() to update the snapshot efficiently
func (ws *withSnapshot[SnapT, OpT]) Commit(repo repository.ClockedRepo) error {
ws.mu.Lock()
defer ws.mu.Unlock()
err := ws.Interface.Commit(repo)
if err != nil {
ws.snap = nil
return err
}
return nil
}
|