Click here to Skip to main content
15,896,606 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.6K   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 Db4objects.Db4o.Internal;
using Db4objects.Db4o.Internal.Marshall;
using Db4objects.Db4o.Internal.Slots;
using Sharpen.Util;

namespace Db4objects.Db4o.Internal.Marshall
{
	public class PrimitiveMarshaller0 : PrimitiveMarshaller
	{
		public override bool UseNormalClassRead()
		{
			return true;
		}

		public override int WriteNew(Transaction trans, PrimitiveFieldHandler yapClassPrimitive
			, object obj, bool topLevel, StatefulBuffer parentWriter, bool withIndirection, 
			bool restoreLinkOffset)
		{
			int id = 0;
			if (obj != null)
			{
				ITypeHandler4 handler = yapClassPrimitive.i_handler;
				ObjectContainerBase stream = trans.Stream();
				id = stream.NewUserObject();
				Slot slot = new Slot(-1, ObjectLength(handler));
				if (!stream.IsClient())
				{
					slot = ((LocalTransaction)trans).File().GetSlot(slot.Length());
				}
				Pointer4 pointer = new Pointer4(id, slot);
				trans.SetPointer(pointer);
				StatefulBuffer writer = new StatefulBuffer(trans, pointer);
				writer.WriteInt(yapClassPrimitive.GetID());
				handler.WriteNew(_family, obj, false, writer, true, false);
				writer.WriteEnd();
				stream.WriteNew(yapClassPrimitive, writer);
			}
			if (parentWriter != null)
			{
				parentWriter.WriteInt(id);
			}
			return id;
		}

		public override Date ReadDate(Db4objects.Db4o.Internal.Buffer bytes)
		{
			long value = bytes.ReadLong();
			if (value == long.MaxValue)
			{
				return null;
			}
			return new Date(value);
		}

		public override object ReadInteger(Db4objects.Db4o.Internal.Buffer bytes)
		{
			int value = bytes.ReadInt();
			if (value == int.MaxValue)
			{
				return null;
			}
			return value;
		}

		public override object ReadFloat(Db4objects.Db4o.Internal.Buffer bytes)
		{
			float value = UnmarshallFloat(bytes);
			if (float.IsNaN(value))
			{
				return null;
			}
			return value;
		}

		public override object ReadDouble(Db4objects.Db4o.Internal.Buffer buffer)
		{
			double value = UnmarshalDouble(buffer);
			if (double.IsNaN(value))
			{
				return null;
			}
			return value;
		}

		public override object ReadLong(Db4objects.Db4o.Internal.Buffer buffer)
		{
			long value = buffer.ReadLong();
			if (value == long.MaxValue)
			{
				return null;
			}
			return value;
		}

		public override object ReadShort(Db4objects.Db4o.Internal.Buffer buffer)
		{
			short value = UnmarshallShort(buffer);
			if (value == short.MaxValue)
			{
				return null;
			}
			return value;
		}

		public static double UnmarshalDouble(Db4objects.Db4o.Internal.Buffer buffer)
		{
			return Platform4.LongToDouble(buffer.ReadLong());
		}

		public static float UnmarshallFloat(Db4objects.Db4o.Internal.Buffer buffer)
		{
			return Sharpen.Runtime.IntBitsToFloat(buffer.ReadInt());
		}

		public static short UnmarshallShort(Db4objects.Db4o.Internal.Buffer buffer)
		{
			int ret = 0;
			for (int i = 0; i < Const4.SHORT_BYTES; i++)
			{
				ret = (ret << 8) + (buffer._buffer[buffer._offset++] & unchecked((int)(0xff)));
			}
			return (short)ret;
		}
	}
}

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