예제 #1
0
        public static void XTable_All(string configurationLine, int expectedRowCount, string[] requiredColumns = null)
        {
            SampleDatabase.EnsureBuilt();

            int  requiredColumnCount = (requiredColumns == null ? 0 : requiredColumns.Length);
            long actualRowCount;

            using (CancellationTokenSource source = new CancellationTokenSource())
            {
                IXTable           pipeline       = null;
                ValidatingTable   innerValidator = null;
                CancellationToken token          = source.Token;

                try
                {
                    pipeline       = SampleDatabase.XDatabaseContext.Load("WebRequest");
                    innerValidator = new ValidatingTable(pipeline);
                    pipeline       = SampleDatabase.XDatabaseContext.Query(configurationLine, innerValidator);

                    // Run without requesting any columns. Validate.
                    actualRowCount = pipeline.RunWithoutDispose(token).RowCount;
                    Assert.AreEqual(expectedRowCount, actualRowCount, "XTable should return correct count with no requested columns.");

                    // Reset; Request all columns. Validate.
                    pipeline.Reset();
                    pipeline       = SampleDatabase.XDatabaseContext.Query("write \"Sample.output.csv\"", pipeline);
                    actualRowCount = pipeline.RunWithoutDispose(token).RowCount;
                }
                finally
                {
                    if (pipeline != null)
                    {
                        pipeline.Dispose();
                        pipeline = null;

                        if (innerValidator != null)
                        {
                            Assert.IsTrue(innerValidator.DisposeCalled, "Source must call Dispose on nested sources.");
                        }
                    }
                }
            }
        }
예제 #2
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.RunWithoutDispose();
                }
            }

            Console.WriteLine($"Done. {rowsWritten:n0} rows written.");
            return(rowsWritten);
        }
예제 #3
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.RunWithoutDispose();
            }
            else
            {
                // If we're done, validate the assert
                long expectedCount = (_type == AssertType.All ? _sourceRowsTotal : 0);
                Assert.AreEqual(expectedCount, _assertRowsTotal, "Pipeline Assert Failed");
            }

            return(count);
        }
예제 #4
0
        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.RunWithoutDispose();

                    // 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();
                }
            }
        }