public void CreateIndexes(ConnectionInfo cnn, string database, string collection, IEnumerable<IndexDescriptor> indexes)
        {
            var operation = new OperationStatus
            {
                IsIndeterminate = true,
                Title = Properties.Resources.ManageIndexes_Applying,
                Description = Properties.Resources.ManageIndexes_Dropping
            };
            _mingApp.AddLongRunningOperation(operation);

            var task = new CancelableTask(() => DoCreateIndexes(operation, cnn, database, collection, indexes), null);
            task.Execute();
        }
        public void CopyCollections(IEnumerable<CopyCollectionDefinition> collections)
        {
            var operation = new OperationStatus
            {
                IsIndeterminate = true,
                Title = string.Format(Properties.Resources.CopyingCollections_Title, collections.Count()),
                Description = Properties.Resources.CopyingCollections_Counting
            };
            _mingApp.AddLongRunningOperation(operation);

            var task = new CancelableTask(() => DoCopyCollections(operation, collections), null);
            task.Execute();
        }
        public void CompactCollections(MingTreeViewItem node, ConnectionInfo cnnInfo, string database)
        {
            var confirm = new MessageBox();
            var message = database == null
                ? string.Format(Properties.Resources.CompactCollections_ConfirmAll, cnnInfo.Host, cnnInfo.Port)
                : string.Format(Properties.Resources.CompactCollections_Confirm, database);
            if (!confirm.ShowConfirm(_mingApp.MainWindow, message))
            {
                return;
            }

            var operation = new OperationStatus
            {
                IsIndeterminate = true,
                Title = Properties.Resources.CompactCollections_Title,
                Description = Properties.Resources.CompactCollections_DescCounting
            };
            _mingApp.AddLongRunningOperation(operation);
            var task = new CancelableTask(() => DoCompactCollections(operation, cnnInfo, database), null);
            task.Execute();
        }
 public void CopyCollection(MingTreeViewItem node, ConnectionInfo cnnInfo, string database, string collection)
 {
     var ted = new TextEntryDialogController(_mingApp.MainWindow, Properties.Resources.CopyCollection_Title, Properties.Resources.CreateCollection_Prompt);
     ted.Text = collection;
     if (!ted.ShowDialog())
     {
         return;
     }
     var name = ted.Text.Trim();
     if (string.IsNullOrWhiteSpace(name))
     {
         return;
     }
     var operation = new OperationStatus
     {
         IsIndeterminate = true,
         Title = Properties.Resources.CopyCollection_OpTitle,
         Description = Properties.Resources.CopyCollection_Counting
     };
     _mingApp.AddLongRunningOperation(operation);
     var task = new CancelableTask(() => DoCopyCollection(node, operation, cnnInfo, database, collection, name), null);
     task.Execute();
 }
        private void DoCopyCollection(MingTreeViewItem node, OperationStatus operation, ConnectionInfo cnnInfo, string database, string collection, string newCollection)
        {
            try
            {
                if (MongoUtilities.Create(cnnInfo).GetDatabase(database).CollectionExists(newCollection))
                {
                    operation.IsSuccess = false;
                    operation.IsComplete = true;
                    operation.Description = string.Format(Properties.Resources.CopyCollection_Exists, newCollection);
                    return;
                }
                var src = MongoUtilities.Create(cnnInfo).GetDatabase(database).GetCollection(collection);
                var dest = MongoUtilities.Create(cnnInfo).GetDatabase(database).GetCollection(newCollection);
                var count = src.Count();
                operation.IsIndeterminate = false;
                operation.Description = string.Format(Properties.Resources.CopyCollection_Copying, 0);

                var copied = 0;
                var srcCur = src.FindAll().SetSnapshot();
                foreach (var item in srcCur)
                {
                    dest.Insert(item);
                    copied++;
                    operation.Description = string.Format(Properties.Resources.CopyCollection_Copying, copied);
                    operation.PercentComplete = (int)((100.0 / (double)count) * (double)copied);
                }
                operation.IsSuccess = true;
                operation.IsComplete = true;
            }
            catch (Exception ex)
            {
                operation.Description = string.Format(Properties.Resources.CopyCollections_Error, ex.Message);
                operation.IsSuccess = false;
                operation.IsComplete = true;
            }
            _mingApp.RefreshTreeViewItem(node.Parent);
        }
 private void DoCompactCollections(OperationStatus operation, ConnectionInfo cnnInfo, string database)
 {
     try
     {
         var databases = new List<string>();
         var cnn = MongoUtilities.Create(cnnInfo);
         if (database != null)
         {
             databases.Add(database);
         }
         else
         {
             cnn.GetDatabaseNames().ToList().ForEach(item => databases.Add(item));
         }
         var collections = new List<Tuple<string, string>>();
         databases.ForEach(db => cnn.GetDatabase(db).GetCollectionNames().ToList().ForEach(col => collections.Add(new Tuple<string, string>(db, col))));
         var done = 0;
         operation.IsIndeterminate = false;
         foreach (var col in collections)
         {
             operation.Description = string.Format(Properties.Resources.CompactCollections_DescCompacting, col.Item1, col.Item2);
             operation.PercentComplete = (int)((100.0 / (double)collections.Count) * (double)done);
             cnn.GetDatabase(col.Item1).Eval(string.Format("db.runCommand({{ compact: \"{0}\", force: true }})", col.Item2));
             done++;
         }
         operation.Description = Properties.Resources.CompactCollections_DescComplete;
         operation.IsSuccess = true;
         operation.IsComplete = true;
     }
     catch (Exception ex)
     {
         operation.Description = string.Format(Properties.Resources.CompactCollections_Error, ex.Message);
         operation.IsSuccess = false;
         operation.IsComplete = true;
     }
 }
        private void DoCopyCollections(OperationStatus operation, IEnumerable<CopyCollectionDefinition> collections)
        {
            CopyCollectionDefinition currentJob = null;
            try
            {
                long docs = 0;
                var colsList = collections.ToList();
                colsList.ForEach(item =>
                    docs += MongoUtilities.Create(item.Source.Connection).GetDatabase(item.Source.Database).GetCollection(item.Source.Collection).Count());

                operation.PercentComplete = 0;
                operation.IsIndeterminate = false;
                long copied = 0;
                foreach (var job in collections)
                {
                    currentJob = job;

                    var src = MongoUtilities.Create(job.Source.Connection).GetDatabase(job.Source.Database).GetCollection(job.Source.Collection);
                    var dest = MongoUtilities.Create(job.Target.Connection).GetDatabase(job.Target.Database).GetCollection(job.Target.Collection);

                    // TODO: Batch these up
                    var srcCur = src.FindAll().SetSnapshot();
                    var thisCol = 0;
                    foreach (var item in srcCur)
                    {
                        dest.Insert(item);
                        copied++;
                        thisCol++;

                        operation.Description = string.Format(Properties.Resources.CopyingCollections_Copying,
                            job.Source.Database, job.Source.Collection,
                            job.Target.Database, job.Target.Collection,
                            thisCol);
                        var percent = (int)((100.0 / (double)docs) * (double)copied);
                        if (percent > 100)
                        {
                            operation.IsIndeterminate = true;
                        }
                        else
                        {
                            operation.PercentComplete = percent;
                        }
                    }

                    operation.Description = string.Format(Properties.Resources.CopyingCollections_CreatingIndexes,
                        job.Target.Database, job.Target.Collection);
                    foreach (var idx in MongoUtilities.Create(job.Source.Connection).GetDatabase(job.Source.Database).GetCollection(job.Source.Collection).GetIndexes())
                    {
                        MongoUtilities.Create(job.Target.Connection).GetDatabase(job.Target.Database).GetCollection(job.Target.Collection).CreateIndex(idx.Key);
                    }
                }

                operation.IsSuccess = true;
                operation.IsComplete = true;
                operation.Description = string.Format(Properties.Resources.CopyingCollections_Complete, collections.Count(), copied);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("not master"))
                {
                    operation.Description = string.Format(Properties.Resources.CopyingCollections_NotMaster, currentJob.Target.Connection.Host, currentJob.Target.Connection.Port);
                }
                else if (ex.Message.Contains("invalid collection name"))
                {
                    operation.Description = string.Format(Properties.Resources.CopyingCollections_InvalidName, currentJob.Target.Collection);
                }
                else
                {
                    operation.Description = string.Format(Properties.Resources.CopyCollections_Error, ex.Message);
                }
                operation.IsSuccess = false;
                operation.IsComplete = true;
            }
        }
        public void DoCreateIndexes(OperationStatus operation, ConnectionInfo cnn, string database, string collection, IEnumerable<IndexDescriptor> indexes)
        {
            var col = MongoUtilities.Create(cnn).GetDatabase(database).GetCollection(collection);

            try
            {
                col.DropAllIndexes();
            }
            catch (Exception ex)
            {
                Utilities.LogException(ex);
            }

            var count = 0;
            var errors = new List<string>();
            foreach (var index in indexes)
            {
                operation.PercentComplete = (int) ((100.0 / (double)indexes.Count()) * (double) count);
                operation.Description = string.Format(Properties.Resources.ManageIndexes_Creating, ++count);

                try
                {
                    var keys = new IndexKeysBuilder();

                    foreach (var property in index.IndexedProperties)
                    {
                        switch (property.IndexType)
                        {
                            case IndexType.Descending:
                                keys.Descending(property.PropertyName);
                                break;
                            case IndexType.Geospatial:
                                keys.GeoSpatial(property.PropertyName);
                                break;
                            default:
                                keys.Ascending(property.PropertyName);
                                break;
                        }
                    }

                    var options = new IndexOptionsBuilder();
                    options.SetSparse(index.IsSparse);
                    options.SetUnique(index.IsUnique);

                    col.CreateIndex(keys, options);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("2d has to be first"))
                    {
                        errors.Add(string.Format(Properties.Resources.ManageIndexes_GeospatialNotFirst, count));
                    }
                    else if (ex.Message.Contains("geo field") || ex.Message.Contains("location object expected"))
                    {
                        errors.Add(string.Format(Properties.Resources.ManageIndexes_InvalidGeospatial, count));
                    }
                    else if (ex.Message.Contains("duplicate key"))
                    {
                        errors.Add(string.Format(Properties.Resources.ManageIndexes_InvalidUnique, count));
                    }
                    else
                    {
                        errors.Add(string.Format(Properties.Resources.ManageIndexes_UnknownError, count, ex.Message));
                        Utilities.LogException(ex);
                    }
                }
            }

            operation.IsComplete = true;
            if (errors.Count == 0)
            {
                operation.IsSuccess = true;
                operation.Description = string.Format(Properties.Resources.ManageIndexes_Success, indexes.Count());
            }
            else
            {
                operation.IsSuccess = false;
                var errorString = new StringBuilder();
                errors.ToList().ForEach(
                    error =>
                    {
                        errorString.Append("\n");
                        errorString.Append(error);
                    });
                operation.Description = string.Format(Properties.Resources.ManageIndexes_Fail, errors.Count, errorString);
            }
        }
Exemple #9
0
 public void AddLongRunningOperation(OperationStatus status)
 {
     _operations.Add(status);
 }
Exemple #10
0
 public void RemoveLongRunningOperation(OperationStatus status)
 {
     _operations.Remove(status);
 }