-
Notifications
You must be signed in to change notification settings - Fork 568
/
tmp.go
48 lines (41 loc) · 1.22 KB
/
tmp.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
package renew
import (
"context"
"fmt"
"sync"
"time"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
"github.com/pachyderm/pachyderm/v2/src/internal/storage/track"
"github.com/pachyderm/pachyderm/v2/src/internal/uuid"
)
// TmpTrackerPrefix is the tracker prefix for temporary objects.
const TmpTrackerPrefix = "tmp/"
type tmpDeleter struct{}
// NewTmpDeleter creates a new temporary deleter.
func NewTmpDeleter() track.Deleter {
return &tmpDeleter{}
}
func (*tmpDeleter) DeleteTx(tx *pachsql.Tx, _ string) error {
return nil
}
// NewTmpComposer returns a ComposeFunc which creates meaningless temporary objects
// Use NewTmpDeleter to get a no-op track.Deleter to handle these objects.
func NewTmpComposer(tr track.Tracker, name string) ComposeFunc {
if name == "" {
panic("must provide non-empty name for tmp ComposeFunc")
}
prefix := fmt.Sprintf("%s/%s-%s", TmpTrackerPrefix, name, uuid.NewWithoutDashes())
var n int
var mu sync.Mutex
return func(ctx context.Context, ids []string, ttl time.Duration) (string, error) {
mu.Lock()
n2 := n
n++
mu.Unlock()
tmpID := fmt.Sprintf("%s/%08x", prefix, n2)
if err := track.Create(ctx, tr, tmpID, ids, ttl); err != nil {
return "", err
}
return tmpID, nil
}
}