Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/workspace/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package workspace

import "errors"

var (
ErrNameAlreadyExists = errors.New("name already exists")
)
56 changes: 56 additions & 0 deletions internal/workspace/registry.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions internal/workspace/workspace.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading