diff --git a/internal/workspace/errors.go b/internal/workspace/errors.go new file mode 100644 index 0000000..a21eedc --- /dev/null +++ b/internal/workspace/errors.go @@ -0,0 +1,7 @@ +package workspace + +import "errors" + +var ( + ErrNameAlreadyExists = errors.New("name already exists") +) diff --git a/internal/workspace/registry.go b/internal/workspace/registry.go new file mode 100644 index 0000000..c496175 --- /dev/null +++ b/internal/workspace/registry.go @@ -0,0 +1,56 @@ +package workspace + +import ( + "fmt" + "sync" +) + +// Registry maps ID to Workspace and Name to ID. +type Registry struct { + nextID uint64 + byID map[ID]*Workspace + nameToID map[Name]ID + mu sync.RWMutex +} + +// NewRegistry creates a Registry. +func NewRegistry() *Registry { + return &Registry{ + byID: make(map[ID]*Workspace), + nameToID: make(map[Name]ID), + } +} + +// Put maps the given Workspace by a generated ID, and maps the given Name to generated ID. +func (r *Registry) Put(name Name, workspace *Workspace) (ID, error) { + r.mu.Lock() + defer r.mu.Unlock() + + if _, ok := r.nameToID[name]; ok { + return 0, fmt.Errorf("%w: %v", ErrNameAlreadyExists, name) + } + + r.nextID++ + id := ID(r.nextID) + r.byID[id] = workspace + r.nameToID[name] = id + return id, nil +} + +// Get returns the Workspace by ID. +func (r *Registry) Get(id ID) (*Workspace, bool) { + r.mu.RLock() + defer r.mu.RUnlock() + + workspace, ok := r.byID[id] + return workspace, ok +} + +// GetID returns the ID by Name. +func (r *Registry) GetID(name Name) (ID, bool) { + r.mu.RLock() + defer r.mu.RUnlock() + + workspace, ok := r.nameToID[name] + return workspace, ok +} diff --git a/internal/workspace/workspace.go b/internal/workspace/workspace.go new file mode 100644 index 0000000..f32c881 --- /dev/null +++ b/internal/workspace/workspace.go @@ -0,0 +1,18 @@ +package workspace + +import ( + "github.com/Skoowshot/qrix-runtime/internal/network" + "github.com/Skoowshot/qrix-runtime/internal/store" +) + +// ID is a unique runtime hot-path identifier of Workspace. +type ID uint64 + +// Name is a unique cold-path name of Workspace. +type Name string + +// Workspace is an isolated self-contained unit holding application state. +type Workspace struct { + Stores *store.Registry + Network *network.Network +}