Esempio n. 1
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)));
			}
		}
Esempio n. 2
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)));
			}
		}
Esempio n. 3
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))));
			}
		}
Esempio n. 4
0
		public void CanDeleteIndex()
		{
			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")));

				tx.Write(mutator => mutator.Indexing.DeleteIndex("def"));
				tx.Read(viewer =>
					Assert.False(viewer.Indexing.GetIndexesStats().Any(x => x.Name == "def")));
			}
		}
Esempio n. 5
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)));
			}
		}
Esempio n. 6
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)));
			}
		}
Esempio n. 7
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));
			}
		}
Esempio n. 8
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)));
			}
		}
Esempio n. 9
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));
			}
		}
Esempio n. 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)));
			}
		}
Esempio n. 11
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()));
			}
		}
Esempio n. 12
0
		public void CanCountDocuments()
		{
			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject()));
				tx.Read(accessor => Assert.Equal(1, accessor.Documents.GetDocumentsCount()));
				JObject metadata;
				tx.Write(mutator => mutator.Documents.DeleteDocument("Ayende", null, out metadata));

				tx.Read(accessor => Assert.Equal(0, accessor.Documents.GetDocumentsCount()));

			}

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Read(viewer => Assert.Null(viewer.Documents.DocumentByKey("Ayende", null)));
				tx.Read(accessor => Assert.Equal(0, accessor.Documents.GetDocumentsCount()));
			}
		}
Esempio n. 13
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")));
			}
		}
Esempio n. 14
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);
			}
		}
Esempio n. 15
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"));
			}
		}
Esempio n. 16
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()));
			}
		}
Esempio n. 17
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)));
			}
		}
Esempio n. 18
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);
				});
			}
		}
Esempio n. 19
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);
				});
			}
		}
Esempio n. 20
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());
				});
			}
		}
Esempio n. 21
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));

				});
			}

		}
Esempio n. 22
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);
				});
			}
		}
Esempio n. 23
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)));
			
			}
		}
Esempio n. 24
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()));
			}
		}
Esempio n. 25
0
		public void AddingDocumentInTxThenAddingWithoutTxAfterTxExpiredWorks()
		{
			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 = "Rahien1" }), new JObject(),
																			   transactionInformation));
				tx.Write(mutator => mutator.Documents.AddDocument("Ayende", Guid.NewGuid(),
																			   JObject.FromObject(new { Name = "Rahien2" }),
																			   new JObject()));

				tx.Read(viewer =>
				{
					var doc  = viewer.Documents.DocumentByKey("Ayende", transactionInformation);
					Assert.Equal("Rahien2", doc.DataAsJson.Value<string>("Name"));
				});

			}
		}
Esempio n. 26
0
		public void CanDeleteDocumentInTransaction()
		{
			var transactionInformation = new TransactionInformation
			{
				Id = Guid.NewGuid(),
				Timeout = TimeSpan.FromDays(7)
			};

			using (var tx = new TransactionalStorage("test"))
			{
				tx.Write(mutator => mutator.Documents.AddDocument("Ayende", null, JObject.FromObject(new { Name = "Rahien" }), new JObject()));
				tx.Write(mutator => mutator.Transactions.DeleteDocumentInTransaction(transactionInformation, "Ayende", null));
				tx.Read(viewer =>
				{
					Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", null));
					Assert.Null(viewer.Documents.DocumentByKey("Ayende", transactionInformation));
				});
			}
		}
Esempio n. 27
0
		public void AddingDocInTxWillReadOldValueOutsideIt()
		{
			var transactionInformation = new TransactionInformation
			{
				Id = Guid.NewGuid(),
				Timeout = TimeSpan.FromDays(7)
			};

			using (var tx = new TransactionalStorage("test"))
			{

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

				tx.Write(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, JObject.FromObject(new { Name = "Rahien2" }), new JObject(),
					transactionInformation));

				tx.Read(viewer =>
				{
					var doc = viewer.Documents.DocumentByKey("Ayende", null);
					Assert.Equal("Rahien", doc.DataAsJson.Value<string>("Name"));
				});
			}
		}