Esempio n. 1
0
          protected override void Destructor()
          {
            DisposableObject.DisposeAndNull(ref m_ManagementEvent);

            foreach(var server in m_Servers) 
              server.Dispose();

            base.Destructor();
            lock(s_InstanceLock)
            {
              if (this==s_Instance)
               s_Instance = null;
            }
          }
Esempio n. 2
0
        internal ServerNode(MongoClient client, Node node) : base(client)
        {
          m_Client = client;
          m_Node = node;

          var cfg = client.ConfigRoot//1. Try to find the SERVER section with name equals this node
                          .Children
                          .FirstOrDefault(c => c.IsSameName(CONFIG_SERVER_SECTION) && c.IsSameNameAttr(node.ConnectString));
          if (cfg==null)
           cfg = client.ConfigRoot //2. If not found, try to find SERVER section without name attr
                       .Children
                       .FirstOrDefault(c => c.IsSameName(CONFIG_SERVER_SECTION) && !c.AttrByName(Configuration.CONFIG_NAME_ATTR).Exists);

          if (cfg!=null)
            ConfigAttribute.Apply(this, client.ConfigRoot);
        }
        public void CollectionDrop()
        {
            using(var client= new MongoClient("My Test"))
              {
            var collection = client.DefaultLocalServer["db1"]["ToDrop"];
            var doc1 = new BSONDocument();

            doc1.Set( new BSONStringElement("_id", "id1"))
            .Set( new BSONStringElement("val", "My value"))
            .Set( new BSONInt32Element("age", 125));

            var r = collection.Insert(doc1);
            Assert.AreEqual(1, r.TotalDocumentsAffected);

            collection.Drop();
            Assert.IsTrue( collection.Disposed );
              }
        }
        public void AllocClient()
        {
            using(var client= new MongoClient("My Test"))
              {
            var server = client.DefaultLocalServer;
            Assert.IsNotNull( server );
            Assert.IsTrue( object.ReferenceEquals(server, client[server.Node]) );

            var db = server["db1"];
            Assert.IsNotNull( db );
            Assert.IsTrue( object.ReferenceEquals(db, server[db.Name]) );

            var collection = db["t1"];
            Assert.IsNotNull( collection );
            Assert.IsTrue( object.ReferenceEquals(collection, db[collection.Name]) );

            var query = new Query();
            var result = collection.FindOne( query );
              }
        }
        public void Save_Parallel_AsInsertAndUpdate()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            const int CNT = 50000;
            const int SAVE_CNT = 75000;
            const int CHUNK = 2000;

            for (int i=0; i<CNT; i++)
            {
              Assert.AreEqual(1, collection.Insert(
                  new BSONDocument().Set(new BSONObjectIDElement("_id", new BSONObjectID(1,2,3, (uint)i)))
                  .Set(new BSONStringElement("name", "People" + i)))
                                       .TotalDocumentsAffected);
            }

            Parallel.For(0, SAVE_CNT/CHUNK, i =>
            {
              for (int j = 0; j < CHUNK; j++)
              {
            var increment = CHUNK*i+j;
            var result = collection.Save(new BSONDocument().Set(new BSONObjectIDElement("_id", new BSONObjectID(1, 2, 3, (uint)increment)))
                                                           .Set(new BSONStringElement("name", "saved")));
            Assert.AreEqual(result.TotalDocumentsAffected, 1);
            Assert.AreEqual(result.WriteErrors, null);
            if (increment < CNT)
            {
              Assert.AreEqual(result.Upserted, null);
              Assert.AreEqual(result.TotalDocumentsUpdatedAffected, 1);
            }
            else
            {
              Assert.AreEqual(result.Upserted.Length, 1);
              Assert.AreEqual(result.TotalDocumentsUpdatedAffected, 0);
            }
              }
            });

            var all = collection.Find(new Query(@"{}", false));
            for (int i=0; i<SAVE_CNT; i++)
            {
              all.MoveNext();
              Assert.AreEqual(all.Current.Count, 2);
              Assert.AreEqual(((BSONStringElement)all.Current["name"]).Value, "saved");
            }
            all.MoveNext();
            Assert.AreEqual(true, all.EOF);
              }
        }
        public void Update_Parallel_SimpleStringInt23Entries()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            const int CNT = 75000;
            const int CHUNK = 1500;

            foreach (var value in Enumerable.Range(0, CNT))
            {
              Assert.AreEqual(1, collection.Insert(
                  new BSONDocument().Set(new BSONStringElement("name", "People" + value))
                                    .Set(new BSONInt32Element("value", value)))
                                       .TotalDocumentsAffected);
            }

            Parallel.For(0, CNT/CHUNK, i =>
            {
              var result = collection.Update(new UpdateEntry(new Query("{ '$and': [ { value: {'$gte':"+i*CHUNK+"}}, { value: {'$lt':"+(i+1)*CHUNK+"}} ]}", false),
                                                         new Query("{ '$set': {name: 'updated'}}", false), true, false));
              Assert.AreEqual(result.TotalDocumentsUpdatedAffected, CHUNK);
              Assert.AreEqual(result.TotalDocumentsAffected, CHUNK);
              Assert.AreEqual(result.Upserted, null);
              Assert.AreEqual(result.WriteErrors, null);
            });

            var updated = collection.Find(new Query(@"{name: 'updated'}", false));
            for (int i = 0; i < CNT; i++)
            {
              updated.MoveNext();
              Assert.AreEqual(updated.Current.Count, 3);
              Assert.AreEqual(((BSONStringElement)updated.Current["name"]).Value, "updated");
            }
            updated.MoveNext();
            Assert.AreEqual(true, updated.EOF);
              }
        }
        public void CollectionLifecycle()
        {
            using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            db["t1"].Drop();
            db["t2"].Drop();
            db["t3"].Drop();

            var collectionCount = db.GetCollectionNames().Length;

            Assert.IsTrue(collectionCount == 0 || collectionCount == 1);

            Assert.AreEqual(1, db["t1"].Insert( new BSONDocument().Set( new BSONInt32Element("_id", 1))
                                                              .Set( new BSONStringElement("val", "one"))
                           ).TotalDocumentsAffected);

            Assert.AreEqual(1, db["t2"].Insert( new BSONDocument().Set( new BSONInt32Element("_id", 1))
                                                              .Set( new BSONStringElement("val", "one"))
                           ).TotalDocumentsAffected);

            var collections = db.GetCollectionNames();
            collectionCount = collections.Length;

            // Different MongoDB Server versions treat system collection differently (newer versions dont count)
            Assert.IsTrue(collectionCount == 2 || collectionCount == 3);
            Assert.IsTrue( collections.Contains("t1"));
            Assert.IsTrue( collections.Contains("t2"));
              }
        }
        public void Save_AsInsertAndUpdate()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            for (int i=0; i<10; i++)
            {
              var result = collection.Save(new BSONDocument().Set(new BSONObjectIDElement("_id", new BSONObjectID(1,2,3, (uint)i)))
                                                         .Set(new BSONStringElement("name", "People"+i)));
              Assert.AreEqual(1, result.TotalDocumentsAffected);
              Assert.AreEqual(0, result.TotalDocumentsUpdatedAffected);
              Assert.AreEqual(1, result.Upserted.Length);
              Assert.AreEqual(null, result.WriteErrors);
            }

            for (int i=5; i<10; i++)
            {
              var result = collection.Save(new BSONDocument().Set(new BSONObjectIDElement("_id", new BSONObjectID(1,2,3, (uint)i)))
                                                         .Set(new BSONStringElement("name", "saved")));
              Assert.AreEqual(1, result.TotalDocumentsAffected);
              Assert.AreEqual(1, result.TotalDocumentsUpdatedAffected);
              Assert.AreEqual(null, result.Upserted);
              Assert.AreEqual(null, result.WriteErrors);
            }

            for (int i=10; i<15; i++)
            {
              var result = collection.Save(new BSONDocument().Set(new BSONObjectIDElement("_id", new BSONObjectID(1,2,3,(uint)i)))
                                                         .Set(new BSONStringElement("name", "saved")));
              Assert.AreEqual(1, result.TotalDocumentsAffected);
              Assert.AreEqual(0, result.TotalDocumentsUpdatedAffected);
              Assert.AreEqual(1, result.Upserted.Length);
              Assert.AreEqual(null, result.WriteErrors);
            }

            var all = collection.Find(new Query(@"{}", false));
            for (int i=0; i<15; i++)
            {
              all.MoveNext();
              Assert.AreEqual(all.Current.Count, 2);
              Assert.AreEqual(((BSONObjectIDElement)all.Current["_id"]).Value.Bytes, new BSONObjectID(1,2,3,(uint)i).Bytes);
              Assert.AreEqual(((BSONStringElement)all.Current["name"]).Value, i<5 ? "People" + i : "saved");
            }
            all.MoveNext();
            Assert.AreEqual(true, all.EOF);
              }
        }
 public void Ping()
 {
     using(var client= new MongoClient("My Test"))
       {
     var db = client.DefaultLocalServer["db1"];
     db.Ping();
       }
 }
        public void Ping_Parallel()
        {
            const int CNT = 123000;

              using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            var sw = Stopwatch.StartNew();
            Parallel.For(0, CNT, (i) => db.Ping());
            var e =sw.ElapsedMilliseconds;

            Console.WriteLine("Ping Parallel: {0:n0} times in {1:n0} ms at {2:n0} ops/sec".Args(CNT, e, CNT / (e/1000d)) );
              }
        }
Esempio n. 11
0
        public void T_03_Update()
        {
            var BIN = new byte[] { 0x00, 0x79, 0x14 };

              using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            db["t1"].Drop();

            var t1 = db["t1"];

            var row = new TestRow()
            {
              _id = 1,

              String1 = "Mudaker",
            };

            var rc = new NFX.Serialization.BSON.RowConverter();
            var doc = rc.RowToBSONDocument(row, "A");
            Assert.AreEqual(1, t1.Insert(doc).TotalDocumentsAffected);

            var updateResult = t1.Update
            (
              new NFX.DataAccess.MongoDB.Connector.UpdateEntry
              (
            Query.ID_EQ_Int32(1),
            new Update("{'String1': '$$VAL'}", true, new TemplateArg("VAL", BSONElementType.String, "makaka")),
            false,
            false
              )
            );

            var got = db["t1"].FindOne(Query.ID_EQ_Int32(1));
            Assert.IsNotNull( got );

            var row1 = new TestRow();
            rc.BSONDocumentToRow(got, row1, "A");

            Assert.AreEqual("makaka", row1.String1);
              }
        }
        public void Delete_Parallel()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            const int CNT = 100000;
            const int CHUNK = 5000;

            foreach (var value in Enumerable.Range(0, CNT))
            {
              Assert.AreEqual(1, collection.Insert(
                  new BSONDocument().Set(new BSONStringElement("name", "People" + value))
                                    .Set(new BSONInt32Element("value", value)))
                                       .TotalDocumentsAffected);
            }

            Parallel.For(0, CNT/CHUNK, i =>
            {
              var result = collection.Delete(new DeleteEntry(new Query("{ '$and': [ { value: {'$gte':"+i*CHUNK+"}}, { value: {'$lt':"+(i+1)*CHUNK+"}} ]}", false),
                                                        DeleteLimit.None));
              Assert.AreEqual(result.TotalDocumentsUpdatedAffected, 0);
              Assert.AreEqual(result.TotalDocumentsAffected, CHUNK);
              Assert.AreEqual(result.Upserted, null);
              Assert.AreEqual(result.WriteErrors, null);
            });

            var all = collection.Find(new Query(@"{}", false));
            all.MoveNext();
            Assert.AreEqual(true, all.EOF);
              }
        }
        public void Insert_FindOne_Parallel(int maxConnections)
        {
            const int CNT = 75000;

              using(var client= new MongoClient("My Test"))
              {
            var server = client.DefaultLocalServer;
            server.MaxConnections = maxConnections;
            var db = server["db1"];
            db["t1"].Drop();
            var t1 = db["t1"];

            var sw = Stopwatch.StartNew();
            Parallel.For(0, CNT, (i) =>
            {
              Assert.AreEqual(1, db["t1"].Insert( new BSONDocument().Set( new BSONInt32Element("_id", i))
                                                                .Set( new BSONStringElement("val", "num-"+i.ToString()))
                           ).TotalDocumentsAffected);
            });

            var e1 =sw.ElapsedMilliseconds;

            Assert.AreEqual(CNT, t1.Count());

            Console.WriteLine("Insert Parallel: {0:n0} times in {1:n0} ms at {2:n0} ops/sec   MAX CONNECTIONS={3} ".Args(CNT, e1, CNT / (e1/1000d), maxConnections ));

            sw.Restart();
            Parallel.For(0, CNT, (i) =>
            {
              var got = db["t1"].FindOne(Query.ID_EQ_Int32(i));
              Assert.IsNotNull( got );
              Assert.AreEqual("num-"+i.ToString(), got["val"].AsString());
            });

            var e2 =sw.ElapsedMilliseconds;

            Console.WriteLine("FindOne Parallel: {0:n0} times in {1:n0} ms at {2:n0} ops/sec   MAX CONNECTIONS={3} ".Args(CNT, e2, CNT / (e2/1000d), maxConnections ));
              }
        }
Esempio n. 14
0
        public void T_02_Update()
        {
            var BIN = new byte[] { 0x00, 0x79, 0x14 };

              using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            db["t1"].Drop();

            var t1 = db["t1"];

            var row = new TestRow()
            {
              _id = 1,

              String1 = "Mudaker",
              String2 = null,
              Date1 = new DateTime(1980, 07, 12, 10, 13, 27, DateTimeKind.Utc),
              Date2 = null,
              Bool1 = true,
              Bool2 = null,
              Guid1 = new Guid("{9195F7DB-FE21-4BB2-B006-2496F4E24D14}"),
              Guid2 = null,
              Gdid1 = new NFX.DataAccess.Distributed.GDID(0, 12345),
              Gdid2 = null,
              Float1 = 127.0123f,
              Float2 = null,
              Double1 = 122345.012d,
              Double2 = null,
              Decimal1 = 1234567.098M,
              Decimal2 = null,
              Amount1 = new Amount("din", 123.11M),
              Amount2 = null,
              Bytes1 = BIN,
              Bytes2 = null,

              Byte1 = 23,
              SByte1 = -3,
              Short1 = -32761,
              UShort1 = 65535,
              Int1 = 4324,
              Uint1 = 42345,
              Long1 = 993,
              ULong1 = 8829383762,
              ETest1 = ETest.Two,
              EFlags1 = EFlags.First | EFlags.Third,

              Byte2 = null,
              SByte2 = null,
              Short2 = null,
              UShort2 = null,
              Int2 = null,
              Uint2 = null,
              Long2 = null,
              ULong2 = null,
              ETest2 = null,
              EFlags2 = null
            };

            var rc = new NFX.Serialization.BSON.RowConverter();
            var doc = rc.RowToBSONDocument(row, "A");
            Assert.AreEqual(1, t1.Insert(doc).TotalDocumentsAffected);

            row.String1 = "makaka";
            row.Int1 = 9789;

            doc = rc.RowToBSONDocument(row, "A");

            var r = t1.Save(doc);
            Assert.AreEqual(1, r.TotalDocumentsAffected);
            Assert.AreEqual(1, r.TotalDocumentsUpdatedAffected);
            Assert.IsNull(r.WriteErrors);

            var got = db["t1"].FindOne(Query.ID_EQ_Int32(1));
            Assert.IsNotNull( got );

            var row1 = new TestRow();
            rc.BSONDocumentToRow(got, row1, "A");

            Assert.AreEqual(row, row1);

              }
        }
        public void InsertWithoutID()
        {
            using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            db["t1"].Drop();

            var t1 = db["t1"];

            Assert.AreEqual(1, t1.Insert( new BSONDocument().Set( new BSONStringElement("val", "one"))
                           ).TotalDocumentsAffected);

            Assert.AreEqual(1, t1.Count());
              }
        }
        public void InsertDelete()
        {
            using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            db["t1"].Drop();

            var t1 = db["t1"];

            Assert.AreEqual(1, t1.Insert( new BSONDocument().Set( new BSONInt32Element("_id", 1))
                                                        .Set( new BSONStringElement("val", "one"))
                           ).TotalDocumentsAffected);

            Assert.AreEqual(1, t1.Insert( new BSONDocument().Set( new BSONInt32Element("_id", 2))
                                                        .Set( new BSONStringElement("val", "two"))
                           ).TotalDocumentsAffected);

            Assert.AreEqual(2, t1.Count());
            Assert.AreEqual( 1, t1.DeleteOne(Query.ID_EQ_Int32(1)).TotalDocumentsAffected );

            Assert.AreEqual(1, t1.Count());
            Assert.AreEqual( 1, t1.DeleteOne(Query.ID_EQ_Int32(2)).TotalDocumentsAffected );

            Assert.AreEqual(0, t1.Count());
              }
        }
        public void Find_Comparison_Int32()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            foreach (var age in Enumerable.Range(1, 100))
            {
              Assert.AreEqual(1, collection.Insert(
                  new BSONDocument().Set(new BSONStringElement("name", Guid.NewGuid().ToString()))
                                    .Set(new BSONInt32Element("age", age)))
                                       .TotalDocumentsAffected);
            }

            var lt1 = collection.Find(new Query(@"{ age: { $lt: 1}}", false));
            var lt5 = collection.Find(new Query(@"{ age: { '$lt': 5}}", false));
            var gte93 = collection.Find(new Query(@"{ age: { '$gte': 93}}", false));

            lt1.MoveNext();
            Assert.AreEqual(true, lt1.EOF);

            for (int i = 1; i < 5; i++)
            {
              lt5.MoveNext();
              Assert.AreEqual(lt5.Current.Count, 3);
              Assert.IsInstanceOf<BSONStringElement>(lt5.Current["name"]);
              Assert.AreEqual(((BSONInt32Element)lt5.Current["age"]).Value, i);
            }
            lt5.MoveNext();
            Assert.AreEqual(true, lt5.EOF);

            for (int i = 93; i <= 100; i++)
            {
              gte93.MoveNext();
              Assert.AreEqual(gte93.Current.Count, 3);
              Assert.IsInstanceOf<BSONStringElement>(gte93.Current["name"]);
              Assert.AreEqual(((BSONInt32Element)gte93.Current["age"]).Value, i);
            }
            gte93.MoveNext();
            Assert.AreEqual(true, gte93.EOF);
              }
        }
        public void Update_SimpleStringInt23Entries()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            foreach (var age in Enumerable.Range(1, 10))
            {
              Assert.AreEqual(1, collection.Insert(
                  new BSONDocument().Set(new BSONStringElement("name", "People" + age))
                                    .Set(new BSONInt32Element("age", age)))
                                       .TotalDocumentsAffected);
            }

            var result1 = collection.Update(new UpdateEntry(new Query("{ name: 'People1'}", false), new Query("{age: 100}", false), false, false));
            var update1 = collection.Find(new Query(@"{ age: 100}", false));

            var result2 = collection.Update(new UpdateEntry(new Query("{ age: { '$lt': 3 }}", false), new Query("{name: 'update2'}", false), false, false));
            var update2 = collection.Find(new Query(@"{name: 'update2'}", false));

            var result3 = collection.Update(new UpdateEntry(new Query("{ '$and': [ { age: { '$gte': 3 }}, { age: { '$lt': 6 }} ]}", false), new Query("{ '$set': {name: 'update3'}}", false), true, false));
            var update3 = collection.Find(new Query(@"{name: 'update3'}", false));

            var result4 = collection.Update(new UpdateEntry(new Query("{ age: -1}", false), new Query("{ '$set': {name: 'update4'}}", false), true, false));
            var update4 = collection.Find(new Query(@"{name: 'update4'}", false));

            var result5 = collection.Update(new UpdateEntry(new Query("{ age: -1}", false), new Query("{ '$set': {name: 'update5'}}", false), true, true));
            var update5 = collection.Find(new Query(@"{name: 'update5'}", false));

            var result6 = collection.Update(new UpdateEntry(new Query("{ '$or': [ {age: 6}, {age: 7}, {age: 8} ]}", false), new Query("{ '$set': {name: 'update6'}}", false), true, false));
            var update6 = collection.Find(new Query(@"{name: 'update6'}", false));

            Assert.AreEqual(result1.TotalDocumentsUpdatedAffected, 1);
            Assert.AreEqual(result1.TotalDocumentsAffected, 1);
            Assert.AreEqual(result1.Upserted, null);
            Assert.AreEqual(result1.WriteErrors, null);
            update1.MoveNext();
            Assert.AreEqual(update1.Current.Count, 2);
            Assert.AreEqual(((BSONInt32Element)update1.Current["age"]).Value,100);
            update1.MoveNext();
            Assert.AreEqual(true, update1.EOF);

            Assert.AreEqual(result2.TotalDocumentsUpdatedAffected, 1);
            Assert.AreEqual(result2.TotalDocumentsAffected, 1);
            Assert.AreEqual(result2.Upserted, null);
            Assert.AreEqual(result2.WriteErrors, null);
            update2.MoveNext();
            Assert.AreEqual(update2.Current.Count, 2);
            Assert.AreEqual(((BSONStringElement)update2.Current["name"]).Value, "update2");
            update2.MoveNext();
            Assert.AreEqual(true, update2.EOF);

            Assert.AreEqual(result3.TotalDocumentsUpdatedAffected, 3);
            Assert.AreEqual(result3.TotalDocumentsAffected, 3);
            Assert.AreEqual(result3.Upserted, null);
            Assert.AreEqual(result3.WriteErrors, null);
            for (int i = 3; i < 6; i++)
            {
              update3.MoveNext();
              Assert.AreEqual(update3.Current.Count, 3);
              Assert.AreEqual(((BSONStringElement)update3.Current["name"]).Value, "update3");
              Assert.AreEqual(((BSONInt32Element)update3.Current["age"]).Value, i);
            }
            update3.MoveNext();
            Assert.AreEqual(true, update3.EOF);

            Assert.AreEqual(result4.TotalDocumentsUpdatedAffected, 0);
            Assert.AreEqual(result4.TotalDocumentsAffected, 0);
            Assert.AreEqual(result4.Upserted, null);
            Assert.AreEqual(result4.WriteErrors, null);
            update4.MoveNext();
            Assert.AreEqual(true, update4.EOF);

            Assert.AreEqual(result5.TotalDocumentsUpdatedAffected, 0);
            Assert.AreEqual(result5.TotalDocumentsAffected, 1);
            Assert.AreEqual(result5.Upserted.Length, 1);
            Assert.AreEqual(result5.WriteErrors, null);
            update5.MoveNext();
            Assert.AreEqual(update5.Current.Count, 3);
            Assert.AreEqual(((BSONStringElement)update5.Current["name"]).Value, "update5");
            Assert.AreEqual(((BSONInt32Element)update5.Current["age"]).Value, -1);
            update5.MoveNext();
            Assert.AreEqual(true, update5.EOF);

            Assert.AreEqual(result6.TotalDocumentsUpdatedAffected, 3);
            Assert.AreEqual(result6.TotalDocumentsAffected, 3);
            Assert.AreEqual(result6.Upserted, null);
            Assert.AreEqual(result6.WriteErrors, null);
            for (int i = 6; i <= 8; i++)
            {
              update6.MoveNext();
              Assert.AreEqual(update6.Current.Count, 3);
              Assert.AreEqual(((BSONStringElement)update6.Current["name"]).Value, "update6");
              Assert.AreEqual(((BSONInt32Element)update6.Current["age"]).Value, i);
            }
            update6.MoveNext();
            Assert.AreEqual(true, update6.EOF);
              }
        }
        public void Delete_NoLimit()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            for (int i=0; i<100; i++)
            {
              collection.Insert(new BSONDocument().Set(new BSONStringElement("name", "People" + i))
                                              .Set(new BSONInt32Element("value", i)));
            }

            var result = collection.Delete(new DeleteEntry(
                                         new Query("{ '$or': [{ name: 'People0' }, { '$and': [{ value: { '$gte': 1 }}, { value: { '$lt': 50 }}] }] }", false),
                                         DeleteLimit.None));
            var all = collection.Find(new Query(@"{}", false));

            Assert.AreEqual(50, result.TotalDocumentsAffected);
            Assert.AreEqual(0, result.TotalDocumentsUpdatedAffected);
            Assert.AreEqual(null, result.Upserted);
            Assert.AreEqual(null, result.WriteErrors);
            for (int i=50; i<100; i++)
            {
              all.MoveNext();
              Assert.AreEqual(all.Current.Count, 3);
              Assert.AreEqual(((BSONStringElement)all.Current["name"]).Value, "People"+i);
              Assert.AreEqual(((BSONInt32Element)all.Current["value"]).Value, i);
            }
            all.MoveNext();
            Assert.AreEqual(true, all.EOF);
              }
        }
        public void CollectionLifecycle()
        {
            using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            db["t1"].Drop();
            db["t2"].Drop();
            db["t3"].Drop();

            Assert.AreEqual(1, db.GetCollectionNames().Length);

            Assert.AreEqual(1, db["t1"].Insert( new BSONDocument().Set( new BSONInt32Element("_id", 1))
                                                              .Set( new BSONStringElement("val", "one"))
                           ).TotalDocumentsAffected);

            Assert.AreEqual(1, db["t2"].Insert( new BSONDocument().Set( new BSONInt32Element("_id", 1))
                                                              .Set( new BSONStringElement("val", "one"))
                           ).TotalDocumentsAffected);

            var collections = db.GetCollectionNames();

            Assert.AreEqual(3, collections.Length);
            Assert.IsTrue( collections.Contains("t1"));
            Assert.IsTrue( collections.Contains("t2"));
              }
        }
        public void Delete_OnlyFirstMatch()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            for (int i=0; i<100; i++)
            {
              collection.Insert(new BSONDocument().Set(new BSONStringElement("name", "People" + i))
                                              .Set(new BSONInt32Element("value", i)));
            }

            var result1 = collection.Delete(new DeleteEntry(
                                         new Query("{ value: { '$gte': 0 }}", false),
                                         DeleteLimit.OnlyFirstMatch));
            var result2 = collection.Delete(new DeleteEntry(
                                         new Query("{ value: { '$lt': 1000 }}", false),
                                         DeleteLimit.OnlyFirstMatch));
            var all = collection.Find(new Query(@"{}", false));

            Assert.AreEqual(1, result1.TotalDocumentsAffected);
            Assert.AreEqual(0, result1.TotalDocumentsUpdatedAffected);
            Assert.AreEqual(null, result1.Upserted);
            Assert.AreEqual(null, result1.WriteErrors);
            Assert.AreEqual(1, result2.TotalDocumentsAffected);
            Assert.AreEqual(0, result2.TotalDocumentsUpdatedAffected);
            Assert.AreEqual(null, result2.Upserted);
            Assert.AreEqual(null, result2.WriteErrors);
            for (int i=2; i<100; i++)
            {
              all.MoveNext();
              Assert.AreEqual(all.Current.Count, 3);
              Assert.AreEqual(((BSONStringElement)all.Current["name"]).Value, "People"+i);
              Assert.AreEqual(((BSONInt32Element)all.Current["value"]).Value, i);
            }
            all.MoveNext();
            Assert.AreEqual(true, all.EOF);
              }
        }
Esempio n. 22
0
        public void T_02_Update_Parallel()
        {
            const int CNT = 11973;
              var BIN = new byte[] { 0x00, 0x79, 0x14 };

              using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            db["t1"].Drop();

            var t1 = db["t1"];

            var rows = new TestRow[CNT];

            var rc = new NFX.Serialization.BSON.RowConverter();

            var sw = new Stopwatch();

            sw.Start();

            Parallel.For(0, CNT, i =>
            {
              var row = new TestRow()
              {
            _id = i,

            String1 = "Mudaker",
            String2 = null,
            Date1 = new DateTime(1980, 07, 12, 10, 13, 27, DateTimeKind.Utc),
            Date2 = null,
            Bool1 = true,
            Bool2 = null,
            Guid1 = new Guid("{9195F7DB-FE21-4BB2-B006-2496F4E24D14}"),
            Guid2 = null,
            Gdid1 = new NFX.DataAccess.Distributed.GDID(0, 12345),
            Gdid2 = null,
            Float1 = 127.0123f,
            Float2 = null,
            Double1 = 122345.012d,
            Double2 = null,
            Decimal1 = 1234567.098M,
            Decimal2 = null,
            Amount1 = new Amount("din", 123.11M),
            Amount2 = null,
            Bytes1 = BIN,
            Bytes2 = null,

            Byte1 = 23,
            SByte1 = -3,
            Short1 = -32761,
            UShort1 = 65535,
            Int1 = 4324,
            Uint1 = 42345,
            Long1 = 993,
            ULong1 = 8829383762,
            ETest1 = ETest.Two,
            EFlags1 = EFlags.First | EFlags.Third,

            Byte2 = null,
            SByte2 = null,
            Short2 = null,
            UShort2 = null,
            Int2 = null,
            Uint2 = null,
            Long2 = null,
            ULong2 = null,
            ETest2 = null,
            EFlags2 = null
              };

              rows[i] = row;

              var doc = rc.RowToBSONDocument(row, "A");
              Assert.AreEqual(1, t1.Insert(doc).TotalDocumentsAffected);
            });
            sw.Stop();
            Console.WriteLine("{0:N0} row inserted in {1:N3} sec on {2:N0} ops/sec", CNT, sw.Elapsed.TotalSeconds, CNT / sw.Elapsed.TotalSeconds);

            sw.Restart();
            Parallel.For(0, CNT, i => {
              var row = rows[i];
              row.String1 = "makaka" + i.ToString();
              row.Int1 = 9789 + (i * 100);

              var doc = rc.RowToBSONDocument(row, "A");

              var r = t1.Save(doc);
              Assert.AreEqual(1, r.TotalDocumentsAffected);
              Assert.AreEqual(1, r.TotalDocumentsUpdatedAffected);
              Assert.IsNull(r.WriteErrors);
            });
            sw.Stop();
            Console.WriteLine("{0:N0} row updated in {1:N3} sec on {2:N0} ops/sec", CNT, sw.Elapsed.TotalSeconds, CNT / sw.Elapsed.TotalSeconds);

            sw.Restart();
            var RCNT = CNT * 3;
            Parallel.For(0, RCNT, i => {
              var j = i % CNT;
              var got = db["t1"].FindOne(Query.ID_EQ_Int32(j));
              Assert.IsNotNull( got );

              var row1 = new TestRow();
              rc.BSONDocumentToRow(got, row1, "A");

              Assert.AreEqual(rows[j], row1);
            });
            sw.Stop();
            Console.WriteLine("{0:N0} row red in {1:N3} sec on {2:N0} ops/sec", RCNT, sw.Elapsed.TotalSeconds, RCNT / sw.Elapsed.TotalSeconds);

              }
        }
        public void Insert_Find_PrimitiveTypesSingleEntry()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            var item = new BSONDocument().Set(new BSONInt32Element("int", int.MaxValue))
                                     .Set(new BSONStringElement("string", "min"))
                                     .Set(new BSONBooleanElement("bool", true))
                                     .Set(new BSONDateTimeElement("datetime", new DateTime(2000, 1, 4, 12, 34, 56, DateTimeKind.Utc)))
                                     .Set(new BSONNullElement("null"))
                                     .Set(new BSONArrayElement("array",
                                              new BSONElement[]
                                              {
                                                new BSONInt32Element(int.MaxValue),
                                                new BSONInt32Element(int.MinValue)
                                              }))
                                     .Set(new BSONBinaryElement("binary", new BSONBinary(BSONBinaryType.UserDefined, Encoding.UTF8.GetBytes("Hello world"))))
                                     .Set(new BSONDocumentElement("document",
                                            new BSONDocument().Set(new BSONInt64Element("innerlong", long.MinValue))))
                                     .Set(new BSONDoubleElement("double", -123.456D))
                                     .Set(new BSONInt64Element("long", long.MaxValue))
                                     .Set(new BSONJavaScriptElement("js", "function(a){var x = a;return x;}"))
                                     .Set(new BSONJavaScriptWithScopeElement("jswithscope",
                                              new BSONCodeWithScope("function(a){var x = a;return x+z;}",
                                                                     new BSONDocument().Set(new BSONInt32Element("z", 12)))))
                                     .Set(new BSONMaxKeyElement("maxkey"))
                                     .Set(new BSONMinKeyElement("minkey"))
                                     .Set(new BSONObjectIDElement("oid", new BSONObjectID(1, 2, 3, 400)))
                                     .Set(new BSONRegularExpressionElement("regex",
                                           new BSONRegularExpression(@"^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$", BSONRegularExpressionOptions.I | BSONRegularExpressionOptions.M)))
                                     .Set(new BSONTimestampElement("timestamp", new BSONTimestamp(new DateTime(2000, 1, 4, 12, 34, 56, DateTimeKind.Utc), 12345)));

            Assert.AreEqual(1, collection.Insert(item).TotalDocumentsAffected);

            var all = collection.Find(new Query());

            all.MoveNext();
            Assert.AreEqual(all.Current.Count, 18);
            Assert.AreEqual(((BSONInt32Element)all.Current["int"]).Value, int.MaxValue);
            Assert.AreEqual(((BSONStringElement)all.Current["string"]).Value, "min");
            Assert.AreEqual(((BSONBooleanElement)all.Current["bool"]).Value, true);
            Assert.AreEqual(((BSONDateTimeElement)all.Current["datetime"]).Value, new DateTime(2000, 1, 4, 12, 34, 56, DateTimeKind.Utc));
            Assert.IsInstanceOf<BSONNullElement>(all.Current["null"]);
            var array = ((BSONArrayElement)all.Current["array"]).Value;
            Assert.AreEqual(array.Length, 2);
            Assert.AreEqual(((BSONInt32Element)array[0]).Value, int.MaxValue);
            Assert.AreEqual(((BSONInt32Element)array[1]).Value, int.MinValue);
            var binary = ((BSONBinaryElement)all.Current["binary"]).Value;
            Assert.AreEqual(binary.Data, Encoding.UTF8.GetBytes("Hello world"));
            Assert.AreEqual(binary.Type, BSONBinaryType.UserDefined);
            var doc = ((BSONDocumentElement)all.Current["document"]).Value;
            Assert.AreEqual(doc.Count, 1);
            Assert.AreEqual(((BSONInt64Element)doc["innerlong"]).Value, long.MinValue);
            Assert.AreEqual(((BSONDoubleElement)all.Current["double"]).Value, -123.456D);
            Assert.AreEqual(((BSONInt64Element)all.Current["long"]).Value, long.MaxValue);
            Assert.AreEqual(((BSONJavaScriptElement)all.Current["js"]).Value, "function(a){var x = a;return x;}");
            var jsScope = ((BSONJavaScriptWithScopeElement)all.Current["jswithscope"]).Value;
            Assert.AreEqual(jsScope.Code, "function(a){var x = a;return x+z;}");
            Assert.AreEqual(jsScope.Scope.Count, 1);
            Assert.AreEqual(((BSONInt32Element)jsScope.Scope["z"]).Value, 12);
            Assert.IsInstanceOf<BSONMaxKeyElement>(all.Current["maxkey"]);
            Assert.IsInstanceOf<BSONMinKeyElement>(all.Current["minkey"]);
            var oid = ((BSONObjectIDElement)all.Current["oid"]).Value;
            Assert.AreEqual(oid.Bytes, new BSONObjectID(1, 2, 3, 400).Bytes);
            Assert.AreEqual(((BSONRegularExpressionElement)all.Current["regex"]).Value,
                        new BSONRegularExpression(@"^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$", BSONRegularExpressionOptions.I | BSONRegularExpressionOptions.M));
            Assert.AreEqual(((BSONTimestampElement)all.Current["timestamp"]).Value,
                        new BSONTimestamp(new DateTime(2000, 1, 4, 12, 34, 56, DateTimeKind.Utc), 12345));

            all.MoveNext();
            Assert.AreEqual(true, all.EOF);
              }
        }
Esempio n. 24
0
        public void T_04_Array()
        {
            using(var client= new MongoClient("My Test"))
              {
            var db = client.DefaultLocalServer["db1"];

            db["t1"].Drop();

            var t1 = db["t1"];

            var row = new ArrayRow
            {
              _id = 1,
              Map = new JSONDataMap{{"Name","Xerson"},{"Age",123}},
              List = new List<object>{ 1,true, "YEZ!", -123.01},
              ObjectArray = new object[]{123, -12, 789d, null, new object[] { 54.67d, "alpIna"}},
              MapArray = new JSONDataMap[]{ new JSONDataMap{{"a",1},{"b",true}},  new JSONDataMap{{"kosmos",234.12},{"b",null}} },
              MapList = new List<JSONDataMap>{ new JSONDataMap{{"abc",0},{"buba", -40.0789}},  new JSONDataMap{{"nothing",null}} }
            };

            var rc = new NFX.Serialization.BSON.RowConverter();
            var doc = rc.RowToBSONDocument(row, "A");
            Assert.AreEqual(1, t1.Insert(doc).TotalDocumentsAffected);

            var got = db["t1"].FindOne(Query.ID_EQ_Int32(1));
            Assert.IsNotNull( got );

            var row1 = new ArrayRow();
            rc.BSONDocumentToRow(got, row1, "A");

            Assert.AreEqual(row, row1);
              }
        }
        public void Insert_Find_UnicodeStings()
        {
            using (var client = new MongoClient("My client"))
              {
            var db = client.DefaultLocalServer["db1"];
            db["t1"].Drop();
            var collection = db["t1"];

            var item = new BSONDocument().Set(new BSONStringElement("eng", "hello"))
              .Set(new BSONStringElement("rus", "привет"))
              .Set(new BSONStringElement("chi", "你好"))
              .Set(new BSONStringElement("jap", "こんにちは"))
              .Set(new BSONStringElement("gre", "γεια σας"))
              .Set(new BSONStringElement("alb", "përshëndetje"))
              .Set(new BSONStringElement("arm", "բարեւ Ձեզ"))
              .Set(new BSONStringElement("vie", "xin chào"))
              .Set(new BSONStringElement("por", "Olá"))
              .Set(new BSONStringElement("ukr", "Привіт"))
              .Set(new BSONStringElement("ger", "wünsche"));

            Assert.AreEqual(1, collection.Insert(item).TotalDocumentsAffected);

            var all = collection.Find(new Query());

            all.MoveNext();
            Assert.AreEqual(all.Current.Count, 12);
            Assert.AreEqual(((BSONStringElement)all.Current["eng"]).Value, "hello");
            Assert.AreEqual(((BSONStringElement)all.Current["rus"]).Value, "привет");
            Assert.AreEqual(((BSONStringElement)all.Current["chi"]).Value, "你好");
            Assert.AreEqual(((BSONStringElement)all.Current["jap"]).Value, "こんにちは");
            Assert.AreEqual(((BSONStringElement)all.Current["gre"]).Value, "γεια σας");
            Assert.AreEqual(((BSONStringElement)all.Current["alb"]).Value, "përshëndetje");
            Assert.AreEqual(((BSONStringElement)all.Current["arm"]).Value, "բարեւ Ձեզ");
            Assert.AreEqual(((BSONStringElement)all.Current["vie"]).Value, "xin chào");
            Assert.AreEqual(((BSONStringElement)all.Current["por"]).Value, "Olá");
            Assert.AreEqual(((BSONStringElement)all.Current["ukr"]).Value, "Привіт");
            Assert.AreEqual(((BSONStringElement)all.Current["ger"]).Value, "wünsche");

            all.MoveNext();
            Assert.AreEqual(true, all.EOF);
              }
        }