Esempio n. 1
0
 /// <summary>
 /// OK
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cmdOK_Click(object sender, EventArgs e)
 {
     if (txtCollectionName.Text != String.Empty)
     {
         try
         {
             String ErrMessage;
             SystemManager.GetCurrentDataBase().IsCollectionNameValid(txtCollectionName.Text, out ErrMessage);
             if (ErrMessage != null)
             {
                 MyMessageBox.ShowMessage("Create MongoDatabase", "Argument Exception", ErrMessage, true);
                 return;
             }
             if (chkAdvance.Checked)
             {
                 CollectionOptionsBuilder option = new CollectionOptionsBuilder();
                 option.SetCapped(chkIsCapped.Checked);
                 option.SetMaxSize((long)numMaxSize.Value);
                 option.SetMaxDocuments((long)numMaxDocument.Value);
                 option.SetAutoIndexId(chkIsAutoIndexId.Checked);
                 Result = MongoDBHelper.CreateCollectionWithOptions(strSvrPathWithTag, treeNode, txtCollectionName.Text, option);
             }
             else
             {
                 Result = MongoDBHelper.CreateCollection(strSvrPathWithTag, treeNode, txtCollectionName.Text);
             }
             this.Close();
         }
         catch (ArgumentException ex)
         {
             MyMessageBox.ShowMessage("Create MongoDatabase", "Argument Exception", ex.Message, true);
             Result = false;
         }
     }
 }
Esempio n. 2
0
        public void CheckCollection(string collectionName, long collectionSize, long?collectionMaxItems, bool createIdField)
        {
            var db = _server.GetDatabase(_database);

            lock (CollectionCache)
            {
                if (CollectionCache.Contains(collectionName))
                {
                    return;
                }

                if (!db.CollectionExists(collectionName))
                {
                    var collectionOptionsBuilder = new CollectionOptionsBuilder();

                    collectionOptionsBuilder.SetCapped(true);
                    collectionOptionsBuilder.SetMaxSize(collectionSize);

                    if (createIdField)
                    {
                        collectionOptionsBuilder.SetAutoIndexId(true);
                    }

                    if (collectionMaxItems.HasValue)
                    {
                        collectionOptionsBuilder.SetMaxDocuments(collectionMaxItems.Value);
                    }

                    db.CreateCollection(collectionName, collectionOptionsBuilder);
                }

                CollectionCache.Add(collectionName);
            }
        }
Esempio n. 3
0
        public void CheckCollection(string collectionName, long collectionSize, long? collectionMaxItems, bool createIdField)
        {
            var db = _server.GetDatabase(_database);

            lock (_collectionCache)
            {
                if (_collectionCache.ContainsKey(collectionName)) return;

                if (!db.CollectionExists(collectionName))
                {
                    var collectionOptionsBuilder = new CollectionOptionsBuilder();

                    collectionOptionsBuilder.SetCapped(true);
                    collectionOptionsBuilder.SetMaxSize(collectionSize);
                    collectionOptionsBuilder.SetAutoIndexId(createIdField);

                    if (collectionMaxItems.HasValue)
                        collectionOptionsBuilder.SetMaxDocuments(collectionMaxItems.Value);

                    db.CreateCollection(collectionName, collectionOptionsBuilder);

                }

                _collectionCache.Add(collectionName, createIdField);
            }
        }
Esempio n. 4
0
        public void TestSetNone()
        {
            var options  = new CollectionOptionsBuilder();
            var expected = "{ }".Replace("'", "\"");

            Assert.AreEqual(expected, options.ToJson());
        }
        /// <summary>
        ///     带有参数的CreateOption
        /// </summary>
        /// <param name="strObjTag"></param>
        /// <param name="treeNode"></param>
        /// <param name="collectionName"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static Boolean CreateCollectionWithOptions(String strObjTag, TreeNode treeNode, String collectionName,
                                                          CollectionOptionsBuilder option)
        {
            //不支持中文 JIRA ticket is created : SERVER-4412
            //SERVER-4412已经在2013/03解决了
            //collection names are limited to 121 bytes after converting to UTF-8.
            Boolean       rtnResult  = false;
            MongoDatabase mongoDB    = GetMongoDBBySvrPath(strObjTag);
            String        strSvrPath = SystemManager.GetTagData(strObjTag);
            String        svrKey     = strSvrPath.Split("/".ToCharArray())[(int)PathLv.InstanceLv];
            String        ConKey     = strSvrPath.Split("/".ToCharArray())[(int)PathLv.ConnectionLv];

            if (mongoDB != null)
            {
                if (!mongoDB.CollectionExists(collectionName))
                {
                    mongoDB.CreateCollection(collectionName, option);
                    foreach (TreeNode item in treeNode.Nodes)
                    {
                        if (item.Tag.ToString().StartsWith(COLLECTION_LIST_TAG))
                        {
                            item.Nodes.Add(UIHelper.FillCollectionInfoToTreeNode(collectionName, mongoDB, ConKey + "/" + svrKey));
                        }
                    }
                    rtnResult = true;
                }
            }
            return(rtnResult);
        }
        /// <summary>
        /// 带有参数的CreateOption
        /// </summary>
        /// <param name="strObjTag"></param>
        /// <param name="treeNode"></param>
        /// <param name="collectionName"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public static Boolean CreateCollectionWithOptions(String strObjTag, TreeNode treeNode, String collectionName,
                                                          CollectionOptionsBuilder option)
        {
            Boolean       rtnResult  = false;
            MongoDatabase mongoDB    = GetMongoDBBySvrPath(strObjTag);
            String        strSvrPath = SystemManager.GetTagData(strObjTag);
            String        svrKey     = strSvrPath.Split("/".ToCharArray())[(int)PathLv.ServerLV];
            String        ConKey     = strSvrPath.Split("/".ToCharArray())[(int)PathLv.ConnectionLV];

            if (mongoDB != null)
            {
                if (!mongoDB.CollectionExists(collectionName))
                {
                    mongoDB.CreateCollection(collectionName, option);
                    foreach (TreeNode item in treeNode.Nodes)
                    {
                        if (item.Tag.ToString().StartsWith(COLLECTION_LIST_TAG))
                        {
                            item.Nodes.Add(FillCollectionInfoToTreeNode(collectionName, mongoDB, ConKey + "/" + svrKey));
                        }
                    }
                    rtnResult = true;
                }
            }
            return(rtnResult);
        }
        protected override void CreateCollection(MongoDatabase database)
        {
            CollectionOptionsBuilder options = CollectionOptions
                                               .SetCapped(true)
                                               .SetMaxSize(5 * 1024 * 1024);

            database.CreateCollection(GetCollectionName(), options);
        }
        private void CappedCollectionExample()
        {
            CollectionOptionsBuilder optionsBuilder = new CollectionOptionsBuilder();

            optionsBuilder.SetCapped(true);
            optionsBuilder.SetMaxSize(52428800);
            CarRentalContext.CarRentalDatabase.CreateCollection("NewCollection", optionsBuilder);
        }
Esempio n. 9
0
 /// <summary>
 ///     带有参数的CreateOption
 /// </summary>
 /// <param name="strObjTag"></param>
 /// <param name="collectionName"></param>
 /// <param name="option"></param>
 /// <param name="mongoDb"></param>
 /// <returns></returns>
 public static bool CreateCollectionWithOptions(string strObjTag, string collectionName,
                                                CollectionOptionsBuilder option, MongoDatabase mongoDb)
 {
     //不支持中文 JIRA ticket is created : SERVER-4412
     //SERVER-4412已经在2013/03解决了
     //collection names are limited to 121 bytes after converting to UTF-8.
     if (mongoDb == null)
     {
         return(false);
     }
     if (mongoDb.CollectionExists(collectionName))
     {
         return(false);
     }
     mongoDb.CreateCollection(collectionName, option);
     return(true);
 }
Esempio n. 10
0
        protected override void OK()
        {
            //db.runCommand({"convertToCapped": "mycoll", size: 100000});  ? не поддерживает maxdocs
            Guid   g = Guid.NewGuid();
            string s = g.ToString();
            var    b = new CollectionOptionsBuilder();

            b.SetCapped(true).SetMaxDocuments(MaxCount).SetMaxSize(MaxSize);
            _coll.Database.CreateCollection(s, b);
            MongoCollection <BsonDocument> newcoll = _coll.Database.GetCollection(s);

            foreach (BsonDocument item in _coll.FindAllAs <BsonDocument>())
            {
                newcoll.Save(item);
            }
            _coll.Drop();
        }
Esempio n. 11
0
        protected override void BeginProcessing()
        {
            // default options
            var options = new CollectionOptionsBuilder();

            // capped collection
            if (MaxSize > 0)
            {
                options.SetCapped(true);
                options.SetMaxSize(MaxSize);
                if (MaxDocuments > 0)
                {
                    options.SetMaxDocuments(MaxDocuments);
                }
            }

            Database.CreateCollection(Name, options);
        }
    /// <summary>
    /// Запись сообщения в БД Mongo
    /// </summary>
    /// <param name="message">Текст сообщения</param>
    private void WriteMessage(string message)
    {
        int hour = DateTime.Now.Hour;

        if (hour >= startHourIncluded && hour <= endHourIncluded)
        {
            MongoServer server = new MongoClient(_connectionString).GetServer();    // MongoServer.Create(_connectionString);
            var         db     = server.GetDatabase(_databaseName);
            if (!db.CollectionExists(_collectionName))
            {
                CollectionOptionsBuilder b = new CollectionOptionsBuilder();
                b = b.SetCapped(true).SetMaxSize(MB(500));
                db.CreateCollection(_collectionName, b);
            }
            var collection = db.GetCollection <GenesisLogElement>(_collectionName);
            collection.Insert(new GenesisLogElement(message));
        }
    }
 /// <summary>
 ///     OK
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void cmdOK_Click(object sender, EventArgs e)
 {
     //不支持中文 JIRA ticket is created : SERVER-4412
     //SERVER-4412已经在2013/03解决了
     //collection names are limited to 121 bytes after converting to UTF-8.
     if (txtCollectionName.Text == string.Empty)
     {
         return;
     }
     try
     {
         string errMessage;
         RuntimeMongoDbContext.GetCurrentDataBase().IsCollectionNameValid(txtCollectionName.Text, out errMessage);
         if (errMessage != null)
         {
             MyMessageBox.ShowMessage("Create MongoDatabase", "Argument Exception", errMessage, true);
             return;
         }
         if (chkAdvance.Checked)
         {
             var option = new CollectionOptionsBuilder();
             option.SetCapped(chkIsCapped.Checked);
             option.SetMaxSize((long)numMaxSize.Value);
             option.SetMaxDocuments((long)numMaxDocument.Value);
             //CappedCollection Default is AutoIndexId After MongoDB 2.2.2
             option.SetAutoIndexId(chkIsAutoIndexId.Checked);
             Result = Operater.CreateCollectionWithOptions(StrSvrPathWithTag, txtCollectionName.Text,
                                                           option, RuntimeMongoDbContext.GetCurrentDataBase());
         }
         else
         {
             Result = Operater.CreateCollection(StrSvrPathWithTag, txtCollectionName.Text,
                                                RuntimeMongoDbContext.GetCurrentDataBase());
         }
         Close();
     }
     catch (ArgumentException ex)
     {
         Utility.ExceptionDeal(ex, "Create MongoDatabase", "Argument Exception");
         Result = false;
     }
 }
        public Task CreateCollectionIfNotExist(string route)
        {
            var collectionName = _mongoAgent.GetEnvelopsCollectionName(route);
            var db             = _mongoAgent.GetDb();

            if (!db.CollectionExists(collectionName))
            {
                var collectionOptionsBuilder = new CollectionOptionsBuilder()
                                               .SetCapped(true)
                                               .SetMaxDocuments(1000000)
                                               .SetMaxSize(500000000);
                db.CreateCollection(collectionName, collectionOptionsBuilder);
                var collection = db.GetCollection(collectionName);
                collection.CreateIndex(IndexKeys <Envelope> .Ascending(x => x.IsRead));
                collection.CreateIndex(IndexKeys <Envelope> .Descending(x => x.ReadAt).Ascending(x => x.ProcessedAt));
                collection.CreateIndex(IndexKeys <Envelope> .Descending(x => x.ProcessingStartedAt).Ascending(x => x.ProcessedAt));
                collection.CreateIndex(IndexKeys <Envelope> .Descending(x => x.Id).Ascending(x => x.ProcessedAt));
            }

            return(Task.FromResult(true));
        }
        protected override void OK()
        {
            var b = new CollectionOptionsBuilder();

            if (Capped)
            {
                b.SetCapped(Capped);
            }
            if (MaxSize.HasValue)
            {
                b.SetMaxSize(MaxSize.Value);
            }
            if (MaxDocuments.HasValue)
            {
                b.SetMaxDocuments(MaxDocuments.Value);
            }
            _db.CreateCollection(Name, b);
            MongoCollection <BsonDocument> mongoCollection = _db.GetCollection(Name);
            var coll = new DatabaseExplorerCollectionViewModel(mongoCollection);

            MessengerInstance.Send(new NotificationMessage <DatabaseExplorerCollectionViewModel>(this, coll, "added"));
            SimpleIoc.Default.GetInstance <MainViewModel>().Content.Remove(this);
        }
Esempio n. 16
0
        protected override void BeginProcessing()
        {
            // default options
            var options = new CollectionOptionsBuilder();

            // capped collection
            if (MaxSize > 0)
            {
                options.SetCapped(true);
                options.SetMaxSize(MaxSize);
                if (MaxDocuments > 0)
                {
                    options.SetMaxDocuments(MaxDocuments);
                }
            }

            // auto arrayIndex explicitly, otherwise default is used
            if (_setAutoIndexId)
            {
                options.SetAutoIndexId(_AutoIndexId);
            }

            Database.CreateCollection(Name, options);
        }
Esempio n. 17
0
        public void SetupCollection()
        {
            var           uri    = new MongoUrl(ConnectionString);
            var           client = new MongoClient(uri);
            MongoDatabase db     = client.GetServer().GetDatabase(uri.DatabaseName);
            Int64         cappedSize;

            if (!Int64.TryParse(CappedSizeInMb, out cappedSize))
            {
                cappedSize = 5 * 1024L;
            }
            if (!db.CollectionExists(CollectionName))
            {
                CollectionOptionsBuilder options = CollectionOptions
                                                   .SetCapped(true)
                                                   .SetMaxSize(1024L * 1024L * cappedSize); //5 gb.
                db.CreateCollection(CollectionName, options);
            }
            LogCollection = db.GetCollection(CollectionName);
            var builder = new IndexOptionsBuilder();

            const string ttlIndex = FieldNames.Timestamp + "_-1";
            var          index    = LogCollection.GetIndexes().SingleOrDefault(x => x.Name == ttlIndex);

            if (index != null)
            {
                if (ExpireAfter != null)
                {
                    if (index.TimeToLive != ExpireAfter.ToTimeSpan())
                    {
                        var d = new CommandDocument()
                        {
                            { "collMod", CollectionName },
                            {
                                "index", new BsonDocument
                                {
                                    { "keyPattern", new BsonDocument {
                                          { FieldNames.Timestamp, -1 }
                                      } },
                                    { "expireAfterSeconds", (int)(ExpireAfter.ToTimeSpan().TotalSeconds) }
                                }
                            }
                        };

                        db.RunCommand(d);
                    }
                }
            }
            else
            {
                if (ExpireAfter != null)
                {
                    builder.SetTimeToLive(ExpireAfter.ToTimeSpan());
                }

                LogCollection.CreateIndex(IndexKeys.Descending(FieldNames.Timestamp), builder);
            }

            LogCollection.CreateIndex(IndexKeys
                                      .Ascending(FieldNames.Level, FieldNames.Thread, FieldNames.Loggername)
                                      );
        }
        public void createCollection(String databaseName)
        {   // creating a collection in database
            CollectionOptionsBuilder options = CollectionOptions.SetCapped(true);

            dataBase.CreateCollection(databaseName, options);
        }
        /// <summary>
        ///     OK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdOK_Click(object sender, EventArgs e)
        {
            //不支持中文 JIRA ticket is created : SERVER-4412
            //SERVER-4412已经在2013/03解决了
            //collection names are limited to 121 bytes after converting to UTF-8.
            if (txtCollectionName.Text == string.Empty)
            {
                MyMessageBox.ShowMessage("Please Input CollectionName", "Please Input CollectionName");
                return;
            }
            CollectionName = txtCollectionName.Text.Trim();
            if (string.IsNullOrEmpty(CollectionName))
            {
                MyMessageBox.ShowMessage("Please Input CollectionName", "Please Input CollectionName");
                return;
            }
            try
            {
                RuntimeMongoDbContext.GetCurrentDataBase().IsCollectionNameValid(txtCollectionName.Text, out string errMessage);
                if (errMessage != null)
                {
                    MyMessageBox.ShowMessage("Create Collection", "Argument Exception", errMessage, true);
                    return;
                }
                var option = new CollectionOptionsBuilder();
                if (chkIsCapped.Checked)
                {
                    if (numMaxSize.Value == 0 || numMaxDocument.Value == 0)
                    {
                        MyMessageBox.ShowMessage("Create Collection", "Argument Exception", "Please Input MaxSize Or MaxDocument When IsCapped", true);
                        return;
                    }
                    option.SetCapped(chkIsCapped.Checked);
                    if (numMaxSize.Value != 0)
                    {
                        option.SetMaxSize((long)numMaxSize.Value);
                    }
                    if (numMaxDocument.Value != 0)
                    {
                        option.SetMaxDocuments((long)numMaxDocument.Value);
                    }
                }

                //CappedCollection Default is AutoIndexId After MongoDB 2.2.2
                //Deprecated since version 3.2: The autoIndexId option will be removed in version 3.4.
                //option.SetAutoIndexId(chkIsAutoIndexId.Checked);

                if (chkValidation.Checked && ValidationDoc != null)
                {
                    //Start From MongoDB 3.2.0
                    QueryDocument queryDoc = new QueryDocument(ValidationDoc);
                    option.SetValidator(queryDoc);
                    //Validation Level
                    if (radLevel_off.Checked)
                    {
                        option.SetValidationLevel(DocumentValidationLevel.Off);
                    }
                    if (radLevel_strict.Checked)
                    {
                        option.SetValidationLevel(DocumentValidationLevel.Strict);
                    }
                    if (radLevel_moderate.Checked)
                    {
                        option.SetValidationLevel(DocumentValidationLevel.Moderate);
                    }
                    //Validation Action
                    if (radAction_error.Checked)
                    {
                        option.SetValidationAction(DocumentValidationAction.Error);
                    }
                    if (radAction_warn.Checked)
                    {
                        option.SetValidationAction(DocumentValidationAction.Warn);
                    }
                }
                if (mCollation != null)
                {
                    option.SetCollation(mCollation);
                }
                Result = Operater.CreateCollectionWithOptions(StrSvrPathWithTag, txtCollectionName.Text,
                                                              option, RuntimeMongoDbContext.GetCurrentDataBase());

                Close();
            }
            catch (ArgumentException ex)
            {
                Utility.ExceptionDeal(ex, "Create MongoDatabase", "Argument Exception");
                Result = false;
            }
        }