Takes two query expressions as strings, and compile them. Along the way we apply some minimal transformations, the end result is an instance of AbstractViewGenerator, representing the map/reduce functions
Esempio n. 1
0
		public void CanDetectGroupByTarget()
		{
			var abstractViewGenerator = new DynamicViewCompiler("test", new IndexDefinition { Map = map, Reduce = reduce },  ".").GenerateInstance();
			var expandoObject = new ExpandoObject();
			((IDictionary<string,object>)expandoObject).Add("blog_id","1");
			Assert.Equal("1", abstractViewGenerator.GroupByExtraction(expandoObject));
		}
		public void Can_create_new_instance_from_query()
		{
			var dynamicQueryCompiler = new DynamicViewCompiler("pagesByTitle", new IndexDefinition { Map = query }, new AbstractDynamicCompilationExtension[0]);
			dynamicQueryCompiler.GenerateInstance();
			var compiled = dynamicQueryCompiler.GeneratedType;
			Activator.CreateInstance(compiled);
		}
Esempio n. 3
0
        public void Can_extend_queries()
        {
            var documents =
                GetDocumentsFromString(
                    @"[{loc: 4, lang: 3, '@metadata': {'@id': 1}}]");
            var transformer = new DynamicViewCompiler("pagesByTitle", new IndexDefinition
            {
                Map = @"
from doc in docs
select new { GeoHash = PerformingQueries.SampleGeoLocation.GeoHash(doc.loc, doc.lang) }
"
            },
                                                      new OrderedPartCollection<AbstractDynamicCompilationExtension>
													  {
													  	new SampleDynamicCompilationExtension()
													  }, ".", new InMemoryRavenConfiguration());
            var compiledQuery = transformer.GenerateInstance();
            var actual = compiledQuery.MapDefinitions[0](documents)
                .Cast<object>().ToArray();
            var expected = new[]
			{
				"{ GeoHash = 4#3, __document_id = 1 }",
			};

            Assert.Equal(expected.Length, actual.Length);
            for (var i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i], actual[i].ToString());
            }
        }
        public void Convert_select_many_will_keep_doc_id()
        {
            IndexDefinition indexDefinition = new IndexDefinition<Order>
            {
                Map = orders => from order in orders
                                from line in order.OrderLines
                                select new { line.ProductId }
            }.ToIndexDefinition(new DocumentConvention());
			var generator = new DynamicViewCompiler("test", indexDefinition, new AbstractDynamicCompilationExtension[0])
                .GenerateInstance();


            var results = generator.MapDefinition(new[]
			{
				GetDocumentFromString(
				@"
                {
                    '@metadata': {'Raven-Entity-Name': 'Orders', '@id': 1},
                    'OrderLines': [{'ProductId': 2}, {'ProductId': 3}]
                }"),
				  GetDocumentFromString(
				@"
                {
                    '@metadata': {'Raven-Entity-Name': 'Orders', '@id': 2},
                    'OrderLines': [{'ProductId': 5}, {'ProductId': 4}]
                }")
			}).Cast<object>().ToArray();

            foreach (var result in results)
            {
                Assert.NotNull(TypeDescriptor.GetProperties(result).Find("__document_id", true));
            }
        }
Esempio n. 5
0
        public void Can_query_json()
        {
            var documents =
                GetDocumentsFromString(
                    @"[
{'type':'page', title: 'hello', content: 'foobar', size: 2, '@metadata': {'@id': 1}},
{'type':'page', title: 'there', content: 'foobar 2', size: 3, '@metadata': {'@id': 2} },
{'type':'revision', size: 4, _id: 3}
]");
            var transformer = new DynamicViewCompiler("pagesByTitle", new IndexDefinition { Map = query }, ".");
            var compiledQuery = transformer.GenerateInstance();
            var actual = compiledQuery.MapDefinitions[0](documents)
                .Cast<object>().ToArray();
            var expected = new[]
			{
				"{ Key = hello, Value = foobar, Size = 2, __document_id = 1 }",
				"{ Key = there, Value = foobar 2, Size = 3, __document_id = 2 }"
			};

            Assert.Equal(expected.Length, actual.Length);
            for (var i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i], actual[i].ToString());
            }
        }
        public void Can_compile_map_using_linq_methods()
        {
            var viewGenerator = new DynamicViewCompiler("test", new IndexDefinition
            {
                Map = @"docs.Users
            .Select(user => new {Location = user.Location, Count = 1})
            .Select(user => new {Location = user.Location})"
            }).GenerateInstance();

            var results = viewGenerator.MapDefinition(new[]
            {
                GetDocumentFromString(
                @"
                {
                    '@metadata': {'Raven-Entity-Name': 'Users', '@id': 1},
                    'Location': 'Tel Aviv'
                }")
            }).Cast<object>().ToArray();

            var expected = new[]
            {
                "{ Location = Tel Aviv, __document_id = 1 }",
            };

            for (var i = 0; i < results.Length; i++)
            {
                Assert.Equal(expected[i], results[i].ToString());
            }
        }
		public void Will_compile_query_successfully()
		{
			var dynamicQueryCompiler = new DynamicViewCompiler("pagesByTitle", new IndexDefinition { Map = query }, new AbstractDynamicCompilationExtension[0]);
			dynamicQueryCompiler.GenerateInstance();
			var compiled = dynamicQueryCompiler.GeneratedType;
			Assert.NotNull(compiled);
		}
		public void Will_keep_cast_in_query()
		{
			var dynamicQueryCompiler = new DynamicViewCompiler("caster", new IndexDefinition { Map = "from x in docs select new { Id = (int)x.Id }" }, new AbstractDynamicCompilationExtension[0], ".");
			dynamicQueryCompiler.GenerateInstance();

			Assert.Contains("(int)x.Id", dynamicQueryCompiler.CompiledQueryText);
		}
Esempio n. 9
0
 public void CanExecuteQuery()
 {
     var dynamicViewCompiler = new DynamicViewCompiler("test", new IndexDefinition { Map = map, Reduce = reduce });
     var abstractViewGenerator = dynamicViewCompiler.GenerateInstance();
     var mapResults = abstractViewGenerator.MapDefinition(source).ToArray();
     var results = abstractViewGenerator.ReduceDefinition(mapResults).ToArray();
     Assert.Equal("{ blog_id = 3, comments_length = 14 }", results[0].ToString());
     Assert.Equal("{ blog_id = 5, comments_length = 7 }", results[1].ToString());
     Assert.Equal("{ blog_id = 6, comments_length = 6 }", results[2].ToString());
     Assert.Equal("{ blog_id = 7, comments_length = 1 }", results[3].ToString());
     Assert.Equal("{ blog_id = 2, comments_length = 8 }", results[4].ToString());
     Assert.Equal("{ blog_id = 4, comments_length = 3 }", results[5].ToString());
 }
		public void Can_execute_query()
		{
			var dynamicQueryCompiler = new DynamicViewCompiler("pagesByTitle", new IndexDefinition { Map = query }, new AbstractDynamicCompilationExtension[0]);
			var generator = dynamicQueryCompiler.GenerateInstance();
			var results = generator.MapDefinition(new[]
			{
				GetDocumentFromString(
					@"
                {
                    '@metadata': {'@id': 1},
                    'Type': 'page',
                    'Title': 'doc1',
                    'Content': 'Foobar',
                    'Size': 31
                }")
				,
				GetDocumentFromString(
					@"
                {
                    '@metadata': {'@id': 2},
                    'Type': 'not a page',
                }")
				,
				GetDocumentFromString(
					@"
                {
                    '@metadata': {'@id': 3},
                    'Type': 'page',
                    'Title': 'doc2',
                    'Content': 'Foobar',
                    'Size': 31
                }")
				,
			}).Cast<object>().ToArray();

			var expected = new[]
			{
				"{ Key = doc1, Value = Foobar, Size = 31, __document_id = 1 }",
				"{ Key = doc2, Value = Foobar, Size = 31, __document_id = 3 }"
			};

			for (var i = 0; i < results.Length; i++)
			{
				Assert.Equal(expected[i], results[i].ToString());
			}
		}
        public void Can_compile_map_reduce_using_linq_methods()
        {
            var viewGenerator = new DynamicViewCompiler("test", new IndexDefinition
            {
                Map = @"docs.Users
            .Select(user => new {Location = user.Location, Count = 1})",
                Reduce =
                    @"results
            .GroupBy(agg => agg.Location)
            .Select(g => new {Location = g.Key, Count = g.Sum(x => x.Count)})"
            },  ".").GenerateInstance();

            var source = viewGenerator.MapDefinitions[0](new[]
            {
                GetDocumentFromString(
                    @"
                {
                    '@metadata': {'Raven-Entity-Name': 'Users', '@id': 1},
                    'Location': 'Tel Aviv'
                }"),
                GetDocumentFromString(
                    @"
                {
                    '@metadata': {'Raven-Entity-Name': 'Users', '@id': 1},
                    'Location': 'Tel Aviv'
                }")
            }).ToArray();
            var results = viewGenerator.ReduceDefinition(source).Cast<object>().ToArray();

            var expected = new[]
            {
                "{ Location = Tel Aviv, Count = 2 }",
            };

            for (var i = 0; i < results.Length; i++)
            {
                Assert.Equal(expected[i], results[i].ToString());
            }
        }
Esempio n. 12
0
 public static void Main()
 {
     BasicConfigurator.Configure(new ConsoleAppender
     {
         Layout = new SimpleLayout()
     });
     try
     {
         var dynamicViewCompiler = new DynamicViewCompiler("a", new IndexDefinition
         {
             Map = @"
     from post in docs.Posts
     where post.Published == 'aasds'
     select new {post.PostedAt }
     "
         });
         dynamicViewCompiler.GenerateInstance();
         Console.WriteLine(dynamicViewCompiler.CompiledQueryText);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Esempio n. 13
0
 private DynamicViewCompiler AddAndCompileIndex(string name, IndexDefinition indexDefinition)
 {
     var transformer = new DynamicViewCompiler(name, indexDefinition);
     var generator = transformer.GenerateInstance();
     indexCache.AddOrUpdate(name, generator, (s, viewGenerator) => generator);
     logger.InfoFormat("New index {0}:\r\n{1}\r\nCompiled to:\r\n{2}", transformer.Name, transformer.CompiledQueryText,
                       transformer.CompiledQueryText);
     return transformer;
 }
Esempio n. 14
0
		private DynamicViewCompiler AddAndCompileIndex(IndexDefinition indexDefinition)
		{
			var name = FixupIndexName(indexDefinition.Name, path);
			var transformer = new DynamicViewCompiler(name, indexDefinition, extensions, path);
			var generator = transformer.GenerateInstance();
			indexCache.AddOrUpdate(name, generator, (s, viewGenerator) => generator);
			indexDefinitions.AddOrUpdate(name, indexDefinition, (s1, definition) =>
		    {
                if (definition.IsCompiled)
					throw new InvalidOperationException("Index " + name + " is a compiled index, and cannot be replaced");
		        return indexDefinition;   
		    });
			logger.InfoFormat("New index {0}:\r\n{1}\r\nCompiled to:\r\n{2}", transformer.Name, transformer.CompiledQueryText,
			                  transformer.CompiledQueryText);
			return transformer;
		}
		private void ReadAllAggregations(DbIterator it)
		{
			Slice prefix = "aggregators/";
			it.Seek(prefix);
			while(it.WithPrefix(prefix))
			{
				string name = Encoding.UTF8.GetString(it.Key.Array, it.Key.Offset, it.Key.Count);
				using (var stream = it.CreateValueStream())
				{
					var indexDefinition =
						new JsonSerializer().Deserialize<IndexDefinition>(new JsonTextReader(new StreamReader(stream)));
					_log.Info("Reading aggregator {0}", indexDefinition.Name);
					AbstractViewGenerator generator = null;
					try
					{
						var dynamicViewCompiler = new DynamicViewCompiler(indexDefinition.Name, indexDefinition,
																		  Path.Combine(_path, "Generators"));
						generator = dynamicViewCompiler.GenerateInstance();
					}
					catch (Exception e)
					{
						_log.WarnException("Could not create instance of aggregator " + indexDefinition.Name, e);
						// could not create generator, ignoring this and deleting the generator
						RemoveAggregation(name);
					}

					if (generator != null)
					{
						var aggregator = new Aggregator(this, indexDefinition.Name, generator);
						_aggregations.TryAdd(indexDefinition.Name, aggregator);
						Background.Work(aggregator.StartAggregation);
					}
				}
				it.Next();
			}
		}
Esempio n. 16
0
 private void Compile(string code)
 {
     var dynamicViewCompiler = new DynamicViewCompiler("test", new IndexDefinition
     {
         Map = code
     }, new OrderedPartCollection<AbstractDynamicCompilationExtension>(), ".", new InMemoryRavenConfiguration());
     dynamicViewCompiler.GenerateInstance();
 }
		public async Task CreateAggregationAsync(IndexDefinition indexDefinition)
		{
			var dynamicViewCompiler = new DynamicViewCompiler(indexDefinition.Name, indexDefinition, Path.Combine(_path, "Generators"));
			var generator = dynamicViewCompiler.GenerateInstance();

			var writeBatch = new WriteBatch();
			var memoryStream = SmallObjectToMemoryStream(indexDefinition);
			writeBatch.Put("aggregators/" + indexDefinition.Name, memoryStream);
			await _storage.Writer.WriteAsync(writeBatch);

			var aggregator = new Aggregator(this, indexDefinition.Name, generator);
			_aggregations.AddOrUpdate(indexDefinition.Name, aggregator, (s, viewGenerator) => aggregator);
			Background.Work(aggregator.StartAggregation);
		}
		public void LinqQueryWithStaticCallOnEnumerableIsCanBeCompiledAndRun()
		{
			var indexDefinition = new IndexDefinitionBuilder<Page>
			{
				Map = pages => from p in pages
							   from coAuthor in p.CoAuthors.DefaultIfEmpty()
							   select new
							   {
								   p.Id,
								   CoAuthorUserID = coAuthor != null ? coAuthor.UserId : -1
							   }
			}.ToIndexDefinition(new DocumentConvention());

			var mapInstance = new DynamicViewCompiler("testView",
													  indexDefinition, ".").
				GenerateInstance();

			var conventions = new DocumentConvention();
			var o = RavenJObject.FromObject(page,conventions.CreateSerializer());
			o["@metadata"] = new RavenJObject {{"Raven-Entity-Name", "Pages"}};
			dynamic dynamicObject = new DynamicJsonObject(o);

			var result = mapInstance.MapDefinitions[0](new[] { dynamicObject }).ToList<object>();
			Assert.Equal("{ Id = 0, CoAuthorUserID = 1, __document_id =  }", result[0].ToString());
			Assert.Equal("{ Id = 0, CoAuthorUserID = 2, __document_id =  }", result[1].ToString());
		}
		public void CanExecuteTheTransformResultFunction()
		{
			var answersIndex = new Answers_ByAnswerEntity2 { Conventions = new DocumentConvention() };
			IndexDefinition indexDefinition = answersIndex.CreateIndexDefinition();
			var dynamicViewCompiler = new DynamicViewCompiler("test", indexDefinition, ".");
			AbstractViewGenerator abstractViewGenerator = dynamicViewCompiler.GenerateInstance();
			using (var documentStore = NewDocumentStore())
			{

				Guid questionId = Guid.NewGuid();
				Guid answerId = Guid.NewGuid();

				using (IDocumentSession session = documentStore.OpenSession())
				{
					var user = new User { Id = @"user\222", DisplayName = "John Doe" };
					session.Store(user);

					var question = new Question2
					{
						Id = questionId,
						Title = "How to do this in RavenDb?",
						Content = "I'm trying to find how to model documents for better DDD support.",
						UserId = @"user\222"
					};
					session.Store(question);


					var answer = new AnswerEntity2
					{
						Id = answerId,
						Question = question,
						Content = "This is doable",
						UserId = user.Id
					};
					answer.Votes = new[]
					{
						new AnswerVoteEntity2
						{
							Id = Guid.NewGuid(),
							QuestionId = questionId,
							Answer = answer,
							Delta = 2
						}
					};


					session.Store(new Answer2
					{
						Id = answerId,
						UserId = user.Id,
						QuestionId = question.Id,
						Content = "This is doable",
						Votes = new[]
						{
							new AnswerVote2
							{
								Id = Guid.NewGuid(),
								QuestionId = questionId,
								AnswerId = answerId,
								Delta = 2
							}
						}
					});

					session.SaveChanges();
				}

				documentStore.DocumentDatabase.TransactionalStorage.Batch(accessor =>
				{
					var documentRetriever = new DocumentRetriever(accessor, new OrderedPartCollection<AbstractReadTrigger>());
					var dynamicJsonObjects = new[] { new DynamicJsonObject(accessor.Documents.DocumentByKey("answer2s/" + answerId.ToString(), null).ToJson()), };
					var transformResultsDefinition = abstractViewGenerator.TransformResultsDefinition(documentRetriever,
																									  dynamicJsonObjects
						);

					transformResultsDefinition.ToArray();
				});



			}
		}
Esempio n. 20
0
 public void CanDetectGroupByTarget()
 {
     var abstractViewGenerator = new DynamicViewCompiler("test", new IndexDefinition { Map = map, Reduce = reduce }).GenerateInstance();
     Assert.Equal("blog_id", abstractViewGenerator.GroupByField);
 }
Esempio n. 21
0
		private DynamicViewCompiler AddAndCompileIndex(IndexDefinition indexDefinition)
		{
			var name = FixupIndexName(indexDefinition.Name, path);
			var transformer = new DynamicViewCompiler(name, indexDefinition, extensions, path, configuration);
			var generator = transformer.GenerateInstance();
			indexCache.AddOrUpdate(name, generator, (s, viewGenerator) => generator);
			
			logger.Info("New index {0}:\r\n{1}\r\nCompiled to:\r\n{2}", transformer.Name, transformer.CompiledQueryText,
							  transformer.CompiledQueryText);
			return transformer;
		}
Esempio n. 22
0
 public void CanCompileQuery()
 {
     var abstractViewGenerator = new DynamicViewCompiler("test", new IndexDefinition { Map = map, Reduce = reduce }).GenerateInstance();
     Assert.NotNull(abstractViewGenerator);
 }