Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KV Atomic Delete and Purge #872

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/NATS.Client/KeyValue/IKeyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,25 @@ public interface IKeyValue
/// <param name="key">the key</param>
void Delete(string key);

/// <summary>
/// Soft deletes the key by placing a delete marker iff the key exists and its last revision matches the expected.
/// </summary>
/// <param name="key">the key</param>
/// <param name="expectedRevision">the expected last revision</param>
void Delete(string key, ulong expectedRevision);

/// <summary>
/// Purge all values/history from the specific key.
/// </summary>
/// <param name="key">the key</param>
void Purge(string key);

/// <summary>
/// Purge all values/history from the specific key iff the key exists and its last revision matches the expected.
/// </summary>
/// <param name="key">the key</param>
/// <param name="expectedRevision">the expected last revision</param>
void Purge(string key, ulong expectedRevision);

/// <summary>
/// Watch updates for a specific key
Expand Down
28 changes: 26 additions & 2 deletions src/NATS.Client/KeyValue/KeyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,33 @@ public ulong Update(string key, byte[] value, ulong expectedRevision)
return _write(key, value, h).Seq;
}

public void Delete(string key) => _write(key, null, DeleteHeaders);
public void Delete(string key)
{
Validator.ValidateKvKeyWildcardAllowedRequired(key);
_write(key, null, DeleteHeaders);
}

public void Delete(string key, ulong expectedRevision)
{
Validator.ValidateKvKeyWildcardAllowedRequired(key);
MsgHeader h = DeleteHeaders;
h[JetStreamConstants.ExpLastSubjectSeqHeader] = expectedRevision.ToString();
_write(key, null, h);
}

public void Purge(string key) => _write(key, null, PurgeHeaders);
public void Purge(string key)
{
Validator.ValidateKvKeyWildcardAllowedRequired(key);
_write(key, null, PurgeHeaders);
}

public void Purge(string key, ulong expectedRevision)
{
Validator.ValidateKvKeyWildcardAllowedRequired(key);
MsgHeader h = PurgeHeaders;
h[JetStreamConstants.ExpLastSubjectSeqHeader] = expectedRevision.ToString();
_write(key, null, h);
}

public KeyValueWatchSubscription Watch(string key, IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions)
{
Expand Down
Loading
Loading