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))); } }
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))); } }
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)))); } }
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"))); } }
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))); } }
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))); } }
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)); } }
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))); } }
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)); } }
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))); } }
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())); } }
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())); } }
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"))); } }
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); } }
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")); } }
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())); } }
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))); } }
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); }); } }
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); }); } }
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()); }); } }
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)); }); } }
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); }); } }
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))); } }
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())); } }
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")); }); } }
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)); }); } }
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")); }); } }