4f0d95fa |
package container // import "github.com/docker/docker/container" |
3c82fad4 |
|
934328d8 |
import (
"sync"
) |
3c82fad4 |
// memoryStore implements a Store in memory.
type memoryStore struct {
s map[string]*Container |
9c4570a9 |
sync.RWMutex |
3c82fad4 |
}
// NewMemoryStore initializes a new memory store.
func NewMemoryStore() Store {
return &memoryStore{
s: make(map[string]*Container),
}
}
// Add appends a new container to the memory store.
// It overrides the id if it existed before.
func (c *memoryStore) Add(id string, cont *Container) {
c.Lock()
c.s[id] = cont
c.Unlock()
}
// Get returns a container from the store by id.
func (c *memoryStore) Get(id string) *Container { |
934328d8 |
var res *Container |
9c4570a9 |
c.RLock() |
934328d8 |
res = c.s[id] |
9c4570a9 |
c.RUnlock() |
3c82fad4 |
return res
}
// Delete removes a container from the store by id.
func (c *memoryStore) Delete(id string) {
c.Lock()
delete(c.s, id)
c.Unlock()
}
// List returns a sorted list of containers from the store.
// The containers are ordered by creation date.
func (c *memoryStore) List() []*Container { |
bd2b3d36 |
containers := History(c.all()) |
3c82fad4 |
containers.sort() |
bd2b3d36 |
return containers |
3c82fad4 |
}
// Size returns the number of containers in the store.
func (c *memoryStore) Size() int { |
9c4570a9 |
c.RLock()
defer c.RUnlock() |
3c82fad4 |
return len(c.s)
}
// First returns the first container found in the store by a given filter.
func (c *memoryStore) First(filter StoreFilter) *Container { |
bd2b3d36 |
for _, cont := range c.all() { |
3c82fad4 |
if filter(cont) {
return cont
}
}
return nil
}
// ApplyAll calls the reducer function with every container in the store. |
e6866492 |
// This operation is asynchronous in the memory store. |
9c4570a9 |
// NOTE: Modifications to the store MUST NOT be done by the StoreReducer. |
3c82fad4 |
func (c *memoryStore) ApplyAll(apply StoreReducer) {
wg := new(sync.WaitGroup) |
bd2b3d36 |
for _, cont := range c.all() { |
3c82fad4 |
wg.Add(1)
go func(container *Container) {
apply(container)
wg.Done()
}(cont)
}
wg.Wait()
}
|
bd2b3d36 |
func (c *memoryStore) all() []*Container {
c.RLock()
containers := make([]*Container, 0, len(c.s))
for _, cont := range c.s {
containers = append(containers, cont)
}
c.RUnlock()
return containers
}
|
3c82fad4 |
var _ Store = &memoryStore{} |