1package dag
2
3import (
4 "crypto/rand"
5 "encoding/json"
6 "fmt"
7 "time"
8
9 "github.com/pkg/errors"
10
11 "github.com/MichaelMure/git-bug/entities/identity"
12 "github.com/MichaelMure/git-bug/entity"
13 "github.com/MichaelMure/git-bug/repository"
14)
15
16// OperationType is an operation type identifier
17type OperationType int
18
19// Operation is a piece of data defining a change to reflect on the state of an Entity.
20// What this Operation or Entity's state looks like is not of the resort of this package as it only deals with the
21// data structure and storage.
22type Operation interface {
23 // Id return the Operation identifier
24 //
25 // Some care need to be taken to define a correct Id derivation and enough entropy in the data used to avoid
26 // collisions. Notably:
27 // - the Id of the first Operation will be used as the Id of the Entity. Collision need to be avoided across entities
28 // of the same type (example: no collision within the "bug" namespace).
29 // - collisions can also happen within the set of Operations of an Entity. Simple Operation might not have enough
30 // entropy to yield unique Ids (example: two "close" operation within the same second, same author).
31 // If this is a concern, it is recommended to include a piece of random data in the operation's data, to guarantee
32 // a minimal amount of entropy and avoid collision.
33 //
34 // Author's note: I tried to find a clever way around that inelegance (stuffing random useless data into the stored
35 // structure is not exactly elegant), but I failed to find a proper way. Essentially, anything that would reuse some
36 // other data (parent operation's Id, lamport clock) or the graph structure (depth) impose that the Id would only
37 // make sense in the context of the graph and yield some deep coupling between Entity and Operation. This in turn
38 // make the whole thing even less elegant.
39 //
40 // A common way to derive an Id will be to use the entity.DeriveId() function on the serialized operation data.
41 Id() entity.Id
42 // Type return the type of the operation
43 Type() OperationType
44 // Validate check if the Operation data is valid
45 Validate() error
46 // Author returns the author of this operation
47 Author() identity.Interface
48 // Time return the time when the operation was added
49 Time() time.Time
50
51 // SetMetadata store arbitrary metadata about the operation
52 SetMetadata(key string, value string)
53 // GetMetadata retrieve arbitrary metadata about the operation
54 GetMetadata(key string) (string, bool)
55 // AllMetadata return all metadata for this operation
56 AllMetadata() map[string]string
57
58 // setId allow to set the Id, used when unmarshalling only
59 setId(id entity.Id)
60 // setAuthor allow to set the author, used when unmarshalling only
61 setAuthor(author identity.Interface)
62 // setExtraMetadataImmutable add a metadata not carried by the operation itself on the operation
63 setExtraMetadataImmutable(key string, value string)
64}
65
66type OperationWithApply[SnapT Snapshot] interface {
67 Operation
68
69 // Apply the operation to a Snapshot to create the final state
70 Apply(snapshot SnapT)
71}
72
73// OperationWithFiles is an optional extension for an Operation that has files dependency, stored in git.
74type OperationWithFiles interface {
75 // GetFiles return the files needed by this operation
76 // This implies that the Operation maintain and store internally the references to those files. This is how
77 // this information is read later, when loading from storage.
78 // For example, an operation that has a text value referencing some files would maintain a mapping (text ref -->
79 // hash).
80 GetFiles() []repository.Hash
81}
82
83// OperationDoesntChangeSnapshot is an interface signaling that the Operation implementing it doesn't change the
84// snapshot, for example a metadata operation that act on other operations.
85type OperationDoesntChangeSnapshot interface {
86 DoesntChangeSnapshot()
87}
88
89// Snapshot is the minimal interface that a snapshot need to implement
90type Snapshot interface {
91 // AllOperations returns all the operations that have been applied to that snapshot, in order
92 AllOperations() []Operation
93}
94
95// OpBase implement the common feature that every Operation should support.
96type OpBase struct {
97 // Not serialized. Store the op's id in memory.
98 id entity.Id
99 // Not serialized
100 author identity.Interface
101
102 OperationType OperationType `json:"type"`
103 UnixTime int64 `json:"timestamp"`
104
105 // mandatory random bytes to ensure a better randomness of the data used to later generate the ID
106 // len(Nonce) should be > 20 and < 64 bytes
107 // It has no functional purpose and should be ignored.
108 Nonce []byte `json:"nonce"`
109
110 Metadata map[string]string `json:"metadata,omitempty"`
111 // Not serialized. Store the extra metadata in memory,
112 // compiled from SetMetadataOperation.
113 extraMetadata map[string]string
114}
115
116func NewOpBase(opType OperationType, author identity.Interface, unixTime int64) OpBase {
117 return OpBase{
118 OperationType: opType,
119 author: author,
120 UnixTime: unixTime,
121 Nonce: makeNonce(20),
122 id: entity.UnsetId,
123 }
124}
125
126func makeNonce(len int) []byte {
127 result := make([]byte, len)
128 _, err := rand.Read(result)
129 if err != nil {
130 panic(err)
131 }
132 return result
133}
134
135func IdOperation(op Operation, base *OpBase) entity.Id {
136 if base.id == "" {
137 // something went really wrong
138 panic("op's id not set")
139 }
140 if base.id == entity.UnsetId {
141 // This means we are trying to get the op's Id *before* it has been stored, for instance when
142 // adding multiple ops in one go in an OperationPack.
143 // As the Id is computed based on the actual bytes written on the disk, we are going to predict
144 // those and then get the Id. This is safe as it will be the exact same code writing on disk later.
145
146 data, err := json.Marshal(op)
147 if err != nil {
148 panic(err)
149 }
150
151 base.id = entity.DeriveId(data)
152 }
153 return base.id
154}
155
156func (base *OpBase) Type() OperationType {
157 return base.OperationType
158}
159
160// Time return the time when the operation was added
161func (base *OpBase) Time() time.Time {
162 return time.Unix(base.UnixTime, 0)
163}
164
165// Validate check the OpBase for errors
166func (base *OpBase) Validate(op Operation, opType OperationType) error {
167 if base.OperationType == 0 {
168 return fmt.Errorf("operation type unset")
169 }
170 if base.OperationType != opType {
171 return fmt.Errorf("incorrect operation type (expected: %v, actual: %v)", opType, base.OperationType)
172 }
173
174 if op.Time().Unix() == 0 {
175 return fmt.Errorf("time not set")
176 }
177
178 if base.author == nil {
179 return fmt.Errorf("author not set")
180 }
181
182 if err := op.Author().Validate(); err != nil {
183 return errors.Wrap(err, "author")
184 }
185
186 if op, ok := op.(OperationWithFiles); ok {
187 for _, hash := range op.GetFiles() {
188 if !hash.IsValid() {
189 return fmt.Errorf("file with invalid hash %v", hash)
190 }
191 }
192 }
193
194 if len(base.Nonce) > 64 {
195 return fmt.Errorf("nonce is too big")
196 }
197 if len(base.Nonce) < 20 {
198 return fmt.Errorf("nonce is too small")
199 }
200
201 return nil
202}
203
204// IsAuthored is a sign post method for gqlgen
205func (base *OpBase) IsAuthored() {}
206
207// Author return author identity
208func (base *OpBase) Author() identity.Interface {
209 return base.author
210}
211
212// IdIsSet returns true if the id has been set already
213func (base *OpBase) IdIsSet() bool {
214 return base.id != "" && base.id != entity.UnsetId
215}
216
217// SetMetadata store arbitrary metadata about the operation
218func (base *OpBase) SetMetadata(key string, value string) {
219 if base.IdIsSet() {
220 panic("set metadata on an operation with already an Id")
221 }
222
223 if base.Metadata == nil {
224 base.Metadata = make(map[string]string)
225 }
226 base.Metadata[key] = value
227}
228
229// GetMetadata retrieve arbitrary metadata about the operation
230func (base *OpBase) GetMetadata(key string) (string, bool) {
231 val, ok := base.Metadata[key]
232
233 if ok {
234 return val, true
235 }
236
237 // extraMetadata can't replace the original operations value if any
238 val, ok = base.extraMetadata[key]
239
240 return val, ok
241}
242
243// AllMetadata return all metadata for this operation
244func (base *OpBase) AllMetadata() map[string]string {
245 result := make(map[string]string)
246
247 for key, val := range base.extraMetadata {
248 result[key] = val
249 }
250
251 // Original metadata take precedence
252 for key, val := range base.Metadata {
253 result[key] = val
254 }
255
256 return result
257}
258
259// setId allow to set the Id, used when unmarshalling only
260func (base *OpBase) setId(id entity.Id) {
261 if base.id != "" && base.id != entity.UnsetId {
262 panic("trying to set id again")
263 }
264 base.id = id
265}
266
267// setAuthor allow to set the author, used when unmarshalling only
268func (base *OpBase) setAuthor(author identity.Interface) {
269 base.author = author
270}
271
272func (base *OpBase) setExtraMetadataImmutable(key string, value string) {
273 if base.extraMetadata == nil {
274 base.extraMetadata = make(map[string]string)
275 }
276 if _, exist := base.extraMetadata[key]; !exist {
277 base.extraMetadata[key] = value
278 }
279}