示例#1
0
        private static long RunFileQuery(string queryFilePath, XDatabaseContext context)
        {
            string query = File.ReadAllText(queryFilePath);

            long rowsWritten = 0;

            using (new TraceWatch(query))
            {
                using (IXTable source = context.Query(query))
                {
                    rowsWritten = source.Count();
                }
            }

            Console.WriteLine($"Done. {rowsWritten:n0} rows written.");
            return(rowsWritten);
        }
示例#2
0
        public override int Next(int desiredCount, CancellationToken cancellationToken)
        {
            // Get the next rows from the real source
            int count = _singlePageSource.SourceNext(desiredCount, cancellationToken);

            if (count > 0)
            {
                // Count source rows
                _sourceRowsTotal += count;

                // Make the asserts run and count matching rows
                _assertRowsTotal += _assertPipeline.Count();
            }
            else
            {
                // If we're done, validate the assert
                long expectedCount = (_type == AssertType.All ? _sourceRowsTotal : 0);
                Assert.AreEqual(expectedCount, _assertRowsTotal, "Pipeline Assert Failed");
            }

            return(count);
        }
        public void Choose()
        {
            XDatabaseContext context = new XDatabaseContext();

            int[] rankPattern = new int[] { 2, 3, 1, 4, 6, 5, 7, 9, 8 };

            // Build three arrays
            int distinctCount = 100000;
            int countPerID    = 9;
            int length        = countPerID * distinctCount;

            int[] id    = new int[length];
            int[] rank  = new int[length];
            int[] value = new int[length];

            for (int i = 0; i < length; ++i)
            {
                id[i]    = i / countPerID;               // ID is the same for three rows at a time
                rank[i]  = rankPattern[i % countPerID];  // Rank is [2, 3, 1] repeating (so the middle is the biggest)
                value[i] = i;                            // Value is the index of the real row
            }

            using (Benchmarker b = new Benchmarker($"Choose [{length:n0}]", 3 * DefaultMeasureMilliseconds))
            {
                b.Measure("Choose", length, () =>
                {
                    IXTable actual = Context.FromArrays(length)
                                     .WithColumn("ID", id)
                                     .WithColumn("Rank", rank)
                                     .WithColumn("Value", value)
                                     .Query("choose Max [Rank] [ID]", context);

                    return(actual.Count());
                });
            }
        }
        public long Run()
        {
            long lastCount = 0;

            try
            {
                while (true)
                {
                    Console.Write("> ");

                    // Read the next query line
                    string nextLine = Console.ReadLine();

                    Stopwatch w = Stopwatch.StartNew();
                    try
                    {
                        if (String.IsNullOrEmpty(nextLine))
                        {
                            return(lastCount);
                        }

                        string[] parts   = nextLine.Split(' ');
                        string   command = parts[0].ToLowerInvariant();
                        switch (command)
                        {
                        case "quit":
                        case "exit":
                            // Stop on empty, "quit", or "exit"
                            return(lastCount);


                        case "back":
                        case "undo":
                            // Unwrap on "back" or "undo"
                            IXTable last = _stages.LastOrDefault();
                            if (last != null)
                            {
                                _pipeline = last;
                                _stages.RemoveAt(_stages.Count - 1);
                                _commands.RemoveAt(_commands.Count - 1);
                            }

                            break;

                        case "save":
                            string tableName = parts[1];
                            string queryPath = _xDatabaseContext.StreamProvider.Path(LocationType.Query, tableName, ".xql");
                            _xDatabaseContext.StreamProvider.WriteAllText(queryPath, String.Join(Environment.NewLine, _commands));
                            Console.WriteLine($"Query saved to \"{tableName}\".");


                            _commands.Clear();
                            _commands.Add($"read \"{tableName}\"");
                            _pipeline = null;
                            _pipeline = AddStage(_commands[0]);

                            break;

                        case "run":
                            LoadScript(parts[1]);
                            break;

                        case "rerun":
                            LoadScript(s_commandCachePath);
                            break;

                        default:
                            try
                            {
                                _pipeline = AddStage(nextLine);
                                break;
                            }
                            catch (Exception ex) when(!Debugger.IsAttached)
                            {
                                Console.WriteLine($"Error: {ex.Message}");
                                continue;
                            }
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        Console.WriteLine(ex.Message);
                        continue;
                    }

                    SaveScript(s_commandCachePath);

                    // Get the first 10 results and 10 columns
                    IXTable firstTenWrapper = _pipeline;
                    firstTenWrapper = _xDatabaseContext.Query("limit 10 10", firstTenWrapper);
                    firstTenWrapper = _xDatabaseContext.Query("write cout", firstTenWrapper);
                    lastCount       = firstTenWrapper.Count();

                    // Get the count
                    RunResult result = _pipeline.RunUntilTimeout(TimeSpan.FromSeconds(3));
                    lastCount += result.RowCount;
                    firstTenWrapper.Reset();

                    Console.WriteLine();
                    Console.WriteLine($"{lastCount:n0} rows in {w.Elapsed.ToFriendlyString()}. {(result.IsComplete ? "" : "[incomplete]")}");
                    Console.WriteLine();
                }
            }
            finally
            {
                if (_pipeline != null)
                {
                    _pipeline.Dispose();
                }
            }
        }