Exemple #1
0
        public async void InnerExecuteFind(bool explain = false)
        {
            Owner.Executing = true;
            Guid operationID = Guid.NewGuid();
            Task <List <BsonDocument> > task = null;
            bool stopRequested = false;

            try
            {
                task = Owner.Service.FindAsync(Owner.Database, Owner.Collection, Find.Deserialize <BsonDocument>(Constants.FindProperty), Sort.Deserialize <BsonDocument>(Constants.SortProperty), Projection.Deserialize <BsonDocument>(Constants.ProjectionProperty), Size, Skip, explain, operationID, Owner.Cts.Token);
                var results = await task.WithCancellation(Owner.Cts.Token);

                Owner.Executing = false;

                if (!explain)
                {
                    Owner.ShowPager = true;
                    StringBuilder sb    = new StringBuilder();
                    int           index = 1;
                    sb.Append("[");
                    foreach (var result in results)
                    {
                        sb.AppendLine();
                        sb.Append("/* # ");
                        sb.Append(index.ToString());
                        sb.AppendLine(" */");
                        sb.Append(result.ToJson(Options.JsonWriterSettings));
                        sb.Append(",");
                        index++;
                    }
                    if (results.Count > 0)
                    {
                        sb.Length -= 1;
                    }
                    sb.AppendLine();
                    sb.Append("]");

                    Owner.RawResult = sb.ToString();
                    sb.Clear();
                }
                else
                {
                    Owner.ShowPager = false;
                    if (results.Count > 0)
                    {
                        Owner.RawResult = results[0].ToJson(Options.JsonWriterSettings);
                    }
                    else
                    {
                        Owner.RawResult = "";
                    }
                }
                Owner.SelectedViewIndex = 0;

                Owner.Root = new ResultsViewModel(results, Owner);
                GC.Collect();
            }
            catch (BsonExtensions.BsonParseException ex)
            {
                LoggerHelper.Logger.Error("Exception while executing find command", ex);
                Owner.SelectedViewIndex = 1;
                Owner.RawResult         = ex.Message;
                Owner.Root = null;
                Messenger.Default.Send(new NotificationMessage <BsonExtensions.BsonParseException>(this, ex, Constants.FindParseException));
            }
            catch (OperationCanceledException)
            {
                stopRequested = true;
            }
            catch (Exception ex)
            {
                Utils.LoggerHelper.Logger.Error("Exception while executing find command", ex);
                Owner.SelectedViewIndex = 1;
                Owner.RawResult         = ex.Message;
                Owner.Root = null;
            }
            finally
            {
                Owner.Executing = false;
            }
            if (stopRequested)
            {
                if (!task.IsCompleted)
                {
                    task.ContinueWith(t =>
                    {
                        if (t.Exception != null)
                        {
                            Utils.LoggerHelper.Logger.Warn("Exception while executing find command", t.Exception);
                        }
                    });
                    var currentOp = await Owner.Service.Eval(Owner.Database, "function() { return db.currentOP(); }");

                    if (currentOp != null)
                    {
                        var operation = currentOp.AsBsonDocument["inprog"].AsBsonArray.FirstOrDefault(item => item.AsBsonDocument.Contains("query") && item.AsBsonDocument["query"].AsBsonDocument.Contains("$comment") && item.AsBsonDocument["query"]["$comment"].AsString == operationID.ToString());
                        if (operation != null)
                        {
                            await Owner.Service.Eval(Owner.Database, string.Format("function() {{ return db.killOp({0}); }}", operation["opid"].AsInt32));
                        }
                    }
                }
            }
        }