-
Notifications
You must be signed in to change notification settings - Fork 13
/
block_writer.go
51 lines (42 loc) · 1.12 KB
/
block_writer.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
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package filesystem
import (
"github.com/orbs-network/orbs-spec/types/go/protocol"
"github.com/pkg/errors"
"io"
"sync"
)
type writerSyncer interface {
io.Writer
io.Closer
Sync() error
}
type blockWriter struct {
sync.Mutex
ws writerSyncer
codec blockCodec
}
func (bw *blockWriter) Close() error {
return bw.ws.Close()
}
func newBlockWriter(ws writerSyncer, codec blockCodec) *blockWriter {
return &blockWriter{
ws: ws,
codec: codec,
}
}
func (bw *blockWriter) writeBlock(blockPair *protocol.BlockPairContainer) (int, error) {
bytes, err := bw.codec.encode(blockPair, bw.ws)
if err != nil {
return 0, errors.Wrap(err, "failed to write block")
}
err = bw.ws.Sync()
if err != nil {
return 0, errors.Wrap(err, "failed to flush blocks to disk")
}
return bytes, nil
}