-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstore.go
89 lines (78 loc) · 2.05 KB
/
store.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
package main
import (
"context"
"database/sql"
"encoding/base32"
"encoding/binary"
"strings"
"time"
"github.com/zeebo/errs/v2"
)
var enc = base32.NewEncoding("0123456789abcdefghjkmnpqrstvwxyz")
func pad(x string) string { return x + "========"[:(8-len(x)%8)%8] }
func unpad(x string) string { return strings.TrimRight(x, "=") }
func idToName(id uint64) string {
var buf [binary.MaxVarintLen64]byte
return unpad(enc.EncodeToString(buf[:binary.PutUvarint(buf[:], id)]))
}
type record struct {
name string
size int
created time.Time
}
type dbStore struct {
db *sql.DB
}
func (d *dbStore) Init(ctx context.Context) error {
_, err := d.db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS profiles (
id integer primary key,
data,
created integer
)`)
return errs.Wrap(err)
}
func (d *dbStore) Save(ctx context.Context, data []byte) (string, error) {
var id uint64
err := d.db.QueryRowContext(ctx,
"INSERT INTO profiles(data, created) VALUES(?, ?) RETURNING id",
data, time.Now().Unix(),
).Scan(&id)
return idToName(id), errs.Wrap(err)
}
func (d *dbStore) Load(ctx context.Context, name string) (data []byte, err error) {
buf, err := enc.DecodeString(pad(name))
if err != nil {
return nil, errs.Wrap(err)
}
id, n := binary.Uvarint(buf)
if n <= 0 {
return nil, errs.Errorf("invalid name: %q", name)
}
err = d.db.QueryRowContext(ctx,
"SELECT data FROM profiles WHERE id = ?",
id,
).Scan(&data)
return data, errs.Wrap(err)
}
func (d *dbStore) Recent(ctx context.Context, n int) (recs []record, err error) {
rows, err := d.db.QueryContext(ctx,
"SELECT id, length(data), created FROM profiles ORDER BY created DESC LIMIT ?",
n,
)
if err != nil {
return nil, errs.Wrap(err)
}
defer rows.Close()
for rows.Next() {
var id, size, created uint64
if err := rows.Scan(&id, &size, &created); err != nil {
return nil, errs.Wrap(err)
}
recs = append(recs, record{
name: idToName(id),
size: int(size),
created: time.Unix(int64(created), 0),
})
}
return recs, errs.Combine(errs.Wrap(rows.Err()), errs.Wrap(rows.Close()))
}