1- // Package metadatadb provides an eventually consistent metadata store for
2- // coordinating state across cachew replicas. Mutations are applied to local
3- // state immediately and synced periodically to a shared backend. Last flush
4- // wins — the lock serialises all writes.
1+ // Package metadatadb provides a metadata store for coordinating state across
2+ // cachew replicas.
53package metadatadb
64
75import (
@@ -18,16 +16,17 @@ import (
1816)
1917
2018// Backend is the pluggable storage layer for the metadata store. Implementations
21- // handle write operations (Apply), read queries (Query), and persistence (Flush).
19+ // handle write operations (Apply), read queries (Query), and remote state
20+ // refreshes (Flush).
2221type Backend interface {
2322 // Apply applies one or more write operations to the given namespace.
2423 Apply (ctx context.Context , namespace string , ops ... Op ) error
2524 // Query executes a read query and unmarshals the result into target.
2625 // Target must be a pointer to the expected result type.
2726 Query (ctx context.Context , namespace string , q ReadOp , target any ) error
28- // Flush forces pending state to be persisted for the given namespace .
27+ // Flush forces the namespace to refresh from the backend .
2928 Flush (ctx context.Context , namespace string ) error
30- // Close shuts down the backend, flushing any pending state .
29+ // Close shuts down the backend.
3130 Close (ctx context.Context ) error
3231}
3332
@@ -75,13 +74,13 @@ type Namespace struct {
7574 name string
7675}
7776
78- // Flush forces an immediate sync with the backend.
77+ // Flush forces an immediate refresh from the backend.
7978func (n * Namespace ) Flush (ctx context.Context ) error {
8079 return errors .Wrap (n .backend .Flush (ctx , n .name ), "flush namespace" )
8180}
8281
83- func (n * Namespace ) apply (ops ... Op ) {
84- _ = n .backend .Apply (context .Background (), n .name , ops ... ) //nolint:errcheck // local backends never fail
82+ func (n * Namespace ) apply (ops ... Op ) error {
83+ return errors . Wrap ( n .backend .Apply (context .Background (), n .name , ops ... ), "apply metadata op" )
8584}
8685
8786func (n * Namespace ) query (q ReadOp , target any ) {
@@ -99,8 +98,12 @@ func NewScalar[V any](ns *Namespace, name string) *Scalar[V] {
9998 return & Scalar [V ]{ns : ns , name : name }
10099}
101100
102- func (s * Scalar [V ]) Set (value V ) { s .ns .apply (ScalarSet {Key : s .name , Value : value }) }
103- func (s * Scalar [V ]) Delete () { s .ns .apply (ScalarDelete {Key : s .name }) }
101+ func (s * Scalar [V ]) Set (value V ) error {
102+ return s .ns .apply (ScalarSet {Key : s .name , Value : value })
103+ }
104+ func (s * Scalar [V ]) Delete () error {
105+ return s .ns .apply (ScalarDelete {Key : s .name })
106+ }
104107
105108func (s * Scalar [V ]) Get () (V , bool ) {
106109 var result struct {
@@ -122,10 +125,18 @@ func NewInt(ns *Namespace, name string) *Int {
122125 return & Int {ns : ns , name : name }
123126}
124127
125- func (i * Int ) Set (value int64 ) { i .ns .apply (IntSet {Key : i .name , Value : value }) }
126- func (i * Int ) Add (delta int64 ) { i .ns .apply (IntAdd {Key : i .name , Delta : delta }) }
127- func (i * Int ) Mul (factor int64 ) { i .ns .apply (IntMul {Key : i .name , Factor : factor }) }
128- func (i * Int ) Div (divisor int64 ) { i .ns .apply (IntDiv {Key : i .name , Divisor : divisor }) }
128+ func (i * Int ) Set (value int64 ) error {
129+ return i .ns .apply (IntSet {Key : i .name , Value : value })
130+ }
131+ func (i * Int ) Add (delta int64 ) error {
132+ return i .ns .apply (IntAdd {Key : i .name , Delta : delta })
133+ }
134+ func (i * Int ) Mul (factor int64 ) error {
135+ return i .ns .apply (IntMul {Key : i .name , Factor : factor })
136+ }
137+ func (i * Int ) Div (divisor int64 ) error {
138+ return i .ns .apply (IntDiv {Key : i .name , Divisor : divisor })
139+ }
129140
130141func (i * Int ) Get () int64 {
131142 var v int64
@@ -144,20 +155,20 @@ func NewIntMap[K comparable](ns *Namespace, name string) *IntMap[K] {
144155 return & IntMap [K ]{ns : ns , name : name }
145156}
146157
147- func (m * IntMap [K ]) Set (key K , value int64 ) {
148- m .ns .apply (IntMapSet {Key : m .name , MapKey : key , Value : value })
158+ func (m * IntMap [K ]) Set (key K , value int64 ) error {
159+ return m .ns .apply (IntMapSet {Key : m .name , MapKey : key , Value : value })
149160}
150- func (m * IntMap [K ]) Add (key K , delta int64 ) {
151- m .ns .apply (IntMapAdd {Key : m .name , MapKey : key , Delta : delta })
161+ func (m * IntMap [K ]) Add (key K , delta int64 ) error {
162+ return m .ns .apply (IntMapAdd {Key : m .name , MapKey : key , Delta : delta })
152163}
153- func (m * IntMap [K ]) Mul (key K , factor int64 ) {
154- m .ns .apply (IntMapMul {Key : m .name , MapKey : key , Factor : factor })
164+ func (m * IntMap [K ]) Mul (key K , factor int64 ) error {
165+ return m .ns .apply (IntMapMul {Key : m .name , MapKey : key , Factor : factor })
155166}
156- func (m * IntMap [K ]) Div (key K , divisor int64 ) {
157- m .ns .apply (IntMapDiv {Key : m .name , MapKey : key , Divisor : divisor })
167+ func (m * IntMap [K ]) Div (key K , divisor int64 ) error {
168+ return m .ns .apply (IntMapDiv {Key : m .name , MapKey : key , Divisor : divisor })
158169}
159- func (m * IntMap [K ]) Delete (key K ) {
160- m .ns .apply (IntMapDelete {Key : m .name , MapKey : key })
170+ func (m * IntMap [K ]) Delete (key K ) error {
171+ return m .ns .apply (IntMapDelete {Key : m .name , MapKey : key })
161172}
162173
163174func (m * IntMap [K ]) Get (key K ) int64 {
@@ -190,8 +201,12 @@ func NewSet[V comparable](ns *Namespace, name string) *Set[V] {
190201 return & Set [V ]{ns : ns , name : name }
191202}
192203
193- func (s * Set [V ]) Add (member V ) { s .ns .apply (SetAdd {Key : s .name , Member : member }) }
194- func (s * Set [V ]) Remove (member V ) { s .ns .apply (SetRemove {Key : s .name , Member : member }) }
204+ func (s * Set [V ]) Add (member V ) error {
205+ return s .ns .apply (SetAdd {Key : s .name , Member : member })
206+ }
207+ func (s * Set [V ]) Remove (member V ) error {
208+ return s .ns .apply (SetRemove {Key : s .name , Member : member })
209+ }
195210
196211func (s * Set [V ]) Contains (member V ) bool {
197212 var v bool
@@ -217,12 +232,12 @@ func NewMap[K comparable, V any](ns *Namespace, name string) *Map[K, V] {
217232 return & Map [K , V ]{ns : ns , name : name }
218233}
219234
220- func (m * Map [K , V ]) Set (key K , value V ) {
221- m .ns .apply (MapSet {Key : m .name , MapKey : key , Value : value })
235+ func (m * Map [K , V ]) Set (key K , value V ) error {
236+ return m .ns .apply (MapSet {Key : m .name , MapKey : key , Value : value })
222237}
223238
224- func (m * Map [K , V ]) Delete (key K ) {
225- m .ns .apply (MapDelete {Key : m .name , MapKey : key })
239+ func (m * Map [K , V ]) Delete (key K ) error {
240+ return m .ns .apply (MapDelete {Key : m .name , MapKey : key })
226241}
227242
228243func (m * Map [K , V ]) Get (key K ) (V , bool ) {
@@ -258,8 +273,8 @@ func NewList[V any](ns *Namespace, name string) *List[V] {
258273 return & List [V ]{ns : ns , name : name }
259274}
260275
261- func (l * List [V ]) Append (value V ) {
262- l .ns .apply (ListAppend {Key : l .name , Value : value })
276+ func (l * List [V ]) Append (value V ) error {
277+ return l .ns .apply (ListAppend {Key : l .name , Value : value })
263278}
264279
265280func (l * List [V ]) Entries () []V {
0 commit comments