forked from cloudfoundry-attic/garden-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_memory_container_repository.go
74 lines (59 loc) · 1.68 KB
/
in_memory_container_repository.go
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
package container_repository
import (
"sync"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/garden-linux/linux_backend"
"code.cloudfoundry.org/lager"
)
type InMemoryContainerRepository struct {
store map[string]linux_backend.Container
mutex *sync.RWMutex
}
func New() *InMemoryContainerRepository {
return &InMemoryContainerRepository{
store: map[string]linux_backend.Container{},
mutex: &sync.RWMutex{},
}
}
func (cr *InMemoryContainerRepository) All() []linux_backend.Container {
return cr.Query(func(c linux_backend.Container) bool {
return true
}, nil)
}
func (cr *InMemoryContainerRepository) Add(container linux_backend.Container) {
cr.mutex.Lock()
defer cr.mutex.Unlock()
cr.store[container.Handle()] = container
}
func (cr *InMemoryContainerRepository) FindByHandle(handle string) (linux_backend.Container, error) {
cr.mutex.RLock()
defer cr.mutex.RUnlock()
container, ok := cr.store[handle]
if !ok {
return nil, garden.ContainerNotFoundError{handle}
}
return container, nil
}
func (cr *InMemoryContainerRepository) Delete(container linux_backend.Container) {
cr.mutex.Lock()
defer cr.mutex.Unlock()
delete(cr.store, container.Handle())
}
func (cr *InMemoryContainerRepository) Query(filter func(linux_backend.Container) bool, logger lager.Logger) []linux_backend.Container {
cr.mutex.RLock()
defer cr.mutex.RUnlock()
var matches []linux_backend.Container
for _, c := range cr.store {
if filter(c) {
if logger != nil {
logger.Debug("matched", lager.Data{"handle": c.Handle()})
}
matches = append(matches, c)
} else {
if logger != nil {
logger.Debug("did-not-match", lager.Data{"handle": c.Handle()})
}
}
}
return matches
}