// constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="SystemProfileReadWriteLockStatisticsSerializer"/> class.
        /// </summary>
        public SystemProfileReadWriteLockStatisticsSerializer()
        {
            var timeSpanSerializationOptions = new TimeSpanSerializationOptions(BsonType.Double, TimeSpanUnits.Milliseconds);

            RegisterMember("Read", "r", TimeSpanSerializer.Instance, typeof(TimeSpan), timeSpanSerializationOptions);
            RegisterMember("Write", "w", TimeSpanSerializer.Instance, typeof(TimeSpan), timeSpanSerializationOptions);
        }
Esempio n. 2
0
        // constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="SystemProfileReadWriteLockStatisticsSerializer"/> class.
        /// </summary>
        public SystemProfileReadWriteLockStatisticsSerializer()
        {
            var timeSpanSerializationOptions = new TimeSpanSerializationOptions(BsonType.Double, TimeSpanUnits.Microseconds);

            RegisterMember("DatabaseReadLock", "r", new TimeSpanSerializer(), typeof(TimeSpan), timeSpanSerializationOptions);
            RegisterMember("GlobalReadLock", "R", new TimeSpanSerializer(), typeof(TimeSpan), timeSpanSerializationOptions);
            RegisterMember("DatabaseWriteLock", "w", new TimeSpanSerializer(), typeof(TimeSpan), timeSpanSerializationOptions);
            RegisterMember("GlobalWriteLock", "W", new TimeSpanSerializer(), typeof(TimeSpan), timeSpanSerializationOptions);
        }
Esempio n. 3
0
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            var timeSpan = (TimeSpan)value;

            // support RepresentationSerializationOptions for backward compatibility
            var representationSerializationOptions = options as RepresentationSerializationOptions;

            if (representationSerializationOptions != null)
            {
                options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation);
            }
            var timeSpanSerializationOptions = EnsureSerializationOptions <TimeSpanSerializationOptions>(options);

            switch (timeSpanSerializationOptions.Representation)
            {
            case BsonType.Double:
                bsonWriter.WriteDouble(ToDouble(timeSpan, timeSpanSerializationOptions.Units));
                break;

            case BsonType.Int32:
                bsonWriter.WriteInt32(ToInt32(timeSpan, timeSpanSerializationOptions.Units));
                break;

            case BsonType.Int64:
                bsonWriter.WriteInt64(ToInt64(timeSpan, timeSpanSerializationOptions.Units));
                break;

            case BsonType.String:
                bsonWriter.WriteString(timeSpan.ToString());     // not XmlConvert.ToString (we're using .NET's format for TimeSpan)
                break;

            default:
                var message = string.Format("'{0}' is not a valid TimeSpan representation.", timeSpanSerializationOptions.Representation);
                throw new BsonSerializationException(message);
            }
        }
Esempio n. 4
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(TimeSpan));

            // support RepresentationSerializationOptions for backward compatibility
            var representationSerializationOptions = options as RepresentationSerializationOptions;

            if (representationSerializationOptions != null)
            {
                options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation);
            }
            var timeSpanSerializationOptions = EnsureSerializationOptions <TimeSpanSerializationOptions>(options);

            BsonType bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Double:
                return(FromDouble(bsonReader.ReadDouble(), timeSpanSerializationOptions.Units));

            case BsonType.Int32:
                return(FromInt32(bsonReader.ReadInt32(), timeSpanSerializationOptions.Units));

            case BsonType.Int64:
                return(FromInt64(bsonReader.ReadInt64(), timeSpanSerializationOptions.Units));

            case BsonType.String:
                return(TimeSpan.Parse(bsonReader.ReadString()));    // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan)

            default:
                var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType);
                throw new Exception(message);
            }
        }
Esempio n. 5
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(TimeSpan));

            // support RepresentationSerializationOptions for backward compatibility
            var representationSerializationOptions = options as RepresentationSerializationOptions;

            if (representationSerializationOptions != null)
            {
                options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation);
            }
            var timeSpanSerializationOptions = EnsureSerializationOptions <TimeSpanSerializationOptions>(options);

            BsonType bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.String)
            {
                return(TimeSpan.Parse(bsonReader.ReadString())); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan)
            }
            else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks)
            {
                long ticks;
                switch (bsonType)
                {
                case BsonType.Double: ticks = (long)bsonReader.ReadDouble(); break;

                case BsonType.Int32: ticks = (long)bsonReader.ReadInt32(); break;

                case BsonType.Int64: ticks = bsonReader.ReadInt64(); break;

                default:
                    var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
                }
                return(new TimeSpan(ticks));
            }
            else
            {
                double interval;
                switch (bsonType)
                {
                case BsonType.Double: interval = bsonReader.ReadDouble(); break;

                case BsonType.Int32: interval = bsonReader.ReadInt32(); break;

                case BsonType.Int64: interval = bsonReader.ReadInt64(); break;

                default:
                    var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
                }

                switch (timeSpanSerializationOptions.Units)
                {
                case TimeSpanUnits.Days: return(TimeSpan.FromDays(interval));

                case TimeSpanUnits.Hours: return(TimeSpan.FromHours(interval));

                case TimeSpanUnits.Minutes: return(TimeSpan.FromMinutes(interval));

                case TimeSpanUnits.Seconds: return(TimeSpan.FromSeconds(interval));

                case TimeSpanUnits.Milliseconds: return(TimeSpan.FromMilliseconds(interval));

                case TimeSpanUnits.Nanoseconds: return(TimeSpan.FromMilliseconds(interval / 1000.0));

                default:
                    var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units);
                    throw new BsonSerializationException(message);
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            var timeSpan = (TimeSpan)value;

            // support RepresentationSerializationOptions for backward compatibility
            var representationSerializationOptions = options as RepresentationSerializationOptions;

            if (representationSerializationOptions != null)
            {
                options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation);
            }
            var timeSpanSerializationOptions = EnsureSerializationOptions <TimeSpanSerializationOptions>(options);

            if (timeSpanSerializationOptions.Representation == BsonType.String)
            {
                bsonWriter.WriteString(timeSpan.ToString()); // for TimeSpan use .NET's format instead of XmlConvert.ToString
            }
            else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks)
            {
                var ticks = timeSpan.Ticks;
                switch (timeSpanSerializationOptions.Representation)
                {
                case BsonType.Double: bsonWriter.WriteDouble((double)ticks); break;

                case BsonType.Int32: bsonWriter.WriteInt32((int)ticks); break;

                case BsonType.Int64: bsonWriter.WriteInt64(ticks); break;

                default:
                    var message = string.Format("'{0}' is not a valid TimeSpan representation.", timeSpanSerializationOptions.Representation);
                    throw new BsonSerializationException(message);
                }
            }
            else
            {
                double interval;
                switch (timeSpanSerializationOptions.Units)
                {
                case TimeSpanUnits.Days: interval = timeSpan.TotalDays; break;

                case TimeSpanUnits.Hours: interval = timeSpan.TotalHours; break;

                case TimeSpanUnits.Minutes: interval = timeSpan.TotalMinutes; break;

                case TimeSpanUnits.Seconds: interval = timeSpan.TotalSeconds; break;

                case TimeSpanUnits.Milliseconds: interval = timeSpan.TotalMilliseconds; break;

                case TimeSpanUnits.Nanoseconds: interval = timeSpan.TotalMilliseconds * 1000.0; break;

                default:
                    var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units);
                    throw new BsonSerializationException(message);
                }

                switch (timeSpanSerializationOptions.Representation)
                {
                case BsonType.Double: bsonWriter.WriteDouble(interval); break;

                case BsonType.Int32: bsonWriter.WriteInt32((int)interval); break;

                case BsonType.Int64: bsonWriter.WriteInt64((long)interval); break;

                default:
                    var message = string.Format("'{0}' is not a valid TimeSpan representation.", timeSpanSerializationOptions.Representation);
                    throw new BsonSerializationException(message);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Gets the serialization info for a member.
        /// </summary>
        /// <param name="memberName">The member name.</param>
        /// <returns>The serialization info for the member.</returns>
        public BsonSerializationInfo GetMemberSerializationInfo(string memberName)
        {
            string                    elementName;
            IBsonSerializer           serializer;
            Type                      nominalType;
            IBsonSerializationOptions serializationOptions = null;

            switch (memberName)
            {
            case "Abbreviated":
                elementName = "abbreviated";
                serializer  = StringSerializer.Instance;
                nominalType = typeof(string);
                break;

            case "Client":
                elementName = "client";
                serializer  = StringSerializer.Instance;
                nominalType = typeof(string);
                break;

            case "Command":
                elementName = "command";
                serializer  = BsonDocumentSerializer.Instance;
                nominalType = typeof(BsonDocument);
                break;

            case "CursorId":
                elementName = "cursorid";
                serializer  = Int64Serializer.Instance;
                nominalType = typeof(long);
                break;

            case "Duration":
                elementName          = "millis";
                serializer           = TimeSpanSerializer.Instance;
                nominalType          = typeof(TimeSpan);
                serializationOptions = new TimeSpanSerializationOptions(BsonType.Double, TimeSpanUnits.Milliseconds);
                break;

            case "Error":
                elementName = "err";
                serializer  = StringSerializer.Instance;
                nominalType = typeof(string);
                break;

            case "Exception":
                elementName = "exception";
                serializer  = StringSerializer.Instance;
                nominalType = typeof(string);
                break;

            case "ExceptionCode":
                elementName = "exceptionCode";
                serializer  = Int32Serializer.Instance;
                nominalType = typeof(int);
                break;

            case "Exhaust":
                elementName = "exhaust";
                serializer  = BooleanSerializer.Instance;
                nominalType = typeof(bool);
                break;

            case "FastMod":
                elementName = "fastmod";
                serializer  = BooleanSerializer.Instance;
                nominalType = typeof(bool);
                break;

            case "FastModInsert":
                elementName = "fastmodinsert";
                serializer  = BooleanSerializer.Instance;
                nominalType = typeof(bool);
                break;

            case "IdHack":
                elementName = "idhack";
                serializer  = BooleanSerializer.Instance;
                nominalType = typeof(bool);
                break;

            case "Info":
                elementName = "info";
                serializer  = StringSerializer.Instance;
                nominalType = typeof(string);
                break;

            case "KeyUpdates":
                elementName = "keyUpdates";
                serializer  = Int32Serializer.Instance;
                nominalType = typeof(int);
                break;

            case "Moved":
                elementName = "moved";
                serializer  = BooleanSerializer.Instance;
                nominalType = typeof(bool);
                break;

            case "Namespace":
                elementName = "ns";
                serializer  = StringSerializer.Instance;
                nominalType = typeof(string);
                break;

            case "NumberReturned":
                elementName = "nreturned";
                serializer  = Int32Serializer.Instance;
                nominalType = typeof(int);
                break;

            case "NumberScanned":
                elementName = "nscanned";
                serializer  = Int32Serializer.Instance;
                nominalType = typeof(int);
                break;

            case "NumberToReturn":
                elementName = "ntoreturn";
                serializer  = Int32Serializer.Instance;
                nominalType = typeof(int);
                break;

            case "NumberToSkip":
                elementName = "ntoskip";
                serializer  = Int32Serializer.Instance;
                nominalType = typeof(int);
                break;

            case "Op":
                elementName = "op";
                serializer  = StringSerializer.Instance;
                nominalType = typeof(string);
                break;

            case "Query":
                elementName = "query";
                serializer  = BsonDocumentSerializer.Instance;
                nominalType = typeof(BsonDocument);
                break;

            case "ResponseLength":
                elementName = "responseLength";
                serializer  = Int32Serializer.Instance;
                nominalType = typeof(int);
                break;

            case "ScanAndOrder":
                elementName = "scanAndOrder";
                serializer  = BooleanSerializer.Instance;
                nominalType = typeof(bool);
                break;

            case "Timestamp":
                elementName = "ts";
                serializer  = DateTimeSerializer.Instance;
                nominalType = typeof(DateTime);
                break;

            case "UpdateObject":
                elementName = "updateobj";
                serializer  = BsonDocumentSerializer.Instance;
                nominalType = typeof(BsonDocument);
                break;

            case "Upsert":
                elementName = "upsert";
                serializer  = BooleanSerializer.Instance;
                nominalType = typeof(bool);
                break;

            case "User":
                elementName = "user";
                serializer  = StringSerializer.Instance;
                nominalType = typeof(string);
                break;

            default:
                var message = string.Format("{0} is not a member of SystemProfileInfo.", memberName);
                throw new ArgumentOutOfRangeException("memberName", message);
            }

            return(new BsonSerializationInfo(elementName, serializer, nominalType, serializationOptions));
        }