aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/store/segment.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/store/segment.go')
-rw-r--r--internal/store/segment.go18
1 files changed, 14 insertions, 4 deletions
diff --git a/internal/store/segment.go b/internal/store/segment.go
index 38859590..79f652e7 100644
--- a/internal/store/segment.go
+++ b/internal/store/segment.go
@@ -54,11 +54,11 @@ func (eh *EntryHandle) Destroy() error {
return nil
}
-// Save encodes [hst.State] and writes it to the underlying file.
+// save encodes [hst.State] and writes it to the underlying file.
// An error is returned if a file already exists with the same identifier.
-// Save does not validate the embedded [hst.Config].
-// A non-nil error returned by Save is of type [hst.AppError].
-func (eh *EntryHandle) Save(state *hst.State) error {
+// save does not validate the embedded [hst.Config].
+// A non-nil error returned by save is of type [hst.AppError].
+func (eh *EntryHandle) save(state *hst.State) error {
f, err := eh.open(os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return err
@@ -123,6 +123,16 @@ func (h *Handle) Lock() (unlock func(), err error) {
return
}
+// Save attempts to save [hst.State] as a segment entry, and returns its [EntryHandle].
+// Must be called while holding [Handle.Lock].
+// An error is returned if an entry already exists with the same identifier.
+// Save does not validate the embedded [hst.Config].
+// A non-nil error returned by Save is of type [hst.AppError].
+func (h *Handle) Save(state *hst.State) (*EntryHandle, error) {
+ eh := EntryHandle{nil, h.Path.Append(state.ID.String()), state.ID}
+ return &eh, eh.save(state)
+}
+
// Entries returns an iterator over all [EntryHandle] held in this segment.
// Must be called while holding [Handle.Lock].
// A non-nil error attached to a [EntryHandle] indicates a malformed identifier and is of type [hst.AppError].