aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/store/compat.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/store/compat.go')
-rw-r--r--internal/store/compat.go60
1 files changed, 58 insertions, 2 deletions
diff --git a/internal/store/compat.go b/internal/store/compat.go
index c0ef6dd7..0db2f5f2 100644
--- a/internal/store/compat.go
+++ b/internal/store/compat.go
@@ -1,6 +1,8 @@
package store
import (
+ "errors"
+ "maps"
"strconv"
"hakurei.app/container/check"
@@ -10,7 +12,7 @@ import (
/* this provides an implementation of Store on top of the improved state tracking to ease in the changes */
-type Store interface {
+type Compat interface {
// Do calls f exactly once and ensures store exclusivity until f returns.
// Returns whether f is called and any errors during the locking process.
// Cursor provided to f becomes invalid as soon as f returns.
@@ -58,7 +60,7 @@ func (s storeAdapter) List() ([]int, error) {
}
// NewMulti returns an instance of the multi-file store.
-func NewMulti(msg message.Msg, prefix *check.Absolute) Store {
+func NewMulti(msg message.Msg, prefix *check.Absolute) Compat {
return storeAdapter{msg, newStore(prefix.Append("state"))}
}
@@ -128,3 +130,57 @@ func (h *storeHandle) Len() (int, error) {
}
return n, err
}
+
+var (
+ ErrDuplicate = errors.New("store contains duplicates")
+)
+
+// Joiner is the interface that wraps the Join method.
+//
+// The Join function uses Joiner if available.
+type Joiner interface {
+ Join() (map[hst.ID]*hst.State, error)
+}
+
+// Join returns joined state entries of all active identities.
+func Join(s Compat) (map[hst.ID]*hst.State, error) {
+ if j, ok := s.(Joiner); ok {
+ return j.Join()
+ }
+
+ var (
+ aids []int
+ entries = make(map[hst.ID]*hst.State)
+
+ el int
+ res map[hst.ID]*hst.State
+ loadErr error
+ )
+
+ if ln, err := s.List(); err != nil {
+ return nil, err
+ } else {
+ aids = ln
+ }
+
+ for _, aid := range aids {
+ if _, err := s.Do(aid, func(c Cursor) {
+ res, loadErr = c.Load()
+ }); err != nil {
+ return nil, err
+ }
+
+ if loadErr != nil {
+ return nil, loadErr
+ }
+
+ // save expected length
+ el = len(entries) + len(res)
+ maps.Copy(entries, res)
+ if len(entries) != el {
+ return nil, ErrDuplicate
+ }
+ }
+
+ return entries, nil
+}