kvstore: add batch delete (#112723)

-add batch delete to the grpc
This commit is contained in:
Georges Chaudy
2025-10-21 21:32:03 +02:00
committed by GitHub
parent def449293b
commit 9a154ac15f
2 changed files with 147 additions and 0 deletions
+29
View File
@@ -53,6 +53,10 @@ type KV interface {
// Delete a value
Delete(ctx context.Context, section string, key string) error
// BatchDelete removes multiple keys from the store.
// Non-existent keys will be skipped silently without error.
BatchDelete(ctx context.Context, section string, keys []string) error
// UnixTimestamp returns the current time in seconds since Epoch.
// This is used to ensure the server and client are not too far apart in time.
UnixTimestamp(ctx context.Context) (int64, error)
@@ -331,3 +335,28 @@ func IsValidKey(key string) bool {
}
return validKeyRegex.MatchString(key)
}
func (k *badgerKV) BatchDelete(ctx context.Context, section string, keys []string) error {
if k.db.IsClosed() {
return fmt.Errorf("database is closed")
}
if section == "" {
return fmt.Errorf("section is required")
}
txn := k.db.NewTransaction(true)
defer txn.Discard()
for _, key := range keys {
keyWithSection := section + "/" + key
// Delete the key (BadgerDB's Delete is idempotent - succeeds even if key doesn't exist)
err := txn.Delete([]byte(keyWithSection))
if err != nil {
return err
}
}
return txn.Commit()
}