storeobject.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package api
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/docker/go-events"
  7. )
  8. var (
  9. errUnknownStoreAction = errors.New("unrecognized action type")
  10. errConflictingFilters = errors.New("conflicting filters specified")
  11. errNoKindSpecified = errors.New("no kind of object specified")
  12. errUnrecognizedAction = errors.New("unrecognized action")
  13. )
  14. // StoreObject is an abstract object that can be handled by the store.
  15. type StoreObject interface {
  16. GetID() string // Get ID
  17. GetMeta() Meta // Retrieve metadata
  18. SetMeta(Meta) // Set metadata
  19. CopyStoreObject() StoreObject // Return a copy of this object
  20. EventCreate() Event // Return a creation event
  21. EventUpdate(oldObject StoreObject) Event // Return an update event
  22. EventDelete() Event // Return a deletion event
  23. }
  24. // Event is the type used for events passed over watcher channels, and also
  25. // the type used to specify filtering in calls to Watch.
  26. type Event interface {
  27. // TODO(stevvooe): Consider whether it makes sense to squish both the
  28. // matcher type and the primary type into the same type. It might be better
  29. // to build a matcher from an event prototype.
  30. // Matches checks if this item in a watch queue Matches the event
  31. // description.
  32. Matches(events.Event) bool
  33. }
  34. func customIndexer(kind string, annotations *Annotations) (bool, [][]byte, error) {
  35. var converted [][]byte
  36. for _, entry := range annotations.Indices {
  37. index := make([]byte, 0, len(kind)+1+len(entry.Key)+1+len(entry.Val)+1)
  38. if kind != "" {
  39. index = append(index, []byte(kind)...)
  40. index = append(index, '|')
  41. }
  42. index = append(index, []byte(entry.Key)...)
  43. index = append(index, '|')
  44. index = append(index, []byte(entry.Val)...)
  45. index = append(index, '\x00')
  46. converted = append(converted, index)
  47. }
  48. // Add the null character as a terminator
  49. return len(converted) != 0, converted, nil
  50. }
  51. func fromArgs(args ...interface{}) ([]byte, error) {
  52. if len(args) != 1 {
  53. return nil, fmt.Errorf("must provide only a single argument")
  54. }
  55. arg, ok := args[0].(string)
  56. if !ok {
  57. return nil, fmt.Errorf("argument must be a string: %#v", args[0])
  58. }
  59. // Add the null character as a terminator
  60. arg += "\x00"
  61. return []byte(arg), nil
  62. }
  63. func prefixFromArgs(args ...interface{}) ([]byte, error) {
  64. val, err := fromArgs(args...)
  65. if err != nil {
  66. return nil, err
  67. }
  68. // Strip the null terminator, the rest is a prefix
  69. n := len(val)
  70. if n > 0 {
  71. return val[:n-1], nil
  72. }
  73. return val, nil
  74. }
  75. func checkCustom(a1, a2 Annotations) bool {
  76. if len(a1.Indices) == 1 {
  77. for _, ind := range a2.Indices {
  78. if ind.Key == a1.Indices[0].Key && ind.Val == a1.Indices[0].Val {
  79. return true
  80. }
  81. }
  82. }
  83. return false
  84. }
  85. func checkCustomPrefix(a1, a2 Annotations) bool {
  86. if len(a1.Indices) == 1 {
  87. for _, ind := range a2.Indices {
  88. if ind.Key == a1.Indices[0].Key && strings.HasPrefix(ind.Val, a1.Indices[0].Val) {
  89. return true
  90. }
  91. }
  92. }
  93. return false
  94. }