public static ICommit ToCommit_original(this BsonDocument doc, IDocumentSerializer serializer)
        {
            if (doc == null)
            {
                return(null);
            }

            string bucketId       = doc[MongoCommitFields.BucketId].AsString;
            string streamId       = doc[MongoCommitFields.StreamId].AsString;
            int    commitSequence = doc[MongoCommitFields.CommitSequence].AsInt32;

            var events = doc[MongoCommitFields.Events]
                         .AsBsonArray
                         .Select(e =>
            {
                BsonValue payload = e.AsBsonDocument[MongoCommitFields.Payload];
                return(payload.IsBsonDocument
                           ? BsonSerializer.Deserialize <EventMessage>(payload.ToBsonDocument())
                           : serializer.Deserialize <EventMessage>(payload.AsByteArray));
            })
                         .ToArray();

            //int streamRevision = doc[MongoCommitFields.Events].AsBsonArray.Last().AsBsonDocument[MongoCommitFields.StreamRevision].AsInt32;
            int streamRevision = doc[MongoCommitFields.StreamRevisionTo].AsInt32;

            return(new Commit(bucketId,
                              streamId,
                              streamRevision,
                              doc[MongoCommitFields.CommitId].AsGuid,
                              commitSequence,
                              doc[MongoCommitFields.CommitStamp].ToUniversalTime(),
                              doc[MongoCommitFields.CheckpointNumber].ToInt64(),
                              doc[MongoCommitFields.Headers].AsDictionary <string, object>(),
                              events));
        }
Esempio n. 2
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "sort":
                _options.Sort = value.ToBsonDocument();
                return(true);

            case "limit":
                _options.Limit = value.ToInt32();
                return(true);

            case "skip":
                _options.Skip = value.ToInt32();
                return(true);

            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return(true);

            case "modifiers":
                _options.Modifiers = (BsonDocument)value;
                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "allowDiskUse":
                _options.AllowDiskUse = value.ToBoolean();
                return(true);

            case "sort":
                _options.Sort = value.ToBsonDocument();
                return(true);

            case "limit":
                _options.Limit = value.ToInt32();
                return(true);

            case "skip":
                _options.Skip = value.ToInt32();
                return(true);

            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return(true);

            case "collation":
                _options.Collation = Collation.FromBsonDocument(value.AsBsonDocument);
                return(true);
            }

            return(false);
        }
Esempio n. 4
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
                case "filter":
                    _filter = (BsonDocument)value;
                    return true;
                case "sort":
                    _options.Sort = value.ToBsonDocument();
                    return true;
                case "limit":
                    _options.Limit = value.ToInt32();
                    return true;
                case "skip":
                    _options.Skip = value.ToInt32();
                    return true;
                case "batchSize":
                    _options.BatchSize = value.ToInt32();
                    return true;
                case "modifiers":
                    _options.Modifiers = (BsonDocument)value;
                    return true;
            }

            return false;
        }
        public static ICommit ToCommit(this BsonDocument doc, IDocumentSerializer serializer)
        {
            if (doc == null)
            {
                return(null);
            }

            var mc = BsonSerializer.Deserialize <MongoCommit>(doc);

            return(new Commit(mc.BucketId,
                              mc.StreamId,
                              mc.StreamRevisionTo,
                              mc.CommitId,
                              mc.CommitSequence,
                              mc.CommitStamp,
                              mc.CheckpointNumber,
                              mc.Headers,
                              mc.Events.Select(e =>
            {
                BsonValue payload = e[MongoCommitFields.Payload];
                return payload.IsBsonDocument
                           ? BsonSerializer.Deserialize <EventMessage>(payload.ToBsonDocument())
                           : serializer.Deserialize <EventMessage>(payload.AsByteArray); // ByteStreamDocumentSerializer ?!?! doesn't work this way!
            }).ToArray()));
        }
        /// <summary>
        /// BsonValue转展示用字符
        /// </summary>
        /// <param name="bsonValue"></param>
        /// <returns></returns>
        public static String ConvertToString(BsonValue bsonValue)
        {
            //二进制数据
            if (bsonValue.IsBsonBinaryData)
            {
                return("[Binary]");
            }
            //空值
            if (bsonValue.IsBsonNull)
            {
                return("[Empty]");
            }
            //文档
            if (bsonValue.IsBsonDocument)
            {
                return(bsonValue.ToString() + "[Contains" + bsonValue.ToBsonDocument().ElementCount + "Documents]");
            }
            //时间
            if (bsonValue.IsBsonDateTime)
            {
                DateTime bsonData = bsonValue.AsDateTime;
                //@flydreamer提出的本地化时间要求
                return(bsonData.ToLocalTime().ToString());
            }

            //字符
            if (bsonValue.IsString)
            {
                //只有在字符的时候加上""
                return("\"" + bsonValue.ToString() + "\"");
            }

            //其他
            return(bsonValue.ToString());
        }
Esempio n. 7
0
        private static BsonDocumentBindable TransformBsonDoc(BsonValue el, string name)
        {
            var bsonElDoc = el.ToBsonDocument();

            //bsonElDoc.AllowDuplicateNames = true;

            return(new BsonDocumentBindable(string.Format("{0} ({1})", name, bsonElDoc.ElementCount),
                                            null, bsonElDoc.BsonType, bsonElDoc));
        }
        protected override void SetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "operation":
                _operation = value.ToBsonDocument();
                return;
            }

            base.SetArgument(name, value);
        }
Esempio n. 9
0
        private void AddSamples(List <string> samples, BsonValue value, string propertyName)
        {
            string[] nameParts = propertyName.Split(".");

            string subProperty = propertyName;

            string currentProperty = nameParts[0];

            if (nameParts.Length > 1)
            {
                subProperty = string.Join(".", nameParts, 1, nameParts.Length - 1);
            }

            if (value.IsBsonDocument)
            {
                if (value.ToBsonDocument().Contains(currentProperty))
                {
                    value = value[currentProperty];
                    AddSamples(samples, value, subProperty);
                }
            }
            else
            {
                if (value.IsBsonArray)
                {
                    for (int i = 0; i < value.AsBsonArray.Count; i++)
                    {
                        AddSamples(samples, value[i], subProperty);
                    }
                }
                else
                {
                    if (!(value is BsonNull))
                    {
                        samples.Add(value.ToString());
                    }
                }
            }
        }
Esempio n. 10
0
        protected override bool TrySetArgument(string name, BsonValue value)
        {
            switch (name)
            {
            case "comment":
                _options.Comment = value.AsString;
                return(true);

            case "filter":
                _filter = (BsonDocument)value;
                return(true);

            case "sort":
                _options.Sort = value.ToBsonDocument();
                return(true);

            case "limit":
                _options.Limit = value.ToInt32();
                return(true);

            case "skip":
                _options.Skip = value.ToInt32();
                return(true);

            case "batchSize":
                _options.BatchSize = value.ToInt32();
                return(true);

            case "modifiers":
#pragma warning disable 618
                _options.Modifiers = (BsonDocument)value;
#pragma warning restore 618
                return(true);

            case "hint":
                _options.Hint = value;
                return(true);

            case "max":
                _options.Max = value.ToBsonDocument();
                return(true);

            case "min":
                _options.Min = value.ToBsonDocument();
                return(true);

            case "maxTimeMS":
                _options.MaxTime = TimeSpan.FromMilliseconds(value.ToInt32());
                return(true);

            case "returnKey":
                _options.ReturnKey = value.ToBoolean();
                return(true);

            case "showRecordId":
                _options.ShowRecordId = value.ToBoolean();
                return(true);
            }

            return(false);
        }
Esempio n. 11
0
 /// <summary>
 /// 单个文档的TextView表示整理
 /// </summary>
 /// <param name="itemName"></param>
 /// <param name="value"></param>
 /// <param name="DeepLv"></param>
 /// <returns></returns>
 public static String GetBsonElementText(String itemName, BsonValue value, int DeepLv)
 {
     String rtnText = String.Empty;
     if (value.IsBsonArray)
     {
         int count = 1;
         BsonArray mBsonlst = value.AsBsonArray;
         for (int BsonNo = 0; BsonNo < mBsonlst.Count; BsonNo++)
         {
             if (BsonNo == 0)
             {
                 if (itemName != String.Empty)
                 {
                     for (int i = 0; i < DeepLv; i++)
                     {
                         rtnText += "  ";
                     }
                     rtnText += "  \"" + itemName + "\":";
                 }
                 rtnText += "[";
                 rtnText += "\r\n";
                 DeepLv++;
             }
             DeepLv++;
             rtnText += GetBsonElementText(String.Empty, mBsonlst[BsonNo], DeepLv);
             DeepLv--;
             if (BsonNo == mBsonlst.Count - 1)
             {
                 DeepLv--;
                 //   "itemName": "Value"
                 for (int i = 0; i < DeepLv; i++)
                 {
                     rtnText += "  ";
                 }
                 rtnText += "  ]";
                 rtnText += "\r\n";
             }
             count++;
         }
     }
     else
     {
         if (value.IsBsonDocument)
         {
             if (itemName != String.Empty)
             {
                 //   "itemName": "Value"
                 for (int i = 0; i < DeepLv; i++)
                 {
                     rtnText += "  ";
                 }
                 rtnText += "  \"" + itemName + "\":";
                 rtnText += "\r\n";
             }
             DeepLv++;
             for (int i = 0; i < DeepLv; i++)
             {
                 rtnText += "  ";
             }
             rtnText += "{";
             rtnText += "\r\n";
             BsonDocument SubDoc = value.ToBsonDocument();
             foreach (String SubitemName in SubDoc.Names)
             {
                 BsonValue Subvalue = SubDoc.GetValue(SubitemName);
                 rtnText += GetBsonElementText(SubitemName, SubDoc.GetValue(SubitemName), DeepLv);
             }
             for (int i = 0; i < DeepLv; i++)
             {
                 rtnText += "  ";
             }
             rtnText += "}";
             rtnText += "\r\n";
             DeepLv--;
         }
         else
         {
             if (itemName != String.Empty)
             {
                 //   "itemName": "Value"
                 for (int i = 0; i < DeepLv; i++)
                 {
                     rtnText += "  ";
                 }
                 rtnText += "  \"" + itemName + "\":";
             }
             rtnText += ConvertForShow(value) + "\r\n";
         }
     }
     return rtnText;
 }
Esempio n. 12
0
        /// <summary>
        /// BsonValue转展示用字符
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static string ConvertForShow(BsonValue val)
        {
            //二进制数据
            if (val.IsBsonBinaryData)
            {
                _hasBSonBinary = true;
                return "[二进制数据]";
            }
            //空值
            if (val.IsBsonNull)
            {
                return "[空值]";
            }

            //文档
            if (val.IsBsonDocument)
            {
                return val.ToString() + "[包含" + val.ToBsonDocument().ElementCount + "个元素的文档]";
            }

            //时间
            if (val.IsBsonDateTime)
            {
                DateTime bsonData = val.AsDateTime;
                //@flydreamer提出的本地化时间要求
                return bsonData.ToLocalTime().ToString();
            }

            //字符
            if (val.IsString)
            {
                //只有在字符的时候加上""
                return "\"" + val.ToString() + "\"";
            }

            //其他
            return val.ToString();
        }
        /// <summary>
        /// BsonValue转展示用字符
        /// </summary>
        /// <param name="bsonValue"></param>
        /// <returns></returns>
        public static String ConvertToString(BsonValue bsonValue)
        {
            //二进制数据
            if (bsonValue.IsBsonBinaryData)
            {
                return "[Binary]";
            }
            //空值
            if (bsonValue.IsBsonNull)
            {
                return "[Empty]";
            }
            //文档
            if (bsonValue.IsBsonDocument)
            {
                return bsonValue.ToString() + "[Contains" + bsonValue.ToBsonDocument().ElementCount + "Documents]";
            }
            //时间
            if (bsonValue.IsBsonDateTime)
            {
                DateTime bsonData = bsonValue.AsDateTime;
                //@flydreamer提出的本地化时间要求
                return bsonData.ToLocalTime().ToString();
            }

            //字符
            if (bsonValue.IsString)
            {
                //只有在字符的时候加上""
                return "\"" + bsonValue.ToString() + "\"";
            }

            //其他
            return bsonValue.ToString();
        }
Esempio n. 14
0
        /// <summary>
        /// 得到客户追踪结果
        /// 废弃该mapreduce方法,太慢
        /// </summary>
        /// <param name="filter">过滤条件</param>
        /// <param name="businessIdList">过滤订单号</param>
        /// <returns>追踪记录</returns>
        public List <MongoCustomerTrack> GetDunCustomerTractByMapReduce(DunSearchFilter filter, List <int> businessIdList)
        {
            IMongoQuery query = Query.Null;

            if (businessIdList != null && businessIdList.Count > 0)
            {
                query = Query.In("BusinessID", businessIdList.Select(o => BsonValue.Create(o)));
            }

            if (filter.LastTrackTime != DateTime.MinValue)
            {
                query = Query.And(query, Query.GTE("CreateTime", BsonValue.Create(filter.LastTrackTime)));
            }

            string finalizeJs = @"function Finalize(key, reduced) {
                                        if(reduced.VaildCode==null)
                                            reduced.VaildCode='';

                                        if(reduced.UnVaildCode==null)
                                            reduced.UnVaildCode='';

	                                    return reduced;
                                    }";
            string mapJs      = @"function Map() {
	                            emit(
		                            this.BusinessID,				
		                            {CreateTime: this.CreateTime, VaildCode: this.VaildCode,UnVaildCode:this.UnVaildCode}); 
                            }";
            string reduceJs   = @"function Reduce(key, values) {
	                            var reduced = {CreateTime:new Date(1900,0,1), VaildCode:'',UnVaildCode:''}; 
	                            values.forEach(function(val) {
                                    if(reduced.CreateTime<val.CreateTime){
		                            reduced.CreateTime = val.CreateTime;    
		                            reduced.VaildCode = val.VaildCode; 
                                    reduced.UnVaildCode = val.UnVaildCode; }});
                                return reduced;}";
            IEnumerable <BsonDocument> bsonDocument = Singleton <BaseMongo> .Instance.MapReduceToEnumerable("Mongo.Suf_CustomerTrack", query, finalizeJs, mapJs, reduceJs,
                                                                                                            0, null);

            List <BsonDocument>       list         = bsonDocument.ToList();
            List <MongoCustomerTrack> customerList = new List <MongoCustomerTrack>();

            foreach (BsonDocument doc in list)
            {
                BsonValue val = doc.GetValue(1);
                if (val == null)
                {
                    continue;
                }

                if (!val.IsBsonDocument)
                {
                    continue;
                }

                BsonDocument       bsonDoc = val.ToBsonDocument();
                BsonValue          bsonVal;
                MongoCustomerTrack track = new MongoCustomerTrack();
                if (bsonDoc.TryGetValue("CreateTime", out bsonVal))
                {
                    track.CreateTime = bsonVal.AsUniversalTime.ToLocalTime();
                }

                if (filter.LastTrackTime != DateTime.MinValue && filter.LastTrackTime != track.CreateTime)
                {
                    continue;
                }

                if (bsonDoc.TryGetValue("VaildCode", out bsonVal))
                {
                    track.VaildCode = bsonVal.ToString();
                }

                if (bsonDoc.TryGetValue("UnVaildCode", out bsonVal))
                {
                    track.UnVaildCode = bsonVal.ToString();
                }

                if (!string.IsNullOrEmpty(filter.LabelType))
                {
                    if (!string.IsNullOrEmpty(track.VaildCode) && !track.VaildCode.Equals(filter.LabelType))
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(track.VaildCode) && !track.UnVaildCode.Equals(filter.LabelType))
                    {
                        continue;
                    }
                }
                customerList.Add(track);
            }
            return(customerList);
        }
Esempio n. 15
0
        private void ProfiledEnumeratorOnEnumerationEnded(object sender, ProfiledEnumerator <TDocument> .EnumerationEndedEventArgs enumerationEndedEventArgs)
        {
            BsonValue hint = null, orderBy = null;

            var hasHint    = Options != null && Options.TryGetValue("$hint", out hint);
            var hasOrderBy = Options != null && Options.TryGetValue("$orderby", out orderBy);

            var commandStringBuilder = new StringBuilder(1024);

            commandStringBuilder.Append(Collection.Name);
            commandStringBuilder.Append(".find(");

            if (Query != null)
            {
                commandStringBuilder.Append("query");
            }

            if (Fields != null)
            {
                commandStringBuilder.Append(",fields");
            }

            commandStringBuilder.Append(")");

            if (hasOrderBy)
            {
                commandStringBuilder.Append(".sort(orderBy)");
            }

            if (hasHint)
            {
                commandStringBuilder.Append(".hint(hint)");
            }

            if (Skip != 0)
            {
                commandStringBuilder.AppendFormat(".skip({0})", Skip);
            }

            if (Limit != 0)
            {
                commandStringBuilder.AppendFormat(".limit({0})", Limit);
            }

            if (Query != null)
            {
                commandStringBuilder.AppendFormat("\nquery = {0}", Query.ToBsonDocument());
            }

            if (Fields != null)
            {
                commandStringBuilder.AppendFormat("\nfields = {0}", Fields.ToBsonDocument());
            }

            if (hasOrderBy)
            {
                commandStringBuilder.AppendFormat("\norderBy = {0}", orderBy.ToBsonDocument());
            }

            if (hasHint)
            {
                commandStringBuilder.AppendFormat("\nhint = {0}", hint.ToBsonDocument());
            }

            // TODO: implement other options printout if needed

            string commandString = commandStringBuilder.ToString();

            ProfilerUtils.AddMongoTiming(commandString, (long)enumerationEndedEventArgs.Elapsed.TotalMilliseconds, ExecuteType.Read);
        }
Esempio n. 16
0
 /// <summary>
 /// get object value from BsonValue
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static object GetValue(this BsonValue value, Type totype = null)
 {
     return(value.BsonType switch
     {
         BsonType.Array => value.AsBsonArray.ToArray().Select(x => x.GetValue()),
         BsonType.Boolean => value.AsBoolean,
         BsonType.DateTime => value.ToUniversalTime(),
         BsonType.Decimal128 => value.AsDecimal,
         BsonType.Document => totype is null?value.ToJson() : BsonSerializer.Deserialize(value.ToBsonDocument(), totype),
             BsonType.Double => value.AsDouble,
             BsonType.Int32 => value.AsInt32,
             BsonType.Int64 => value.AsInt64,
             BsonType.Null => null,
             BsonType.ObjectId => value.AsString,
             BsonType.String => value.AsString,
             BsonType.Timestamp => value.AsString,
             BsonType.Undefined => null,
             _ => null
     });
 /// <summary>
 /// BsonValue转展示用字符
 /// </summary>
 /// <param name="val"></param>
 /// <returns></returns>
 public static string ConvertForShow(BsonValue val)
 {
     string strVal;
     if (val.IsBsonBinaryData)
     {
         _hasBSonBinary = true;
         return "[二进制数据]";
     }
     if (val.IsBsonNull)
     {
         return "[空值]";
     }
     if (val.IsBsonDocument)
     {
         strVal = val.ToString() + "[包含" + val.ToBsonDocument().ElementCount + "个元素的文档]";
     }
     else
     {
         strVal = val.ToString();
     }
     return strVal;
 }