operation.go

 1package dag
 2
 3import (
 4	"github.com/MichaelMure/git-bug/entity"
 5	"github.com/MichaelMure/git-bug/identity"
 6	"github.com/MichaelMure/git-bug/repository"
 7)
 8
 9// Operation is a piece of data defining a change to reflect on the state of an Entity.
10// What this Operation or Entity's state looks like is not of the resort of this package as it only deals with the
11// data structure and storage.
12type Operation interface {
13	// Id return the Operation identifier
14	//
15	// Some care need to be taken to define a correct Id derivation and enough entropy in the data used to avoid
16	// collisions. Notably:
17	// - the Id of the first Operation will be used as the Id of the Entity. Collision need to be avoided across entities
18	//   of the same type (example: no collision within the "bug" namespace).
19	// - collisions can also happen within the set of Operations of an Entity. Simple Operation might not have enough
20	//   entropy to yield unique Ids (example: two "close" operation within the same second, same author).
21	//   If this is a concern, it is recommended to include a piece of random data in the operation's data, to guarantee
22	//   a minimal amount of entropy and avoid collision.
23	//
24	//   Author's note: I tried to find a clever way around that inelegance (stuffing random useless data into the stored
25	//   structure is not exactly elegant) but I failed to find a proper way. Essentially, anything that would reuse some
26	//   other data (parent operation's Id, lamport clock) or the graph structure (depth) impose that the Id would only
27	//   make sense in the context of the graph and yield some deep coupling between Entity and Operation. This in turn
28	//   make the whole thing even less elegant.
29	//
30	// A common way to derive an Id will be to use the entity.DeriveId() function on the serialized operation data.
31	Id() entity.Id
32	// Validate check if the Operation data is valid
33	Validate() error
34	// Author returns the author of this operation
35	Author() identity.Interface
36}
37
38// OperationWithFiles is an extended Operation that has files dependency, stored in git.
39type OperationWithFiles interface {
40	Operation
41
42	// GetFiles return the files needed by this operation
43	// This implies that the Operation maintain and store internally the references to those files. This is how
44	// this information is read later, when loading from storage.
45	// For example, an operation that has a text value referencing some files would maintain a mapping (text ref -->
46	// hash).
47	GetFiles() []repository.Hash
48}