Exemplo n.º 1
0
		public void BulkInsertShouldNotUpdateDocsIfTheyHaveTheSameContent()
		{
			using (var store = CreateStore())
			{
				var keyEtagCollection = new Dictionary<string, Etag>();
				var entities = new List<User>();

				using (var session = store.OpenSession())
				{
					for (var i = 0; i < 10; i++)
					{
						var entity = new User()
						{
							Id = "users/" + i,
							Name = "Name/" + i
						};
						session.Store(entity);

						entities.Add(entity);
					}

					session.SaveChanges();

					foreach (var user in entities)
					{
						keyEtagCollection.Add(session.Advanced.GetDocumentId(user), session.Advanced.GetEtagFor(user));
					}
				}

				using (var bulk = store.BulkInsert(options: new BulkInsertOptions
				{
					OverwriteExisting = true,
					SkipOverwriteIfUnchanged = true
				}))
				{
					for (var i = 0; i < 10; i++)
					{
						var entity = new User()
						{
							Id = "users/" + i,
							Name = "Name/" + i + ((i % 2 == 0) ? " Changed" : string.Empty)
						};

						bulk.Store(entity);
					}
				}

				for (var i = 0; i < 10; i++)
				{
					var jsonDocument = store.DatabaseCommands.Get("users/" + i);

					if (i % 2 == 0)
						Assert.NotEqual(keyEtagCollection["users/" + i], jsonDocument.Etag);
					else
						Assert.Equal(keyEtagCollection["users/" + i], jsonDocument.Etag);
				}
			}
		}
Exemplo n.º 2
0
        public void CanWorkWithDateTimeOffset()
        {
            using (var store = NewDocumentStore())
            {
                store.DatabaseCommands.PutIndex("test", new IndexDefinition
                {
                    Map = @"from doc in docs select new { Time = doc[""@metadata""].time }"
                });

                using (var session = store.OpenSession())
                {
                    var entity = new User();
                    session.Store(entity);
                    session.Advanced.GetMetadataFor(entity)["time"] = new DateTimeOffset(2012, 11, 08, 11, 20, 0, TimeSpan.FromHours(2));
                    session.SaveChanges();
                }

                WaitForIndexing(store);
                WaitForUserToContinueTheTest(store);
                Assert.Empty(store.SystemDatabase.Statistics.Errors);
            }
        }
Exemplo n.º 3
0
		public void StronglyTypedSubscribtionWithStartEtag()
		{
			using (var store = NewDocumentStore())
			{
				var us1 = new User { Id = "users/1", Name = "john", Age = 22 };
				var us2 = new User { Id = "users/2", Name = "KY", Age = 30 };
				var us3 = new User { Id = "users/3", Name = "BEN", Age = 30 };
				var us4 = new User { Id = "users/4", Name = "Hila", Age = 29 };
				var us5 = new User { Id = "users/5", Name = "Revital", Age = 34 };

				using (var session = store.OpenSession())
				{
					session.Store(us1);
					session.Store(us2);
					session.Store(us3);
					session.Store(us4);
					session.Store(us5);
					session.SaveChanges();

					var user2Etag = session.Advanced.GetEtagFor(us2);
					var id = store.Subscriptions.Create(new SubscriptionCriteria<User>
					{
						StartEtag = user2Etag
					});

					var users = new List<User>();

					using (var subscription = store.Subscriptions.Open<User>(id, new SubscriptionConnectionOptions()))
					{

						var docs = new BlockingCollection<User>();
						var keys = new BlockingCollection<string>();
						var ages = new BlockingCollection<int>();

						subscription.Subscribe(x => keys.Add(x.Id));
						subscription.Subscribe(x => ages.Add(x.Age));

						subscription.Subscribe(docs.Add);

						User doc;
						Assert.True(docs.TryTake(out doc, waitForDocTimeout));
						users.Push(doc);
						Assert.True(docs.TryTake(out doc, waitForDocTimeout));
						users.Push(doc);
						Assert.True(docs.TryTake(out doc, waitForDocTimeout));
						users.Push(doc);
						var cnt = users.Count;
						Assert.Equal(3, cnt);


						string key;
						Assert.True(keys.TryTake(out key, waitForDocTimeout));
						Assert.Equal("users/3", key);

						Assert.True(keys.TryTake(out key, waitForDocTimeout));
						Assert.Equal("users/4", key);

						Assert.True(keys.TryTake(out key, waitForDocTimeout));
						Assert.Equal("users/5", key);

						int age;
						Assert.True(ages.TryTake(out age, waitForDocTimeout));
						Assert.Equal(30, age);

						Assert.True(ages.TryTake(out age, waitForDocTimeout));
						Assert.Equal(29, age);

						Assert.True(ages.TryTake(out age, waitForDocTimeout));
						Assert.Equal(34, age);
					}
				}
			}
		}
Exemplo n.º 4
0
        public void SubscribtionWithEtag_MultipleOpens()
        {
            using (var store = NewDocumentStore())
            {
                var us1 = new User { Id = "users/1", Name = "john", Age = 22 };
                var us2 = new User { Id = "users/2", Name = "KY", Age = 30 };
                var us3 = new User { Id = "users/3", Name = "BEN", Age = 30 };
                var us4 = new User { Id = "users/4", Name = "Hila", Age = 29 };
                var us5 = new User { Id = "users/5", Name = "Revital", Age = 34 };

                long subscriptionId;
                using (var session = store.OpenSession())
                {
                    session.Store(us1);
                    session.Store(us2);
                    session.Store(us3);
                    session.Store(us4);
                    session.Store(us5);
                    session.SaveChanges();

                    var user2Etag = session.Advanced.GetEtagFor(us2);
                    subscriptionId = store.Subscriptions.Create(new SubscriptionCriteria
                    {
                        StartEtag = user2Etag
                    });

                    var users = new List<RavenJObject>();

                    using (var subscription = store.Subscriptions.Open(subscriptionId, new SubscriptionConnectionOptions()))
                    {

                        var docs = new BlockingCollection<RavenJObject>();
                        var keys = new BlockingCollection<string>();
                        var ages = new BlockingCollection<int>();

                        subscription.Subscribe(x => keys.Add(x[Constants.Metadata].Value<string>("@id")));
                        subscription.Subscribe(x => ages.Add(x.Value<int>("Age")));

                        subscription.Subscribe(docs.Add);

                        RavenJObject doc;
                        Assert.True(docs.TryTake(out doc, waitForDocTimeout));
                        users.Push(doc);
                        Assert.True(docs.TryTake(out doc, waitForDocTimeout));
                        users.Push(doc);
                        Assert.True(docs.TryTake(out doc, waitForDocTimeout));
                        users.Push(doc);
                        var cnt = users.Count;
                        Assert.Equal(3, cnt);

                       
                        string key;
                        Assert.True(keys.TryTake(out key, waitForDocTimeout));
                        Assert.Equal("users/3", key);

                        Assert.True(keys.TryTake(out key, waitForDocTimeout));
                        Assert.Equal("users/4", key);

                        Assert.True(keys.TryTake(out key, waitForDocTimeout));
                        Assert.Equal("users/5", key);

                        int age;
                        Assert.True(ages.TryTake(out age, waitForDocTimeout));
                        Assert.Equal(30, age);

                        Assert.True(ages.TryTake(out age, waitForDocTimeout));
                        Assert.Equal(29, age);

                        Assert.True(ages.TryTake(out age, waitForDocTimeout));
                        Assert.Equal(34, age);
                    }
                }
                using (var subscription = store.Subscriptions.Open(subscriptionId, new SubscriptionConnectionOptions()))
                {
                    var docs = new BlockingCollection<RavenJObject>();

                    subscription.Subscribe(o => docs.Add(o));

                    RavenJObject item;
                    Assert.False(docs.TryTake(out item,TimeSpan.FromMilliseconds(250)));

                }
            }
        }
Exemplo n.º 5
0
        public void ShouldPurgeTombstones()
        {
            using (var store = NewDocumentStore())
            {
                SystemTime.UtcDateTime = () => DateTime.UtcNow.Subtract(store.Configuration.TombstoneRetentionTime);

                // create document
                string user1;
                using (var session = store.OpenSession())
                {
                    var user = new User
                    {
                        Name = "arek"
                    };
                    session.Store(user);

                    user1 = user.Id;
                    session.SaveChanges();
                }

                //now delete it to create tombstone
                using (var session = store.OpenSession())
                {
                    session.Delete(user1);
                    session.SaveChanges();
                }

                store.DocumentDatabase.TransactionalStorage.Batch(accessor =>
                {
                    var tombstone = accessor.Lists.Read(Constants.RavenPeriodicExportsDocsTombstones, user1);
                    Assert.NotNull(tombstone);
                    tombstone = accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, user1);
                    Assert.NotNull(tombstone);
                });

                SystemTime.UtcDateTime = () => DateTime.UtcNow;

                string user2;
                using (var session = store.OpenSession())
                {
                    var user = new User
                    {
                        Name = "ayende"
                    };
                    session.Store(user);

                    user2 = user.Id;

                    session.SaveChanges();
                }

                //now delete it to create tombstone
                using (var session = store.OpenSession())
                {
                    session.Delete(user2);
                    session.SaveChanges();
                }

                store.DocumentDatabase.Maintenance.PurgeOutdatedTombstones();

                store.DocumentDatabase.TransactionalStorage.Batch(accessor =>
                {
                    var tombstone = accessor.Lists.Read(Constants.RavenPeriodicExportsDocsTombstones, user1);
                    Assert.Null(tombstone);

                    tombstone = accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, user1);
                    Assert.Null(tombstone);

                    tombstone = accessor.Lists.Read(Constants.RavenPeriodicExportsDocsTombstones, user2);
                    Assert.NotNull(tombstone);

                    tombstone = accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, user2);
                    Assert.NotNull(tombstone);
                });

            }
        }