private void CountWithinTimeout(string query, TimeSpan timeout, DateTime asOfDate, IHttpResponse response) { IXTable pipeline = null; try { XDatabaseContext context = _xDatabaseContext; // Build for another moment in time if requested if (asOfDate != _xDatabaseContext.RequestedAsOfDateTime) { context = new XDatabaseContext(_xDatabaseContext) { RequestedAsOfDateTime = asOfDate }; } // Build a Pipeline for the Query pipeline = context.Query(query); // If there was no query, return an empty result if (pipeline == null) { return; } // Try to get the count up to the timeout if (Debugger.IsAttached) { timeout = TimeSpan.MaxValue; } RunResult result = pipeline.RunUntilTimeout(timeout); using (ITabularWriter writer = WriterForFormat("json", response)) { writer.SetColumns(new string[] { "Count", "IsComplete", "RuntimeMs" }); writer.Write(result.RowCount); writer.Write(result.IsComplete); writer.Write((int)result.Elapsed.TotalMilliseconds); writer.NextRow(); } } finally { if (pipeline != null) { pipeline.Dispose(); pipeline = null; } } }
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(); } } }