-
Notifications
You must be signed in to change notification settings - Fork 67
/
compact.go
57 lines (54 loc) · 1.6 KB
/
compact.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
package exec
import (
"context"
"errors"
"github.com/brimdata/zed"
"github.com/brimdata/zed/lake"
"github.com/brimdata/zed/lake/commits"
"github.com/brimdata/zed/runtime"
"github.com/brimdata/zed/runtime/sam/op/meta"
"github.com/brimdata/zed/zbuf"
"github.com/segmentio/ksuid"
)
func Compact(ctx context.Context, lk *lake.Root, pool *lake.Pool, branchName string, objectIDs []ksuid.KSUID, writeVectors bool, author, message, info string) (ksuid.KSUID, error) {
if len(objectIDs) < 2 {
return ksuid.Nil, errors.New("compact: two or more source objects required")
}
branch, err := pool.OpenBranchByName(ctx, branchName)
if err != nil {
return ksuid.Nil, err
}
base, err := pool.Snapshot(ctx, branch.Commit)
if err != nil {
return ksuid.Nil, err
}
compact := commits.NewSnapshot()
for _, oid := range objectIDs {
o, err := base.Lookup(oid)
if err != nil {
return ksuid.Nil, err
}
compact.AddDataObject(o)
}
zctx := zed.NewContext()
lister := meta.NewSortedListerFromSnap(ctx, zed.NewContext(), pool, compact, nil)
rctx := runtime.NewContext(ctx, zctx)
slicer := meta.NewSlicer(lister, zctx)
puller := meta.NewSequenceScanner(rctx, slicer, pool, nil, nil, nil)
w := lake.NewSortedWriter(ctx, zctx, pool, writeVectors)
if err := zbuf.CopyPuller(w, puller); err != nil {
puller.Pull(true)
w.Abort()
return ksuid.Nil, err
}
if err := w.Close(); err != nil {
w.Abort()
return ksuid.Nil, err
}
commit, err := branch.CommitCompact(ctx, compact.SelectAll(), w.Objects(), w.Vectors(), author, message, info)
if err != nil {
w.Abort()
return ksuid.Nil, err
}
return commit, nil
}