-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropstorage.go
232 lines (196 loc) · 6.55 KB
/
dropstorage.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright 2019 Insolar Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package storage
import (
"context"
"sync"
"github.com/dgraph-io/badger"
"github.com/insolar/insolar/core"
"github.com/insolar/insolar/ledger/storage/jet"
"github.com/pkg/errors"
)
// DropStorage jet-drops
//go:generate minimock -i github.com/insolar/insolar/ledger/storage.DropStorage -o ./ -s _mock.go
type DropStorage interface {
CreateDrop(ctx context.Context, jetID core.RecordID, pulse core.PulseNumber, prevHash []byte) (
*jet.JetDrop,
[][]byte,
uint64,
error,
)
SetDrop(ctx context.Context, jetID core.RecordID, drop *jet.JetDrop) error
GetDrop(ctx context.Context, jetID core.RecordID, pulse core.PulseNumber) (*jet.JetDrop, error)
AddDropSize(ctx context.Context, dropSize *jet.DropSize) error
SetDropSizeHistory(ctx context.Context, jetID core.RecordID, dropSizeHistory jet.DropSizeHistory) error
GetDropSizeHistory(ctx context.Context, jetID core.RecordID) (jet.DropSizeHistory, error)
GetJetSizesHistoryDepth() int
}
type dropStorage struct {
DB DBContext `inject:""`
PlatformCryptographyScheme core.PlatformCryptographyScheme `inject:""`
addBlockSizeLock sync.RWMutex
jetSizesHistoryDepth int
}
func NewDropStorage(jetSizesHistoryDepth int) DropStorage {
return &dropStorage{jetSizesHistoryDepth: jetSizesHistoryDepth}
}
// CreateDrop creates and stores jet drop for given pulse number.
//
// On success returns saved drop object, slot records, drop size.
func (ds *dropStorage) CreateDrop(ctx context.Context, jetID core.RecordID, pulse core.PulseNumber, prevHash []byte) (
*jet.JetDrop,
[][]byte,
uint64,
error,
) {
var err error
ds.DB.waitingFlight()
hw := ds.PlatformCryptographyScheme.ReferenceHasher()
_, err = hw.Write(prevHash)
if err != nil {
return nil, nil, 0, err
}
var messages [][]byte
_, jetPrefix := jet.Jet(jetID)
// messagesPrefix := prefixkey(scopeIDMessage, jetPrefix, pulse.Bytes())
// err = db.db.View(func(txn *badger.Txn) error {
// it := txn.NewIterator(badger.DefaultIteratorOptions)
// defer it.Close()
//
// for it.Seek(messagesPrefix); it.ValidForPrefix(messagesPrefix); it.Next() {
// val, err := it.Item().ValueCopy(nil)
// if err != nil {
// return err
// }
// messages = append(messages, val)
// }
// return nil
// })
// if err != nil {
// return nil, nil, 0, err
// }
var dropSize uint64
recordPrefix := prefixkey(scopeIDRecord, jetPrefix, pulse.Bytes())
err = ds.DB.GetBadgerDB().View(func(txn *badger.Txn) error {
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Seek(recordPrefix); it.ValidForPrefix(recordPrefix); it.Next() {
val, err := it.Item().ValueCopy(nil)
if err != nil {
return err
}
_, err = hw.Write(val)
if err != nil {
return err
}
dropSize += uint64(len(val))
}
return nil
})
if err != nil {
return nil, nil, 0, err
}
drop := jet.JetDrop{
Pulse: pulse,
PrevHash: prevHash,
Hash: hw.Sum(nil),
}
return &drop, messages, dropSize, nil
}
// SetDrop saves provided JetDrop in db.
func (ds *dropStorage) SetDrop(ctx context.Context, jetID core.RecordID, drop *jet.JetDrop) error {
_, prefix := jet.Jet(jetID)
k := prefixkey(scopeIDJetDrop, prefix, drop.Pulse.Bytes())
_, err := ds.DB.get(ctx, k)
if err == nil {
return ErrOverride
}
encoded, err := jet.Encode(drop)
if err != nil {
return err
}
return ds.DB.set(ctx, k, encoded)
}
// GetDrop returns jet drop for a given pulse number and jet id.
func (ds *dropStorage) GetDrop(ctx context.Context, jetID core.RecordID, pulse core.PulseNumber) (*jet.JetDrop, error) {
_, prefix := jet.Jet(jetID)
k := prefixkey(scopeIDJetDrop, prefix, pulse.Bytes())
// buf, err := db.get(ctx, k)
buf, err := ds.DB.get(ctx, k)
if err != nil {
return nil, err
}
drop, err := jet.Decode(buf)
if err != nil {
return nil, err
}
return drop, nil
}
// AddDropSize adds Jet drop size stats (required for split decision).
func (ds *dropStorage) AddDropSize(ctx context.Context, dropSize *jet.DropSize) error {
ds.addBlockSizeLock.Lock()
defer ds.addBlockSizeLock.Unlock()
k := dropSizesPrefixKey(dropSize.JetID)
buff, err := ds.DB.get(ctx, k)
if err != nil && err != ErrNotFound {
return errors.Wrapf(err, "[ AddDropSize ] Can't get object: %s", string(k))
}
var dropSizes = jet.DropSizeHistory{}
if err != ErrNotFound {
dropSizes, err = jet.DeserializeJetDropSizeHistory(ctx, buff)
if err != nil {
return errors.Wrapf(err, "[ AddDropSize ] Can't decode dropSizes")
}
if len([]jet.DropSize(dropSizes)) >= ds.jetSizesHistoryDepth {
dropSizes = dropSizes[1:]
}
}
dropSizes = append(dropSizes, *dropSize)
return ds.DB.set(ctx, k, dropSizes.Bytes())
}
// SetDropSizeHistory saves drop sizes history.
func (ds *dropStorage) SetDropSizeHistory(ctx context.Context, jetID core.RecordID, dropSizeHistory jet.DropSizeHistory) error {
ds.addBlockSizeLock.Lock()
defer ds.addBlockSizeLock.Unlock()
k := dropSizesPrefixKey(jetID)
err := ds.DB.set(ctx, k, dropSizeHistory.Bytes())
return errors.Wrap(err, "[ ResetDropSizeHistory ] Can't db.set")
}
// GetDropSizeHistory returns last drops sizes.
func (ds *dropStorage) GetDropSizeHistory(ctx context.Context, jetID core.RecordID) (jet.DropSizeHistory, error) {
ds.addBlockSizeLock.RLock()
defer ds.addBlockSizeLock.RUnlock()
k := dropSizesPrefixKey(jetID)
buff, err := ds.DB.get(ctx, k)
if err != nil && err != ErrNotFound {
return nil, errors.Wrap(err, "[ GetDropSizeHistory ] Can't db.set")
}
if err == ErrNotFound {
return jet.DropSizeHistory{}, nil
}
dropSizes, err := jet.DeserializeJetDropSizeHistory(ctx, buff)
if err != nil {
return nil, errors.Wrapf(err, "[ GetDropSizeHistory ] Can't decode dropSizes")
}
return dropSizes, nil
}
// GetJetSizesHistoryDepth returns max amount of drop sizes
func (ds *dropStorage) GetJetSizesHistoryDepth() int {
return ds.jetSizesHistoryDepth
}
func dropSizesPrefixKey(jetID core.RecordID) []byte {
return prefixkey(scopeIDSystem, []byte{sysDropSizeHistory}, jetID.Bytes())
}