-
Notifications
You must be signed in to change notification settings - Fork 212
/
activesets.go
85 lines (79 loc) · 2.09 KB
/
activesets.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
package activesets
import (
"fmt"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
)
func Add(db sql.Executor, id types.Hash32, set *types.EpochActiveSet) error {
_, err := db.Exec(`insert into activesets
(id, epoch, active_set)
values (?1, ?2, ?3);`,
func(stmt *sql.Statement) {
stmt.BindBytes(1, id[:])
stmt.BindInt64(2, int64(set.Epoch))
stmt.BindBytes(3, codec.MustEncode(set))
}, nil)
if err != nil {
return fmt.Errorf("add active set %v: %w", id.String(), err)
}
return nil
}
func Get(db sql.Executor, id types.Hash32) (*types.EpochActiveSet, error) {
var (
rst types.EpochActiveSet
err2 error
)
rows, err := db.Exec("select active_set from activesets where id = ?1;",
func(stmt *sql.Statement) {
stmt.BindBytes(1, id.Bytes())
},
func(stmt *sql.Statement) bool {
_, err2 = codec.DecodeFrom(stmt.ColumnReader(0), &rst)
return true
},
)
if rows == 0 {
return nil, fmt.Errorf("active set %v: %w", id.String(), sql.ErrNotFound)
}
if err != nil {
return nil, fmt.Errorf("copy active set %v: %w", id.String(), err)
}
if err2 != nil {
return nil, fmt.Errorf("failed to decode data %v: %w", id.String(), err2)
}
return &rst, nil
}
func GetBlob(db sql.Executor, id []byte) ([]byte, error) {
var rst []byte
rows, err := db.Exec("select active_set from activesets where id = ?1;",
func(stmt *sql.Statement) {
stmt.BindBytes(1, id)
},
func(stmt *sql.Statement) bool {
rd := stmt.ColumnReader(0)
rst = make([]byte, rd.Len())
rd.Read(rst)
return true
},
)
if rows == 0 {
return nil, fmt.Errorf("active set %x: %w", id, sql.ErrNotFound)
}
if err != nil {
return nil, fmt.Errorf("get active set blob %x: %w", id, err)
}
return rst, nil
}
func DeleteBeforeEpoch(db sql.Executor, epoch types.EpochID) error {
_, err := db.Exec("delete from activesets where epoch < ?1;",
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(epoch))
},
nil,
)
if err != nil {
return fmt.Errorf("delete activesets before %v: %w", epoch, err)
}
return nil
}