Skip to content

Commit

Permalink
KV Atomic Delete and Purge (#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Mar 6, 2024
1 parent 81371d7 commit f7a537a
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 107 deletions.
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

0 comments on commit f7a537a

Please sign in to comment.