Click here to Skip to main content
15,892,005 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 267.2K   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.Collections;
using System.IO;
using System.Linq;
using System.Management.Automation.Provider;
using Newtonsoft.Json.Linq;
using Raven.Database;

namespace Raven.Server.PowerShellProvider
{
	public class RavenDBContentReader : IContentReader
	{
		private readonly DocumentDatabase _db;
		private readonly bool _typeIsDocument;
		private int _currentOffset;

		public RavenDBContentReader(DocumentDatabase db, bool typeIsDocument)
		{
			this._typeIsDocument = typeIsDocument;
			this._db = db;
		}

		#region IContentReader Members

		public void Close()
		{
			Dispose();
		}

		public IList Read(long readCount)
		{
			JArray retVal;
			if (this._typeIsDocument)
				retVal = this._db.GetDocuments(_currentOffset, Convert.ToInt32(readCount));
			else
				retVal = this._db.GetIndexes(_currentOffset, Convert.ToInt32(readCount));

			if (retVal == null || retVal.Count == 0)
				return null;

			this._currentOffset += retVal.Count;
			return retVal.ToList();
		}

		public void Seek(long offset, SeekOrigin origin)
		{
			int totalCount;
			if (this._typeIsDocument)
				totalCount = this._db.Statistics.CountOfDocuments;
			else
				totalCount = this._db.Statistics.CountOfIndexes;


			if (offset > totalCount)
			{
				throw new
					ArgumentException(
					"Offset cannot be greater than the number of " + (_typeIsDocument ? "documents" : "indexes")
					);
			}

			if (origin == SeekOrigin.Begin)
			{
				// starting from Beginning with an index 0, the current offset
				// has to be advanced to offset - 1
				_currentOffset = _currentOffset - 1;
			}
			else if (origin == SeekOrigin.End)
			{
				// starting from the end which is numRows - 1, the current
				// offset is so much less than numRows - 1
				_currentOffset = totalCount - 1 - _currentOffset;
			}
			else
			{
				// calculate from the previous value of current offset
				// advancing forward always
				_currentOffset += Convert.ToInt32(offset);
			}
		}

		public void Dispose()
		{
		}

		#endregion
	}
}

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