/
objectid.go
194 lines (149 loc) · 3.98 KB
/
objectid.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
package object
import (
"encoding/json"
"strings"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo/content"
"github.com/kopia/kopia/repo/content/index"
)
// ID is an identifier of a repository object. Repository objects can be stored.
//
// 1. In a single content block, this is the most common case for small objects.
// 2. In a series of content blocks with an indirect block pointing at them (multiple indirections are allowed).
// This is used for larger files. Object IDs using indirect blocks start with "I"
type ID struct {
cid content.ID
indirection byte
compression bool
}
// MarshalJSON implements JSON serialization of IDs.
func (i ID) MarshalJSON() ([]byte, error) {
s := i.String()
//nolint:wrapcheck
return json.Marshal(s)
}
// UnmarshalJSON implements JSON deserialization of IDs.
func (i *ID) UnmarshalJSON(v []byte) error {
var s string
if err := json.Unmarshal(v, &s); err != nil {
return errors.Wrap(err, "unable to unmarshal object ID")
}
tmp, err := ParseID(s)
if err != nil {
return errors.Wrap(err, "invalid object ID")
}
*i = tmp
return nil
}
// EmptyID is an empty object ID equivalent to an empty string.
//
//nolint:gochecknoglobals
var EmptyID = ID{}
// HasObjectID exposes the identifier of an object.
type HasObjectID interface {
ObjectID() ID
}
// String returns string representation of ObjectID that is suitable for displaying in the UI.
func (i ID) String() string {
var (
indirectPrefix string
compressionPrefix string
)
switch i.indirection {
case 0:
// no prefix
case 1:
indirectPrefix = "I"
default:
indirectPrefix = strings.Repeat("I", int(i.indirection))
}
if i.compression {
compressionPrefix = "Z"
}
return indirectPrefix + compressionPrefix + i.cid.String()
}
// Append appends string representation of ObjectID that is suitable for displaying in the UI.
func (i ID) Append(out []byte) []byte {
for j := 0; j < int(i.indirection); j++ {
out = append(out, 'I')
}
if i.compression {
out = append(out, 'Z')
}
return i.cid.Append(out)
}
// IndexObjectID returns the object ID of the underlying index object.
func (i ID) IndexObjectID() (ID, bool) {
if i.indirection > 0 {
i2 := i
i2.indirection--
return i2, true
}
return i, false
}
// ContentID returns the ID of the underlying content.
func (i ID) ContentID() (id content.ID, compressed, ok bool) {
if i.indirection > 0 {
return content.EmptyID, false, false
}
return i.cid, i.compression, true
}
// IDsFromStrings converts strings to IDs.
func IDsFromStrings(str []string) ([]ID, error) {
var result []ID
for _, v := range str {
id, err := ParseID(v)
if err != nil {
return nil, err
}
result = append(result, id)
}
return result, nil
}
// IDsToStrings converts the IDs to strings.
func IDsToStrings(input []ID) []string {
var result []string
for _, v := range input {
result = append(result, v.String())
}
return result
}
// DirectObjectID returns direct object ID based on the provided block ID.
func DirectObjectID(contentID content.ID) ID {
return ID{cid: contentID}
}
// Compressed returns object ID with 'Z' prefix indicating it's compressed.
func Compressed(objectID ID) ID {
objectID.compression = true
return objectID
}
// IndirectObjectID returns indirect object ID based on the underlying index object ID.
func IndirectObjectID(indexObjectID ID) ID {
indexObjectID.indirection++
return indexObjectID
}
// ParseID converts the specified string into object ID.
func ParseID(s string) (ID, error) {
var id ID
for len(s) > 0 && s[0] == 'I' {
id.indirection++
s = s[1:]
}
if len(s) > 0 && s[0] == 'Z' {
id.compression = true
s = s[1:]
}
if len(s) > 0 && s[0] == 'D' {
// no-op, legacy case
s = s[1:]
}
if id.indirection > 0 && id.compression {
return id, errors.Errorf("malformed object ID - compression and indirection are mutually exclusive")
}
cid, err := index.ParseID(s)
if err != nil {
return id, errors.Wrapf(err, "malformed content ID: %q", s)
}
id.cid = cid
return id, nil
}