From 7c59f9d01149d908290d865715322c2299f79e0b Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 10:13:27 +0000 Subject: [PATCH] fix(store): allow discard after workspace close Make workspace cleanup idempotent so a closed workspace can still be discarded and removed from disk later. Also clarify the configuration comments for AX-oriented usage. Co-Authored-By: Virgil --- store.go | 4 +++- workspace.go | 16 +++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/store.go b/store.go index 9ab9a7f..96d197f 100644 --- a/store.go +++ b/store.go @@ -29,7 +29,8 @@ const ( // Usage example: `storeOptions := []store.StoreOption{store.WithJournal("http://127.0.0.1:8086", "core", "events")}` // Prefer `store.NewConfigured(store.StoreConfig{...})` when the configuration -// is already known as a struct literal. +// is already known as a struct literal. Use `StoreOption` only when the values +// need to be assembled incrementally. type StoreOption func(*StoreConfig) // Usage example: `config := store.StoreConfig{DatabasePath: ":memory:", PurgeInterval: 30 * time.Second}` @@ -43,6 +44,7 @@ type StoreConfig struct { } // Usage example: `config := storeInstance.JournalConfiguration(); fmt.Println(config.EndpointURL, config.Organisation, config.BucketName)` +// The values are copied into the store and used as journal metadata. type JournalConfiguration struct { // Usage example: `config := store.JournalConfiguration{EndpointURL: "http://127.0.0.1:8086"}` EndpointURL string diff --git a/workspace.go b/workspace.go index b1e3418..a207c09 100644 --- a/workspace.go +++ b/workspace.go @@ -67,6 +67,7 @@ func (workspace *Workspace) DatabasePath() string { // Close leaves the SQLite workspace file `.core/state/scroll-session-2026-03-30.duckdb` // on disk so a later store instance can recover it as an orphan. +// Call `Discard()` afterwards if you decide the file should be removed. // // Usage example: `if err := workspace.Close(); err != nil { return }; orphans := storeInstance.RecoverOrphans(".core/state"); _ = orphans` func (workspace *Workspace) Close() error { @@ -417,15 +418,16 @@ func (workspace *Workspace) closeAndCleanup(removeFiles bool) error { } workspace.closeLock.Lock() - defer workspace.closeLock.Unlock() - - if workspace.closed { - return nil + alreadyClosed := workspace.closed + if !alreadyClosed { + workspace.closed = true } - workspace.closed = true + workspace.closeLock.Unlock() - if err := workspace.workspaceDatabase.Close(); err != nil { - return core.E("store.Workspace.closeAndRemoveFiles", "close workspace database", err) + if !alreadyClosed { + if err := workspace.workspaceDatabase.Close(); err != nil { + return core.E("store.Workspace.closeAndRemoveFiles", "close workspace database", err) + } } if !removeFiles { return nil