2016-01-15 23:55:46 +00:00
|
|
|
package container
|
|
|
|
|
2016-06-07 07:45:21 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2016-01-15 23:55:46 +00:00
|
|
|
|
|
|
|
// memoryStore implements a Store in memory.
|
|
|
|
type memoryStore struct {
|
|
|
|
s map[string]*Container
|
2016-03-18 18:50:19 +00:00
|
|
|
sync.RWMutex
|
2016-01-15 23:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-06-07 07:45:21 +00:00
|
|
|
var res *Container
|
2016-03-18 18:50:19 +00:00
|
|
|
c.RLock()
|
2016-06-07 07:45:21 +00:00
|
|
|
res = c.s[id]
|
2016-03-18 18:50:19 +00:00
|
|
|
c.RUnlock()
|
2016-01-15 23:55:46 +00:00
|
|
|
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 {
|
2016-05-23 18:45:04 +00:00
|
|
|
containers := History(c.all())
|
2016-01-15 23:55:46 +00:00
|
|
|
containers.sort()
|
2016-05-23 18:45:04 +00:00
|
|
|
return containers
|
2016-01-15 23:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the number of containers in the store.
|
|
|
|
func (c *memoryStore) Size() int {
|
2016-03-18 18:50:19 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2016-01-15 23:55:46 +00:00
|
|
|
return len(c.s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First returns the first container found in the store by a given filter.
|
|
|
|
func (c *memoryStore) First(filter StoreFilter) *Container {
|
2016-05-23 18:45:04 +00:00
|
|
|
for _, cont := range c.all() {
|
2016-01-15 23:55:46 +00:00
|
|
|
if filter(cont) {
|
|
|
|
return cont
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyAll calls the reducer function with every container in the store.
|
2016-10-29 07:03:26 +00:00
|
|
|
// This operation is asynchronous in the memory store.
|
2016-03-18 18:50:19 +00:00
|
|
|
// NOTE: Modifications to the store MUST NOT be done by the StoreReducer.
|
2016-01-15 23:55:46 +00:00
|
|
|
func (c *memoryStore) ApplyAll(apply StoreReducer) {
|
|
|
|
wg := new(sync.WaitGroup)
|
2016-05-23 18:45:04 +00:00
|
|
|
for _, cont := range c.all() {
|
2016-01-15 23:55:46 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(container *Container) {
|
|
|
|
apply(container)
|
|
|
|
wg.Done()
|
|
|
|
}(cont)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-05-23 18:45:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-15 23:55:46 +00:00
|
|
|
var _ Store = &memoryStore{}
|