2026-03-19 16:37:57 +08:00

89 lines
1.5 KiB
Go

package archivex
import (
"context"
"os"
"b612.me/starlog/internal/runtimex"
)
type FileRecord struct {
FullPath string
Pointer *os.File
}
type Runner struct {
Cancel context.CancelFunc
Done chan struct{}
}
type Store struct {
files runtimex.MapKV
runners runtimex.MapKV
}
func NewStore() *Store {
return &Store{
files: runtimex.NewMapKV(),
runners: runtimex.NewMapKV(),
}
}
func (store *Store) SetFile(id string, record FileRecord) error {
if store == nil {
return nil
}
return store.files.Store(id, record)
}
func (store *Store) GetFile(id string) (FileRecord, bool) {
if store == nil {
return FileRecord{}, false
}
val := store.files.MustGet(id)
if val == nil {
return FileRecord{}, false
}
record, ok := val.(FileRecord)
if !ok {
return FileRecord{}, false
}
return record, true
}
func (store *Store) DeleteFile(id string) error {
if store == nil {
return nil
}
return store.files.Delete(id)
}
func (store *Store) SetRunner(id string, runner *Runner) error {
if store == nil {
return nil
}
return store.runners.Store(id, runner)
}
func (store *Store) GetRunner(id string) (*Runner, bool) {
if store == nil {
return nil, false
}
val := store.runners.MustGet(id)
if val == nil {
return nil, false
}
runner, ok := val.(*Runner)
if !ok || runner == nil {
return nil, false
}
return runner, true
}
func (store *Store) DeleteRunner(id string) error {
if store == nil {
return nil
}
return store.runners.Delete(id)
}