-
Notifications
You must be signed in to change notification settings - Fork 2
/
batch.go
44 lines (36 loc) · 1.02 KB
/
batch.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
// Copyright (C) 2019-2023, Lux Partners Limited. All rights reserved.
// See the file LICENSE for licensing terms.
package archivedb
import "github.com/luxdefi/node/database"
var _ database.Batch = (*batch)(nil)
// batch is how a user performs modifications to the database.
//
// It consumes puts and deletes at a specified height. When committing, an
// atomic operation is created which registers the modifications at the
// specified height and updates the last tracked height to be equal to this
// batch's height.
type batch struct {
db *Database
height uint64
database.BatchOps
}
func (c *batch) Write() error {
batch := c.db.db.NewBatch()
for _, op := range c.Ops {
key, _ := newDBKeyFromUser(op.Key, c.height)
var value []byte
if !op.Delete {
value = newDBValue(op.Value)
}
if err := batch.Put(key, value); err != nil {
return err
}
}
if err := database.PutUInt64(batch, heightKey, c.height); err != nil {
return err
}
return batch.Write()
}
func (c *batch) Inner() database.Batch {
return c
}