etcd: extend kvdb_etcd with a real ForAll implementation

This commit is contained in:
Andras Banki-Horvath committed 2022-02-28 12:03:04 +01:00
1 parent 4d9a05c2f4
commit 40af029413
3 files changed
+83 -9

No files matched your search

+51
View File
@@ -81,6 +81,11 @@ type STM interface {
// Prefetch prefetches the passed keys and prefixes. For prefixes it'll
// fetch the whole range.
Prefetch(keys []string, prefix []string)
// FetchRangePaginatedRaw will fetch the range with the passed prefix up
// to the passed limit per page.
FetchRangePaginatedRaw(prefix string, limit int64,
cb func(kv KV) error) error
}
// CommitError is used to check if there was an error
@@ -589,6 +594,52 @@ func (ws writeSet) puts() []v3.Op {
return puts
}
// FetchRangePaginatedRaw will fetch the range with the passed prefix up to the
// passed limit per page.
func (s *stm) FetchRangePaginatedRaw(prefix string, limit int64,
cb func(kv KV) error) error {
s.callCount++
opts := []v3.OpOption{
v3.WithSort(v3.SortByKey, v3.SortAscend),
v3.WithRange(v3.GetPrefixRangeEnd(prefix)),
v3.WithLimit(limit),
}
key := prefix
for {
resp, err := s.client.Get(
s.options.ctx, key, append(opts, s.getOpts...)...,
)
if err != nil {
return DatabaseError{
msg: "stm.fetch() failed",
err: err,
}
}
// Fill the read set with key/values returned.
for _, kv := range resp.Kvs {
err := cb(KV{string(kv.Key), string(kv.Value)})
if err != nil {
return err
}
}
// We've reached the range end.
if !resp.More {
break
}
// Continue from the page end + "\x00".
key = string(resp.Kvs[len(resp.Kvs)-1].Key) + "\x00"
}
return nil
}
// fetch is a helper to fetch key/value given options. If a value is returned
// then fetch will try to fix the STM's snapshot revision (if not already set).
// We'll also cache the returned key/value in the read set.