storeobject.go 2.2 KB

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