コード例 #1
0
        public void Create_Database_With_Initial_Size_Encrypted()
        {
            var initial = 40 * 1024; // initial size: 40kb
            var minimal = 4096 * 5;  // 1 header + 1 lock + 1 collection + 1 data + 1 index = 5 pages minimal

            using (var file = new TempFile(checkIntegrity: false))
                using (var db = new UltraLiteDatabase(file.Conn("initial size=40kb; password=123")))
                {
                    // just ensure open datafile
                    var uv = db.Engine.UserVersion;

                    // test if file has 40kb
                    Assert.AreEqual(initial, file.Size);

                    // simple insert to test if datafile still with 40kb
                    BsonDocument doc = new BsonDocument();
                    doc["a"] = 1;
                    db.Engine.Insert("col1", doc);

                    Assert.AreEqual(initial, file.Size);

                    // ok, now shrink and test if file are minimal size
                    db.Shrink();

                    Assert.AreEqual(minimal, file.Size);
                }
        }
コード例 #2
0
        public async Task PerformanceTest1()
        {
            AssertV2.throwExeptionIfAssertionFails = true;

            var dataTree = NewTreeLayer("1", 1000, () => NewTreeLayer("2", 2, () => NewTreeLayer("3", 4, () => NewTreeLayer("4", 1))));

            var testFolder = EnvironmentV2.instance.GetOrAddTempFolder("tests.io.db").CreateV2();

            Log.d("Path=" + testFolder);
            var dbFile = testFolder.GetChild("PerformanceTestDB_" + Guid.NewGuid().ToString());

            // Open database (or create if doesn't exist)
            using (var db = new UltraLiteDatabase(dbFile.OpenOrCreateForReadWrite(), disposeStream: true)) {
                await InsertIntoDb(dataTree, db);
                await ReadFromDb(dataTree, db);
            }

            await WriteFiles(dataTree, testFolder);
            await ReadFiles(dataTree, testFolder);

            // cleanup after the test
            await ParallelExec(dataTree, (elem) => { GetFileForElem(testFolder, elem).DeleteV2(); });

            Assert.True(dbFile.DeleteV2());
            Assert.False(dbFile.Exists);
        }
コード例 #3
0
ファイル: ULongList_Tests.cs プロジェクト: rejemy/UltraLiteDB
        public void ULongList_Mapper()
        {
            using (var file = new TempFile())
                using (var db = new UltraLiteDatabase(file.Filename))
                {
                    var col = db.GetCollection <Gang>();

                    col.Insert(new Gang
                    {
                        GuildId  = 1,
                        LeaderId = 2,
                        Members  = new List <ulong> {
                            5, 6, 7
                        }
                    });

                    ulong userId  = 5;
                    ulong guildId = 1;

                    Query q = Query.And(Query.EQ("GuildId", guildId), Query.EQ("LeaderId", userId));
                    var   e = col.Exists(q);

                    //Assert.IsTrue(e);
                }
        }
コード例 #4
0
        public void FindAll()
        {
            using (var f = new TempFile())
            {
                using (var db = new UltraLiteDatabase(f.Filename))
                {
                    var col = db.GetCollection <Person>("Person");

                    col.Insert(new Person {
                        Fullname = "John"
                    });
                    col.Insert(new Person {
                        Fullname = "Doe"
                    });
                    col.Insert(new Person {
                        Fullname = "Joana"
                    });
                    col.Insert(new Person {
                        Fullname = "Marcus"
                    });
                }
                // close datafile

                using (var db = new UltraLiteDatabase(f.Filename))
                {
                    var p = db.GetCollection <Person>("Person").Find(Query.All("Fullname", Query.Ascending));

                    Assert.AreEqual(4, p.Count());
                }
            }
        }
コード例 #5
0
        public void Query_Min_Max()
        {
            using (var f = new TempFile())
                using (var db = new UltraLiteDatabase(f.Filename))
                {
                    var c = db.GetCollection <EntityMinMax>("col");

                    c.Insert(new EntityMinMax {
                    });
                    c.Insert(new EntityMinMax
                    {
                        ByteValue = 200,
                        IntValue  = 443500,
                        LongValue = 443500,
                        UintValue = 443500
                    });

                    c.EnsureIndex("ByteValue");
                    c.EnsureIndex("IntValue");
                    c.EnsureIndex("LongValue");
                    c.EnsureIndex("UintValue");

                    Assert.AreEqual(200, c.Max("ByteValue").AsInt32);
                    Assert.AreEqual(443500, c.Max("IntValue").AsInt32);
                    Assert.AreEqual(443500, c.Max("LongValue").AsInt64);
                    Assert.AreEqual(443500, c.Max("UintValue").AsInt32);
                }
        }
コード例 #6
0
        public void Delete_By_Name()
        {
            using (var f = new TempFile())
                using (var db = new UltraLiteDatabase(f.Filename))
                {
                    var col = db.GetCollection <Person>("Person");

                    col.Insert(new Person {
                        Fullname = "John"
                    });
                    col.Insert(new Person {
                        Fullname = "Doe"
                    });
                    col.Insert(new Person {
                        Fullname = "Joana"
                    });
                    col.Insert(new Person {
                        Fullname = "Marcus"
                    });

                    // lets auto-create index in FullName and delete from a non-pk node
                    var del = col.Delete(Query.StartsWith("Fullname", "J"));

                    Assert.AreEqual(2, del);
                }
        }
コード例 #7
0
        public void Simple_Polymorphics()
        {
            using (var file = new TempFile())
            {
                using (var db = new UltraLiteDatabase(file.Filename))
                {
                    var col = db.GetCollection <MyBase>("col1");

                    col.Insert(new Descendant1()
                    {
                        Id = 1
                    });
                    col.Insert(new Descendant2()
                    {
                        Id = 2
                    });
                }

                using (var db = new UltraLiteDatabase(file.Filename))
                {
                    var col = db.GetCollection <MyBase>("col1");

                    var d1 = col.FindById(1);
                    var d2 = col.FindById(2);

                    Assert.AreEqual(typeof(Descendant1), d1.GetType());
                    Assert.AreEqual(typeof(Descendant2), d2.GetType());
                }
            }
        }
コード例 #8
0
 private void Init(Stream stream, string collectionName = "Default")
 {
     dbStream   = stream;
     bsonMapper = new BsonMapper();
     bsonMapper.IncludeFields = true;
     db         = new UltraLiteDatabase(dbStream, bsonMapper);
     collection = db.GetCollection(collectionName);
 }
コード例 #9
0
 private void Init(FileEntry dbFile, string collectionName = "Default")
 {
     bsonMapper = new BsonMapper();
     bsonMapper.IncludeFields = true;
     dbStream   = dbFile.OpenOrCreateForReadWrite();
     db         = new UltraLiteDatabase(dbStream, bsonMapper);
     collection = db.GetCollection(collectionName);
 }
コード例 #10
0
        public StorageManager()
        {
            var filepath = Application.isEditor ? Application.streamingAssetsPath : Application.persistentDataPath;

            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);
            }
            _database = new UltraLiteDatabase(new ConnectionString($"Filename={filepath}/{DatabaseName};Password=efcf8794-8c30-4d1b-940a-df1475c7b26d"));
        }
コード例 #11
0
        public void ConnectionString_Sets_Log_Level()
        {
            var connectionString = "filename=foo;";
            var db = new UltraLiteDatabase(connectionString);

            Assert.AreEqual(0, db.Log.Level);

            connectionString = "filename=foo;log=" + Logger.FULL;
            db = new UltraLiteDatabase(connectionString);
            Assert.AreEqual(Logger.FULL, db.Log.Level);
        }
コード例 #12
0
        public void DateTimeMinMax_Test()
        {
            var memory = new MemoryStream();

            using (var db = new UltraLiteDatabase(memory))
            {
                var col = db.GetCollection <DateTimeTest>();
                col.EnsureIndex("Date");

                col.Insert(new DateTimeTest()
                {
                    Id = 1, Date = new DateTime(2018, 02, 22, 0, 0, 0)
                });
                col.Insert(new DateTimeTest()
                {
                    Id = 2, Date = new DateTime(2018, 02, 22, 23, 59, 59)
                });

                MinMaxCommon(col);
            }

            using (var db = new UltraLiteDatabase(memory))
            {
                var col = db.GetCollection <DateTimeTest>();

                MinMaxCommon(col);

                col.Insert(new DateTimeTest()
                {
                    Id = 3, Date = new DateTime(2018, 02, 21, 23, 59, 59)
                });
                col.Insert(new DateTimeTest()
                {
                    Id = 4, Date = new DateTime(2018, 02, 23, 0, 0, 0)
                });
                col.Insert(new DateTimeTest()
                {
                    Id = 5, Date = new DateTime(2018, 02, 22, 0, 0, 1)
                });
                col.Insert(new DateTimeTest()
                {
                    Id = 6, Date = new DateTime(2018, 02, 22, 23, 59, 58)
                });

                MinMaxCommon(col);
            }

            using (var db = new UltraLiteDatabase(memory))
            {
                var col = db.GetCollection <DateTimeTest>();

                MinMaxCommon(col);
            }
        }
コード例 #13
0
        private static async Task ReadFromDb(List <TreeElem> dataTree, UltraLiteDatabase db)
        {
            var readTimer = Log.MethodEntered("LiteDbPerformanceTest1.ReadFromDb");
            var elements  = db.GetCollection <TreeElem>("elements");

            await ParallelExec(dataTree, (elem) => {
                var found = elements.FindById(elem.id);
                Assert.Equal(elem.name, found.name);
            });

            Log.MethodDone(readTimer, 800);
        }
コード例 #14
0
        private static async Task InsertIntoDb(List <TreeElem> dataTree, UltraLiteDatabase db)
        {
            var    insertTimer = Log.MethodEntered("LiteDbPerformanceTest1.InsertIntoDb");
            var    elements    = db.GetCollection <TreeElem>("elements");
            object threadLock  = new object();

            await ParallelExec(dataTree, (elem) => {
                lock (threadLock) {
                    elements.Insert(elem);
                }
            });

            Log.MethodDone(insertTimer, 4000);
            Assert.Equal(dataTree.Count, elements.FindAll().Count());
        }
コード例 #15
0
        public void Init()
        {
            // Open database (or create if doesn't exist)
            var db = new UltraLiteDatabase("MyData.db");

            return;

            // Get a collection
            var col = db.GetCollection(this.GetType().Name);

            // Create a new character document
            var character = new BsonDocument();

            character["Name"]     = "John Doe";
            character["Level"]    = 1;
            character["IsActive"] = true;
            // Insert new customer document (Id will be auto generated)
            BsonValue id = col.Insert(character);

            // new Id has also been added to the document at character["_id"]

            // Update a document inside a collection
            character["Name"] = "Joana Doe";
            col.Update(character);

            // Insert a document with a manually chosen Id
            var character2 = new BsonDocument();

            character2["_id"]      = 10;
            character2["Name"]     = "Test Bob";
            character2["Level"]    = 10;
            character2["IsActive"] = true;
            col.Insert(character2);
            // Load all documents
            List <BsonDocument> allCharacters = new List <BsonDocument>(col.FindAll());

            // Delete something
            col.Delete(10);

            // Upsert (Update if present or insert if not)
            col.Upsert(character);

            // Don't forget to cleanup!
            db.Dispose();
        }
コード例 #16
0
        public LiteDBStorageItem(UltraLiteDatabase db, string key)
        {
            Key         = key;
            _database   = db;
            _collection = _database.GetCollection <Definition>(TableName);
            _filter     = Query.EQ("Key", new BsonValue(key));
            var doc = _collection.FindOne(_filter);

            if (doc == null)
            {
                _collection.Insert(new Definition {
                    Key = key
                });
            }
            else if (!string.IsNullOrWhiteSpace(doc.Json))
            {
                data = JsonConvert.DeserializeObject <T>(doc.Json);
            }
        }
コード例 #17
0
ファイル: LiteDbTests.cs プロジェクト: shoshiiran/cscore
        void ExampleUsage1()
        {
            string testId = Guid.NewGuid().ToString();

            var dbFile = EnvironmentV2.instance.GetOrAddTempFolder("tests.io.db").GetChild("TestDB_" + testId);

            // Open database (or create if doesn't exist)
            using (var db = new UltraLiteDatabase(dbFile.OpenOrCreateForReadWrite(), disposeStream: true)) {
                var users = db.GetCollection <User>("users");

                var user1 = new User {
                    id   = testId,
                    name = "John Doe",
                    age  = 39
                };

                // Create unique index in Name field
                // https://github.com/mbdavid/LiteDB/wiki/Indexes#ensureindex
                users.EnsureIndex("name", true);

                users.Insert(user1);

                user1.name = "Joana Doe";
                users.Update(user1);
                Assert.Equal("Joana Doe", users.FindById(testId).name);

                user1.name = "Joana Doe 2";
                users.Upsert(user1); // insert or update if already found
                Assert.Equal("Joana Doe 2", users.FindById(testId).name);

                // Use LINQ to query documents (with no index)
                var queryResults = users.Find(Query.GT("age", 20));
                Assert.Single(queryResults);
                Assert.Equal("Joana Doe 2", queryResults.First().name);
            }
            Assert.True(dbFile.IsNotNullAndExists());
            dbFile.DeleteV2(); // cleanup after the test
        }
コード例 #18
0
        // [TestMethod]
        public void UpgradeScript_Test()
        {
            // export data TO JSON
            using (var db = new UltraLiteDatabase(new MemoryStream()))
            {
                var data = new BsonDocument();
                foreach (var name in db.GetCollectionNames())
                {
                    data[name] = new BsonArray(db.GetCollection(name).FindAll());
                }
                File.WriteAllText(@"C:\Temp\dump-data.json", JsonSerializer.Serialize(data));
            }

            // import data FROM JSON
            using (var db = new UltraLiteDatabase(new MemoryStream()))
            {
                var data = JsonSerializer.Deserialize(File.ReadAllText(@"C:\Temp\dump-data.json")).AsDocument;
                foreach (var name in data.Keys)
                {
                    db.GetCollection(name).Insert(data[name].AsArray.Select(x => x.AsDocument));
                }
            }
        }
コード例 #19
0
        public void Derived_Type()
        {
            using (var db = new UltraLiteDatabase(new MemoryStream()))
            {
                var derived1 = new Derived1 {
                    Id = 1, Member1 = "Derived1"
                };
                var derived2 = new Derived2 {
                    Id = 2, Member2 = "Dereived2"
                };

                var colTyped = db.GetCollection <Base>("Collection");

                colTyped.Insert(derived1);
                colTyped.Insert(derived2);

                var colBson = db.GetCollection <BsonDocument>("Collection");

                var docs = colBson.FindAll().ToList();

                Assert.IsTrue(docs.Count > 0);

                // checks if BsonDocument contains _type
                var doc1 = colBson.FindById(1);
                var doc2 = colBson.FindById(2);

                Assert.IsTrue(doc1["_type"].AsString.Contains("Derived1"));
                Assert.IsTrue(doc2["_type"].AsString.Contains("Derived2"));

                // now, test if all document will deserialize with right type
                var d1 = colTyped.FindById(1);
                var d2 = colTyped.FindById(2);

                Assert.IsTrue(d1 is Derived1);
                Assert.IsTrue(d2 is Derived2);
            }
        }
コード例 #20
0
        void ExampleUsage1()
        {
            var dbFile = EnvironmentV2.instance.GetOrAddTempFolder("tests.io.db").GetChild("TestDB_" + NewId());

            // Open database (or create if doesn't exist)
            using (var db = new UltraLiteDatabase(dbFile.OpenOrCreateForReadWrite(), disposeStream: true)) {
                var elems = db.GetCollection <ClassA>("users");
                {
                    var ele = new ClassA {
                        id = NewId(), a = "abc"
                    };
                    elems.Insert(ele);
                    Assert.Equal("abc", elems.FindById(ele.id).a);
                }
                {
                    var ele = new ClassA1 {
                        id = NewId(), a = "abc", name = "A1"
                    };
                    elems.Insert(ele);
                    var r = elems.FindById(ele.id) as ClassA1;
                    Assert.Equal("abc", r.a);
                    Assert.Equal("A1", r.name);
                }
                {
                    var ele = new ClassA2 {
                        id = NewId(), a = "abc", age = 99
                    };
                    elems.Insert(ele);
                    var r = elems.FindById(ele.id) as ClassA2;
                    Assert.Equal("abc", r.a);
                    Assert.Equal(99, r.age);
                }
            }
            Assert.True(dbFile.IsNotNullAndExists());
            dbFile.DeleteV2(); // cleanup after the test
        }
コード例 #21
0
        public void AutoId_Strong_Typed()
        {
            var mapper = new BsonMapper();

            using (var db = new UltraLiteDatabase(new MemoryStream(), mapper))
            {
                var cs_int  = db.GetCollection <EntityInt>("int");
                var cs_long = db.GetCollection <EntityLong>("long");
                var cs_guid = db.GetCollection <EntityGuid>("guid");
                var cs_oid  = db.GetCollection <EntityOid>("oid");
                var cs_str  = db.GetCollection <EntityString>("str");

                // int32
                var cint_1 = new EntityInt()
                {
                    Name = "R1"
                };
                var cint_2 = new EntityInt()
                {
                    Name = "R2"
                };
                var cint_3 = new EntityInt()
                {
                    Name = "R3"
                };
                var cint_4 = new EntityInt()
                {
                    Name = "R4"
                };

                // long
                var clong_1 = new EntityLong()
                {
                    Name = "R1"
                };
                var clong_2 = new EntityLong()
                {
                    Name = "R2"
                };
                var clong_3 = new EntityLong()
                {
                    Name = "R3"
                };
                var clong_4 = new EntityLong()
                {
                    Name = "R4"
                };

                // guid
                var cguid_1 = new EntityGuid()
                {
                    Name = "R1"
                };
                var cguid_2 = new EntityGuid()
                {
                    Name = "R2"
                };
                var cguid_3 = new EntityGuid()
                {
                    Name = "R3"
                };
                var cguid_4 = new EntityGuid()
                {
                    Name = "R4"
                };

                // oid
                var coid_1 = new EntityOid()
                {
                    Name = "R1"
                };
                var coid_2 = new EntityOid()
                {
                    Name = "R2"
                };
                var coid_3 = new EntityOid()
                {
                    Name = "R3"
                };
                var coid_4 = new EntityOid()
                {
                    Name = "R4"
                };

                // string - there is no AutoId for string
                var cstr_1 = new EntityString()
                {
                    Id = "a", Name = "R1"
                };
                var cstr_2 = new EntityString()
                {
                    Id = "b", Name = "R2"
                };
                var cstr_3 = new EntityString()
                {
                    Id = "c", Name = "R3"
                };
                var cstr_4 = new EntityString()
                {
                    Id = "d", Name = "R4"
                };

                // insert first 3 documents
                cs_int.Insert(new[] { cint_1, cint_2, cint_3 });
                cs_long.Insert(new[] { clong_1, clong_2, clong_3 });
                cs_guid.Insert(new[] { cguid_1, cguid_2, cguid_3 });
                cs_oid.Insert(new[] { coid_1, coid_2, coid_3 });
                cs_str.Insert(new[] { cstr_1, cstr_2, cstr_3 });

                // change document 2
                cint_2.Name  = "Changed 2";
                clong_2.Name = "Changed 2";
                cguid_2.Name = "Changed 2";
                coid_2.Name  = "Changed 2";
                cstr_2.Name  = "Changed 2";

                // update document 2
                var nu_int  = cs_int.Update(cint_2);
                var nu_long = cs_long.Update(clong_2);
                var nu_guid = cs_guid.Update(cguid_2);
                var nu_oid  = cs_oid.Update(coid_2);
                var nu_str  = cs_str.Update(cstr_2);

                Assert.IsTrue(nu_int);
                Assert.IsTrue(nu_long);
                Assert.IsTrue(nu_guid);
                Assert.IsTrue(nu_oid);
                Assert.IsTrue(nu_str);

                // change document 3
                cint_3.Name  = "Changed 3";
                clong_3.Name = "Changed 3";
                cguid_3.Name = "Changed 3";
                coid_3.Name  = "Changed 3";
                cstr_3.Name  = "Changed 3";

                // upsert (update) document 3
                var fu_int  = cs_int.Upsert(cint_3);
                var fu_long = cs_long.Upsert(clong_3);
                var fu_guid = cs_guid.Upsert(cguid_3);
                var fu_oid  = cs_oid.Upsert(coid_3);
                var fu_str  = cs_str.Upsert(cstr_3);

                Assert.IsFalse(fu_int);
                Assert.IsFalse(fu_long);
                Assert.IsFalse(fu_guid);
                Assert.IsFalse(fu_oid);
                Assert.IsFalse(fu_str);

                // test if was changed
                Assert.AreEqual(cint_3.Name, cs_int.FindOne(Query.EQ("_id", cint_3.Id)).Name);
                Assert.AreEqual(clong_3.Name, cs_long.FindOne(Query.EQ("_id", clong_3.Id)).Name);
                Assert.AreEqual(cguid_3.Name, cs_guid.FindOne(Query.EQ("_id", cguid_3.Id)).Name);
                Assert.AreEqual(coid_3.Name, cs_oid.FindOne(Query.EQ("_id", coid_3.Id)).Name);
                Assert.AreEqual(cstr_3.Name, cs_str.FindOne(Query.EQ("_id", cstr_3.Id)).Name);

                // upsert (insert) document 4
                var tu_int  = cs_int.Upsert(cint_4);
                var tu_long = cs_long.Upsert(clong_4);
                var tu_guid = cs_guid.Upsert(cguid_4);
                var tu_oid  = cs_oid.Upsert(coid_4);
                var tu_str  = cs_str.Upsert(cstr_4);

                Assert.IsTrue(tu_int);
                Assert.IsTrue(tu_long);
                Assert.IsTrue(tu_guid);
                Assert.IsTrue(tu_oid);
                Assert.IsTrue(tu_str);

                // test if was included
                Assert.AreEqual(cint_4.Name, cs_int.FindOne(Query.EQ("_id", cint_4.Id)).Name);
                Assert.AreEqual(clong_4.Name, cs_long.FindOne(Query.EQ("_id", clong_4.Id)).Name);
                Assert.AreEqual(cguid_4.Name, cs_guid.FindOne(Query.EQ("_id", cguid_4.Id)).Name);
                Assert.AreEqual(coid_4.Name, cs_oid.FindOne(Query.EQ("_id", coid_4.Id)).Name);
                Assert.AreEqual(cstr_4.Name, cs_str.FindOne(Query.EQ("_id", cstr_4.Id)).Name);

                // count must be 4
                Assert.AreEqual(4, cs_int.Count(Query.All()));
                Assert.AreEqual(4, cs_long.Count(Query.All()));
                Assert.AreEqual(4, cs_guid.Count(Query.All()));
                Assert.AreEqual(4, cs_oid.Count(Query.All()));
                Assert.AreEqual(4, cs_str.Count(Query.All()));

                // for Int32 (or Int64) - add "bouble" on sequence
                var cint_10 = new EntityInt {
                    Id = 10, Name = "R10"
                };
                var cint_11 = new EntityInt {
                    Name = "R11"
                };
                var cint_7 = new EntityInt {
                    Id = 7, Name = "R7"
                };
                var cint_12 = new EntityInt {
                    Name = "R12"
                };

                cs_int.Insert(cint_10); // "loose" sequente between 5-9
                cs_int.Insert(cint_11); // insert as 11
                cs_int.Insert(cint_7);  // insert as 7
                cs_int.Insert(cint_12); // insert as 12

                Assert.AreEqual(10, cint_10.Id);
                Assert.AreEqual(11, cint_11.Id);
                Assert.AreEqual(7, cint_7.Id);
                Assert.AreEqual(12, cint_12.Id);
            }
        }
コード例 #22
0
        public void AutoId_No_Duplicate_After_Delete()
        {
            // using strong type
            using (var db = new UltraLiteDatabase(new MemoryStream()))
            {
                var col = db.GetCollection <EntityInt>("col1");

                var one = new EntityInt {
                    Name = "One"
                };
                var two = new EntityInt {
                    Name = "Two"
                };
                var three = new EntityInt {
                    Name = "Three"
                };
                var four = new EntityInt {
                    Name = "Four"
                };

                // insert
                col.Insert(one);
                col.Insert(two);

                Assert.AreEqual(1, one.Id);
                Assert.AreEqual(2, two.Id);

                // now delete first 2 rows
                col.Delete(one.Id);
                col.Delete(two.Id);

                // and insert new documents
                col.Insert(new EntityInt[] { three, four });

                Assert.AreEqual(3, three.Id);
                Assert.AreEqual(4, four.Id);
            }

            // using bsondocument/engine
            using (var db = new UltraLiteEngine(new MemoryStream()))
            {
                var one = new BsonDocument {
                    ["Name"] = "One"
                };
                var two = new BsonDocument {
                    ["Name"] = "Two"
                };
                var three = new BsonDocument {
                    ["Name"] = "Three"
                };
                var four = new BsonDocument {
                    ["Name"] = "Four"
                };

                db.Insert("col", one, BsonAutoId.Int32);
                db.Insert("col", two, BsonAutoId.Int32);

                Assert.AreEqual(1, one["_id"].AsInt32);
                Assert.AreEqual(2, two["_id"].AsInt32);

                // now delete first 2 rows
                db.Delete("col", one["_id"].AsInt32);
                db.Delete("col", two["_id"].AsInt32);

                // and insert new documents
                db.Insert("col", new BsonDocument[] { three, four }, BsonAutoId.Int32);

                Assert.AreEqual(3, three["_id"].AsInt32);
                Assert.AreEqual(4, four["_id"].AsInt32);
            }
        }
コード例 #23
0
ファイル: Shrink_Tests.cs プロジェクト: rejemy/UltraLiteDB
        public void Shrink_Large_Files()
        {
            // do some tests
            Action <UltraLiteEngine> DoTest = (db) =>
            {
                Assert.AreEqual(1, db.Count("col", null));
                Assert.AreEqual(99, db.UserVersion);
                Assert.IsNotNull(db.GetIndexes("col").FirstOrDefault(x => x.Field == "name"));
                Assert.IsTrue(db.GetIndexes("col").FirstOrDefault(x => x.Field == "name").Unique);
            };

            using (var file = new TempFile())
            {
                using (var dbe = new UltraLiteDatabase(file.Filename))
                {
                    var db = dbe.Engine;

                    db.UserVersion = 99;
                    db.EnsureIndex("col", "name", true);
                    db.Insert("col", GetDocs(1, 40000));
                    db.Delete("col", Query.GT("_id", 1)); // delete 29.999 docs

                    Assert.AreEqual(1, db.Count("col", null));

                    // file still large than 20mb (even with only 1 document)
                    Assert.IsTrue(file.Size > 20 * 1024 * 1024);

                    // reduce datafile (use temp disk) (from UltraLiteDatabase)
                    dbe.Shrink();

                    // now file are small than 50kb
                    Assert.IsTrue(file.Size < 50 * 1024);

                    DoTest(db);
                }

                // re-open datafile to check if is ok
                using (var db = new UltraLiteDatabase(file.Filename))
                {
                    // still 1 doc and 1 name unique index
                    DoTest(db.Engine);

                    // shrink again but now with password
                    var reduced = db.Shrink("abc123");

                    // file still same size (but now are encrypted)
                    Assert.AreEqual(0, reduced);

                    // still 1 doc and 1 name unique index
                    DoTest(db.Engine);
                }

                // re-open, again, but now with password
                using (var db = new UltraLiteEngine(file.Filename, "abc123"))
                {
                    DoTest(db);

                    // now, remove password
                    db.Shrink();

                    // test again
                    DoTest(db);
                }
            }
        }