-
Notifications
You must be signed in to change notification settings - Fork 568
/
util.go
44 lines (39 loc) · 1.53 KB
/
util.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
package chunk
import (
"context"
"testing"
"github.com/pachyderm/pachyderm/v2/src/internal/dbutil"
"github.com/pachyderm/pachyderm/v2/src/internal/dockertestenv"
"github.com/pachyderm/pachyderm/v2/src/internal/obj"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
"github.com/pachyderm/pachyderm/v2/src/internal/require"
"github.com/pachyderm/pachyderm/v2/src/internal/storage/kv"
"github.com/pachyderm/pachyderm/v2/src/internal/storage/track"
)
// NewTestStorage creates a local storage instance for testing during the lifetime of
// the callback.
func NewTestStorage(ctx context.Context, t testing.TB, db *pachsql.DB, tr track.Tracker, opts ...StorageOption) (obj.Client, *Storage) {
objC := dockertestenv.NewTestObjClient(ctx, t)
db.MustExec(`CREATE SCHEMA IF NOT EXISTS storage`)
require.NoError(t, dbutil.WithTx(context.Background(), db, SetupPostgresStoreV0))
return objC, NewStorage(objC, kv.NewMemCache(10), db, tr, opts...)
}
// FullRef creates a data reference for the full chunk referenced by a data reference.
func FullRef(dataRef *DataRef) *DataRef {
chunkDataRef := &DataRef{}
chunkDataRef.Ref = dataRef.Ref
chunkDataRef.SizeBytes = dataRef.Ref.SizeBytes
return chunkDataRef
}
func NewDataRef(chunkRef *DataRef, chunkBytes []byte, offset, size int64) *DataRef {
dataRef := &DataRef{}
dataRef.Ref = chunkRef.Ref
if chunkRef.SizeBytes == size {
dataRef.Hash = chunkRef.Hash
} else {
dataRef.Hash = Hash(chunkBytes[offset : offset+size])
}
dataRef.OffsetBytes = offset
dataRef.SizeBytes = size
return dataRef
}