/
object_writer.go
145 lines (116 loc) · 2.89 KB
/
object_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
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
package object
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"sync"
"github.com/pkg/errors"
)
// Writer allows writing content to the storage and supports automatic deduplication and encryption
// of written data.
type Writer interface {
io.WriteCloser
Result() (ID, error)
}
type blockTracker struct {
mu sync.Mutex
blocks map[string]bool
}
func (t *blockTracker) addBlock(blockID string) {
t.mu.Lock()
defer t.mu.Unlock()
if t.blocks == nil {
t.blocks = make(map[string]bool)
}
t.blocks[blockID] = true
}
func (t *blockTracker) blockIDs() []string {
t.mu.Lock()
defer t.mu.Unlock()
result := make([]string, 0, len(t.blocks))
for k := range t.blocks {
result = append(result, k)
}
return result
}
type objectWriter struct {
ctx context.Context
repo *Manager
prefix string
buffer bytes.Buffer
totalLength int64
currentPosition int64
blockIndex []indirectObjectEntry
description string
splitter Splitter
}
func (w *objectWriter) Close() error {
return nil
}
func (w *objectWriter) Write(data []byte) (n int, err error) {
dataLen := len(data)
w.totalLength += int64(dataLen)
for _, d := range data {
w.buffer.WriteByte(d)
if w.splitter.ShouldSplit(d) {
if err := w.flushBuffer(); err != nil {
return 0, err
}
}
}
return dataLen, nil
}
func (w *objectWriter) flushBuffer() error {
length := w.buffer.Len()
chunkID := len(w.blockIndex)
w.blockIndex = append(w.blockIndex, indirectObjectEntry{})
w.blockIndex[chunkID].Start = w.currentPosition
w.blockIndex[chunkID].Length = int64(length)
w.currentPosition += int64(length)
var b2 bytes.Buffer
w.buffer.WriteTo(&b2) //nolint:errcheck
w.buffer.Reset()
blockID, err := w.repo.blockMgr.WriteBlock(w.ctx, b2.Bytes(), w.prefix)
w.repo.trace("OBJECT_WRITER(%q) stored %v (%v bytes)", w.description, blockID, length)
if err != nil {
return fmt.Errorf("error when flushing chunk %d of %s: %v", chunkID, w.description, err)
}
w.blockIndex[chunkID].Object = DirectObjectID(blockID)
return nil
}
func (w *objectWriter) Result() (ID, error) {
if w.buffer.Len() > 0 || len(w.blockIndex) == 0 {
if err := w.flushBuffer(); err != nil {
return "", err
}
}
if len(w.blockIndex) == 1 {
return w.blockIndex[0].Object, nil
}
iw := &objectWriter{
ctx: w.ctx,
repo: w.repo,
description: "LIST(" + w.description + ")",
splitter: w.repo.newSplitter(),
prefix: w.prefix,
}
ind := indirectObject{
StreamID: "kopia:indirect",
Entries: w.blockIndex,
}
if err := json.NewEncoder(iw).Encode(ind); err != nil {
return "", errors.Wrap(err, "unable to write indirect block index")
}
oid, err := iw.Result()
if err != nil {
return "", err
}
return IndirectObjectID(oid), nil
}
// WriterOptions can be passed to Repository.NewWriter()
type WriterOptions struct {
Description string
Prefix string // empty string or a single-character ('g'..'z')
}