-
Notifications
You must be signed in to change notification settings - Fork 360
/
actions_source.go
74 lines (66 loc) · 1.92 KB
/
actions_source.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
package catalog
import (
"context"
"fmt"
"io"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/graveler"
)
const repositoryLocation = "_lakefs_actions/"
type ActionsSource struct {
catalog *Catalog
}
func NewActionsSource(catalog *Catalog) *ActionsSource {
return &ActionsSource{
catalog: catalog,
}
}
func (s *ActionsSource) List(ctx context.Context, record graveler.HookRecord) ([]string, error) {
const amount = 1000
var after string
hasMore := true
var names []string
for hasMore {
var res []*DBEntry
var err error
res, hasMore, err = s.catalog.ListEntries(ctx, record.RepositoryID.String(), record.SourceRef.String(), repositoryLocation, after, DefaultPathDelimiter, amount)
if err != nil {
return nil, fmt.Errorf("listing actions: %w", err)
}
for _, result := range res {
names = append(names, result.Path)
}
}
return names, nil
}
func (s *ActionsSource) Load(ctx context.Context, record graveler.HookRecord, name string) ([]byte, error) {
// get name's address
repositoryID := record.RepositoryID
ent, err := s.catalog.GetEntry(ctx, repositoryID.String(), record.SourceRef.String(), name, GetEntryParams{})
if err != nil {
return nil, fmt.Errorf("get action file metadata %s: %w", name, err)
}
// get repo storage namespace
repo, err := s.catalog.GetRepository(ctx, repositoryID.String())
if err != nil {
return nil, fmt.Errorf("get repository %s: %w", repositoryID, err)
}
// get action address
blockAdapter := s.catalog.BlockAdapter
reader, err := blockAdapter.Get(ctx, block.ObjectPointer{
StorageNamespace: repo.StorageNamespace,
Identifier: ent.PhysicalAddress,
}, 0)
if err != nil {
return nil, fmt.Errorf("getting action file %s: %w", name, err)
}
defer func() {
_ = reader.Close()
}()
// read action
bytes, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("reading action file %s: %w", name, err)
}
return bytes, nil
}