-
Notifications
You must be signed in to change notification settings - Fork 659
/
state.go
72 lines (57 loc) · 1.76 KB
/
state.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package queue
import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
type state struct{ jobs *Jobs }
func (s *state) SetInt(db database.Database, key []byte, size uint32) error {
p := wrappers.Packer{Bytes: make([]byte, wrappers.IntLen)}
p.PackInt(size)
return db.Put(key, p.Bytes)
}
func (s *state) Int(db database.Database, key []byte) (uint32, error) {
value, err := db.Get(key)
if err != nil {
return 0, err
}
p := wrappers.Packer{Bytes: value}
return p.UnpackInt(), p.Err
}
func (s *state) SetJob(db database.Database, key []byte, job Job) error {
return db.Put(key, job.Bytes())
}
func (s *state) Job(db database.Database, key []byte) (Job, error) {
value, err := db.Get(key)
if err != nil {
return nil, err
}
return s.jobs.parser.Parse(value)
}
// IDs returns a slice of IDs from storage
func (s *state) IDs(db database.Database, prefix []byte) ([]ids.ID, error) {
idSlice := []ids.ID(nil)
iter := prefixdb.NewNested(prefix, db).NewIterator()
defer iter.Release()
for iter.Next() {
keyID, err := ids.ToID(iter.Key())
if err != nil {
return nil, err
}
idSlice = append(idSlice, keyID)
}
return idSlice, nil
}
// AddID saves an ID to the prefixed database
func (s *state) AddID(db database.Database, prefix []byte, key ids.ID) error {
pdb := prefixdb.NewNested(prefix, db)
return pdb.Put(key[:], nil)
}
// RemoveID removes an ID from the prefixed database
func (s *state) RemoveID(db database.Database, prefix []byte, key ids.ID) error {
pdb := prefixdb.NewNested(prefix, db)
return pdb.Delete(key[:])
}