move refs/pman over to owncloud/ocis/ocis/pkg/runtime

This commit is contained in:
A.Unger
2021-01-25 16:06:22 +01:00
parent 1404a8beaa
commit e50ee6e002
23 changed files with 994 additions and 5 deletions
+64
View File
@@ -0,0 +1,64 @@
package storage
import (
"github.com/owncloud/ocis/ocis/pkg/runtime/process"
"sync"
)
// Map synchronizes access to extension+pid tuples.
type Map struct {
c *sync.Map
}
// NewMapStorage initializes a new Storage.
func NewMapStorage() Storage {
return &Map{
c: &sync.Map{},
}
}
// Store a value on the underlying data structure.
func (m *Map) Store(e process.ProcEntry) error {
m.c.Store(e.Extension, e.Pid)
return nil
}
// Delete a value on the underlying data structure.
func (m *Map) Delete(e process.ProcEntry) error {
m.c.Delete(e.Extension)
return nil
}
// Load a single pid.
func (m *Map) Load(name string) int {
var val int
m.c.Range(func(k, v interface{}) bool {
if k.(string) == name {
val = v.(int)
return false
}
return true
})
return val
}
// LoadAll values from the underlying data structure.
func (m *Map) LoadAll() Entries {
e := make(map[string]int, 0)
m.c.Range(func(k, v interface{}) bool {
ks, ok := k.(string)
if !ok {
return false
}
vs, ok := v.(int)
if !ok {
return false
}
e[ks] = vs
return true
})
return e
}
+43
View File
@@ -0,0 +1,43 @@
package storage
import (
"fmt"
"math/rand"
"os"
"strconv"
"testing"
"github.com/owncloud/ocis/ocis/pkg/runtime/process"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
loadStore()
os.Exit(m.Run())
}
var (
store = NewMapStorage()
)
func loadStore() {
for i := 0; i < 20; i++ {
store.Store(process.ProcEntry{
Pid: rand.Int(),
Extension: fmt.Sprintf("extension-%s", strconv.Itoa(i)),
})
}
}
func TestLoadAll(t *testing.T) {
all := store.LoadAll()
assert.NotNil(t, all["extension-1"])
}
func TestDelete(t *testing.T) {
store.Delete(process.ProcEntry{
Extension: "extension-1",
})
all := store.LoadAll()
assert.Zero(t, all["extension-1"])
}
+21
View File
@@ -0,0 +1,21 @@
package storage
import "github.com/owncloud/ocis/ocis/pkg/runtime/process"
// Entries is a tuple of <extension:pid>
type Entries map[string]int
// Storage defines a basic persistence interface layer.
type Storage interface {
// Store a representation of a process.
Store(e process.ProcEntry) error
// Delete a representation of a process.
Delete(e process.ProcEntry) error
// Load a single entry.
Load(name string) int
// LoadAll retrieves a set of entries of running processes on the host machine.
LoadAll() Entries
}