public ActionResult Render([FromForm] IEnumerable <string> target, [FromForm] string from, [FromForm] string until, [FromForm] string format, [FromForm] string maxDataPoints)
        {
            if (!from.TryParseGraphiteTime(out DateTime fromDate) ||
                !until.TryParseGraphiteTime(out DateTime toDate) ||
                !int.TryParse(maxDataPoints, out int count))
            {
                return(StatusCode((int)HttpStatusCode.OK));
            }

            if (!format.Equals("json", StringComparison.InvariantCultureIgnoreCase))
            {
                return(StatusCode((int)HttpStatusCode.BadRequest));
            }

            _dataSource.Span = new TimeSeriesSpan(fromDate, toDate, count);
            var optimizer = new QueryOptimizer(new Parser()
            {
                DataSource = _dataSource
            });

            return(Json(optimizer.ParseAndOptimize(target).Select(g => new
            {
                target = g.Name,
                datapoints = g.TimestampedPoints()
            })));
        }
Exemple #2
0
        internal static Settings GetInstance()
        {
            if (instance == null)
            {
                if (!File.Exists(SettingsXmlPath))
                {
                    (instance = new Settings()).setDefaultValues();
                }
                else
                {
                    using (TextReader textReader = new StreamReader(SettingsXmlPath))
                    {
                        Settings s = null;
                        try
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                            s = serializer.Deserialize(textReader) as Settings;
                        }
                        catch (Exception ex)
                        {
                            if (QueryOptimizer.GetLoger() != null)
                            {
                                QueryOptimizer.GetLoger().Log("QueryOptimizer", string.Format("Unable to read file with setting\n{0}", ex), MessageLevel.Error);
                            }
                        }

                        textReader.Close();
                        instance = s ?? new Settings();
                    }
                }
            }
            return(instance);
        }
Exemple #3
0
        /// <summary>
        /// Delete all records that match a where condition
        /// </summary>
        /// <param name="where">The expression that determines the records deleted</param>
        /// <param name="optimizer">The optimization object to use for running queries</param>
        /// <param name="startup">The startup options</param>
        /// <param name="connectionString">The database connection string to use for this access</param>
        /// <returns>The number of rows deleted</returns>
        public static int DeleteData(Expression <Func <Gravitybox.GeoLocation.EFDAL.CanadaPostalCodeQuery, bool> > where, QueryOptimizer optimizer, ContextStartup startup, string connectionString)
        {
            if (optimizer == null)
            {
                optimizer = new QueryOptimizer();
            }
            if (startup == null)
            {
                startup = new ContextStartup(null);
            }

            using (var connection = Gravitybox.GeoLocation.EFDAL.DBHelper.GetConnection(Gravitybox.GeoLocation.EFDAL.Util.StripEFCS2Normal(connectionString)))
            {
                using (var dc = new DataContext(connection))
                {
                    var template = dc.GetTable <Gravitybox.GeoLocation.EFDAL.CanadaPostalCodeQuery>();
                    using (var cmd = BusinessEntityQuery.GetCommand <Gravitybox.GeoLocation.EFDAL.CanadaPostalCodeQuery>(dc, template, where))
                    {
                        if (!startup.DefaultTimeout && startup.CommandTimeout > 0)
                        {
                            cmd.CommandTimeout = startup.CommandTimeout;
                        }
                        else
                        {
                            var cb = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);
                            cmd.CommandTimeout = cb.ConnectTimeout;
                        }

                        var parser = LinqSQLParser.Create(cmd.CommandText, LinqSQLParser.ObjectTypeConstants.Table);
                        var sql    = "CREATE TABLE #t ([RowId] [Int])";
                        sql += "set rowcount " + optimizer.ChunkSize + ";";
                        sql += "INSERT INTO #t ([RowId])";
                        sql += "SELECT [t0].[RowId] #t\r\n";
                        sql += parser.GetFromClause(optimizer) + "\r\n";
                        sql += parser.GetWhereClause();
                        sql += "\r\n";

                        var noLock = string.Empty;
                        noLock          = (optimizer.NoLocking ? "WITH (READUNCOMMITTED) " : string.Empty);
                        sql            += "DELETE [CanadaPostalCode] FROM [dbo].[CanadaPostalCode] " + noLock + "INNER JOIN #t ON [dbo].[CanadaPostalCode].[RowId] = #t.[RowId]\r\n";
                        sql            += ";select @@rowcount";
                        sql             = "set ansi_nulls off;" + sql + ";drop table #t;";
                        cmd.CommandText = sql;
                        dc.Connection.Open();
                        var startTime = DateTime.Now;
                        var affected  = 0;
                        var count     = 0;
                        do
                        {
                            count     = (int)cmd.ExecuteScalar();
                            affected += count;
                        } while (count > 0 && optimizer.ChunkSize > 0);
                        var endTime = DateTime.Now;
                        optimizer.TotalMilliseconds = (long)endTime.Subtract(startTime).TotalMilliseconds;
                        dc.Connection.Close();
                        return(affected);
                    }
                }
            }
        }
        public LinqToCloudIndexWithSpatial(CloudSearchSearchContext context, IExecutionContext[] executionContexts) : base(context, executionContexts)
        {
            var parameters = new CloudIndexParameters(context.Index.Configuration.IndexFieldStorageValueFormatter, context.Index.Configuration.VirtualFields, context.Index.FieldNameTranslator, typeof(TItem), false, executionContexts, context.Index.Schema);

            this.queryMapper    = new CloudSpatialQueryMapper(parameters);
            this.queryOptimizer = new SpatialCloudQueryOptimizer();
        }
        /// <summary>
        /// Delete all records that match a where condition
        /// </summary>
        /// <param name="where">The expression that determines the records deleted</param>
        /// <param name="optimizer">The optimization object to use for running queries</param>
        /// <param name="startup">The startup options</param>
        /// <param name="connectionString">The database connection string to use for this access</param>
        /// <returns>The number of rows deleted</returns>
        public static int DeleteData(Expression <Func <Gravitybox.Datastore.EFDAL.DeleteQueueItemQuery, bool> > where, QueryOptimizer optimizer, ContextStartup startup, string connectionString)
        {
            if (optimizer == null)
            {
                optimizer = new QueryOptimizer();
            }
            if (startup == null)
            {
                startup = new ContextStartup(null);
            }

            using (var connection = Gravitybox.Datastore.EFDAL.DBHelper.GetConnection(Gravitybox.Datastore.EFDAL.Util.StripEFCS2Normal(connectionString)))
            {
                using (var dc = new DataContext(connection))
                {
                    var template = dc.GetTable <Gravitybox.Datastore.EFDAL.DeleteQueueItemQuery>();
                    using (var cmd = BusinessEntityQuery.GetCommand <Gravitybox.Datastore.EFDAL.DeleteQueueItemQuery>(dc, template, where))
                    {
                        if (!startup.DefaultTimeout && startup.CommandTimeout > 0)
                        {
                            cmd.CommandTimeout = startup.CommandTimeout;
                        }
                        else
                        {
                            var cb = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);
                            cmd.CommandTimeout = cb.ConnectTimeout;
                        }

                        var parser = LinqSQLParser.Create(cmd.CommandText, LinqSQLParser.ObjectTypeConstants.Table);
                        var sb     = new StringBuilder();
                        sb.AppendLine("SET ROWCOUNT " + optimizer.ChunkSize + ";");
                        sb.AppendLine("delete [X] from [dbo].[DeleteQueueItem] [X] inner join (");
                        sb.AppendLine("SELECT [t0].[ParentRowId], [t0].[RecordIdx]");
                        sb.AppendLine(parser.GetFromClause(optimizer));
                        sb.AppendLine(parser.GetWhereClause());
                        sb.AppendLine(") AS [Extent2]");
                        sb.AppendLine("ON [X].[ParentRowId] = [Extent2].[ParentRowId] AND [X].[RecordIdx] = [Extent2].[RecordIdx]");
                        sb.AppendLine("select @@ROWCOUNT");
                        cmd.CommandText = sb.ToString();
                        dc.Connection.Open();
                        var startTime = DateTime.Now;
                        var affected  = 0;
                        var count     = 0;
                        do
                        {
                            count     = (int)cmd.ExecuteScalar();
                            affected += count;
                        } while (count > 0 && optimizer.ChunkSize > 0);
                        var endTime = DateTime.Now;
                        optimizer.TotalMilliseconds = (long)endTime.Subtract(startTime).TotalMilliseconds;
                        dc.Connection.Close();
                        return(affected);
                    }
                }
            }
        }
Exemple #6
0
        private void setDefaultValues()
        {
            this.maxTimeSearchingBestQueryPlan = 500000;
            if (QueryOptimizer.GetLoger() != null)
            {
                QueryOptimizer.GetLoger().Log("QueryOptimizer", "Assigned default values", MessageLevel.Info);
            }

            this.SettingsValueChanged();
        }
        public LinqToSolrIndexWithSpatial(SolrSearchContext context, IExecutionContext[] executionContexts) : base(context, executionContexts)
        {
            var parameters =
                new SolrIndexParameters(
                    context.Index.Configuration.IndexFieldStorageValueFormatter,
                    context.Index.Configuration.VirtualFieldProcessors,
                    context.Index.FieldNameTranslator, executionContexts,
                    context.Index.Configuration.FieldMap);

            this.queryMapper    = new SolrSpatialQueryMapper(parameters);
            this.queryOptimizer = new SpatialSolrQueryOptimizer();
        }
        protected override TQuery GetQuery(Expression expression)
        {
            Trace(expression, "Expression");

            // here we inject a special expression parser that fixes a few issues slated to be resolved in later releases of SC7
            IndexQuery rawQuery = new ExpressionParserWithSpatial(typeof(TElement), ItemType, FieldNameTranslator).Parse(expression);

            Trace(rawQuery, "Raw query:");
            IndexQuery optimizedQuery = QueryOptimizer.Optimize(rawQuery);

            Trace(optimizedQuery, "Optimized query:");
            TQuery mappedQuery = QueryMapper.MapQuery(optimizedQuery);

            return(mappedQuery);
        }
Exemple #9
0
        internal CouchDatabase(IFlurlClient flurlClient, CouchOptions options, QueryContext queryContext)
        {
            _flurlClient  = flurlClient;
            _options      = options;
            _queryContext = queryContext;

            var queryOptimizer  = new QueryOptimizer();
            var queryTranslator = new QueryTranslator(options);
            var querySender     = new QuerySender(flurlClient, queryContext);
            var queryCompiler   = new QueryCompiler(queryOptimizer, queryTranslator, querySender);

            _queryProvider = new CouchQueryProvider(queryCompiler);

            Security       = new CouchSecurity(NewRequest);
            LocalDocuments = new LocalDocuments(flurlClient, queryContext);
        }
        public static async Task <ChangesFeedResponse <TSource> > QueryWithFilterAsync <TSource>(this IFlurlRequest request, CouchOptions options, ChangesFeedFilter filter,
                                                                                                 CancellationToken cancellationToken)
            where TSource : CouchDocument
        {
            if (filter is DocumentIdsChangesFeedFilter documentIdsFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_doc_ids")
                       .PostJsonAsync(new ChangesFeedFilterDocuments(documentIdsFilter.Value), cancellationToken)
                       .ReceiveJson <ChangesFeedResponse <TSource> >()
                       .ConfigureAwait(false));
            }

            if (filter is SelectorChangesFeedFilter <TSource> selectorFilter)
            {
                MethodCallExpression whereExpression = Expression.Call(typeof(Queryable), nameof(Queryable.Where),
                                                                       new[] { typeof(TSource) }, Expression.Constant(Array.Empty <TSource>().AsQueryable()), selectorFilter.Value);

                var        optimizer      = new QueryOptimizer();
                Expression optimizedQuery = optimizer.Optimize(whereExpression);
                var        jsonSelector   = new QueryTranslator(options).Translate(optimizedQuery);
                return(await request
                       .WithHeader("Content-Type", "application/json")
                       .SetQueryParam("filter", "_selector")
                       .PostStringAsync(jsonSelector, cancellationToken)
                       .ReceiveJson <ChangesFeedResponse <TSource> >()
                       .ConfigureAwait(false));
            }

            if (filter is DesignChangesFeedFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_design")
                       .GetJsonAsync <ChangesFeedResponse <TSource> >(cancellationToken)
                       .ConfigureAwait(false));
            }

            if (filter is ViewChangesFeedFilter viewFilter)
            {
                return(await request
                       .SetQueryParam("filter", "_view")
                       .SetQueryParam("view", viewFilter.Value)
                       .GetJsonAsync <ChangesFeedResponse <TSource> >(cancellationToken)
                       .ConfigureAwait(false));
            }
            throw new InvalidOperationException($"Filter of type {filter.GetType().Name} not supported.");
        }
Exemple #11
0
        internal CouchDatabase(IFlurlClient flurlClient, CouchOptions options, QueryContext queryContext, string?discriminator)
        {
            _feedChangeLineStartPattern = new Regex(@"{""seq");
            _flurlClient   = flurlClient;
            _options       = options;
            _queryContext  = queryContext;
            _discriminator = discriminator;

            var queryOptimizer  = new QueryOptimizer();
            var queryTranslator = new QueryTranslator(options);
            var querySender     = new QuerySender(flurlClient, queryContext);
            var queryCompiler   = new QueryCompiler(queryOptimizer, queryTranslator, querySender, _discriminator);

            _queryProvider = new CouchQueryProvider(queryCompiler);

            Security       = new CouchSecurity(NewRequest);
            LocalDocuments = new LocalDocuments(flurlClient, queryContext);
        }
Exemple #12
0
        public void TestQueryElementView()
        {
            DTOQuery inputQuery = new DTOQuery();

            inputQuery.QueryText = "Student where na==2 and bc==3;";
            IQueryAnalyzer analizer      = new EBNFQueryAnalyzer();
            var            queryTree     = analizer.ParseQuery(inputQuery);
            QueryPlanTree  queryPlanTree = QueryOptimizer.BuildTreeSummary(queryTree, new QueryParameters());

            // queryTree.GetComposite().elements[ElementType.SELECT];
            SelectStatement query_elements = (SelectStatement)analizer.ParseQuery(inputQuery);
            //  queryTree.GetComposite().GetElement(ElementType.WHERE).GetComposite().GetElement(ElementType.COMPERISION).GetComposite().GetElement(ElementType.LEFT_OPERAND).GetComposite().GetElement(ElementType.CLASS_PROPERTY);
            var elements = query_elements.GetElements();
            //var test = QueryOptimizer.findElementType(queryTree, ElementType.CLASS_PROPERTY);
            ClassProperty classProperty = new ClassProperty();

            Console.WriteLine(elements[ElementType.CLASS_NAME].ToString());
            SelectStatement s = new SelectStatement();
        }
Exemple #13
0
 private void SettingsValueChanged()
 {
     try
     {
         using (TextWriter textWriter = new StreamWriter(SettingsXmlPath))
         {
             XmlSerializer serializer = new XmlSerializer(typeof(Settings));
             serializer.Serialize(textWriter, instance);
             textWriter.Close();
         }
     }
     catch (Exception ex)
     {
         if (QueryOptimizer.GetLoger() != null)
         {
             QueryOptimizer.GetLoger().Log("QueryOptimizer", string.Format("Unable to save file with setting\n{0}", ex), MessageLevel.Error);
         }
     }
 }
Exemple #14
0
 public void SetUp()
 {
     _optimizer = new QueryOptimizer();
 }
 /// <summary>
 /// Delete all records that match a where condition
 /// </summary>
 /// <param name="where">The expression that determines the records deleted</param>
 /// <param name="optimizer">The optimization object to use for running queries</param>
 /// <returns>The number of rows deleted</returns>
 public static int DeleteData(Expression <Func <Gravitybox.Datastore.EFDAL.DeleteQueueItemQuery, bool> > where, QueryOptimizer optimizer)
 {
     return(DeleteData(where : where, optimizer: optimizer, startup: new ContextStartup(null), connectionString: Gravitybox.Datastore.EFDAL.DatastoreEntities.GetConnectionString()));
 }
Exemple #16
0
 /// <summary>
 /// Delete all records that match a where condition
 /// </summary>
 /// <param name="where">The expression that determines the records deleted</param>
 /// <param name="optimizer">The optimization object to use for running queries</param>
 /// <returns>The number of rows deleted</returns>
 public static int DeleteData(Expression <Func <Gravitybox.GeoLocation.EFDAL.CanadaPostalCodeQuery, bool> > where, QueryOptimizer optimizer)
 {
     return(DeleteData(where : where, optimizer: optimizer, startup: new ContextStartup(null), connectionString: Gravitybox.GeoLocation.EFDAL.GeoLocationEntities.GetConnectionString()));
 }