Exemple #1
0
            /// <summary>
            /// Gets the join result
            /// </summary>
            /// <returns> A string with the query result </returns>
            public string Find(IMongoQuery query = null)
            {
                var myQuery = "{}";

                if (query != null)
                {
                    myQuery = query.ToJson();
                }

                string         queryFunction = "generalSearch ( \"" + this.collection + "\"" + ", " + myQuery + ", " + JsonConvert.SerializeObject(this.joins) + " )";
                BsonJavaScript JoinFunction  = new BsonJavaScript(queryFunction);

                //Calling the stores Mongo Function
                MongoConection      conection = (MongoConection)Conection.getConection();
                BsonArray           result    = conection.getDataBase().Eval(JoinFunction).AsBsonArray;
                List <BsonDocument> documents = new List <BsonDocument>();

                foreach (BsonDocument document in result)
                {
                    document.Set("_id", document.GetElement("_id").Value.ToString());
                    try
                    {
                        document.Set("CreatedTimeStamp", document.GetElement("CreatedTimeStamp").Value.ToString());
                    }
                    catch (Exception ex) { }

                    documents.Add(document);
                }

                return(documents.ToJson());
            }
        /// <summary>
        /// Downloads a specific version of a GridFS file.
        /// </summary>
        /// <param name="stream">The destination stream.</param>
        /// <param name="query">The GridFS file.</param>
        /// <param name="version">The version to download.</param>
        public void Download(Stream stream, IMongoQuery query, int version)
        {
            var fileInfo = FindOne(query, version);

            if (fileInfo == null)
            {
                var    jsonQuery    = query.ToJson();
                string errorMessage = string.Format("GridFS file '{0}' not found.", jsonQuery);
                throw new FileNotFoundException(errorMessage, jsonQuery);
            }
            Download(stream, fileInfo);
        }
        private static IMongoQuery GetQuery(WebSites webSite, ContentTypeQuota quota, AggregateFilter filters, out string queryUniqueString)
        {
            IMongoQuery        query   = null;
            List <IMongoQuery> queries = new List <IMongoQuery>();
            StringBuilder      sb      = new StringBuilder("w" + ((int)webSite).ToString());

            if (quota != null)
            {
                sb.Append("q" + quota.Id.ToString());
                foreach (var filter in quota.Values.Values)
                {
                    queries.Add(Query.EQ(filter.MongoName, GetFilterValue(filter.Value, filter.IsString)));
                    Log.Debug("ProductDataAccess.GetQuery", "quota filter", queries.Last());
                }
            }

            if (filters != null && filters.Filters != null)
            {
                //TODO Replace GetHashCode and Fibonacci approach by other way
                string filterStr = filters.ToString();
                sb.Append(filterStr.GetHashCode());
                for (int i = 0; i < Fibonacci.Length; i++)
                {
                    if (filterStr.Length < Fibonacci[i])
                    {
                        sb.Append(filterStr[Fibonacci[i]]);
                    }
                    else
                    {
                        break;
                    }
                }

                queries.AddRange(GetFilterQuery(filters));
            }

            queryUniqueString = sb.ToString();
            Log.Debug("ProductDataAccess.GetQuery", "queryUniqueString", queryUniqueString);

            // only active products
            queries.Add(Query.EQ("ItemStatus", 1));

            // final query
            var webSiteId = (int)webSite;

            queries.Add(Query.EQ(WebSiteIdField, new BsonInt32(webSiteId)));
            query = Query.And(queries);
            Log.Debug("ProductDataAccess.GetQuery", "Final query", query.ToJson());
            return(query);
        }
Exemple #4
0
 public void LogCount(MongoCollection collection, IMongoQuery query)
 {
     if (_writeToFile == null)
     {
         return;
     }
     _writeToFile.Write("{0:yyyy-MM-dd HH:mm:ss.ffffff} ", DateTime.Now);
     _writeToFile.Write("Count : {0}", collection.zGetFullName());
     if (query != null)
     {
         _writeToFile.Write(" query ");
         _writeToFile.Write(query.ToJson());
     }
     _writeToFile.Write(" count ");
 }
 /// <summary>
 /// Downloads a specific version of a GridFS file.
 /// </summary>
 /// <param name="stream">The destination stream.</param>
 /// <param name="query">The GridFS file.</param>
 /// <param name="version">The version to download.</param>
 public void Download(
     Stream stream,
     IMongoQuery query,
     int version
 ) {
     var fileInfo = FindOne(query, version);
     if (fileInfo == null) {
         var jsonQuery = query.ToJson();
         string errorMessage = string.Format("GridFS file '{0}' not found.", jsonQuery);
         throw new FileNotFoundException(errorMessage, jsonQuery);
     }
     Download(stream, fileInfo);
 }
        public static ProductInfoCollection GetProductInfos(List <ScoringFunctionResult> items, AggregateFilter filters, List <string> fieldsToAdd)
        {
            IStopwatch sw = MonitoringTimers.Current.GetNewStopwatch(true);

            try
            {
                ProductInfoCollection result = new ProductInfoCollection();
                Dictionary <ItemTypes, List <IMongoQuery> > queries = new Dictionary <ItemTypes, List <IMongoQuery> >();
                fieldsToAdd = new List <string>(fieldsToAdd);
                fieldsToAdd.Add(ProductIdField);
                foreach (var item in items)
                {
                    ItemTypes type   = item.GetItemType();
                    int       itemId = item.GetId();
                    if (queries.ContainsKey(type))
                    {
                        queries[type].Add(Query.EQ(ProductIdField, itemId));
                    }
                    else
                    {
                        queries.Add(type, new List <IMongoQuery>());
                        queries[type].Add(Query.EQ(ProductIdField, itemId));
                    }
                }

                // adding filters
                IMongoQuery filterQuery  = null;
                var         queryFilters = GetFilterQuery(filters);
                if (queryFilters != null)
                {
                    if (queryFilters.Count > 1)
                    {
                        filterQuery = Query.And(queryFilters);
                    }
                    else if (queryFilters.Count == 1)
                    {
                        filterQuery = queryFilters[0];
                    }
                }

                foreach (var query in queries)
                {
                    var         idQuery    = Query.Or(query.Value);
                    IMongoQuery finalQuery = null;
                    if (filterQuery != null)
                    {
                        finalQuery = Query.And(new IMongoQuery[] { idQuery, filterQuery });
                    }
                    else
                    {
                        finalQuery = idQuery;
                    }

                    Log.Debug("ProductDataAccess.GetProductInfos", "Final query", finalQuery.ToJson(), query.Key);
                    ItemTypes itemType = query.Key;
                    var       docs     = _mongoContext.GetDocuments <BsonDocument>(finalQuery, _collectionName, fieldsToAdd.ToArray(), items.Count());
                    foreach (var doc in docs)
                    {
                        int         itemId      = doc.GetValue(ProductIdField).AsInt32;
                        ProductInfo ProductInfo = new ProductInfo()
                        {
                            ItemId = itemId, Type = itemType, Fields = new Dictionary <string, string>()
                        };
                        foreach (var field in fieldsToAdd)
                        {
                            ProductInfo.Fields.Add(field, doc.GetValue(field).ToString());
                        }

                        result.AddProductInfo(ProductInfo);
                    }
                }

                return(result);
            }
            finally
            {
                sw.Stop();
                MonitoringTimers.Current.AddTime(Counters.ProductDataAccess_GetProductInfos, sw);
            }
        }