Наследование: ITransactionalStorage
Пример #1
0
		public void CanModifyTxId()
		{
			var transactionInformation = new TransactionInformation
			{
				Id = Guid.NewGuid(),
				Timeout = TimeSpan.FromDays(7)
			};

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject(),
					transactionInformation));

				var txInfo2 = new TransactionInformation
				{
					Id = Guid.NewGuid(),
					Timeout = TimeSpan.FromDays(1)
				};

				tx.Write(mutator => mutator.Transactions.ModifyTransactionId(transactionInformation.Id, txInfo2.Id, txInfo2.Timeout));


				tx.Read(viewer =>
					Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", txInfo2)));
			}
		}
Пример #2
0
		public void CanHandleTruncatedFile()
		{
			var fileName = Path.Combine("test", "storage.raven");
			long lengthAfterFirstTransaction;
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject()));

				lengthAfterFirstTransaction = new FileInfo(fileName).Length;

				tx.Write(mutator => mutator.Documents.AddDocument("Oren", null, JObject.FromObject(new { Name = "Eini" }), new JObject()));

			}

			using (var fileStream = File.Open(fileName, FileMode.Open))//simulate crash in the middle of a transaction write
			{
				fileStream.SetLength(lengthAfterFirstTransaction + (fileStream.Length - lengthAfterFirstTransaction) / 2);
			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer => Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", null)));
				tx.Read(viewer => Assert.Null(viewer.Documents.DocumentByKey("Oren", null)));
			}
		}
Пример #3
0
		public void CanRecordAttemptsDecrements()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Indexing.AddIndex("def"));
				tx.Write(mutator =>
				{
					mutator.Indexing.SetCurrentIndexStatsTo("def");

					mutator.Indexing.IncrementIndexingAttempt();

					mutator.Indexing.FlushIndexStats();
				});
				tx.Read(viewer =>
					Assert.Equal(1, viewer.Indexing.GetFailureRate("def").Attempts));

				tx.Write(mutator =>
				{
					mutator.Indexing.SetCurrentIndexStatsTo("def");

					mutator.Indexing.DecrementIndexingAttempt();

					mutator.Indexing.FlushIndexStats();
				});
				tx.Read(viewer =>
					Assert.Equal(0, viewer.Indexing.GetFailureRate("def").Attempts));
			}
		}
Пример #4
0
		public void AfterCommittingCanSeeChangesWithoutTx()
		{
			var transactionInformation = new TransactionInformation
			{
				Id = Guid.NewGuid(),
				Timeout = TimeSpan.FromDays(7)
			};

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject(),
					transactionInformation));

				tx.Write(mutator => mutator.Transactions.CompleteTransaction(transactionInformation.Id, data =>
				{
					if (data.Delete)
					{
						JObject metadata;
						mutator.Documents.DeleteDocument(data.Key, null, out metadata);
					}
					else
						mutator.Documents.AddDocument(data.Key, null, data.Data, data.Metadata);
				}));
				tx.Read(viewer =>
					Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", null)));
			}
		}
Пример #5
0
		public void CanGetDocumentByUpdateOrder()
		{

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator =>
				{
					mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject());
					mutator.Documents.AddDocument("Oren", null, JObject.FromObject(new { Name = "Eini" }), new JObject());
				});
			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer =>
				{
					Assert.Equal(2, viewer.Documents.GetDocumentsByReverseUpdateOrder(0).Count());
					var tuples = viewer.Documents.GetDocumentsByReverseUpdateOrder(0).ToArray();
					Assert.Equal(2, tuples.Length);
					Assert.Equal("Oren", tuples[0].Key);
					Assert.Equal("Ayende", tuples[1].Key);

					Assert.Equal(1, viewer.Documents.GetDocumentsByReverseUpdateOrder(1).Count());
					tuples = viewer.Documents.GetDocumentsByReverseUpdateOrder(1).ToArray();
					Assert.Equal("Ayende", tuples[0].Key);
				});
			}
		}
Пример #6
0
		public void CanAddAndReadIndexFailureRate()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Indexing.AddIndex("def"));
				tx.Read(viewer =>
					Assert.Equal("def", viewer.Indexing.GetFailureRate("def").Name));
			}
		}
Пример #7
0
		public void CanCheckForExistanceOfTasks()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer => Assert.False(viewer.Tasks.DoesTasksExistsForIndex("test", null))); 
				tx.Write(mutator => mutator.Tasks.AddTask(new MyTask { Index = "test" }));
				tx.Read(viewer => Assert.True(viewer.Tasks.DoesTasksExistsForIndex("test", null)));
			}
		}
Пример #8
0
		public void CanGetTask()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Tasks.AddTask(new MyTask { Index = "test" }));
				int tasks;
				tx.Write(mutator => Assert.NotNull(mutator.Tasks.GetMergedTask(out tasks)));
			}
		}
Пример #9
0
		public void CanAddAndReadIndex()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Indexing.AddIndex("def"));
				tx.Read(viewer =>
					Assert.True(viewer.Indexing.GetIndexesStats().Any(x => x.Name == "def")));
			}
		}
Пример #10
0
		public void CanStoreAndGetMappedResult()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.MappedResults.PutMappedResult("test", "users/ayende","ayende", JObject.FromObject(new { Name = "Rahien" }), null));

				tx.Read(viewer => Assert.NotEmpty(viewer.MappedResults.GetMappedResults("test", "ayende", null)));
			}
		}
Пример #11
0
		public void CanEnqueueAndPeek()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Queue.EnqueueToQueue("ayende", new byte[] {1, 2}));

				tx.Write(
					mutator => Assert.Equal(new byte[] {1, 2}, mutator.Queue.PeekFromQueue("ayende").First().Item1));
			}
		}
Пример #12
0
		public void AfterGettingTaskOnceWillNotGetItAgain()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Tasks.AddTask(new MyTask { Index = "test" }));
				int tasks;
				tx.Write(mutator => Assert.NotNull(mutator.Tasks.GetMergedTask(out tasks)));
				tx.Write(mutator => Assert.Null(mutator.Tasks.GetMergedTask(out tasks)));
			}
		}
Пример #13
0
		public void CanHaveTwoResultsForSameDoc()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.MappedResults.PutMappedResult("test", "users/ayende", "ayende", JObject.FromObject(new { Name = "Rahien" }), null));
				tx.Write(mutator => mutator.MappedResults.PutMappedResult("test", "users/ayende", "ayende", JObject.FromObject(new { Name = "Rahien" }), null));

				tx.Read(viewer => Assert.Equal(2, viewer.MappedResults.GetMappedResults("test", "ayende", null).Count()));
			}
		}
Пример #14
0
		public void CanCheckForExistanceOfTasksWithCutOffs()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				var cutoff = DateTime.UtcNow;
				tx.Write(mutator => mutator.Tasks.AddTask(new MyTask { Index = "test" }));
				tx.Read(viewer => Assert.True(viewer.Tasks.DoesTasksExistsForIndex("test", null)));
				tx.Read(viewer => Assert.True(viewer.Tasks.DoesTasksExistsForIndex("test", cutoff.AddMinutes(1))));
				tx.Read(viewer => Assert.False(viewer.Tasks.DoesTasksExistsForIndex("test", cutoff.AddMinutes(-1))));
			}
		}
Пример #15
0
		public void CanDeleteQueuedData()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Queue.EnqueueToQueue("ayende", new byte[] { 1, 2 }));

				tx.Write(mutator => 
				{
					mutator.Queue.DeleteFromQueue("ayende", mutator.Queue.PeekFromQueue("ayende").First().Item2);
					Assert.Equal(null, mutator.Queue.PeekFromQueue("ayende").FirstOrDefault());
				});
			}
		}
Пример #16
0
		public void CanDeleteAttachment()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(accessor => accessor.Attachments.AddAttachment("Ayende", null, new byte[] { 1, 2, 3 }, new JObject()));
				tx.Write(accessor => accessor.Attachments.DeleteAttachment("Ayende", null));
			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer => Assert.Null(viewer.Attachments.GetAttachment("Ayende")));
			}
		}
Пример #17
0
		public void CanCheckForExistanceOfTasksAfterTaskWasRemoved()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer => Assert.False(viewer.Tasks.DoesTasksExistsForIndex("test", null)));
				tx.Write(mutator => mutator.Tasks.AddTask(new MyTask { Index = "test" }));
				tx.Read(viewer => Assert.True(viewer.Tasks.DoesTasksExistsForIndex("test", null))); 
				
				int tasks = 0;
				tx.Write(mutator => mutator.Tasks.GetMergedTask(out tasks));
				Assert.Equal(1, tasks);
				tx.Read(viewer => Assert.False(viewer.Tasks.DoesTasksExistsForIndex("test", null)));
			}
		}
Пример #18
0
		public void CanAddAndRead()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject()));

				JObject document = null;
				tx.Read(viewer =>
				{
					document = viewer.Documents.DocumentByKey("Ayende", null).DataAsJson;
				});

				Assert.Equal("Rahien", document.Value<string>("Name"));
			}
		}
Пример #19
0
		public void CanAddAndReadAttachments()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(accessor => accessor.Attachments.AddAttachment("Ayende", null, new byte[] { 1, 2, 3 }, new JObject()));

				Attachment attachment = null;
				tx.Read(viewer =>
				{
					attachment = viewer.Attachments.GetAttachment("Ayende");
				});

				Assert.Equal(new byte[] { 1, 2, 3 }, attachment.Data);
			}
		}
Пример #20
0
		public void CanStoreAndGetMappedResultWithSeveralResultsForSameReduceKey()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator =>
				{
					mutator.MappedResults.PutMappedResult("test", "users/ayende", "ayende", JObject.FromObject(new {Name = "Rahien"}),
					                                      null);
					mutator.MappedResults.PutMappedResult("test", "users/rahien", "ayende", JObject.FromObject(new { Name = "Rahien" }),
														  null);
				});

				tx.Read(viewer => Assert.Equal(2, viewer.MappedResults.GetMappedResults("test", "ayende", null).Count()));
			}
		}
Пример #21
0
		public void CanDeleteFile()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject()));
				JObject metadata;
				tx.Write(mutator => mutator.Documents.DeleteDocument("Ayende", null, out metadata));
			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer => Assert.Null(viewer.Documents.DocumentByKey("Ayende", null)));

			}
		}
Пример #22
0
		public void PoisonMessagesWillBeDeleted()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Queue.EnqueueToQueue("ayende", new byte[] {1, 2}));

				tx.Write(mutator =>
				{
					for (int i = 0; i < 5; i++)
					{
						mutator.Queue.PeekFromQueue("ayende").First();
					}
					Assert.Equal(null, mutator.Queue.PeekFromQueue("ayende").FirstOrDefault());
				});
			}
		}
Пример #23
0
		public void AddingDocInTxCannotBeReadOutside()
		{
			var transactionInformation = new TransactionInformation
			{
				Id = Guid.NewGuid(),
				Timeout = TimeSpan.FromDays(7)
			};

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject(),
					transactionInformation));

				tx.Read(viewer => 
					Assert.Null(viewer.Documents.DocumentByKey("Ayende", null)));
			}
		}
Пример #24
0
		public void CanGetDocumentIds()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator =>
				{
					mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject());
					mutator.Documents.AddDocument("Oren", null, JObject.FromObject(new { Name = "Eini" }), new JObject());
				});
			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer =>
				{
					var firstAndLastDocumentIds = viewer.Documents.FirstAndLastDocumentIds();
					Assert.Equal(1, firstAndLastDocumentIds.Item1);
					Assert.Equal(2, firstAndLastDocumentIds.Item2);
				});
			}
		}
Пример #25
0
		public void CanGetDocumentByDocIds()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator =>
				{
					mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject());
					mutator.Documents.AddDocument("Oren", null, JObject.FromObject(new { Name = "Eini" }), new JObject());
				});
			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer =>
				{
					var tuples = viewer.Documents.DocumentsById(1, 2).ToArray();
					Assert.Equal(2, tuples.Length);
					Assert.Equal("Ayende", tuples[0].Item1.Key);
					Assert.Equal("Oren", tuples[1].Item1.Key);
				});
			}
		}
Пример #26
0
		public void CanGetDocumentByEtag()
		{

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator =>
				{
					mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject());
					mutator.Documents.AddDocument("Oren", null, JObject.FromObject(new { Name = "Eini" }), new JObject());
				});
			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer =>
				{
					Assert.Equal(2, viewer.Documents.GetDocumentsAfter(Guid.Empty).Count());
					var doc1 = viewer.Documents.DocumentByKey("Ayende", null);
					Assert.Equal(1, viewer.Documents.GetDocumentsAfter(doc1.Etag).Count());
				});
			}
		}
Пример #27
0
		public void EtagsAreAlwaysIncreasing()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator =>
				{
					mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject());
					mutator.Documents.AddDocument("Oren", null, JObject.FromObject(new { Name = "Eini" }), new JObject());
				});
			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer =>
				{
					var doc1 = viewer.Documents.DocumentByKey("Ayende", null);
					var doc2 = viewer.Documents.DocumentByKey("Oren", null);
					Assert.Equal(1, doc2.Etag.CompareTo(doc1.Etag));

				});
			}

		}
Пример #28
0
		public void CanGetNewIdentityValue()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator =>
				{
					Assert.Equal(1, mutator.General.GetNextIdentityValue("ayende"));
					Assert.Equal(1, mutator.General.GetNextIdentityValue("rahien"));

					Assert.Equal(2, mutator.General.GetNextIdentityValue("ayende"));
					Assert.Equal(2, mutator.General.GetNextIdentityValue("rahien"));	
				});


				tx.Write(mutator =>
				{
					Assert.Equal(3, mutator.General.GetNextIdentityValue("ayende"));
					Assert.Equal(3, mutator.General.GetNextIdentityValue("rahien"));

					Assert.Equal(4, mutator.General.GetNextIdentityValue("ayende"));
					Assert.Equal(4, mutator.General.GetNextIdentityValue("rahien"));
				});
			}
		}
Пример #29
0
		public void CanGetTxIdValues()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				var txId = Guid.NewGuid();
				tx.Write(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, new JObject(), new JObject(), new TransactionInformation
				{
					Id = txId,
					Timeout = TimeSpan.FromDays(7)
				}));


				tx.Read(viewer =>
					Assert.Equal(new[] { txId }, viewer.Transactions.GetTransactionIds().ToArray()));
			}
		}
Пример #30
0
		public void AfterRollbackCannotSeeChangesEvenInSameTxId()
		{
			var transactionInformation = new TransactionInformation
			{
				Id = Guid.NewGuid(),
				Timeout = TimeSpan.FromDays(7)
			};

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject(),
					transactionInformation));

				tx.Read(viewer =>
					Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", transactionInformation)));
			
				tx.Write(mutator => mutator.Transactions.RollbackTransaction(transactionInformation.Id));

				tx.Read(viewer =>
					Assert.Null(viewer.Documents.DocumentByKey("Ayende", transactionInformation)));
			
			}
		}