예제 #1
0
파일: CowDatabase.cs 프로젝트: K-Eo/FooDB
		/// <summary>
		/// Update given cow
		/// </summary>
		public void Update (CowModel cow)
		{
			if (disposed) {
				throw new ObjectDisposedException ("CowDatabase");
			}

			throw new NotImplementedException ();
		}
예제 #2
0
        /// <summary>
        /// Update given cow
        /// </summary>
        public void Update(CowModel cow)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("CowDatabase");
            }

            throw new NotImplementedException();
        }
예제 #3
0
파일: CowDatabase.cs 프로젝트: K-Eo/FooDB
		/// <summary>
		/// Insert a new cow entry into our cow database
		/// </summary>
		public void Insert (CowModel cow)
		{
			if (disposed) {
				throw new ObjectDisposedException ("CowDatabase");
			}

			// Serialize the cow and insert it
			var recordId = this.cowRecords.Create (this.cowSerializer.Serialize(cow));

			// Primary index
			this.primaryIndex.Insert (cow.Id, recordId);

			// Secondary index
			this.secondaryIndex.Insert (new Tuple<string, int>(cow.Breed, cow.Age), recordId);
		}
예제 #4
0
        /// <summary>
        /// Insert a new cow entry into our cow database
        /// </summary>
        public void Insert(CowModel cow)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("CowDatabase");
            }

            // Serialize the cow and insert it
            var recordId = this.cowRecords.Create(this.cowSerializer.Serialize(cow));

            // Primary index
            this.primaryIndex.Insert(cow.Id, recordId);

            // Secondary index
            this.secondaryIndex.Insert(new Tuple <string, int>(cow.Breed, cow.Age), recordId);
        }
예제 #5
0
        public CowModel Deserializer(byte[] data)
        {
            var cowModel = new CowModel();

            // Read id
            cowModel.Id = BufferHelper.ReadBufferGuid(data, 0);

            // Read breed
            var breedLength = BufferHelper.ReadBufferInt32(data, 16);

            if (breedLength < 0 || breedLength > (16 * 1024))
            {
                throw new Exception("Invalid string length: " + breedLength);
            }
            cowModel.Breed = System.Text.Encoding.UTF8.GetString(data, 16 + 4, breedLength);

            // Read name
            var nameLength = BufferHelper.ReadBufferInt32(data, 16 + 4 + breedLength);

            if (nameLength < 0 || nameLength > (16 * 1024))
            {
                throw new Exception("Invalid string length: " + nameLength);
            }
            cowModel.Name = System.Text.Encoding.UTF8.GetString(data, 16 + 4 + breedLength + 4, nameLength);

            // Read age
            cowModel.Age = BufferHelper.ReadBufferInt32(data, 16 + 4 + breedLength + 4 + nameLength);

            // Read DNA data
            var dnaLength = BufferHelper.ReadBufferInt32(data, 16 + 4 + breedLength + 4 + nameLength + 4);

            if (dnaLength < 0 || dnaLength > (64 * 1024))
            {
                throw new Exception("Invalid DNA data length: " + dnaLength);
            }
            cowModel.DnaData = new byte[dnaLength];
            Buffer.BlockCopy(src: data, srcOffset: 16 + 4 + breedLength + 4 + nameLength + 4 + 4, dst: cowModel.DnaData, dstOffset: 0, count: cowModel.DnaData.Length);

            // Return constructed model
            return(cowModel);
        }
예제 #6
0
 /// <summary>
 /// Delete specified cow from our database
 /// </summary>
 public void Delete(CowModel cow)
 {
     throw new NotImplementedException();
 }
예제 #7
0
        public byte[] Serialize(CowModel cow)
        {
            var breedBytes = System.Text.Encoding.UTF8.GetBytes(cow.Breed);
            var nameBytes  = System.Text.Encoding.UTF8.GetBytes(cow.Breed);
            var cowData    = new byte[
                16 +                                   // 16 bytes for Guid id
                4 +                                    // 4 bytes indicate the length of `breed` string
                breedBytes.Length +                    // n bytes for breed string
                4 +                                    // 4 bytes indicate the length of the `name` string
                nameBytes.Length +                     // z bytes for name
                4 +                                    // 4 bytes for age
                4 +                                    // 4 bytes indicate length of DNA data
                cow.DnaData.Length                     // y bytes of DNA data
                             ];

            // Id

            Buffer.BlockCopy(
                src: cow.Id.ToByteArray(),
                srcOffset: 0,
                dst: cowData,
                dstOffset: 0,
                count: 16
                );

            // Breed

            Buffer.BlockCopy(
                src: LittleEndianByteOrder.GetBytes((int)breedBytes.Length),
                srcOffset: 0,
                dst: cowData,
                dstOffset: 16,
                count: 4
                );

            Buffer.BlockCopy(
                src: breedBytes,
                srcOffset: 0,
                dst: cowData,
                dstOffset: 16 + 4,
                count: breedBytes.Length
                );

            // Name

            Buffer.BlockCopy(
                src: LittleEndianByteOrder.GetBytes((int)nameBytes.Length),
                srcOffset: 0,
                dst: cowData,
                dstOffset: 16 + 4 + breedBytes.Length,
                count: 4
                );

            Buffer.BlockCopy(
                src: nameBytes,
                srcOffset: 0,
                dst: cowData,
                dstOffset: 16 + 4 + breedBytes.Length + 4,
                count: nameBytes.Length
                );

            // Age

            Buffer.BlockCopy(
                src: LittleEndianByteOrder.GetBytes((int)cow.Age),
                srcOffset: 0,
                dst: cowData,
                dstOffset: 16 + 4 + breedBytes.Length + 4 + nameBytes.Length,
                count: 4
                );

            // DNA

            Buffer.BlockCopy(
                src: LittleEndianByteOrder.GetBytes(cow.DnaData.Length),
                srcOffset: 0,
                dst: cowData,
                dstOffset: 16 + 4 + breedBytes.Length + 4 + nameBytes.Length + 4,
                count: 4
                );

            Buffer.BlockCopy(
                src: cow.DnaData,
                srcOffset: 0,
                dst: cowData,
                dstOffset: 16 + 4 + breedBytes.Length + 4 + nameBytes.Length + 4 + 4,
                count: cow.DnaData.Length
                );

            return(cowData);
        }
예제 #8
0
파일: CowSerializer.cs 프로젝트: K-Eo/FooDB
		public byte[] Serialize (CowModel cow)
		{
			var breedBytes = System.Text.Encoding.UTF8.GetBytes (cow.Breed);
			var nameBytes = System.Text.Encoding.UTF8.GetBytes (cow.Breed);
			var cowData = new byte[
				16 +                   // 16 bytes for Guid id
				4 +                    // 4 bytes indicate the length of `breed` string
				breedBytes.Length +    // n bytes for breed string
				4 +                    // 4 bytes indicate the length of the `name` string
				nameBytes.Length +     // z bytes for name 
				4 +                    // 4 bytes for age
				4 +                    // 4 bytes indicate length of DNA data
				cow.DnaData.Length     // y bytes of DNA data
			];

			// Id

			Buffer.BlockCopy (
				      src: cow.Id.ToByteArray(), 
				srcOffset: 0, 
				      dst: cowData, 
				dstOffset: 0, 
				    count: 16
			);

			// Breed

			Buffer.BlockCopy (
				      src: LittleEndianByteOrder.GetBytes((int)breedBytes.Length), 
				srcOffset: 0, 
				      dst: cowData, 
				dstOffset: 16, 
				    count: 4
			);

			Buffer.BlockCopy (
				      src: breedBytes, 
				srcOffset: 0, 
				      dst: cowData, 
				dstOffset: 16 + 4, 
				    count: breedBytes.Length
			);

			// Name

			Buffer.BlockCopy (
				      src: LittleEndianByteOrder.GetBytes((int)nameBytes.Length), 
				srcOffset: 0, 
				      dst: cowData, 
				dstOffset: 16 + 4 + breedBytes.Length, 
				    count: 4
			);

			Buffer.BlockCopy (
				      src: nameBytes, 
				srcOffset: 0, 
				      dst: cowData, 
				dstOffset: 16 + 4 + breedBytes.Length + 4, 
				    count: nameBytes.Length
			);

			// Age

			Buffer.BlockCopy (
				      src: LittleEndianByteOrder.GetBytes((int)cow.Age), 
				srcOffset: 0, 
				      dst: cowData, 
				dstOffset: 16 + 4 + breedBytes.Length + 4 + nameBytes.Length, 
				    count: 4
			);

			// DNA

			Buffer.BlockCopy (
				      src: LittleEndianByteOrder.GetBytes(cow.DnaData.Length), 
				srcOffset: 0, 
				      dst: cowData, 
				dstOffset: 16 + 4 + breedBytes.Length + 4 + nameBytes.Length + 4, 
				    count: 4
			);

			Buffer.BlockCopy (
				      src: cow.DnaData, 
				srcOffset: 0, 
				      dst: cowData, 
				dstOffset: 16 + 4 + breedBytes.Length + 4 + nameBytes.Length + 4 + 4, 
				    count: cow.DnaData.Length
			);

			return cowData;
		}
예제 #9
0
파일: CowSerializer.cs 프로젝트: K-Eo/FooDB
		public CowModel Deserializer (byte[] data)
		{
			var cowModel = new CowModel ();

			// Read id
			cowModel.Id = BufferHelper.ReadBufferGuid (data, 0);

			// Read breed
			var breedLength = BufferHelper.ReadBufferInt32 (data, 16);
			if (breedLength < 0 || breedLength > (16*1024)) {
				throw new Exception ("Invalid string length: " + breedLength);
			}
			cowModel.Breed = System.Text.Encoding.UTF8.GetString (data, 16 + 4, breedLength);

			// Read name
			var nameLength = BufferHelper.ReadBufferInt32 (data, 16 + 4 + breedLength);
			if (nameLength < 0 || nameLength > (16*1024)) {
				throw new Exception ("Invalid string length: " + nameLength);
			}
			cowModel.Name = System.Text.Encoding.UTF8.GetString (data, 16 + 4 + breedLength + 4, nameLength);

			// Read age
			cowModel.Age = BufferHelper.ReadBufferInt32 (data, 16 + 4 + breedLength + 4 + nameLength);

			// Read DNA data
			var dnaLength = BufferHelper.ReadBufferInt32 (data, 16 + 4 + breedLength + 4 + nameLength + 4);
			if (dnaLength < 0 || dnaLength > (64*1024)) {
				throw new Exception ("Invalid DNA data length: " + dnaLength);
			}
			cowModel.DnaData = new byte[dnaLength];
			Buffer.BlockCopy (src: data, srcOffset: 16 + 4 + breedLength + 4 + nameLength + 4 + 4, dst: cowModel.DnaData, dstOffset: 0, count: cowModel.DnaData.Length);

			// Return constructed model
			return cowModel;
		}
예제 #10
0
파일: CowDatabase.cs 프로젝트: K-Eo/FooDB
		/// <summary>
		/// Delete specified cow from our database
		/// </summary>
		public void Delete (CowModel cow)
		{
			throw new NotImplementedException ();
		}