public ConsulKeyValueStore(IStoreConfiguration storeConfig, IKeyValueWatcher watcher, IInitialKeyValuesProvider <T> provider, ILogger log)
        {
            this.StoreConfig = storeConfig;
            this.Watcher     = watcher;
            this.Provider    = provider;
            this.Log         = log;

            this.LoadValuesAndWatchAsync().Wait();
        }
Example #2
0
 public ConsulStringKeyValueStore(IStoreConfiguration storeConfig, IKeyValueWatcher watcher, IInitialKeyValuesProvider <string> provider, ILogger log)
     : base(storeConfig, watcher, provider, log)
 {
 }
Example #3
0
 public KeyValueWatchSubscription WatchAll(IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions)
 {
     Validator.ValidateNotNull(watcher, "Watcher is required");
     return(new KeyValueWatchSubscription(this, ">", watcher, watchOptions));
 }
Example #4
0
 public KeyValueWatchSubscription Watch(string key, IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions)
 {
     Validator.ValidateKvKeyWildcardAllowedRequired(key);
     Validator.ValidateNotNull(watcher, "Watcher is required");
     return(new KeyValueWatchSubscription(this, key, watcher, watchOptions));
 }
        public KeyValueWatchSubscription(KeyValue kv, string keyPattern,
                                         IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions)
        {
            string subject = kv.RawKeySubject(keyPattern);

            // figure out the result options
            bool          headersOnly    = false;
            bool          includeDeletes = true;
            DeliverPolicy deliverPolicy  = DeliverPolicy.LastPerSubject;

            foreach (KeyValueWatchOption wo in watchOptions)
            {
                switch (wo)
                {
                case KeyValueWatchOption.MetaOnly: headersOnly = true; break;

                case KeyValueWatchOption.IgnoreDelete: includeDeletes = false; break;

                case KeyValueWatchOption.UpdatesOnly: deliverPolicy = DeliverPolicy.New; break;

                case KeyValueWatchOption.IncludeHistory: deliverPolicy = DeliverPolicy.All; break;
                }
            }

            if (deliverPolicy == DeliverPolicy.New)
            {
                endOfDataSent = new InterlockedBoolean(true);
                watcher.EndOfData();
            }
            else
            {
                KeyValueEntry kveCheckPending = kv._kvGetLastMessage(keyPattern);
                if (kveCheckPending == null)
                {
                    endOfDataSent = new InterlockedBoolean(true);
                    watcher.EndOfData();
                }
                else
                {
                    endOfDataSent = new InterlockedBoolean(false);
                }
            }

            PushSubscribeOptions pso = PushSubscribeOptions.Builder()
                                       .WithStream(kv.StreamName)
                                       .WithOrdered(true)
                                       .WithConfiguration(
                ConsumerConfiguration.Builder()
                .WithAckPolicy(AckPolicy.None)
                .WithDeliverPolicy(deliverPolicy)
                .WithHeadersOnly(headersOnly)
                .WithFilterSubject(subject)
                .Build())
                                       .Build();

            EventHandler <MsgHandlerEventArgs> handler = (sender, args) =>
            {
                KeyValueEntry kve = new KeyValueEntry(args.msg);
                if (includeDeletes || kve.Operation.Equals(KeyValueOperation.Put))
                {
                    watcher.Watch(kve);
                }

                if (endOfDataSent.IsFalse() && kve.Delta == 0)
                {
                    endOfDataSent.Set(true);
                    watcher.EndOfData();
                }
            };

            sub = kv.js.PushSubscribeAsync(subject, handler, false, pso);
            if (endOfDataSent.IsFalse())
            {
                ulong pending = sub.GetConsumerInformation().CalculatedPending;
                if (pending == 0)
                {
                    endOfDataSent.Set(true);
                    watcher.EndOfData();
                }
            }
        }