Click here to Skip to main content
15,891,864 members
Articles / Database Development / NoSQL

RavenDB - An Introduction

,
Rate me:
Please Sign up or sign in to vote.
4.87/5 (38 votes)
28 Apr 2010CPOL7 min read 267K   2.7K   112  
An introduction to RavenDB - a new open source .NET document database using .NET 4.0 and VS 2010
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using Raven.Database.Indexing;
using Raven.Database.Linq;
using Xunit;

namespace Raven.Tests.Views
{
	public class ViewCompilation
	{
		private const string map =
			@"from post in docs
select new {
  post.blog_id, 
  comments_length = post.comments.Length 
  }";

		private const string reduce =
			@"
from agg in results
group agg by agg.blog_id into g
select new { 
  blog_id = g.Key, 
  comments_length = g.Sum(x=>x.comments_length) 
  }";

		private readonly IEnumerable<DynamicJsonObject> source;

		public ViewCompilation()
		{
			source = ConvertToExpando(new[]
			{
				new {blog_id = 3, comments = new object[3], __document_id = 1},
				new {blog_id = 5, comments = new object[4], __document_id = 1},
				new {blog_id = 6, comments = new object[6], __document_id = 1},
				new {blog_id = 7, comments = new object[1], __document_id = 1},
				new {blog_id = 3, comments = new object[3], __document_id = 1},
				new {blog_id = 3, comments = new object[5], __document_id = 1},
				new {blog_id = 2, comments = new object[8], __document_id = 1},
				new {blog_id = 4, comments = new object[3], __document_id = 1},
				new {blog_id = 5, comments = new object[2], __document_id = 1},
				new {blog_id = 3, comments = new object[3], __document_id = 1},
				new {blog_id = 5, comments = new object[1], __document_id = 1}
			});
		}

		[Fact]
		public void CanDetectGroupByTarget()
		{
			var abstractViewGenerator = new DynamicViewCompiler("test", new IndexDefinition { Map = map, Reduce = reduce }).GenerateInstance();
			Assert.Equal("blog_id", abstractViewGenerator.GroupByField);
		}

		[Fact]
		public void CanCompileQuery()
		{
			var abstractViewGenerator = new DynamicViewCompiler("test", new IndexDefinition { Map = map, Reduce = reduce }).GenerateInstance();
			Assert.NotNull(abstractViewGenerator);
		}

		[Fact]
		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());
		}

		private static IEnumerable<DynamicJsonObject> ConvertToExpando(IEnumerable<object> objects)
		{
			return objects.Select(obj => new DynamicJsonObject(JObject.FromObject(obj)));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions