Esempio n. 1
0
 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;
         }
     }
 }
Esempio n. 2
0
 /// <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()));
 }
Esempio n. 3
0
 public KeyValueEntry(Msg m)
 {
     bucketAndKey = new BucketAndKey(m.Subject);
     Value        = ExtractValue(m.Data);
     DataLength   = CalculateLength(Value, m.Header);
     Created      = m.MetaData.Timestamp;
     Revision     = m.MetaData.StreamSequence;
     Delta        = m.MetaData.NumPending;
     Operation    = KeyValueUtil.GetOperation(m.Header, KeyValueOperation.Put);
 }
Esempio n. 4
0
 public KeyValueEntry(MessageInfo mi)
 {
     bucketAndKey = new BucketAndKey(mi.Subject);
     Value        = ExtractValue(mi.Data);
     DataLength   = CalculateLength(Value, mi.Headers);
     Created      = mi.Time;
     Revision     = mi.Sequence;
     Delta        = 0;
     Operation    = KeyValueUtil.GetOperation(mi.Headers, KeyValueOperation.Put);
 }
Esempio n. 5
0
        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);
            }
        }
Esempio n. 6
0
        public IList <string> Keys()
        {
            IList <string> list = new List <string>();

            VisitSubject(RawKeySubject(">"), DeliverPolicy.LastPerSubject, true, false, m => {
                KeyValueOperation op = KeyValueUtil.GetOperation(m.Header, KeyValueOperation.Put);
                if (op.Equals(KeyValueOperation.Put))
                {
                    list.Add(new BucketAndKey(m).Key);
                }
            });
            return(list);
        }
Esempio n. 7
0
        public IList <string> GetBucketNames()
        {
            IList <string> buckets = new List <string>();
            IList <string> names   = jsm.GetStreamNames();

            foreach (string name in names)
            {
                if (name.StartsWith(KeyValueUtil.KvStreamPrefix))
                {
                    buckets.Add(KeyValueUtil.ExtractBucketName(name));
                }
            }
            return(buckets);
        }
Esempio n. 8
0
 /// <summary>
 /// Construct the builder by copying another configuration
 /// </summary>
 /// <param name="kvc">the other configuration</param>
 public KeyValueConfigurationBuilder(KeyValueConfiguration kvc)
 {
     if (kvc == null)
     {
         scBuilder = new StreamConfigurationBuilder();
         WithMaxHistoryPerKey(1);
         WithReplicas(1);
     }
     else
     {
         scBuilder = new StreamConfigurationBuilder(kvc.BackingConfig);
         _name     = KeyValueUtil.ExtractBucketName(kvc.BackingConfig.Name);
     }
 }
Esempio n. 9
0
 public void Delete(string bucketName)
 {
     Validator.ValidateKvBucketNameRequired(bucketName);
     jsm.DeleteStream(KeyValueUtil.ToStreamName(bucketName));
 }
Esempio n. 10
0
 public KeyValueStatus GetBucketInfo(string bucketName)
 {
     Validator.ValidateKvBucketNameRequired(bucketName);
     return(new KeyValueStatus(jsm.GetStreamInfo(KeyValueUtil.ToStreamName(bucketName))));
 }
Esempio n. 11
0
 internal KeyValueConfiguration(StreamConfiguration sc)
 {
     BackingConfig = sc;
     BucketName    = KeyValueUtil.ExtractBucketName(sc.Name);
 }
Esempio n. 12
0
 public KeyValueStatus Status()
 {
     return(new KeyValueStatus(jsm.GetStreamInfo(KeyValueUtil.ToStreamName(BucketName))));
 }