示例#1
0
        public void TestFailingProjection()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new MyClass {
                        Index = 1, Prop1 = "prop1", Prop2 = "prop2"
                    });
                    session.Store(new MyClass {
                        Index = 2, Prop1 = "prop1", Prop2 = "prop2"
                    });
                    session.Store(new MyClass {
                        Index = 3, Prop1 = "prop1", Prop2 = "prop2"
                    });

                    session.SaveChanges();
                }
                using (var session = store.OpenSession())
                {
                    JsonOperationContext context;
                    store.GetRequestExecuter(store.DefaultDatabase).ContextPool.AllocateOperationContext(out context);

                    var indexDef = new IndexDefinitionBuilder <MyClass>()
                    {
                        Map = docs => from doc in docs select new { Index = doc.Index }
                    };
                    ;

                    var putIndexOperation = new PutIndexOperation(context);
                    var indexCommand      = putIndexOperation.CreateRequest(store.Conventions, "MyClass/ByIndex", indexDef.ToIndexDefinition(store.Conventions, true));
                    if (indexCommand != null)
                    {
                        store.GetRequestExecuter(store.DefaultDatabase).Execute(indexCommand, context);
                    }

                    WaitForIndexing(store);

                    var query = session.Query <MyClass>("MyClass/ByIndex")
                                .Select(x => new MyClass
                    {
                        Index = x.Index,
                        Prop1 = x.Prop1
                    });

                    var enumerator = session.Advanced.Stream(query);
                    int count      = 0;
                    while (enumerator.MoveNext())
                    {
                        Assert.IsType <MyClass>(enumerator.Current.Document);
                        Assert.Null(((MyClass)enumerator.Current.Document).Prop2);
                        count++;
                    }

                    Assert.Equal(3, count);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Executes the index creation against the specified document database using the specified conventions
        /// </summary>
        public virtual void Execute(IDocumentStore documentStore, DocumentConvention documentConvention)
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            var requestExecuter = documentStore.GetRequestExecuter(documentStore.DefaultDatabase);
            JsonOperationContext jsonOperationContext;

            requestExecuter.ContextPool.AllocateOperationContext(out jsonOperationContext);


            if (documentConvention.PrettifyGeneratedLinqExpressions)
            {
                var getIndexOperation = new GetIndexOperation(jsonOperationContext);

                var getCommand = getIndexOperation.CreateRequest(IndexName);
                if (getCommand != null)
                {
                    requestExecuter.Execute(getCommand, jsonOperationContext);
                    getIndexOperation.SetResult(getCommand.Result);
                }
                var serverIndexDefinition = getIndexOperation.IndexDefinitionResult;

                if (serverIndexDefinition != null) //TODO iftah && CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverIndexDefinition, indexDefinition))
                {
                    //AfterExecute(databaseCommands, documentConvention);
                    return;
                }
            }

            var putIndexOperation = new PutIndexOperation(jsonOperationContext);

            var putCommand = putIndexOperation.CreateRequest(documentConvention, IndexName, indexDefinition);

            if (putCommand != null)
            {
                requestExecuter.Execute(putCommand, jsonOperationContext);
                putIndexOperation.SetResult(putCommand.Result);
            }

            /*if (Priority != null)
             *  databaseCommands.SetIndexPriority(IndexName, Priority.Value);
             *
             * AfterExecute(databaseCommands, documentConvention);*/
        }
示例#3
0
        public async Task CanPerformAdvancedWithSetBasedUpdates()
        {
            using (var store = GetDocumentStore())
            {
                var item1 = new CustomType
                {
                    Id       = "someId/1",
                    Owner    = "bob",
                    Value    = 12143,
                    Comments = new List <string>(new[] { "one", "two", "seven" })
                };
                var item2 = new CustomType
                {
                    Id       = "someId/2",
                    Owner    = "NOT bob",
                    Value    = 9999,
                    Comments = new List <string>(new[] { "one", "two", "seven" })
                };

                using (var session = store.OpenAsyncSession())
                {
                    await session.StoreAsync(item1);

                    await session.StoreAsync(item2);

                    await session.SaveChangesAsync();
                }

                JsonOperationContext context;
                store.GetRequestExecuter(store.DefaultDatabase).ContextPool.AllocateOperationContext(out context);

                var putIndexOperation = new PutIndexOperation(context);
                var indexCommand      = putIndexOperation.CreateRequest(store.Conventions, "TestIndex", new IndexDefinition
                {
                    Maps = { @"from doc in docs.CustomTypes 
                                     select new { doc.Owner }" }
                });
                if (indexCommand != null)
                {
                    store.GetRequestExecuter(store.DefaultDatabase).Execute(indexCommand, context);
                }

                WaitForIndexing(store);
                store.OpenSession().Advanced.DocumentQuery <CustomType>("TestIndex")
                .WaitForNonStaleResults().ToList();

                var patchByIndexOperation = new PatchByIndexOperation(context);
                var patchCommand          = patchByIndexOperation.CreateRequest("TestIndex",
                                                                                new IndexQuery {
                    Query = "Owner:bob"
                },
                                                                                null, new PatchRequest {
                    Script = sampleScript
                }, store);
                if (patchCommand != null)
                {
                    store.GetRequestExecuter(store.DefaultDatabase).Execute(patchCommand, context);
                }

                var getDocumentCommand = new GetDocumentCommand
                {
                    Ids     = new[] { item1.Id },
                    Context = context
                };
                store.GetRequestExecuter(store.DefaultDatabase).Execute(getDocumentCommand, context);

                var results = getDocumentCommand.Result.Results;
                Assert.Equal(1, results.Length);
                var    res = (BlittableJsonReaderObject)results[0];
                object obj;
                res.TryGetMember("Comments", out obj);
                Assert.Equal(2, ((BlittableJsonReaderArray)obj).Length);
                Assert.Equal("one test", ((BlittableJsonReaderArray)obj).GetStringByIndex(0));
                Assert.Equal("two", ((BlittableJsonReaderArray)obj).GetStringByIndex(1));
                res.TryGetMember("Value", out obj);
                Assert.Equal(12144, ((Int64)obj));
                res.TryGetMember("newValue", out obj);
                Assert.Equal("err!!", obj.ToString());


                getDocumentCommand = new GetDocumentCommand
                {
                    Ids     = new[] { item2.Id },
                    Context = context
                };
                store.GetRequestExecuter(store.DefaultDatabase).Execute(getDocumentCommand, context);

                results = getDocumentCommand.Result.Results;
                Assert.Equal(1, results.Length);
                res = (BlittableJsonReaderObject)results[0];
                res.TryGetMember("Comments", out obj);
                Assert.Equal(3, ((BlittableJsonReaderArray)obj).Length);
                Assert.Equal("one", ((BlittableJsonReaderArray)obj).GetStringByIndex(0));
                Assert.Equal("two", ((BlittableJsonReaderArray)obj).GetStringByIndex(1));
                Assert.Equal("seven", ((BlittableJsonReaderArray)obj).GetStringByIndex(2));
                res.TryGetMember("Value", out obj);
                Assert.Equal(9999, ((Int64)obj));
            }
        }
示例#4
0
        public async Task CanCreateDocumentsIfPatchingAppliedByIndex()
        {
            using (var store = GetDocumentStore())
            {
                using (var session = store.OpenAsyncSession())
                {
                    await session.StoreAsync(new CustomType
                    {
                        Id    = "Item/1",
                        Value = 1
                    });

                    await session.StoreAsync(new CustomType
                    {
                        Id    = "Item/2",
                        Value = 2
                    });

                    await session.SaveChangesAsync();
                }

                JsonOperationContext context;
                store.GetRequestExecuter(store.DefaultDatabase).ContextPool.AllocateOperationContext(out context);

                var putIndexOperation = new PutIndexOperation(context);
                var indexCommand      = putIndexOperation.CreateRequest(store.Conventions, "TestIndex", new IndexDefinition
                {
                    Maps = { @"from doc in docs.CustomTypes 
                                     select new { doc.Value }" }
                });
                if (indexCommand != null)
                {
                    store.GetRequestExecuter(store.DefaultDatabase).Execute(indexCommand, context);
                }



                using (var session = store.OpenAsyncSession())
                {
                    await session.Advanced.AsyncDocumentQuery <CustomType>("TestIndex")
                    .WaitForNonStaleResults()
                    .ToListAsync();
                }

                var patchByIndexOperation = new PatchByIndexOperation(context);
                var patchCommand          = patchByIndexOperation.CreateRequest("TestIndex",
                                                                                new IndexQuery {
                    Query = "Value:1"
                },
                                                                                null, new PatchRequest {
                    Script = @"PutDocument('NewItem/3', {'CopiedValue': this.Value });"
                }, store);
                if (patchCommand != null)
                {
                    store.GetRequestExecuter(store.DefaultDatabase).Execute(patchCommand, context);
                }

                var getDocumentCommand = new GetDocumentCommand
                {
                    Ids     = new[] { "Item/1", "Item/2" },
                    Context = context
                };
                store.GetRequestExecuter(store.DefaultDatabase).Execute(getDocumentCommand, context);

                var results = getDocumentCommand.Result.Results;
                Assert.Equal(2, results.Length);

                getDocumentCommand = new GetDocumentCommand
                {
                    Ids     = new[] { "NewItem/3" },
                    Context = context
                };
                store.GetRequestExecuter(store.DefaultDatabase).Execute(getDocumentCommand, context);

                results = getDocumentCommand.Result.Results;
                Assert.Equal(1, results.Length);
                var    res = (BlittableJsonReaderObject)results[0];
                object obj;
                res.TryGetMember("CopiedValue", out obj);
                Assert.Equal(1, ((Int64)obj));
            }
        }