public long CreateSubscription(SubscriptionCriteria criteria)
        {
            long id = -1;

            Database.TransactionalStorage.Batch(accessor =>
            {
                id = accessor.General.GetNextIdentityValue(Constants.RavenSubscriptionsPrefix);

                var config = new SubscriptionConfig
                {
                    SubscriptionId = id,
                    Criteria = criteria,
                    AckEtag = criteria.StartEtag ?? Etag.Empty,
                };

                SaveSubscriptionConfig(id, config);
            });

            return id;
        }
        private static bool MatchCriteria(SubscriptionCriteria criteria, JsonDocument doc)
        {
            if (criteria.BelongsToAnyCollection != null &&
                criteria.BelongsToAnyCollection.Contains(doc.Metadata.Value<string>(Constants.RavenEntityName), StringComparer.InvariantCultureIgnoreCase) == false)
                return false;

            if (criteria.KeyStartsWith != null && doc.Key.StartsWith(criteria.KeyStartsWith) == false)
                return false;

            if (criteria.PropertiesMatch != null)
            {
                foreach (var match in criteria.PropertiesMatch)
                {
                    var tokens = doc.DataAsJson.SelectTokenWithRavenSyntaxReturningFlatStructure(match.Key).Select(x => x.Item1).ToArray();
                                    
                    foreach (var curVal in tokens)
                    {
                        if (RavenJToken.DeepEquals(curVal, match.Value) == false)
                            return false;
                    }

                    if (tokens.Length == 0)
                        return false;					
                }
            }

            if (criteria.PropertiesNotMatch != null)
            {
                foreach (var match in criteria.PropertiesNotMatch)
                {
                    var tokens = doc.DataAsJson.SelectTokenWithRavenSyntaxReturningFlatStructure(match.Key).Select(x => x.Item1).ToArray();
                                        
                    foreach (var curVal in tokens)
                    {
                        if (RavenJToken.DeepEquals(curVal, match.Value) == true)
                            return false;
                    }					
                }				
            }

            return true;
        }
Exemple #3
0
		public async Task StronglyTypedDataSubscriptions()
		{
			using (var store = NewDocumentStore())
			{
				using (var session = store.OpenSession())
				{
					for (int i = 0; i < 10; i++)
					{
						session.Store(new PersonWithAddress()
						{
							Name = "James",
							Address = new Address()
							{
								ZipCode = 12345
							}
						});

						session.Store(new PersonWithAddress()
						{
							Name = "James",
							Address = new Address()
							{
								ZipCode = 54321
							}
						});

						session.Store(new PersonWithAddress()
						{
							Name = "David",
							Address = new Address()
							{
								ZipCode = 12345
							}
						});

						session.Store(new Person());
					}

					session.SaveChanges();
				}

				var criteria = new SubscriptionCriteria<PersonWithAddress>();
				criteria.PropertyMatch(x => x.Name, "James");
				criteria.PropertyNotMatch(x => x.Address.ZipCode, 54321);

				var id = await store.AsyncSubscriptions.CreateAsync(criteria);

				var subscription = await store.AsyncSubscriptions.OpenAsync<PersonWithAddress>(id, new SubscriptionConnectionOptions());

				var users = new List<PersonWithAddress>();

				subscription.Subscribe(users.Add);

				Assert.True(SpinWait.SpinUntil(() => users.Count >= 10, TimeSpan.FromSeconds(60)));

				Assert.Equal(10, users.Count);

				foreach (var user in users)
				{
					Assert.Equal("James", user.Name);
					Assert.Equal(12345, user.Address.ZipCode);
				}
			}
		}
		private static bool MatchCriteria(SubscriptionCriteria criteria, JsonDocument doc)
		{
			if (criteria.BelongsToAnyCollection != null &&
			    criteria.BelongsToAnyCollection.Contains(doc.Metadata.Value<string>(Constants.RavenEntityName), StringComparer.InvariantCultureIgnoreCase) == false)
				return false;

			if (criteria.KeyStartsWith != null && doc.Key.StartsWith(criteria.KeyStartsWith) == false)
				return false;

			if (criteria.PropertiesMatch != null)
			{
				foreach (var match in criteria.PropertiesMatch)
				{
					var value = doc.DataAsJson.SelectToken(match.Key);

					if (value == null)
						return false;

					if (RavenJToken.DeepEquals(value, match.Value) == false)
						return false;
				}
			}

			if (criteria.PropertiesNotMatch != null)
			{
				foreach (var notMatch in criteria.PropertiesNotMatch)
				{
					var value = doc.DataAsJson.SelectToken(notMatch.Key);

					if (value != null)
					{
						if (RavenJToken.DeepEquals(value, notMatch.Value))
							return false;
					}
				}
			}

			return true;
		}