コード例 #1
0
        /// <summary>
        ///     Back up the database
        /// </summary>
        /// <typeparam name="T">The database type</typeparam>
        /// <param name="writer">A writer to receive the backup</param>
        public void Backup <T>(BinaryWriter writer) where T : BaseDatabaseInstance
        {
            _RequiresActivation();

            var databaseQuery = from d in _databases where d.Value.Item1.Equals(typeof(T)) select d.Value.Item2;

            if (!databaseQuery.Any())
            {
                throw new SterlingDatabaseNotFoundException(typeof(T).FullName);
            }
            var database = databaseQuery.First();

            database.Flush();

            // first write the version
            _serializer.Serialize(_databaseVersion, writer);

            var typeMaster = database.Driver.GetTypes();

            // now the type master
            writer.Write(typeMaster.Count);
            foreach (var type in typeMaster)
            {
                writer.Write(type);
            }

            // now iterate tables
            foreach (var table in ((BaseDatabaseInstance)database).TableDefinitions)
            {
                // get the key list
                var keys = database.Driver.DeserializeKeys(table.Key, table.Value.KeyType,
                                                           table.Value.GetNewDictionary());

                // reality check
                if (keys == null)
                {
                    writer.Write(0);
                }
                else
                {
                    // write the count for the keys
                    writer.Write(keys.Count);

                    // for each key, serialize it out along with the object - indexes  can be rebuilt on the flipside
                    foreach (var key in keys.Keys)
                    {
                        _serializer.Serialize(key, writer);
                        writer.Write((int)keys[key]);

                        // get the instance
                        using (var instance = database.Driver.Load(table.Key, (int)keys[key]))
                        {
                            var bytes = instance.ReadBytes((int)instance.BaseStream.Length);
                            writer.Write(bytes.Length);
                            writer.Write(bytes);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void TestSerialization()
        {
            var charArray = TEST_STRING.ToCharArray();
            var byteArray = Encoding.UTF8.GetBytes(TEST_STRING);

            int    targetFive;
            double targetPi;
            string targetTestString;

            char[] targetCharArray;
            byte[] targetByteArray;

            using (var mem = new MemoryStream())
                using (var bw = new BinaryWriter(mem))
                {
                    _target.Serialize(FIVE, bw);
                    _target.Serialize(PI, bw);
                    _target.Serialize(TEST_STRING, bw);
                    _target.Serialize(charArray, bw);
                    _target.Serialize(byteArray, bw);

                    mem.Seek(0, SeekOrigin.Begin);

                    using (var br = new BinaryReader(mem))
                    {
                        targetFive       = _target.Deserialize <int>(br);
                        targetPi         = _target.Deserialize <double>(br);
                        targetTestString = _target.Deserialize <string>(br);
                        targetCharArray  = _target.Deserialize <char[]>(br);
                        targetByteArray  = (byte[])_target.Deserialize(typeof(byte[]), br);
                    }
                }

            Assert.AreEqual(FIVE, targetFive, "Integer did not deserialize correctly.");
            Assert.AreEqual(PI, targetPi, "Double did not deserialize correctly.");
            Assert.AreEqual(TEST_STRING, targetTestString, "String did not deserialize correctly.");

            Assert.AreEqual(charArray.Length, targetCharArray.Length, "Character array length mismatch.");
            if (charArray.Length == targetCharArray.Length)
            {
                for (var idx = 0; idx < charArray.Length; idx++)
                {
                    Assert.AreEqual(charArray[idx], targetCharArray[idx], "Character array did not deserialize correctly.");
                }
            }

            Assert.AreEqual(byteArray.Length, targetByteArray.Length, "Byte array length mismatch.");
            if (byteArray.Length == targetByteArray.Length)
            {
                for (var idx = 0; idx < byteArray.Length; idx++)
                {
                    Assert.AreEqual(byteArray[idx], targetByteArray[idx], "Byte array did not deserialize correctly.");
                }
            }
        }
コード例 #3
0
        /// <summary>
        ///     Serializes a property
        /// </summary>
        /// <param name="type">The parent type</param>
        /// <param name="propertyName">The property name</param>
        /// <param name="propertyValue">The property value</param>
        /// <param name="bw">The writer</param>
        private void _SerializeProperty(Type type, string propertyName, object propertyValue, BinaryWriter bw)
        {
            bw.Write(propertyName + PROPERTY_VALUE_SEPARATOR);
            bw.Write(_typeResolver(type.AssemblyQualifiedName));

            var isNull = propertyValue == null;

            _SerializeNull(bw, isNull);

            if (isNull)
            {
                return;
            }

            _serializer.Serialize(propertyValue, bw);
        }
コード例 #4
0
        public void TestSerializerException()
        {
            var exception = false;

            using (var mem = new MemoryStream())
            {
                using (var bw = new BinaryWriter(mem))
                {
                    try
                    {
                        _target.Serialize(_date, bw);
                    }
                    catch (SterlingSerializerException)
                    {
                        exception = true;
                    }

                    Assert.IsTrue(exception, "Sterling did not throw an exception when attemping to serialize the date.");
                }
            }
        }
コード例 #5
0
        public void TestSerialization()
        {
            decimal  decimalTest;
            DateTime dateTest, date2Test;
            Uri      uriTest;
            Guid     guidTest;
            TimeSpan timeSpanTest;

            using (var mem = new MemoryStream())
                using (var bw = new BinaryWriter(mem))
                {
                    _target.Serialize(DECIMAL, bw);
                    _target.Serialize(_date, bw);
                    _target.Serialize(_secondDate, bw);
                    _target.Serialize(_uri, bw);
                    _target.Serialize(_guid, bw);
                    _target.Serialize(_timeSpan, bw);

                    mem.Seek(0, SeekOrigin.Begin);

                    using (var br = new BinaryReader(mem))
                    {
                        decimalTest  = _target.Deserialize <decimal>(br);
                        dateTest     = _target.Deserialize <DateTime>(br);
                        date2Test    = _target.Deserialize <DateTime>(br);
                        uriTest      = _target.Deserialize <Uri>(br);
                        guidTest     = _target.Deserialize <Guid>(br);
                        timeSpanTest = (TimeSpan)_target.Deserialize(typeof(TimeSpan), br);
                    }
                }

            Assert.Equal(DECIMAL, decimalTest);    //Decimal did not deserialize correctly.");
            Assert.Equal(_date, dateTest);         //DateTime did not deserialize correctly.");
            Assert.Equal(_secondDate, date2Test);  //Second DateTime did not deserialize correctly.");
            Assert.Equal(_uri, uriTest);           //Uri did not deserialize correctly.");
            Assert.Equal(_guid, guidTest);         //Guid did not de-serialized correctly.");
            Assert.Equal(_timeSpan, timeSpanTest); //Time span did not deserialize correctly.");
        }