-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libkpod: sandbox: refactor to memory store
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
- Loading branch information
Showing
5 changed files
with
174 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package sandbox | ||
|
||
import "sort" | ||
|
||
// History is a convenience type for storing a list of sandboxes, | ||
// sorted by creation date in descendant order. | ||
type History []*Sandbox | ||
|
||
// Len returns the number of sandboxes in the history. | ||
func (history *History) Len() int { | ||
return len(*history) | ||
} | ||
|
||
// Less compares two sandboxes and returns true if the second one | ||
// was created before the first one. | ||
func (history *History) Less(i, j int) bool { | ||
sandboxes := *history | ||
// FIXME: state access should be serialized | ||
return sandboxes[j].created.Before(sandboxes[i].created) | ||
} | ||
|
||
// Swap switches sandboxes i and j positions in the history. | ||
func (history *History) Swap(i, j int) { | ||
sandboxes := *history | ||
sandboxes[i], sandboxes[j] = sandboxes[j], sandboxes[i] | ||
} | ||
|
||
// sort orders the history by creation date in descendant order. | ||
func (history *History) sort() { | ||
sort.Sort(history) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package sandbox | ||
|
||
import "sync" | ||
|
||
// memoryStore implements a Store in memory. | ||
type memoryStore struct { | ||
s map[string]*Sandbox | ||
sync.RWMutex | ||
} | ||
|
||
// NewMemoryStore initializes a new memory store. | ||
func NewMemoryStore() Storer { | ||
return &memoryStore{ | ||
s: make(map[string]*Sandbox), | ||
} | ||
} | ||
|
||
// Add appends a new sandbox to the memory store. | ||
// It overrides the id if it existed before. | ||
func (c *memoryStore) Add(id string, cont *Sandbox) { | ||
c.Lock() | ||
c.s[id] = cont | ||
c.Unlock() | ||
} | ||
|
||
// Get returns a sandbox from the store by id. | ||
func (c *memoryStore) Get(id string) *Sandbox { | ||
c.RLock() | ||
res := c.s[id] | ||
c.RUnlock() | ||
return res | ||
} | ||
|
||
// Delete removes a sandbox 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 sandboxes from the store. | ||
// The sandboxes are ordered by creation date. | ||
func (c *memoryStore) List() []*Sandbox { | ||
sandboxes := History(c.all()) | ||
sandboxes.sort() | ||
return sandboxes | ||
} | ||
|
||
// Size returns the number of sandboxes in the store. | ||
func (c *memoryStore) Size() int { | ||
c.RLock() | ||
defer c.RUnlock() | ||
return len(c.s) | ||
} | ||
|
||
// First returns the first sandbox found in the store by a given filter. | ||
func (c *memoryStore) First(filter StoreFilter) *Sandbox { | ||
for _, cont := range c.all() { | ||
if filter(cont) { | ||
return cont | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// ApplyAll calls the reducer function with every sandbox in the store. | ||
// This operation is asynchronous in the memory store. | ||
// NOTE: Modifications to the store MUST NOT be done by the StoreReducer. | ||
func (c *memoryStore) ApplyAll(apply StoreReducer) { | ||
wg := new(sync.WaitGroup) | ||
for _, cont := range c.all() { | ||
wg.Add(1) | ||
go func(sandbox *Sandbox) { | ||
apply(sandbox) | ||
wg.Done() | ||
}(cont) | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
func (c *memoryStore) all() []*Sandbox { | ||
c.RLock() | ||
sandboxes := make([]*Sandbox, 0, len(c.s)) | ||
for _, cont := range c.s { | ||
sandboxes = append(sandboxes, cont) | ||
} | ||
c.RUnlock() | ||
return sandboxes | ||
} | ||
|
||
var _ Storer = &memoryStore{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sandbox | ||
|
||
// StoreFilter defines a function to filter | ||
// sandboxes in the store. | ||
type StoreFilter func(*Sandbox) bool | ||
|
||
// StoreReducer defines a function to | ||
// manipulate sandboxes in the store | ||
type StoreReducer func(*Sandbox) | ||
|
||
// Storer defines an interface that any container store must implement. | ||
type Storer interface { | ||
// Add appends a new sandbox to the store. | ||
Add(string, *Sandbox) | ||
// Get returns a sandbox from the store by the identifier it was stored with. | ||
Get(string) *Sandbox | ||
// Delete removes a sandbox from the store by the identifier it was stored with. | ||
Delete(string) | ||
// List returns a list of sandboxes from the store. | ||
List() []*Sandbox | ||
// Size returns the number of sandboxes in the store. | ||
Size() int | ||
// First returns the first sandbox found in the store by a given filter. | ||
First(StoreFilter) *Sandbox | ||
// ApplyAll calls the reducer function with every sandbox in the store. | ||
ApplyAll(StoreReducer) | ||
} |