| 1 |
// Package noder provide an interface for defining nodes in a |
| 2 |
// merkletrie, their hashes and their paths (a noders and its |
| 3 |
// ancestors). |
| 4 |
// |
| 5 |
// The hasher interface is easy to implement naively by elements that |
| 6 |
// already have a hash, like git blobs and trees. More sophisticated |
| 7 |
// implementations can implement the Equal function in exotic ways |
| 8 |
// though: for instance, comparing the modification time of directories |
| 9 |
// in a filesystem. |
| 10 |
package noder |
| 11 |
|
| 12 |
import "fmt" |
| 13 |
|
| 14 |
// Hasher interface is implemented by types that can tell you |
| 15 |
// their hash. |
| 16 |
type Hasher interface { |
| 17 |
Hash() []byte |
| 18 |
} |
| 19 |
|
| 20 |
// Equal functions take two hashers and return if they are equal. |
| 21 |
// |
| 22 |
// These functions are expected to be faster than reflect.Equal or |
| 23 |
// reflect.DeepEqual because they can compare just the hash of the |
| 24 |
// objects, instead of their contents, so they are expected to be O(1). |
| 25 |
type Equal func(a, b Hasher) bool |
| 26 |
|
| 27 |
// The Noder interface is implemented by the elements of a Merkle Trie. |
| 28 |
// |
| 29 |
// There are two types of elements in a Merkle Trie: |
| 30 |
// |
| 31 |
// - file-like nodes: they cannot have children. |
| 32 |
// |
| 33 |
// - directory-like nodes: they can have 0 or more children and their |
| 34 |
// hash is calculated by combining their children hashes. |
| 35 |
type Noder interface { |
| 36 |
Hasher |
| 37 |
fmt.Stringer // for testing purposes |
| 38 |
// Name returns the name of an element (relative, not its full |
| 39 |
// path). |
| 40 |
Name() string |
| 41 |
// IsDir returns true if the element is a directory-like node or |
| 42 |
// false if it is a file-like node. |
| 43 |
IsDir() bool |
| 44 |
// Children returns the children of the element. Note that empty |
| 45 |
// directory-like noders and file-like noders will both return |
| 46 |
// NoChildren. |
| 47 |
Children() ([]Noder, error) |
| 48 |
// NumChildren returns the number of children this element has. |
| 49 |
// |
| 50 |
// This method is an optimization: the number of children is easily |
| 51 |
// calculated as the length of the value returned by the Children |
| 52 |
// method (above); yet, some implementations will be able to |
| 53 |
// implement NumChildren in O(1) while Children is usually more |
| 54 |
// complex. |
| 55 |
NumChildren() (int, error) |
| 56 |
Skip() bool |
| 57 |
} |
| 58 |
|
| 59 |
// NoChildren represents the children of a noder without children. |
| 60 |
var NoChildren = []Noder{} |