本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.1KB

  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. import "fmt"
  12. // Hasher interface is implemented by types that can tell you
  13. // their hash.
  14. type Hasher interface {
  15. Hash() []byte
  16. }
  17. // Equal functions take two hashers and return if they are equal.
  18. //
  19. // These functions are expected to be faster than reflect.Equal or
  20. // reflect.DeepEqual because they can compare just the hash of the
  21. // objects, instead of their contents, so they are expected to be O(1).
  22. type Equal func(a, b Hasher) bool
  23. // The Noder interface is implemented by the elements of a Merkle Trie.
  24. //
  25. // There are two types of elements in a Merkle Trie:
  26. //
  27. // - file-like nodes: they cannot have children.
  28. //
  29. // - directory-like nodes: they can have 0 or more children and their
  30. // hash is calculated by combining their children hashes.
  31. type Noder interface {
  32. Hasher
  33. fmt.Stringer // for testing purposes
  34. // Name returns the name of an element (relative, not its full
  35. // path).
  36. Name() string
  37. // IsDir returns true if the element is a directory-like node or
  38. // false if it is a file-like node.
  39. IsDir() bool
  40. // Children returns the children of the element. Note that empty
  41. // directory-like noders and file-like noders will both return
  42. // NoChildren.
  43. Children() ([]Noder, error)
  44. // NumChildren returns the number of children this element has.
  45. //
  46. // This method is an optimization: the number of children is easily
  47. // calculated as the length of the value returned by the Children
  48. // method (above); yet, some implementations will be able to
  49. // implement NumChildren in O(1) while Children is usually more
  50. // complex.
  51. NumChildren() (int, error)
  52. }
  53. // NoChildren represents the children of a noder without children.
  54. var NoChildren = []Noder{}
上海开阖软件有限公司 沪ICP备12045867号-1