Click here to Skip to main content
15,885,914 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 262.6K   2.7K   112  
An introduction to RavenDB - a new open source .NET document database using .NET 4.0 and VS 2010
using System;
using System.Threading;
using Newtonsoft.Json.Linq;
using Raven.Database;
using Raven.Database.Data;
using Raven.Database.Indexing;
using Raven.Tests.Storage;
using Xunit;

namespace Raven.Tests.Indexes
{
	public class DocumentsToIndex : AbstractDocumentStorageTest
	{
		private readonly DocumentDatabase db;

		public DocumentsToIndex()
		{
			db = new DocumentDatabase(new RavenConfiguration {DataDirectory = "raven.db.test.esent"});
			db.SpinBackgroundWorkers();
		}

		#region IDisposable Members

		public override void Dispose()
		{
			db.Dispose();
			base.Dispose();
		}

		#endregion

		[Fact]
		public void Can_Read_values_from_index()
		{
			db.PutIndex("pagesByTitle2",
					   new IndexDefinition
					   {
						   Map = @"
                    from doc in docs
                    where doc.type == ""page""
                    select new { doc.some };
                "
					   });
			db.Put("1", Guid.Empty,
			       JObject.Parse(
			       	@"{
                type: 'page', 
                some: 'val', 
                other: 'var', 
                content: 'this is the content', 
                title: 'hello world', 
                size: 5,
                '@metadata': {'@id': 1}
            }"),
                   new JObject(), null);

			QueryResult docs;
			do
			{
				docs = db.Query("pagesByTitle2", new IndexQuery
				{
					Query = "some:val",
					Start = 0,
					PageSize = 10
				});
				if (docs.IsStale)
					Thread.Sleep(100);
			} while (docs.IsStale);
			Assert.Equal(1, docs.Results.Length);
		}

		[Fact]
		public void Can_Read_Values_Using_Deep_Nesting()
		{
			db.PutIndex(@"DocsByProject",
						new IndexDefinition
						{
							Map = @"
from doc in docs
from prj in doc.projects
select new{project_name = prj.name}
"
						});

			var document =
				JObject.Parse(
					"{'name':'ayende','email':'ayende@ayende.com','projects':[{'name':'raven'}], '@metadata': { '@id': 1}}");
            db.Put("1", Guid.Empty, document, new JObject(), null);

			QueryResult docs;
			do
			{
				docs = db.Query("DocsByProject", new IndexQuery
				{
					Query = "project_name:raven",
					Start = 0,
					PageSize = 10
				});
				if (docs.IsStale)
					Thread.Sleep(100);
			} while (docs.IsStale);
			Assert.Equal(1, docs.Results.Length);
			var jProperty = docs.Results[0].Property("name");
			Assert.Equal("ayende", jProperty.Value.Value<string>());
		}

		[Fact]
		public void Can_Read_Values_Using_MultipleValues_From_Deep_Nesting()
		{
			db.PutIndex(@"DocsByProject",
						new IndexDefinition
						{
							Map = @"
from doc in docs
from prj in doc.projects
select new{project_name = prj.name, project_num = prj.num}
"
						});
			var document =
				JObject.Parse(
					"{'name':'ayende','email':'ayende@ayende.com','projects':[{'name':'raven', 'num': 5}, {'name':'crow', 'num': 6}], '@metadata': { '@id': 1}}");
            db.Put("1", Guid.Empty, document, new JObject(), null);

			QueryResult docs;
			do
			{
				docs = db.Query("DocsByProject", new IndexQuery
				{
					Query = "+project_name:raven +project_num:6",
					Start = 0,
					PageSize = 10
				});
				if (docs.IsStale)
					Thread.Sleep(100);
			} while (docs.IsStale);
			Assert.Equal(0, docs.Results.Length);
		}

		[Fact]
		public void Can_Read_values_when_two_indexes_exist()
		{
			db.PutIndex("pagesByTitle",
						new IndexDefinition
						{
							Map = @" 
    from doc in docs
    where doc.type == ""page""
    select new { doc.other};
"
						});
			db.PutIndex("pagesByTitle2",
					   new IndexDefinition
					   {
						   Map = @"
    from doc in docs
    where doc.type == ""page""
    select new { doc.some };
"
					   });
			db.Put("1", Guid.Empty,
			       JObject.Parse(
			       	"{type: 'page', some: 'val', other: 'var', content: 'this is the content', title: 'hello world', size: 5}"),
                   new JObject(), null);


			QueryResult docs;
			do
			{
				docs = db.Query("pagesByTitle2", new IndexQuery
				{
					Query = "some:val",
					Start = 0,
					PageSize = 10
				});
				if (docs.IsStale)
					Thread.Sleep(100);
			} while (docs.IsStale);
			Assert.Equal(1, docs.Results.Length);
		}

		[Fact]
		public void Updating_an_index_will_result_in_new_values()
		{
			db.PutIndex("pagesByTitle",
					   new IndexDefinition
					   {
						   Map = @"
    from doc in docs
    where doc.type == ""page""
    select new { doc.other};
"
					   });
			db.PutIndex("pagesByTitle",
					   new IndexDefinition
					   {
						   Map = @"
    from doc in docs
    where doc.type == ""page""
    select new { doc.other };
"
					   });
			db.Put("1", Guid.Empty,
			       JObject.Parse(
			       	"{type: 'page', some: 'val', other: 'var', content: 'this is the content', title: 'hello world', size: 5}"),
                   new JObject(), null);


			QueryResult docs;
			do
			{
				docs = db.Query("pagesByTitle", new IndexQuery
				{
					Query = "other:var",
					Start = 0,
					PageSize = 10
				});
				if (docs.IsStale)
					Thread.Sleep(100);
			} while (docs.IsStale);
			Assert.Equal(1, docs.Results.Length);
		}

		[Fact]
		public void Can_read_values_from_index_of_documents_already_in_db()
		{
			db.Put("1", Guid.Empty,
			       JObject.Parse(
			       	"{type: 'page', some: 'val', other: 'var', content: 'this is the content', title: 'hello world', size: 5}"),
                   new JObject(), null);

			db.PutIndex("pagesByTitle",
					   new IndexDefinition
					   {
						   Map = @"
    from doc in docs
    where doc.type == ""page""
    select new { doc.other };
"
					   });
			QueryResult docs;
			do
			{
				docs = db.Query("pagesByTitle", new IndexQuery
				{
					Query = "other:var",
					Start = 0,
					PageSize = 10
				});
				if (docs.IsStale)
					Thread.Sleep(100);
			} while (docs.IsStale);
			Assert.Equal(1, docs.Results.Length);
		}

		[Fact]
		public void Can_read_values_from_indexes_of_documents_already_in_db_when_multiple_docs_exists()
		{
			db.Put(null, Guid.Empty,
			       JObject.Parse(
			       	"{type: 'page', some: 'val', other: 'var', content: 'this is the content', title: 'hello world', size: 5}"),
                   new JObject(), null);
			db.Put(null, Guid.Empty,
			       JObject.Parse(
			       	"{type: 'page', some: 'val', other: 'var', content: 'this is the content', title: 'hello world', size: 5}"),
                   new JObject(), null);

			db.PutIndex("pagesByTitle",
						new IndexDefinition
						{
							Map = @"
    from doc in docs
    where doc.type == ""page""
    select new { doc.other };
"
						});
			QueryResult docs;
			do
			{
				docs = db.Query("pagesByTitle", new IndexQuery
				{
					Query = "other:var",
					Start = 0,
					PageSize = 10
				});
				if (docs.IsStale)
					Thread.Sleep(100);
			} while (docs.IsStale);
			Assert.Equal(2, docs.Results.Length);
		}
    }
}

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