-
Notifications
You must be signed in to change notification settings - Fork 342
/
inventory_iterator.go
161 lines (149 loc) · 4.43 KB
/
inventory_iterator.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package s3
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/cloud/aws/s3inventory"
"github.com/treeverse/lakefs/pkg/cmdutils"
)
var ErrInventoryNotSorted = errors.New("got unsorted s3 inventory")
type InventoryIterator struct {
*Inventory
err error
val *block.InventoryObject
buffer []*s3inventory.InventoryObject
inventoryFileIndex int
valIndexInBuffer int
inventoryFileProgress *cmdutils.Progress
currentFileProgress *cmdutils.Progress
currentPrefix int
}
func NewInventoryIterator(inv *Inventory) *InventoryIterator {
creationTimestamp, err := strconv.ParseInt(inv.Manifest.CreationTimestamp, 10, 64) //nolint: gomnd
if err != nil {
inv.logger.WithError(err).Error("Failed to get creation timestamp from manifest")
creationTimestamp = 0
}
t := time.Unix(creationTimestamp/int64(time.Second/time.Millisecond), 0)
inventoryFileProgress := cmdutils.NewActiveProgress(fmt.Sprintf("Inventory (%s) Files Read", t.Format("2006-01-02")), cmdutils.Bar)
inventoryFileProgress.SetTotal(int64(len(inv.Manifest.Files)))
return &InventoryIterator{
Inventory: inv,
inventoryFileIndex: -1,
inventoryFileProgress: inventoryFileProgress,
currentFileProgress: cmdutils.NewActiveProgress(fmt.Sprintf("Inventory (%s) Current File", t.Format("2006-01-02")), cmdutils.Bar),
}
}
func (it *InventoryIterator) Next() bool {
for {
if len(it.Manifest.Files) == 0 {
// empty manifest
return false
}
val := it.nextFromBuffer()
if val != nil {
// validate element order
if it.shouldSort && it.val != nil && val.Key < it.val.Key {
it.err = ErrInventoryNotSorted
return false
}
it.currentFileProgress.SetCurrent(int64(it.valIndexInBuffer + 1))
it.val = val
return true
}
// value not found in buffer, need to reload the buffer
it.valIndexInBuffer = -1
if !it.moveToNextInventoryFile() {
// no more files left
it.inventoryFileProgress.SetCompleted(true)
it.currentFileProgress.SetCompleted(true)
return false
}
if !it.fillBuffer() {
return false
}
}
}
func (it *InventoryIterator) moveToNextInventoryFile() bool {
if it.inventoryFileIndex == len(it.Manifest.Files)-1 {
return false
}
it.inventoryFileIndex += 1
it.inventoryFileProgress.Incr()
it.logger.WithField("file", it.Manifest.Files[it.inventoryFileIndex].Key).Debug("Moving to next manifest file")
it.buffer = nil
return true
}
func (it *InventoryIterator) fillBuffer() bool {
it.logger.Debug("Start reading rows from inventory to buffer")
rdr, err := it.reader.GetFileReader(it.Manifest.Format, it.Manifest.inventoryBucket, it.Manifest.Files[it.inventoryFileIndex].Key)
if err != nil {
it.err = err
return false
}
it.currentFileProgress.SetTotal(rdr.GetNumRows())
it.currentFileProgress.SetCurrent(0)
defer func() {
err = rdr.Close()
if err != nil {
it.logger.WithError(err).WithField("file", it.Manifest.Files[it.inventoryFileIndex].Key).Error("Failed to close manifest file reader")
}
}()
it.buffer, err = rdr.Read(int(rdr.GetNumRows()))
if err != nil {
it.err = err
return false
}
return true
}
func (it *InventoryIterator) nextFromBuffer() *block.InventoryObject {
for i := it.valIndexInBuffer + 1; i < len(it.buffer); i++ {
obj := it.buffer[i]
if !obj.IsLatest || obj.IsDeleteMarker {
continue
}
if len(it.prefixes) > 0 {
// check file against prefix filter
// loop while current prefix may fit current object
for it.prefixes[it.currentPrefix] < obj.Key {
if strings.HasPrefix(obj.Key, it.prefixes[it.currentPrefix]) {
// found current prefix
break
}
it.currentPrefix++
if it.currentPrefix == len(it.prefixes) {
// no more prefixes
return nil
}
}
if !strings.HasPrefix(obj.Key, it.prefixes[it.currentPrefix]) {
continue
}
}
res := block.InventoryObject{
Bucket: obj.Bucket,
Key: obj.Key,
PhysicalAddress: obj.GetPhysicalAddress(),
Size: obj.Size,
LastModified: obj.LastModified,
Checksum: obj.Checksum,
}
it.valIndexInBuffer = i
return &res
}
return nil
}
func (it *InventoryIterator) Err() error {
return it.err
}
func (it *InventoryIterator) Get() *block.InventoryObject {
return it.val
}
func (it *InventoryIterator) Progress() []*cmdutils.Progress {
return []*cmdutils.Progress{
it.inventoryFileProgress, it.currentFileProgress,
}
}