-
Notifications
You must be signed in to change notification settings - Fork 567
/
metadata.go
119 lines (99 loc) · 2.86 KB
/
metadata.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
package chunk
import (
"context"
"encoding/hex"
"strings"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/pachhash"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
)
// ID uniquely identifies a chunk. It is the hash of its content
type ID []byte
// Hash produces an ID by hashing data
func Hash(data []byte) ID {
sum := pachhash.Sum(data)
return sum[:]
}
// ParseTrackerID parses a trackerID into a chunk
func ParseTrackerID(trackerID string) (ID, error) {
if !strings.HasPrefix(trackerID, TrackerPrefix) {
return nil, errors.Errorf("tracker ID is not for chunk: %q", trackerID)
}
return IDFromHex(trackerID[len(TrackerPrefix):])
}
// IDFromHex parses a hex string into an ID
func IDFromHex(h string) (ID, error) {
res, err := hex.DecodeString(h)
return res, errors.EnsureStack(err)
}
func (id ID) String() string {
return id.HexString()
}
// HexString hex encodes the ID
func (id ID) HexString() string {
return hex.EncodeToString(id)
}
// TrackerID returns an ID for use with the tracker.
func (id ID) TrackerID() string {
return TrackerPrefix + id.HexString()
}
// Metadata holds metadata about a chunk
type Metadata struct {
Size int
PointsTo []ID
}
// Entry is an chunk object mapping
type Entry struct {
ChunkID ID `db:"chunk_id"`
Gen uint64 `db:"gen"`
Uploaded bool `db:"uploaded"`
Tombstone bool `db:"tombstone"`
}
// SetupPostgresStoreV0 sets up tables in db
// DO NOT MODIFY THIS FUNCTION
// IT HAS BEEN USED IN A RELEASED MIGRATION
func SetupPostgresStoreV0(tx *pachsql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE storage.chunk_objects (
chunk_id BYTEA NOT NULL,
gen BIGSERIAL NOT NULL,
uploaded BOOLEAN NOT NULL DEFAULT FALSE,
tombstone BOOLEAN NOT NULL DEFAULT FALSE,
size INT8 NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(chunk_id, gen)
);
CREATE TABLE storage.keys (
name VARCHAR(128) NOT NULL,
data BYTEA NOT NULL,
PRIMARY KEY(name)
)
`)
return errors.EnsureStack(err)
}
// KeyStore is a store for named secret keys
type KeyStore interface {
Create(ctx context.Context, name string, data []byte) error
Get(ctx context.Context, name string) ([]byte, error)
}
type postgresKeyStore struct {
db *pachsql.DB
}
func NewPostgresKeyStore(db *pachsql.DB) *postgresKeyStore {
return &postgresKeyStore{
db: db,
}
}
func (s *postgresKeyStore) Create(ctx context.Context, name string, data []byte) error {
_, err := s.db.ExecContext(ctx, `
INSERT INTO storage.keys (name, data) VALUES ($1, $2)
`, name, data)
return errors.EnsureStack(err)
}
func (s *postgresKeyStore) Get(ctx context.Context, name string) ([]byte, error) {
var data []byte
if err := s.db.GetContext(ctx, &data, `SELECT data FROM storage.keys WHERE name = $1 LIMIT 1`, name); err != nil {
return nil, errors.EnsureStack(err)
}
return data, nil
}