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.go41
1 files changed, 27 insertions, 14 deletions
diff --git a/internal/store/segment.go b/internal/store/segment.go
index c2a718ca..06849749 100644
--- a/internal/store/segment.go
+++ b/internal/store/segment.go
@@ -14,6 +14,7 @@ import (
)
// EntryHandle is a handle on a state entry retrieved from a [Handle].
+//
// Must only be used while its parent [Handle.Lock] is held.
type EntryHandle struct {
// Error returned while decoding pathname.
@@ -27,6 +28,7 @@ type EntryHandle struct {
}
// open opens the underlying state entry file.
+//
// A non-nil error returned by open is of type [hst.AppError].
func (eh *EntryHandle) open(flag int, perm os.FileMode) (*os.File, error) {
if eh.DecodeErr != nil {
@@ -41,6 +43,7 @@ func (eh *EntryHandle) open(flag int, perm os.FileMode) (*os.File, error) {
}
// Destroy removes the underlying state entry.
+//
// A non-nil error returned by Destroy is of type [hst.AppError].
func (eh *EntryHandle) Destroy() error {
// destroy does not go through open
@@ -55,8 +58,10 @@ func (eh *EntryHandle) Destroy() error {
}
// 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 {
f, err := eh.open(os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
@@ -71,17 +76,19 @@ func (eh *EntryHandle) save(state *hst.State) error {
return err
}
-// Load loads and validates the state entry header, and returns the [hst.Enablement] byte.
-// for a non-nil v, the full state payload is decoded and stored in the value pointed to by v.
-// Load validates the embedded [hst.Config] value.
-// A non-nil error returned by Load is of type [hst.AppError].
-func (eh *EntryHandle) Load(v *hst.State) (hst.Enablement, error) {
+// Load loads and validates the state entry header, and returns the
+// [hst.Enablements] byte. For a non-nil v, the full state payload is decoded
+// and stored in the value pointed to by v.
+//
+// Load validates the embedded [hst.Config] value. A non-nil error returned by
+// Load is of type [hst.AppError].
+func (eh *EntryHandle) Load(v *hst.State) (hst.Enablements, error) {
f, err := eh.open(os.O_RDONLY, 0)
if err != nil {
return 0, err
}
- var et hst.Enablement
+ var et hst.Enablements
if v != nil {
et, err = entryDecode(f, v)
if err == nil && v.ID != eh.ID {
@@ -99,6 +106,7 @@ func (eh *EntryHandle) Load(v *hst.State) (hst.Enablement, error) {
}
// Handle is a handle on a [Store] segment.
+//
// Initialised by [Store.Handle].
type Handle struct {
// Identity of instances tracked by this segment.
@@ -113,8 +121,9 @@ type Handle struct {
mu sync.Mutex
}
-// Lock attempts to acquire a lock on [Handle].
-// If successful, Lock returns a non-nil unlock function.
+// Lock attempts to acquire a lock on [Handle]. If successful, Lock returns a
+// non-nil unlock function.
+//
// A non-nil error returned by Lock is of type [hst.AppError].
func (h *Handle) Lock() (unlock func(), err error) {
if unlock, err = h.fileMu.Lock(); err != nil {
@@ -123,20 +132,24 @@ 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].
+// 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].
-// A non-nil error returned by Entries is of type [hst.AppError].
+// 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]. A non-nil error returned by Entries is of type
+// [hst.AppError].
func (h *Handle) Entries() (iter.Seq[*EntryHandle], int, error) {
// for error reporting
const step = "read store segment entries"