Click here to Skip to main content
15,886,019 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.7K   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.Net;
using Newtonsoft.Json.Linq;
using System;
using Raven.Database.Data;

namespace Raven.Server.Responders
{
    public class DocumentBatch : RequestResponder
    {
        public override string UrlPattern
        {
            get { return @"/bulk_docs"; }
        }

        public override string[] SupportedVerbs
        {
            get { return new[] { "POST" }; }
        }

        public override void Respond(HttpListenerContext context)
        {
            switch (context.Request.HttpMethod)
            {                
                case "POST":
                    Batch(context);
                    break;
            }
        }

        private void Batch(HttpListenerContext context)
        {
            var jsonCommandArray = context.ReadJsonArray();
        	var commands = new List<ICommandData>();

			foreach (JObject jsonCommand in jsonCommandArray)
        	{
        		var key = jsonCommand["key"];
        		switch (jsonCommand["method"].Value<string>())
        		{
        			case "PUT":
        				commands.Add(new PutCommandData
        				{
        					Key = key.Value<string>(),
        					Etag = GetEtagFromCommand(jsonCommand),
        					Document = jsonCommand["document"] as JObject,
        					Metadata = jsonCommand["@metadata"] as JObject,
                            TransactionInformation = GetRequestTransaction(context)
        				});
        				break;
        			case "DELETE":
        				commands.Add(new DeleteCommandData
        				{
        					Key = key.Value<string>(),
        					Etag = GetEtagFromCommand(jsonCommand),
                            TransactionInformation = GetRequestTransaction(context)
        				});
        				continue;
        			default:
        				throw new ArgumentException("Batching only supports PUT and DELETE.");
        		}
            }

            var batchResult = Database.Batch(commands);
            context.WriteJson(batchResult);
        }

    	private static Guid? GetEtagFromCommand(JToken jsonCommand)
    	{
			return jsonCommand["etag"] != null && jsonCommand["etag"].Value<string>() != null ? new Guid(jsonCommand["etag"].Value<string>()) : (Guid?)null;
    	}
    }
}

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