Skip to content

Commit

Permalink
added kv config udpate, removed beta markers since ordered consumer i…
Browse files Browse the repository at this point in the history
…s done, also fixed Ttl to a duration since it's nanos, not millis, glad this was still marked as beta (#613)
  • Loading branch information
scottf committed Jun 18, 2022
1 parent 1571439 commit 8d7cd68
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 30 deletions.
17 changes: 0 additions & 17 deletions src/NATS.Client/KeyValue/IKeyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,18 @@ public interface IKeyValue
{
/// <summary>
/// The name of the bucket
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
string BucketName { get; }

/// <summary>
/// Get the entry for a key
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
/// <returns>The entry</returns>
KeyValueEntry Get(string key);

/// <summary>
/// Get the specific revision of an entry for a key
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
/// <param name="revision">the specific revision</param>
Expand All @@ -83,7 +80,6 @@ public interface IKeyValue

/// <summary>
/// Put a byte[] as the value for a key
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
/// <param name="value">the bytes of the value</param>
Expand All @@ -92,7 +88,6 @@ public interface IKeyValue

/// <summary>
/// Put a string as the value for a key
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
/// <param name="value">the UTF-8 string</param>
Expand All @@ -101,7 +96,6 @@ public interface IKeyValue

/// <summary>
///Put a long as the value for a key
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
/// <param name="value">the number</param>
Expand All @@ -111,7 +105,6 @@ public interface IKeyValue
/// <summary>
/// Put as the value for a key iff the key does not exist (there is no history)
/// or is deleted (history shows the key is deleted)
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
/// <param name="value">the bytes of the value</param>
Expand All @@ -120,7 +113,6 @@ public interface IKeyValue

/// <summary>
/// Put as the value for a key iff the key exists and its last revision matches the expected
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
/// <param name="value">the bytes of the value</param>
Expand All @@ -130,21 +122,18 @@ public interface IKeyValue

/// <summary>
/// Soft deletes the key by placing a delete marker.
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
void Delete(string key);

/// <summary>
/// Purge all values/history from the specific key.
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
void Purge(string key);

/// <summary>
/// Watch updates for a specific key
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// WARNING: This api requires an internal consumer the enforces ordering of messages.
/// This portion of the implementation is not complete yet. If there was some sort of
/// error from the server and messages were skipped or came out of order the data received
Expand All @@ -158,7 +147,6 @@ public interface IKeyValue

/// <summary>
/// Watch updates for all keys
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// WARNING: This api requires an internal consumer the enforces ordering of messages.
/// This portion of the implementation is not complete yet. If there was some sort of
/// error from the server and messages were skipped or came out of order the data received
Expand All @@ -171,14 +159,12 @@ public interface IKeyValue

/// <summary>
/// Get a list of the keys in a bucket.
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <returns>The list of keys</returns>
IList<string> Keys();

/// <summary>
/// Get the history (list of KeyValueEntry) for a key
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="key">the key</param>
/// <returns>The list of KeyValueEntry</returns>
Expand All @@ -187,19 +173,16 @@ public interface IKeyValue
/// <summary>
/// Remove history from all keys that currently are deleted or purged,
/// using a default KeyValuePurgeOptions
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
void PurgeDeletes();

/// <summary>
/// Remove history from all keys that currently are deleted or purged
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
void PurgeDeletes(KeyValuePurgeOptions options);

/// <summary>
/// Get the KeyValueStatus object
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <returns>the status object</returns>
KeyValueStatus Status();
Expand Down
11 changes: 7 additions & 4 deletions src/NATS.Client/KeyValue/IKeyValueManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,33 @@ public interface IKeyValueManagement
{
/// <summary>
/// Create a key value store.
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="config">the key value configuration</param>
/// <returns></returns>
KeyValueStatus Create(KeyValueConfiguration config);

/// <summary>
/// Update a key value store configuration. Storage type cannot change.
/// </summary>
/// <param name="config">the key value configuration</param>
/// <returns></returns>
KeyValueStatus Update(KeyValueConfiguration config);

/// <summary>
/// Get the list of bucket names.
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <returns>list of bucket names</returns>
IList<string> GetBucketNames();

/// <summary>
/// Gets the info for an existing bucket.
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="bucketName">the bucket name to use</param>
/// <returns>the bucket status object</returns>
KeyValueStatus GetBucketInfo(string bucketName);

/// <summary>
/// Deletes an existing bucket. Will throw a NATSJetStreamException if the delete fails.
/// THIS IS A BETA FEATURE AND SUBJECT TO CHANGE
/// </summary>
/// <param name="bucketName"></param>
void Delete(string bucketName);
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client/KeyValue/KeyValueConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal KeyValueConfiguration(StreamConfiguration sc)
/// <summary>
/// The maximum age for a value in this bucket
/// </summary>
public long Ttl => BackingConfig.MaxAge.Millis;
public Duration Ttl => BackingConfig.MaxAge;

/// <summary>
/// The storage type for this bucket
Expand Down
5 changes: 5 additions & 0 deletions src/NATS.Client/KeyValue/KeyValueManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public KeyValueStatus Create(KeyValueConfiguration config)
}
return new KeyValueStatus(jsm.AddStream(sc));
}

public KeyValueStatus Update(KeyValueConfiguration config)
{
return new KeyValueStatus(jsm.UpdateStream(config.BackingConfig));
}

public IList<string> GetBucketNames()
{
Expand Down
3 changes: 2 additions & 1 deletion src/NATS.Client/KeyValue/KeyValueStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using NATS.Client.Internals;
using NATS.Client.JetStream;

namespace NATS.Client.KeyValue
Expand Down Expand Up @@ -65,7 +66,7 @@ public class KeyValueStatus
/// <summary>
/// The maximum age for a value in this bucket
/// </summary>
public long Ttl => Config.Ttl;
public Duration Ttl => Config.Ttl;

/// <summary>
/// The storage type for this bucket
Expand Down
16 changes: 12 additions & 4 deletions src/Samples/KeyValueFull/KeyValueFull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public static void Main(string[] args)
IKeyValueManagement kvm = c.CreateKeyValueManagementContext();

// create the bucket
KeyValueConfiguration bc = KeyValueConfiguration.Builder()
KeyValueConfiguration kvc = KeyValueConfiguration.Builder()
.WithName(helper.Bucket)
.WithDescription(helper.Description)
.WithMaxHistoryPerKey(5)
.WithStorageType(StorageType.Memory)
.Build();

KeyValueStatus kvs = kvm.Create(bc);
KeyValueStatus kvs = kvm.Create(kvc);
Console.WriteLine(kvs);

// get the kv context for the specific bucket
Expand Down Expand Up @@ -178,12 +178,20 @@ public static void Main(string[] args)
Console.WriteLine(LongKey + " from TryGetLongValue: " + lvalue);

// let's check the bucket info
Console.WriteLine("\n9.1 Bucket before delete");
Console.WriteLine("\n9.1 Bucket before update/delete");
kvs = kvm.GetBucketInfo(helper.Bucket);
Console.WriteLine(kvs);

kvc = KeyValueConfiguration.Builder(kvs.Config)
.WithDescription(helper.Description + "-changed")
.WithMaxHistoryPerKey(6)
.Build();
kvs = kvm.Update(kvc);
Console.WriteLine("\n9.2 Bucket after update");
Console.WriteLine(kvs);

// delete the bucket
Console.WriteLine("\n9.2 Delete");
Console.WriteLine("\n9.3 Delete");
kvm.Delete(helper.Bucket);

try {
Expand Down
74 changes: 72 additions & 2 deletions src/Tests/IntegrationTests/TestKeyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Text;
using System.Threading;
using NATS.Client;
using NATS.Client.Internals;
using NATS.Client.JetStream;
using NATS.Client.KeyValue;
using Xunit;
Expand Down Expand Up @@ -72,8 +73,8 @@ public void TestWorkFLow()
Assert.Equal(-1, kvc.MaxBucketSize);
Assert.Equal(-1, status.MaxValueSize);
Assert.Equal(-1, kvc.MaxValueSize);
Assert.Equal(0, status.Ttl);
Assert.Equal(0, kvc.Ttl);
Assert.Equal(Duration.Zero, status.Ttl);
Assert.Equal(Duration.Zero, kvc.Ttl);
Assert.Equal(StorageType.Memory, status.StorageType);
Assert.Equal(StorageType.Memory, kvc.StorageType);
Assert.Equal(1, status.Replicas);
Expand Down Expand Up @@ -466,6 +467,75 @@ public void TestWorkFLow()
Assert.Equal(1, list.Count);
});
}

[Fact]
public void TestCreateUpdate() {
Context.RunInJsServer(c =>
{
// get the kv management context
IKeyValueManagement kvm = c.CreateKeyValueManagementContext();
Assert.Throws<NATSJetStreamException>(() => kvm.GetBucketInfo(BUCKET));
KeyValueStatus kvs = kvm.Create(KeyValueConfiguration.Builder()
.WithName(BUCKET)
.WithStorageType(StorageType.Memory)
.Build());
Assert.Equal(BUCKET, kvs.BucketName);
Assert.Empty(kvs.Description);
Assert.Equal(1, kvs.MaxHistoryPerKey);
Assert.Equal(-1, kvs.MaxBucketSize);
Assert.Equal(-1, kvs.MaxValueSize);
Assert.Equal(Duration.Zero, kvs.Ttl);
Assert.Equal(StorageType.Memory, kvs.StorageType);
Assert.Equal(1, kvs.Replicas);
Assert.Equal(0U, kvs.EntryCount);
Assert.Equal("JetStream", kvs.BackingStore);
IKeyValue kv = c.CreateKeyValueContext(BUCKET);
kv.Put(KEY, 1);
kv.Put(KEY, 2);
IList<KeyValueEntry> history = kv.History(KEY);
Assert.Equal(1, history.Count);
long lvalue = 0;
Assert.True(history[0].TryGetLongValue(out lvalue));
Assert.Equal(2, lvalue);
KeyValueConfiguration kvc = KeyValueConfiguration.Builder(kvs.Config)
.WithDescription(Plain)
.WithMaxHistoryPerKey(3)
.WithMaxBucketSize(10_000)
.WithMaxValueSize(100)
.WithTtl(Duration.OfHours(1))
.Build();
kvs = kvm.Update(kvc);
Assert.Equal(BUCKET, kvs.BucketName);
Assert.Equal(Plain, kvs.Description);
Assert.Equal(3, kvs.MaxHistoryPerKey);
Assert.Equal(10_000, kvs.MaxBucketSize);
Assert.Equal(100, kvs.MaxValueSize);
Assert.Equal(Duration.OfHours(1), kvs.Ttl);
Assert.Equal(StorageType.Memory, kvs.StorageType);
Assert.Equal(1, kvs.Replicas);
Assert.Equal(1U, kvs.EntryCount);
Assert.Equal("JetStream", kvs.BackingStore);
history = kv.History(KEY);
Assert.Equal(1, history.Count);
lvalue = 0;
Assert.True(history[0].TryGetLongValue(out lvalue));
Assert.Equal(2, lvalue);
KeyValueConfiguration kvcStor = KeyValueConfiguration.Builder(kvs.Config)
.WithStorageType(StorageType.File)
.Build();
Assert.Throws<NATSJetStreamException>(() => kvm.Update(kvcStor));
});
}

[Fact]
public void TestPurgeDeletes() {
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/UnitTests/JetStream/TestKeyValueConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class TestKeyValueConfiguration
Assert.Equal(44, bc.MaxHistoryPerKey);
Assert.Equal(555, bc.MaxBucketSize);
Assert.Equal(666, bc.MaxValueSize);
Assert.Equal(777, bc.Ttl);
Assert.Equal(Duration.OfMillis(777), bc.Ttl);
Assert.Equal(StorageType.Memory, bc.StorageType);
Assert.Equal(2, bc.Replicas);
}
Expand Down

0 comments on commit 8d7cd68

Please sign in to comment.