Click here to Skip to main content
15,878,945 members
Articles / Programming Languages / C#

Using a Database Over a Webservice

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
29 May 20073 min read 48.5K   539   44  
This article shows an example implementation of a database used over a Web-Service
/* Copyright (C) 2004 - 2007  db4objects Inc.  http://www.db4o.com */

using System;
using System.Collections;
using Db4objects.Db4o;
using Db4objects.Db4o.Ext;
using Db4objects.Db4o.Foundation;
using Db4objects.Db4o.Internal;
using Db4objects.Db4o.Internal.Cluster;
using Db4objects.Db4o.Internal.Query;
using Db4objects.Db4o.Internal.Query.Processor;
using Db4objects.Db4o.Internal.Query.Result;
using Db4objects.Db4o.Query;

namespace Db4objects.Db4o.Internal.Cluster
{
	/// <exclude></exclude>
	public class ClusterQueryResult : IQueryResult
	{
		private readonly Db4objects.Db4o.Cluster.Cluster _cluster;

		private readonly IObjectSet[] _objectSets;

		private readonly int[] _sizes;

		private readonly int _size;

		public ClusterQueryResult(Db4objects.Db4o.Cluster.Cluster cluster, IQuery[] queries
			)
		{
			_cluster = cluster;
			_objectSets = new IObjectSet[queries.Length];
			_sizes = new int[queries.Length];
			int size = 0;
			for (int i = 0; i < queries.Length; i++)
			{
				_objectSets[i] = queries[i].Execute();
				_sizes[i] = _objectSets[i].Size();
				size += _sizes[i];
			}
			_size = size;
		}

		private sealed class ClusterQueryResultIntIterator : IIntIterator4
		{
			private readonly CompositeIterator4 _delegate;

			public ClusterQueryResultIntIterator(IEnumerator[] iterators)
			{
				_delegate = new CompositeIterator4(iterators);
			}

			public bool MoveNext()
			{
				return _delegate.MoveNext();
			}

			public object Current
			{
				get
				{
					return _delegate.Current;
				}
			}

			public void Reset()
			{
				_delegate.Reset();
			}

			public int CurrentInt()
			{
				return ((IIntIterator4)_delegate.CurrentIterator()).CurrentInt();
			}
		}

		public virtual IIntIterator4 IterateIDs()
		{
			lock (_cluster)
			{
				IEnumerator[] iterators = new IEnumerator[_objectSets.Length];
				for (int i = 0; i < _objectSets.Length; i++)
				{
					iterators[i] = ((ObjectSetFacade)_objectSets[i])._delegate.IterateIDs();
				}
				return new ClusterQueryResult.ClusterQueryResultIntIterator(iterators);
			}
		}

		public virtual IEnumerator GetEnumerator()
		{
			lock (_cluster)
			{
				IEnumerator[] iterators = new IEnumerator[_objectSets.Length];
				for (int i = 0; i < _objectSets.Length; i++)
				{
					iterators[i] = ((ObjectSetFacade)_objectSets[i])._delegate.Iterator();
				}
				return new CompositeIterator4(iterators);
			}
		}

		public virtual int Size()
		{
			return _size;
		}

		public virtual object Get(int index)
		{
			lock (_cluster)
			{
				if (index < 0 || index >= Size())
				{
					throw new IndexOutOfRangeException();
				}
				int i = 0;
				while (index >= _sizes[i])
				{
					index -= _sizes[i];
					i++;
				}
				return ((ObjectSetFacade)_objectSets[i]).Get(index);
			}
		}

		public virtual object Lock()
		{
			return _cluster;
		}

		public virtual IExtObjectContainer ObjectContainer()
		{
			throw new NotSupportedException();
		}

		public virtual int IndexOf(int id)
		{
			throw new NotSupportedException();
		}

		public virtual void Sort(IQueryComparator cmp)
		{
			throw new NotSupportedException();
		}

		/// <param name="c"></param>
		public virtual void LoadFromClassIndex(ClassMetadata c)
		{
			throw new NotSupportedException();
		}

		/// <param name="q"></param>
		public virtual void LoadFromQuery(QQuery q)
		{
			throw new NotSupportedException();
		}

		/// <param name="i"></param>
		public virtual void LoadFromClassIndexes(ClassMetadataIterator i)
		{
			throw new NotSupportedException();
		}

		/// <param name="r"></param>
		public virtual void LoadFromIdReader(Db4objects.Db4o.Internal.Buffer r)
		{
			throw new NotSupportedException();
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions