-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
101 lines (77 loc) · 1.72 KB
/
log.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
package bltfs
import (
"sync"
"hpt.space/bltfs/backend"
pb "hpt.space/bltfs/proto"
)
type synchronizedWriter struct {
mu struct {
sync.Mutex
backend backend.Interface
}
}
func (rw *synchronizedWriter) Read(p []byte) (n int, err error) {
rw.mu.Lock()
defer rw.mu.Unlock()
return rw.mu.backend.Read(p)
}
func (rw *synchronizedWriter) Write(p []byte) (n int, err error) {
rw.mu.Lock()
defer rw.mu.Unlock()
return rw.mu.backend.Write(p)
}
type Log interface {
Create(*pb.Entry)
Remove(*pb.Entry)
Change(*pb.Entry)
}
type Incremental struct {
*pb.Log
}
type Differential struct {
*pb.Log
}
func NewIncremental(epoch *Incremental) *Incremental {
return &Incremental{
Log: &pb.Log{
Class: pb.Log_INC,
Prev: epoch.Block,
Entries: make([]*pb.Entry, 0),
Extents: make([]*pb.Extent, 0),
},
}
}
func (inc *Incremental) Create(e *pb.Entry) {
e.Operation = pb.Entry_ADD
inc.Entries = append(inc.Entries, e)
}
func (inc *Incremental) Remove(e *pb.Entry) {
e.Operation = pb.Entry_RM
inc.Entries = append(inc.Entries, e)
}
func (inc *Incremental) Change(e *pb.Entry) {
e.Operation = pb.Entry_CH
inc.Entries = append(inc.Entries, e)
}
func NewDifferential(epoch *pb.Index) *Differential {
return &Differential{
Log: &pb.Log{
Class: pb.Log_DIFF,
Prev: epoch.Block,
Entries: make([]*pb.Entry, 0),
Extents: make([]*pb.Extent, 0),
},
}
}
func (diff *Differential) Create(e *pb.Entry) {
e.Operation = pb.Entry_ADD
diff.Entries = append(diff.Entries, e)
}
func (diff *Differential) Remove(e *pb.Entry) {
e.Operation = pb.Entry_RM
diff.Entries = append(diff.Entries, e)
}
func (diff *Differential) Change(e *pb.Entry) {
e.Operation = pb.Entry_CH
diff.Entries = append(diff.Entries, e)
}