File: storeobject.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (123 lines) | stat: -rw-r--r-- 3,408 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package api

import (
	"errors"
	"fmt"
	"strings"

	"github.com/docker/go-events"
)

var (
	errUnknownStoreAction = errors.New("unrecognized action type")
	errConflictingFilters = errors.New("conflicting filters specified")
	errNoKindSpecified    = errors.New("no kind of object specified")
	errUnrecognizedAction = errors.New("unrecognized action")
)

// StoreObject is an abstract object that can be handled by the store.
type StoreObject interface {
	GetID() string                           // Get ID
	GetMeta() Meta                           // Retrieve metadata
	SetMeta(Meta)                            // Set metadata
	CopyStoreObject() StoreObject            // Return a copy of this object
	EventCreate() Event                      // Return a creation event
	EventUpdate(oldObject StoreObject) Event // Return an update event
	EventDelete() Event                      // Return a deletion event
}

// Event is the type used for events passed over watcher channels, and also
// the type used to specify filtering in calls to Watch.
type Event interface {
	// TODO(stevvooe): Consider whether it makes sense to squish both the
	// matcher type and the primary type into the same type. It might be better
	// to build a matcher from an event prototype.

	// Matches checks if this item in a watch queue Matches the event
	// description.
	Matches(events.Event) bool
}

// EventCreate is an interface implemented by every creation event type
type EventCreate interface {
	IsEventCreate() bool
}

// EventUpdate is an interface implemented by every update event type
type EventUpdate interface {
	IsEventUpdate() bool
}

// EventDelete is an interface implemented by every delete event type
type EventDelete interface {
	IsEventDelete()
}

func customIndexer(kind string, annotations *Annotations) (bool, [][]byte, error) {
	var converted [][]byte

	for _, entry := range annotations.Indices {
		index := make([]byte, 0, len(kind)+1+len(entry.Key)+1+len(entry.Val)+1)
		if kind != "" {
			index = append(index, []byte(kind)...)
			index = append(index, '|')
		}
		index = append(index, []byte(entry.Key)...)
		index = append(index, '|')
		index = append(index, []byte(entry.Val)...)
		index = append(index, '\x00')
		converted = append(converted, index)
	}

	// Add the null character as a terminator
	return len(converted) != 0, converted, nil
}

func fromArgs(args ...interface{}) ([]byte, error) {
	if len(args) != 1 {
		return nil, fmt.Errorf("must provide only a single argument")
	}
	arg, ok := args[0].(string)
	if !ok {
		return nil, fmt.Errorf("argument must be a string: %#v", args[0])
	}
	// Add the null character as a terminator
	arg += "\x00"
	return []byte(arg), nil
}

func prefixFromArgs(args ...interface{}) ([]byte, error) {
	val, err := fromArgs(args...)
	if err != nil {
		return nil, err
	}

	// Strip the null terminator, the rest is a prefix
	n := len(val)
	if n > 0 {
		return val[:n-1], nil
	}
	return val, nil
}

func checkCustom(a1, a2 Annotations) bool {
	if len(a1.Indices) == 1 {
		for _, ind := range a2.Indices {
			if ind.Key == a1.Indices[0].Key && ind.Val == a1.Indices[0].Val {
				return true
			}
		}
	}
	return false
}

func checkCustomPrefix(a1, a2 Annotations) bool {
	if len(a1.Indices) == 1 {
		for _, ind := range a2.Indices {
			if ind.Key == a1.Indices[0].Key && strings.HasPrefix(ind.Val, a1.Indices[0].Val) {
				return true
			}
		}
	}
	return false
}