-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathversion_log.go
71 lines (58 loc) · 1.44 KB
/
version_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
package deltago
import (
"github.com/csimplestring/delta-go/action"
"github.com/csimplestring/delta-go/iter"
"github.com/csimplestring/delta-go/store"
)
type VersionLog interface {
Version() int64
Actions() ([]action.Action, error)
ActionIter() (iter.Iter[action.Action], error)
}
var _ VersionLog = &InMemVersionLog{}
var _ VersionLog = &MemOptimizedVersionLog{}
type InMemVersionLog struct {
version int64
actions []action.Action
}
func (v *InMemVersionLog) Version() int64 {
return v.version
}
func (v *InMemVersionLog) Actions() ([]action.Action, error) {
return v.actions, nil
}
func (v *InMemVersionLog) ActionIter() (iter.Iter[action.Action], error) {
return iter.FromSlice(v.actions), nil
}
type MemOptimizedVersionLog struct {
version int64
path string
store store.Store
}
func (m *MemOptimizedVersionLog) Version() int64 {
return m.version
}
func (m *MemOptimizedVersionLog) Actions() ([]action.Action, error) {
i, err := m.store.Read(m.path)
if err != nil {
return nil, err
}
defer i.Close()
return iter.Map(i, func(t string) (action.Action, error) {
return action.FromJson(t)
})
}
func (m *MemOptimizedVersionLog) ActionIter() (iter.Iter[action.Action], error) {
i, err := m.store.Read(m.path)
if err != nil {
return nil, err
}
defer i.Close()
mapIter := &iter.MapIter[string, action.Action]{
It: i,
Mapper: func(s string) (action.Action, error) {
return action.FromJson(s)
},
}
return mapIter, nil
}