/// <summary>
 /// Initializes a new instance of the MongoDbIndexViewModel class.
 /// </summary>
 public MongoDbIndexViewModel(MongoDbCollectionViewModel collection, string name)
 {
     _collection = collection;
     _name = name;
     ConfirmDropIndex = new RelayCommand(InternalConfirmDropIndex);
     EditIndex = new RelayCommand(InternalEditIndex);
 }
        private async Task LoadCollectionStats(MongoDbCollectionViewModel collection)
        {
            collection.Stats = await Server.MongoDbService.ExecuteRawCommandAsync(Name, BsonDocument.Parse("{ collStats: \"" + collection.Name + "\", verbose: true }"), cts.Token);

            switch (collection.Stats["storageSize"].BsonType)
            {
            case MongoDB.Bson.BsonType.Int32:
                collection.SizeOnDisk = collection.Stats["storageSize"].AsInt32;
                break;

            case MongoDB.Bson.BsonType.Int64:
                collection.SizeOnDisk = collection.Stats["storageSize"].AsInt64;
                break;

            case MongoDB.Bson.BsonType.Double:
                collection.SizeOnDisk = collection.Stats["storageSize"].AsDouble;
                break;
            }

            switch (collection.Stats["count"].BsonType)
            {
            case MongoDB.Bson.BsonType.Int32:
                collection.ItemsCount = collection.Stats["count"].AsInt32;
                break;

            case MongoDB.Bson.BsonType.Int64:
                collection.ItemsCount = collection.Stats["count"].AsInt64;
                break;
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the MongoDbIndexViewModel class.
 /// </summary>
 public MongoDbIndexViewModel(MongoDbCollectionViewModel collection, string name)
 {
     _collection      = collection;
     _name            = name;
     ConfirmDropIndex = new RelayCommand(InternalConfirmDropIndex);
     EditIndex        = new RelayCommand(InternalEditIndex);
 }
        public async void LoadCollections()
        {
            _collections.IsBusy = true;
            try
            {
                var collections = await Server.MongoDbService.GetCollectionsAsync(Name);

                List <MongoDbCollectionViewModel> systemCollections   = new List <MongoDbCollectionViewModel>();
                List <MongoDbCollectionViewModel> standardCollections = new List <MongoDbCollectionViewModel>();
                foreach (var collection in collections)
                {
                    var collectionVm = new MongoDbCollectionViewModel(this, collection["name"].AsString);
                    collectionVm.Database = this;

                    if (collection["name"].AsString.StartsWith("system."))
                    {
                        systemCollections.Add(collectionVm);
                    }
                    else
                    {
                        standardCollections.Add(collectionVm);
                    }
                }
                FolderViewModel systemCollectionsFolder = new FolderViewModel("System", this);
                foreach (var systemCollection in systemCollections.OrderBy(o => o.Name))
                {
                    systemCollectionsFolder.Children.Add(systemCollection);
                }

                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    _collections.Children.Clear();

                    _collections.Children.Add(systemCollectionsFolder);

                    foreach (var collection in standardCollections.OrderBy(o => o.Name))
                    {
                        _collections.Children.Add(collection);
                    }

                    _collectionsLoaded      = true;
                    _collections.ItemsCount = _collections.Children.OfType <MongoDbCollectionViewModel>().Count();
                });
                _collections.IsBusy = false;

                await LoadCollectionsStats(systemCollections.Union(standardCollections));
            }
            catch (Exception ex)
            {
                Utils.LoggerHelper.Logger.Error(string.Format("Failed to get collections on database '{0}', server '{1}'", Name, Server.Name), ex);
            }
            finally
            {
                _collections.IsBusy = false;
            }
        }
        private async void InnerCreateNewCollection(NotificationMessage <CreateCollectionViewModel> message)
        {
            if (message.Notification == Constants.CreateCollectionMessage && message.Target == this)
            {
                try
                {
                    _collections.IsBusy = true;
                    MongoDB.Driver.CreateCollectionOptions options = new MongoDB.Driver.CreateCollectionOptions();
                    options.AutoIndexId      = message.Content.AutoIndexId;
                    options.Capped           = message.Content.Capped;
                    options.MaxDocuments     = message.Content.MaxDocuments;
                    options.MaxSize          = message.Content.MaxSize;
                    options.UsePowerOf2Sizes = message.Content.UsePowerOf2Sizes;
                    if (!string.IsNullOrWhiteSpace(message.Content.StorageEngine))
                    {
                        options.StorageEngine = BsonDocument.Parse(message.Content.StorageEngine);
                    }
                    await Server.MongoDbService.CreateCollectionAsync(Name, message.Content.Name, options);

                    var newCollection = new MongoDbCollectionViewModel(this, message.Content.Name);
                    newCollection.IsSelected     = true;
                    newCollection.IsBusy         = true;
                    this.IsExpanded              = true;
                    this._collections.IsExpanded = true;
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        _collections.Children.Add(newCollection);
                        _collections.ItemsCount = _collections.Children.OfType <MongoDbCollectionViewModel>().Count();
                    });
                    await LoadCollectionStats(newCollection);
                }
                catch (Exception ex)
                {
                    Utils.LoggerHelper.Logger.Error(string.Format("Failed to create collection '{0}' on database '{1}', server '{2}'", message.Content.Name, Name, Server.Name), ex);
                }
                finally
                {
                    _collections.IsBusy = false;
                }
            }
        }
        private async void InnerCreateNewCollection(NotificationMessage<CreateCollectionViewModel> message)
        {
            if (message.Notification == Constants.CreateCollectionMessage && message.Target == this)
            {
                try
                {
                    _collections.IsBusy = true;
                    MongoDB.Driver.CreateCollectionOptions options = new MongoDB.Driver.CreateCollectionOptions();
                    options.AutoIndexId = message.Content.AutoIndexId;
                    options.Capped = message.Content.Capped;
                    options.MaxDocuments = message.Content.MaxDocuments;
                    options.MaxSize = message.Content.MaxSize;
                    options.UsePowerOf2Sizes = message.Content.UsePowerOf2Sizes;
                    if (!string.IsNullOrWhiteSpace(message.Content.StorageEngine))
                        options.StorageEngine = BsonDocument.Parse(message.Content.StorageEngine);
                    await Server.MongoDbService.CreateCollectionAsync(Name, message.Content.Name, options);

                    var newCollection = new MongoDbCollectionViewModel(this, message.Content.Name);
                    newCollection.IsSelected = true;
                    newCollection.IsBusy = true;
                    this.IsExpanded = true;
                    this._collections.IsExpanded = true;
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        _collections.Children.Add(newCollection);
                        _collections.ItemsCount = _collections.Children.OfType<MongoDbCollectionViewModel>().Count();
                    });
                    await LoadCollectionStats(newCollection);
                }
                catch (Exception ex)
                {
                    Utils.LoggerHelper.Logger.Error(string.Format("Failed to create collection '{0}' on database '{1}', server '{2}'", message.Content.Name, Name, Server.Name), ex);
                }
                finally
                {
                    _collections.IsBusy = false;
                }
            }
        }
        private async Task LoadCollectionStats(MongoDbCollectionViewModel collection)
        {
            collection.Stats = await Server.MongoDbService.ExecuteRawCommandAsync(Name, BsonDocument.Parse("{ collStats: \"" + collection.Name + "\", verbose: true }"), cts.Token);
            switch (collection.Stats["storageSize"].BsonType)
            {
                case MongoDB.Bson.BsonType.Int32:
                    collection.SizeOnDisk = collection.Stats["storageSize"].AsInt32;
                    break;
                case MongoDB.Bson.BsonType.Int64:
                    collection.SizeOnDisk = collection.Stats["storageSize"].AsInt64;
                    break;
                case MongoDB.Bson.BsonType.Double:
                    collection.SizeOnDisk = collection.Stats["storageSize"].AsDouble;
                    break;
            }

            switch (collection.Stats["count"].BsonType)
            {
                case MongoDB.Bson.BsonType.Int32:
                    collection.ItemsCount = collection.Stats["count"].AsInt32;
                    break;
                case MongoDB.Bson.BsonType.Int64:
                    collection.ItemsCount = collection.Stats["count"].AsInt64;
                    break;
            }
        }
        public async void LoadCollections()
        {
            _collections.IsBusy = true;
            try
            {
                var collections = await Server.MongoDbService.GetCollectionsAsync(Name);

                List<MongoDbCollectionViewModel> systemCollections = new List<MongoDbCollectionViewModel>();
                List<MongoDbCollectionViewModel> standardCollections = new List<MongoDbCollectionViewModel>();
                foreach (var collection in collections)
                {
                    var collectionVm = new MongoDbCollectionViewModel(this, collection["name"].AsString);
                    collectionVm.Database = this;

                    if (collection["name"].AsString.StartsWith("system."))
                        systemCollections.Add(collectionVm);
                    else
                        standardCollections.Add(collectionVm);
                }
                FolderViewModel systemCollectionsFolder = new FolderViewModel("System", this);
                foreach (var systemCollection in systemCollections.OrderBy(o => o.Name))
                    systemCollectionsFolder.Children.Add(systemCollection);

                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    _collections.Children.Clear();

                    _collections.Children.Add(systemCollectionsFolder);

                    foreach (var collection in standardCollections.OrderBy(o => o.Name))
                        _collections.Children.Add(collection);

                    _collectionsLoaded = true;
                    _collections.ItemsCount = _collections.Children.OfType<MongoDbCollectionViewModel>().Count();
                });
                _collections.IsBusy = false;

                await LoadCollectionsStats(systemCollections.Union(standardCollections));
            }
            catch (Exception ex)
            {
                Utils.LoggerHelper.Logger.Error(string.Format("Failed to get collections on database '{0}', server '{1}'", Name, Server.Name), ex);
            }
            finally
            {
                _collections.IsBusy = false;
            }
        }