Пример #1
0
        public void Can_update_alert()
        {
            using (var database = CreateDocumentDatabase())
                using (var _ctx = DocumentsOperationContext.ShortTermSingleUse(database))
                {
                    var alert1 = SampleAlert();

                    database.Alerts.AddAlert(alert1);

                    var alert2 = SampleAlert();
                    alert2.Message = "Updated message";
                    database.Alerts.AddAlert(alert2);

                    List <Alert> alerts;

                    using (var ms = new MemoryStream())
                    {
                        using (var writer = new BlittableJsonTextWriter(_ctx, ms))
                        {
                            database.Alerts.WriteAlerts(writer);
                        }

                        ms.Position = 0;

                        alerts = DeserializeAlerts(ms);
                    }

                    Assert.Equal(1, alerts.Count);
                    var loadedAlert = alerts[0];
                    Assert.Equal(alert2.Message, loadedAlert.Message);
                }
        }
Пример #2
0
        public async Task Should_use_auto_index_even_if_idle_when_match_is_complete()
        {
            using (var database = CreateDocumentDatabase())
            {
                var autoIndex = await database.IndexStore.CreateIndex(new AutoMapIndexDefinition("Users", new[]
                {
                    new AutoIndexField
                    {
                        Name = "FirstName",
                    },
                    new AutoIndexField
                    {
                        Name = "LastName",
                    }
                }));

                autoIndex.SetState(IndexState.Idle);

                using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
                {
                    // it shouldn't throw
                    await database.QueryRunner.ExecuteQuery(new IndexQueryServerSide("from Users where LastName = 'Arek'"), context, null,
                                                            OperationCancelToken.None);
                }

                var sameIndex = database.IndexStore.GetIndex(autoIndex.Name);

                Assert.Same(autoIndex, sameIndex);
                Assert.Equal(IndexState.Normal, sameIndex.State);
            }
        }
Пример #3
0
        public void PutAndGetDocumentById(string id)
        {
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = id
                }, id, BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    _documentDatabase.DocumentsStorage.Put(ctx, id, null, doc);
                }
                ctx.Transaction.Commit();
            }

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                var document = _documentDatabase.DocumentsStorage.Get(ctx, id);
                Assert.NotNull(document);
                Assert.Equal(1, document.Etag);
                Assert.Equal(id, document.Id);
                string name;
                document.Data.TryGet("Name", out name);
                Assert.Equal(id, name);

                ctx.Transaction.Commit();
            }
        }
Пример #4
0
        public void Update_should_retain_dismissed_date()
        {
            using (var database = CreateDocumentDatabase())
                using (var _ctx = DocumentsOperationContext.ShortTermSingleUse(database))
                {
                    var alert1 = SampleAlert();
                    alert1.DismissedUntil = new DateTime(2014, 10, 2);

                    database.Alerts.AddAlert(alert1);

                    var alert2 = SampleAlert();
                    database.Alerts.AddAlert(alert2);

                    List <Alert> alerts;

                    using (var ms = new MemoryStream())
                    {
                        using (var writer = new BlittableJsonTextWriter(_ctx, ms))
                        {
                            database.Alerts.WriteAlerts(writer);
                        }

                        ms.Position = 0;

                        alerts = DeserializeAlerts(ms);
                    }

                    Assert.Equal(1, alerts.Count);
                    var loadedAlert = alerts[0];
                    Assert.Equal(alert1.DismissedUntil, loadedAlert.DismissedUntil);
                }
        }
Пример #5
0
        public async Task By_single_array_object()
        {
            using (var database = CreateDocumentDatabase())
            {
                using (var index = MapReduceIndex.CreateNew(new IndexDefinition()
                {
                    Etag = 10,
                    Name = "Users_GroupByHobbies",
                    Maps = { @"from user in docs.Users select new { 
                                user.Hobbies,
                                Count = 1
                            }" },
                    Reduce = @"from result in results group result by result.Hobbies into g select new { 
                                Hobbies = g.Key, 
                                Count = g.Sum(x => x.Count)
                            }",
                    Fields = new Dictionary <string, IndexFieldOptions>()
                    {
                        { "Hobbies", new IndexFieldOptions()
                          {
                              Indexing = FieldIndexing.Search,
                          } }
                    }
                }, database))
                {
                    DocumentQueryResult queryResult;
                    using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
                    {
                        Put_docs(context, database);

                        var batchStats = new IndexingRunStats();
                        var scope      = new IndexingStatsScope(batchStats);
                        index.DoIndexingWork(scope, CancellationToken.None);

                        queryResult =
                            await index.Query(new IndexQueryServerSide($"FROM INDEX '{index.Name}'"), context, OperationCancelToken.None);

                        Assert.Equal(2, queryResult.Results.Count);
                    }
                    using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
                    {
                        queryResult = await index.Query(new IndexQueryServerSide($"FROM INDEX '{index.Name}' WHERE Hobbies = 'music'"), context, OperationCancelToken.None);

                        var results = queryResult.Results;

                        Assert.Equal(1, results.Count);

                        Assert.Equal(1, queryResult.Results.Count);
                        Assert.Equal("music", ((BlittableJsonReaderArray)results[0].Data["Hobbies"])[0].ToString());
                        Assert.Equal("sport", ((BlittableJsonReaderArray)results[0].Data["Hobbies"])[1].ToString());
                        Assert.Equal(2L, results[0].Data["Count"]);

                        foreach (var document in results)
                        {
                            document.Data.Dispose();
                        }
                    }
                }
            }
        }
Пример #6
0
        public void Can_delete_alert()
        {
            using (var database = CreateDocumentDatabase())
                using (var _ctx = DocumentsOperationContext.ShortTermSingleUse(database))
                {
                    var alert1 = SampleAlert();

                    database.Alerts.AddAlert(alert1);

                    database.Alerts.DeleteAlert(alert1.Type, alert1.Key);

                    List <Alert> alerts;

                    using (var ms = new MemoryStream())
                    {
                        using (var writer = new BlittableJsonTextWriter(_ctx, ms))
                        {
                            database.Alerts.WriteAlerts(writer);
                        }

                        ms.Position = 0;

                        alerts = DeserializeAlerts(ms);
                    }

                    Assert.Equal(0, alerts.Count);
                }
        }
Пример #7
0
        public void CanDelete(string id)
        {
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = id
                }, id, BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    _documentDatabase.DocumentsStorage.Put(ctx, id, null, doc);
                }
                ctx.Transaction.Commit();
            }

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                _documentDatabase.DocumentsStorage.Delete(ctx, id, null);

                ctx.Transaction.Commit();
            }

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                var document = _documentDatabase.DocumentsStorage.Get(ctx, id);
                Assert.Null(document);

                ctx.Transaction.Commit();
            }
        }
        public static void CreateUsers(DocumentDatabase db, long numberOfUsers, params string[] locations)
        {
            using (var context = DocumentsOperationContext.ShortTermSingleUse(db))
            {
                using (var tx = context.OpenWriteTransaction())
                {
                    for (int i = 0; i < numberOfUsers; i++)
                    {
                        using (var doc = context.ReadObject(new DynamicJsonValue
                        {
                            ["Name"] = $"User-{i}",
                            ["Location"] = locations[i % locations.Length],
                            ["Age"] = 20 + i,
                            [Constants.Documents.Metadata.Key] = new DynamicJsonValue
                            {
                                [Constants.Documents.Metadata.Collection] = "Users"
                            }
                        }, $"users/{i}"))
                        {
                            db.DocumentsStorage.Put(context, $"users/{i}", null, doc);
                        }
                    }

                    tx.Commit();
                }
            }
        }
Пример #9
0
        public void CanQueryByGlobalEtag()
        {
            Initialize();
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Users"
                    }
                }, "users/1", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    _documentDatabase.DocumentsStorage.Put(ctx, "users/1", null, doc);
                }
                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Ayende",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Users"
                    }
                }, "users/2", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    _documentDatabase.DocumentsStorage.Put(ctx, "users/2", null, doc);
                }
                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Arava",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Dogs"
                    }
                }, "pets/1", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    _documentDatabase.DocumentsStorage.Put(ctx, "pets/1", null, doc);
                }
                ctx.Transaction.Commit();
            }

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                var documents = _documentDatabase.DocumentsStorage.GetDocumentsFrom(ctx, 0, 0, 100).ToList();
                Assert.Equal(3, documents.Count);
                string name;
                documents[0].Data.TryGet("Name", out name);
                Assert.Equal("Oren", name);
                documents[1].Data.TryGet("Name", out name);
                Assert.Equal("Ayende", name);
                documents[2].Data.TryGet("Name", out name);
                Assert.Equal("Arava", name);

                ctx.Transaction.Commit();
            }
        }
Пример #10
0
        public void MaxNumberOfDocsToDeleteParameterShouldBeRespected()
        {
            using (var db = CreateDocumentDatabase())
            {
                using (var ctx = DocumentsOperationContext.ShortTermSingleUse(db))
                {
                    using (var tx = ctx.OpenWriteTransaction())
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            using (var doc = CreateDocument(ctx, $"users/{i}",
                                                            new DynamicJsonValue
                            {
                                ["Name"] = "John", [Constants.Documents.Metadata.Key] = new DynamicJsonValue {
                                    [Constants.Documents.Metadata.Collection] = "Users"
                                }
                            }))
                            {
                                db.DocumentsStorage.Put(ctx, $"users/{i}", null, doc);
                            }
                        }

                        tx.Commit();
                    }
                }

                using (var ctx = DocumentsOperationContext.ShortTermSingleUse(db))
                {
                    using (var tx = ctx.OpenWriteTransaction())
                    {
                        // should delete all
                        var result = db.DocumentsStorage.DeleteDocumentsStartingWith(ctx, "users/");

                        Assert.Equal(10, result.Count);

                        // intentionally not committing
                    }

                    using (var tx = ctx.OpenWriteTransaction())
                    {
                        var result = db.DocumentsStorage.DeleteDocumentsStartingWith(ctx, "users/", maxDocsToDelete: 1);

                        Assert.Equal(1, result.Count);

                        result = db.DocumentsStorage.DeleteDocumentsStartingWith(ctx, "users/", maxDocsToDelete: 2);

                        Assert.Equal(2, result.Count);

                        result = db.DocumentsStorage.DeleteDocumentsStartingWith(ctx, "users/", maxDocsToDelete: 5);

                        Assert.Equal(5, result.Count);

                        result = db.DocumentsStorage.DeleteDocumentsStartingWith(ctx, "users/");

                        Assert.Equal(2, result.Count);
                    }
                }
            }
        }
Пример #11
0
        public async Task CanGroupByNestedFieldAndAggregateOnCollection()
        {
            using (var db = CreateDocumentDatabase())
                using (var mri = AutoMapReduceIndex.CreateNew(1, new AutoMapReduceIndexDefinition(
                                                                  new [] { "Orders" },
                                                                  new []
                {
                    new IndexField
                    {
                        Name = "Lines,Quantity",
                        MapReduceOperation = FieldMapReduceOperation.Sum,
                        Storage = FieldStorage.Yes
                    },
                    new IndexField
                    {
                        Name = "Lines,Price",
                        MapReduceOperation = FieldMapReduceOperation.Sum,
                        Storage = FieldStorage.Yes
                    }
                },
                                                                  new []
                {
                    new IndexField
                    {
                        Name = "ShipTo.Country",
                        Storage = FieldStorage.Yes
                    },
                }), db))
                {
                    CreateOrders(db, 5, new[] { "Poland", "Israel" });

                    mri.DoIndexingWork(new IndexingStatsScope(new IndexingRunStats()), CancellationToken.None);

                    using (var context = DocumentsOperationContext.ShortTermSingleUse(db))
                    {
                        var queryResult = await mri.Query(new IndexQueryServerSide()
                        {
                            Query = "ShipTo_Country:Poland"
                        }, context, OperationCancelToken.None);

                        Assert.Equal(1, queryResult.Results.Count);
                        var result = queryResult.Results[0].Data;

                        string location;
                        Assert.True(result.TryGet("ShipTo.Country", out location));
                        Assert.Equal("Poland", location);

                        var price = result["Lines,Price"] as LazyDoubleValue;

                        Assert.NotNull(price);

                        Assert.Equal(63.6, price, 1);

                        var quantity = result["Lines,Quantity"];

                        Assert.Equal(9L, quantity);
                    }
                }
        }
Пример #12
0
        public void CanQueryByPrefix()
        {
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["Raven-Entity-Name"] = "Users"
                    }
                }, "users/10", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    _documentDatabase.DocumentsStorage.Put(ctx, "users/10", null, doc);
                }
                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Ayende",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["Raven-Entity-Name"] = "Users"
                    }
                }, "users/02", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    _documentDatabase.DocumentsStorage.Put(ctx, "users/02", null, doc);
                }
                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Arava",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["Raven-Entity-Name"] = "Dogs"
                    }
                }, "pets/1", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    _documentDatabase.DocumentsStorage.Put(ctx, "pets/1", null, doc);
                }
                ctx.Transaction.Commit();
            }

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                var documents = _documentDatabase.DocumentsStorage.GetDocumentsStartingWith(ctx, "users/", null, null, 0, 100).ToList();
                Assert.Equal(2, documents.Count);
                string name;

                documents[0].Data.TryGet("Name", out name);
                Assert.Equal("Ayende", name);
                documents[1].Data.TryGet("Name", out name);
                Assert.Equal("Oren", name);
                ctx.Transaction.Commit();
            }
        }
Пример #13
0
        public async Task By_single_complex_object()
        {
            using (var database = CreateDocumentDatabase())
            {
                using (var index = MapReduceIndex.CreateNew(1, new IndexDefinition()
                {
                    Name = "Users_ByCount_GroupByLocation",
                    Maps = { @"from user in docs.Users select new { 
                                user.Location,
                                Count = 1
                            }" },
                    Reduce = @"from result in results group result by result.Location into g select new { 
                                Location = g.Key, 
                                Count = g.Sum(x => x.Count)
                            }",
                    Fields = new Dictionary <string, IndexFieldOptions>()
                    {
                        { "Location", new IndexFieldOptions()
                          {
                              Indexing = FieldIndexing.Analyzed,
                          } }
                    }
                }, database))
                {
                    using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
                    {
                        Put_docs(context, database);

                        var batchStats = new IndexingRunStats();
                        var scope      = new IndexingStatsScope(batchStats);
                        index.DoIndexingWork(scope, CancellationToken.None);

                        var queryResult = await index.Query(new IndexQueryServerSide(), context, OperationCancelToken.None);

                        Assert.Equal(2, queryResult.Results.Count);

                        context.ResetAndRenew();

                        queryResult = await index.Query(new IndexQueryServerSide()
                        {
                            Query = @"Location:Poland"
                        }, context, OperationCancelToken.None);

                        var results = queryResult.Results;

                        Assert.Equal(1, results.Count);

                        Assert.Equal(1, queryResult.Results.Count);
                        Assert.Equal(@"{""Country"":""Poland"",""State"":""Pomerania""}", results[0].Data["Location"].ToString());
                        Assert.Equal(2L, results[0].Data["Count"]);
                    }
                }
            }
        }
Пример #14
0
        public void WillVerifyEtags_OnDeleteNotThere()
        {
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                Assert.Throws <ConcurrencyException>(() => _documentDatabase.DocumentsStorage.Delete(ctx, "users/1", 3));

                ctx.Transaction.Commit();
            }
        }
Пример #15
0
        public void GetSliceFromId_WhenEmptyLazyString_ShouldNotThrow()
        {
            using var ctx = DocumentsOperationContext.ShortTermSingleUse(null);
            const string str        = "";
            var          lazyString = ctx.GetLazyString(str);

            using (DocumentIdWorker.GetSliceFromId(ctx, lazyString, out var lowerId))
            {
                Assert.Equal(str.ToLower(), lowerId.ToString());
            }
        }
Пример #16
0
        public void WillVerifyEtags_OnDeleteNotThere()
        {
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();
                var changeVector = ctx.GetLazyString($"A:3-{_documentDatabase.DocumentsStorage.Environment.Base64Id}");
                Assert.Throws <ConcurrencyException>(() => _documentDatabase.DocumentsStorage.Delete(ctx, "users/1", changeVector));

                ctx.Transaction.Commit();
            }
        }
Пример #17
0
    public void ErrorShouldIncludeTheActualItemAndReduceKey()
    {
        var numberOfDocs = 100;

        using (var database = CreateDocumentDatabase())
        {
            using (var index = MapReduceIndex.CreateNew <MapReduceIndex>(new IndexDefinition()
            {
                Name = "Users_DivideByZero",
                Maps = { @"from order in docs.Orders
from line in order.Lines
select new { Product = line.Product, FakeValue = 0 }" },
                Reduce = @"from result in mapResults
group result by result.Product into g
select new
{
    Product = g.Key,
    FakeValue = (long) (128 / g.Sum(x=> x.Total) - g.Sum(x=> x.Count))
}",
            }, database))
            {
                using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
                {
                    for (int i = 0; i < numberOfDocs; i++)
                    {
                        var order = CreateOrder();
                        PutOrder(database, order, context, i);
                    }

                    var stats = new IndexingRunStats();
                    var scope = new IndexingStatsScope(stats);

                    var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));

                    try
                    {
                        index.DoIndexingWork(scope, cts.Token);
                    }
                    catch (ExcessiveNumberOfReduceErrorsException)
                    {
                        // expected
                    }

                    List <IndexingError> indexingErrors = stats.Errors;

                    Assert.Equal(1, indexingErrors.Count);
                    Assert.Contains(@"current item to reduce: {""Product"":""Milk"",""FakeValue"":0}", indexingErrors.First().Error);
                    Assert.Equal(@"Reduce key: { 'Product' : Milk }", indexingErrors.First().Document);
                }
            }
        }
    }
Пример #18
0
        public async Task PatchByIndex_WhenFinish_ShouldFreeInternalUsageMemory()
        {
            using (var store = GetDocumentStore())
            {
                var database = await GetDatabase(store.Database);

                var index = new IndexDefinition
                {
                    Name = "Users_ByName",
                    Maps = { "from user in docs.Users select new { user.Name }" },
                    Type = IndexType.Map
                };

                await store
                .Maintenance
                .SendAsync(new PutIndexesOperation(new[] { index }));

                using (var session = store.OpenAsyncSession())
                {
                    for (var i = 0; i < 100; i++)
                    {
                        await session.StoreAsync(new User { Name = "John" });
                    }
                    await session.SaveChangesAsync();
                }
                WaitForIndexing(store);

                using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
                {
                    var query = new IndexQueryServerSide($"FROM index '{index.Name}'");
                    var patch = new PatchRequest("var u = this; u.is = true;", PatchRequestType.Patch, query.Metadata.DeclaredFunctions);

                    var before = context.AllocatedMemory;
                    await database.QueryRunner.ExecutePatchQuery(
                        query,
                        new QueryOperationOptions { RetrieveDetails = true },
                        patch,
                        query.QueryParameters,
                        context,
                        p => { },
                        new OperationCancelToken(CancelAfter, CancellationToken.None));

                    var after = context.AllocatedMemory;

                    //In a case of fragmentation, we don't immediately freeing memory so the memory can be a little bit higher
                    const long threshold = 256;
                    Assert.True(Math.Abs(before - after) < threshold);
                }
            }
        }
Пример #19
0
        public void CanCreateAndGetTombstone()
        {
            using (var database = CreateDocumentDatabase())
            {
                using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
                {
                    DocumentsStorage.PutOperationResults result;
                    using (var tx = context.OpenWriteTransaction())
                    {
                        using (var doc = CreateDocument(context, "key/1", new DynamicJsonValue
                        {
                            ["Name"] = "John",
                            [Constants.Documents.Metadata.Key] = new DynamicJsonValue
                            {
                                [Constants.Documents.Metadata.Collection] = "Users"
                            }
                        }))
                        {
                            result = database.DocumentsStorage.Put(context, "key/1", null, doc);
                        }

                        tx.Commit();
                    }

                    using (var tx = context.OpenWriteTransaction())
                    {
                        Assert.NotNull(database.DocumentsStorage.Delete(context, "key/1", null));

                        tx.Commit();
                    }

                    using (context.OpenReadTransaction())
                    {
                        var tombstones = database
                                         .DocumentsStorage
                                         .GetTombstonesFrom(context, "Users", 0, 0, int.MaxValue)
                                         .ToList();

                        Assert.Equal(1, tombstones.Count);

                        var tombstone = tombstones[0];

                        Assert.True(tombstone.StorageId > 0);
                        Assert.Equal(result.Etag, tombstone.DeletedEtag);
                        Assert.Equal(result.Etag + 1, tombstone.Etag);
                        Assert.Equal(result.Id, tombstone.LowerId);
                    }
                }
            }
        }
Пример #20
0
        public async Task EtagsArePersistedWithDeletes()
        {
            Initialize();
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Users"
                    }
                }, "users/1", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    var putResult = _documentDatabase.DocumentsStorage.Put(ctx, "users/1", null, doc);
                    Assert.Equal(1, putResult.Etag);
                    _documentDatabase.DocumentsStorage.Delete(ctx, "users/1", null);
                }

                ctx.Transaction.Commit();
            }

            await Restart();

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();
                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Users"
                    }
                }, "users/2", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    var putResult = _documentDatabase.DocumentsStorage.Put(ctx, "users/2", null, doc);

                    //this should be 3 because in the use-case of this test,
                    //the tombstone that was created when users/1 was deleted, will have etag == 2
                    //thus, next document that is created will have etag == 3
                    Assert.Equal(3, putResult.Etag);
                }

                ctx.Transaction.Commit();
            }
        }
Пример #21
0
        public void PutDocumentWithoutId()
        {
            Initialize();
            var id = "users/";

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                for (int i = 1; i < 5; i++)
                {
                    using (var doc = ctx.ReadObject(new DynamicJsonValue
                    {
                        ["ThisDocId"] = $"{i}"
                    }, id + i, BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                    {
                        var putResult = _documentDatabase.DocumentsStorage.Put(ctx, id + i, null, doc);
                        Assert.Equal(i, putResult.Etag);
                    }
                }
                ctx.Transaction.Commit();
            }

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                _documentDatabase.DocumentsStorage.Delete(ctx, "users/2", null);

                ctx.Transaction.Commit();
            }

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["ThisDocId"] = "2"
                }, id, BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    var putResult = _documentDatabase.DocumentsStorage.Put(ctx, id + 5, null, doc);
                    Assert.True(putResult.Etag >= 5);
                    Assert.Equal("users/5", putResult.Id);
                }
                ctx.Transaction.Commit();
            }
        }
        public async Task MultipleReduceKeys(int numberOfUsers, string[] locations)
        {
            using (var db = CreateDocumentDatabase())
                using (var index = AutoMapReduceIndex.CreateNew(GetUsersCountByLocationIndexDefinition(), db))
                {
                    CreateUsers(db, numberOfUsers, locations);

                    var batchStats = new IndexingRunStats();
                    var scope      = new IndexingStatsScope(batchStats);

                    var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
                    while (index.DoIndexingWork(scope, cts.Token))
                    {
                        ;
                    }

                    Assert.Equal(numberOfUsers, batchStats.MapAttempts);
                    Assert.Equal(numberOfUsers, batchStats.MapSuccesses);
                    Assert.Equal(0, batchStats.MapErrors);
                    Assert.True(batchStats.ReduceAttempts >= numberOfUsers, $"{batchStats.ReduceAttempts} >= {numberOfUsers}");
                    Assert.True(batchStats.ReduceSuccesses >= numberOfUsers, $"{batchStats.ReduceSuccesses} >= {numberOfUsers}");
                    Assert.Equal(batchStats.ReduceAttempts, batchStats.ReduceSuccesses);
                    Assert.Equal(0, batchStats.ReduceErrors);

                    using (var context = DocumentsOperationContext.ShortTermSingleUse(db))
                    {
                        var queryResult = await index.Query(new IndexQueryServerSide($"FROM INDEX '{index.Name}'")
                        {
                            WaitForNonStaleResultsTimeout = TimeSpan.FromMinutes(1)
                        }, context, OperationCancelToken.None);

                        Assert.False(queryResult.IsStale);

                        var results = queryResult.Results;

                        Assert.Equal(locations.Length, results.Count);

                        for (int i = 0; i < locations.Length; i++)
                        {
                            Assert.Equal(locations[i], results[i].Data["Location"].ToString());

                            long expected = numberOfUsers / locations.Length + numberOfUsers % (locations.Length - i);
                            Assert.Equal(expected, results[i].Data["Count"]);
                        }
                    }
                }
        }
Пример #23
0
        public async Task EtagsArePersisted()
        {
            Initialize();
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Users"
                    }
                }, "users/1", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    var putResult = _documentDatabase.DocumentsStorage.Put(ctx, "users/1", null, doc);
                    Assert.Equal(1, putResult.Etag);
                }

                ctx.Transaction.Commit();
            }

            await Restart();

            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Users"
                    }
                }, "users/2", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    var putResult = _documentDatabase.DocumentsStorage.Put(ctx, "users/2", null, doc);
                    Assert.Equal(2, putResult.Etag);
                }

                ctx.Transaction.Commit();
            }
        }
        private static void CreateOrders(DocumentDatabase db, int numberOfOrders, string[] countries = null, string[] employees = null, string[] companies = null)
        {
            using (var context = DocumentsOperationContext.ShortTermSingleUse(db))
            {
                using (var tx = context.OpenWriteTransaction())
                {
                    for (int i = 0; i < numberOfOrders; i++)
                    {
                        using (var doc = context.ReadObject(new DynamicJsonValue
                        {
                            ["Employee"] = employees?[i % employees.Length],
                            ["Company"] = companies?[i % companies.Length],
                            ["ShipTo"] = new DynamicJsonValue
                            {
                                ["Country"] = countries?[i % countries.Length],
                            },
                            ["Lines"] = new DynamicJsonArray
                            {
                                new DynamicJsonValue
                                {
                                    ["Price"] = 10.5,
                                    ["Quantity"] = 1
                                },
                                new DynamicJsonValue
                                {
                                    ["Price"] = 10.7,
                                    ["Quantity"] = 2
                                }
                            },
                            [Constants.Documents.Metadata.Key] = new DynamicJsonValue
                            {
                                [Constants.Documents.Metadata.Collection] = "Orders"
                            }
                        }, $"orders/{i}"))
                        {
                            db.DocumentsStorage.Put(context, $"orders/{i}", null, doc);
                        }
                    }

                    tx.Commit();
                }
            }
        }
Пример #25
0
        public async Task CanUseSimpleReduction()
        {
            using (var db = CreateDocumentDatabase())
                using (var mri = AutoMapReduceIndex.CreateNew(1, GetUsersCountByLocationIndexDefinition(), db))
                {
                    CreateUsers(db, 2, "Poland");

                    mri.DoIndexingWork(new IndexingStatsScope(new IndexingRunStats()), CancellationToken.None);

                    using (var context = DocumentsOperationContext.ShortTermSingleUse(db))
                    {
                        var queryResult = await mri.Query(new IndexQueryServerSide(), context, OperationCancelToken.None);

                        Assert.Equal(1, queryResult.Results.Count);
                        var result = queryResult.Results[0].Data;

                        string location;
                        Assert.True(result.TryGet("Location", out location));
                        Assert.Equal("Poland", location);

                        var count = result["Count"];

                        Assert.Equal(2L, count);
                    }

                    using (var context = DocumentsOperationContext.ShortTermSingleUse(db))
                    {
                        var queryResult = await mri.Query(new IndexQueryServerSide()
                        {
                            Query = "Count_Range:[Lx2 TO Lx10]"
                        }, context, OperationCancelToken.None);

                        Assert.Equal(1, queryResult.Results.Count);

                        queryResult = await mri.Query(new IndexQueryServerSide()
                        {
                            Query = "Count_Range:[Lx10 TO NULL]"
                        }, context, OperationCancelToken.None);

                        Assert.Equal(0, queryResult.Results.Count);
                    }
                }
        }
Пример #26
0
        public void WillVerifyEtags_New()
        {
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["Raven-Entity-Name"] = "Users"
                    }
                }, "users/1", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    Assert.Throws <ConcurrencyException>(() => _documentDatabase.DocumentsStorage.Put(ctx, "users/1", 1, doc));
                }

                ctx.Transaction.Commit();
            }
        }
Пример #27
0
        public void WillVerifyEtags_VerifyNew()
        {
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Users"
                    }
                }, "users/1", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    var changeVector = ctx.GetLazyString("");
                    _documentDatabase.DocumentsStorage.Put(ctx, "users/1", changeVector, doc);
                }

                ctx.Transaction.Commit();
            }
        }
Пример #28
0
        public void WillVerifyEtags_New()
        {
            using (var ctx = DocumentsOperationContext.ShortTermSingleUse(_documentDatabase))
            {
                ctx.OpenWriteTransaction();

                using (var doc = ctx.ReadObject(new DynamicJsonValue
                {
                    ["Name"] = "Oren",
                    ["@metadata"] = new DynamicJsonValue
                    {
                        ["@collection"] = "Users"
                    }
                }, "users/1", BlittableJsonDocumentBuilder.UsageMode.ToDisk))
                {
                    var changeVector = ctx.GetLazyString($"A:1-{_documentDatabase.DocumentsStorage.Environment.Base64Id}");
                    Assert.Throws <ConcurrencyException>(() => _documentDatabase.DocumentsStorage.Put(ctx, "users/1", changeVector, doc));
                }

                ctx.Transaction.Commit();
            }
        }
Пример #29
0
 public MergedBatchPutCommand(DocumentDatabase database, BuildVersionType buildType,
                              Logger log, ConcurrentDictionary <string, CollectionName> missingDocumentsForRevisions = null)
 {
     _database     = database;
     _buildType    = buildType;
     _log          = log;
     _resetContext = _database.DocumentsStorage.ContextPool.AllocateOperationContext(out _context);
     _missingDocumentsForRevisions = missingDocumentsForRevisions;
     Is32Bit = _database.Configuration.Storage.ForceUsing32BitsPager || PlatformDetails.Is32Bits;
     if (Is32Bit)
     {
         using (var ctx = DocumentsOperationContext.ShortTermSingleUse(database))
             using (ctx.OpenReadTransaction())
             {
                 _collectionNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                 foreach (var collection in _database.DocumentsStorage.GetCollections(ctx))
                 {
                     _collectionNames.Add(collection.Name);
                 }
             }
     }
 }
Пример #30
0
        private static void PutUser(DocumentDatabase db)
        {
            using (var context = DocumentsOperationContext.ShortTermSingleUse(db))
            {
                using (var tx = context.OpenWriteTransaction())
                {
                    using (var doc = CreateDocument(context, "users/1", new DynamicJsonValue
                    {
                        ["Name"] = "John",
                        [Constants.Documents.Metadata.Key] = new DynamicJsonValue
                        {
                            [Constants.Documents.Metadata.Collection] = "Users"
                        }
                    }))
                    {
                        db.DocumentsStorage.Put(context, "users/1", null, doc);
                    }

                    tx.Commit();
                }
            }
        }