internal KeyValue(IConnection connection, string bucketName, KeyValueOptions kvo) { BucketName = Validator.ValidateKvBucketNameRequired(bucketName); StreamName = KeyValueUtil.ToStreamName(BucketName); StreamSubject = KeyValueUtil.ToStreamSubject(BucketName); RawKeyPrefix = KeyValueUtil.ToKeyPrefix(bucketName); if (kvo == null) { js = new JetStream.JetStream(connection, null); jsm = new JetStreamManagement(connection, null); PubSubKeyPrefix = RawKeyPrefix; } else { js = new JetStream.JetStream(connection, kvo.JSOptions); jsm = new JetStreamManagement(connection, kvo.JSOptions); if (kvo.JSOptions.IsDefaultPrefix) { PubSubKeyPrefix = RawKeyPrefix; } else { PubSubKeyPrefix = kvo.JSOptions.Prefix + RawKeyPrefix; } } }
/// <summary> /// Builds the KeyValueConfiguration /// </summary> /// <returns>the KeyValueConfiguration</returns> public KeyValueConfiguration Build() { _name = Validator.ValidateKvBucketNameRequired(_name); scBuilder.WithName(KeyValueUtil.ToStreamName(_name)) .WithSubjects(KeyValueUtil.ToStreamSubject(_name)) .WithAllowRollup(true) .WithDiscardPolicy(DiscardPolicy.New) .WithDenyDelete(true); return(new KeyValueConfiguration(scBuilder.Build())); }
public void PurgeDeletes(KeyValuePurgeOptions options) { long dmThresh = options == null ? KeyValuePurgeOptions.DefaultThresholdMillis : options.DeleteMarkersThresholdMillis; DateTime limit; if (dmThresh < 0) { limit = DateTime.UtcNow.AddMilliseconds(600000); // long enough in the future to clear all } else if (dmThresh == 0) { limit = DateTime.UtcNow.AddMilliseconds(KeyValuePurgeOptions.DefaultThresholdMillis); } else { limit = DateTime.UtcNow.AddMilliseconds(-dmThresh); } IList <string> noKeepList = new List <string>(); IList <string> keepList = new List <string>(); VisitSubject(KeyValueUtil.ToStreamSubject(BucketName), DeliverPolicy.LastPerSubject, true, false, m => { KeyValueEntry kve = new KeyValueEntry(m); if (!kve.Operation.Equals(KeyValueOperation.Put)) { if (kve.Created > limit) // created > limit, so created after { keepList.Add(new BucketAndKey(m).Key); } else { noKeepList.Add(new BucketAndKey(m).Key); } } }); foreach (string key in noKeepList) { jsm.PurgeStream(StreamName, PurgeOptions.WithSubject(RawKeySubject(key))); } foreach (string key in keepList) { PurgeOptions po = PurgeOptions.Builder() .WithSubject(RawKeySubject(key)) .WithKeep(1) .Build(); jsm.PurgeStream(StreamName, po); } }