Exemplo n.º 1
0
        /// <summary>
        /// Read all colleciton, indexes and documents inside current datafile
        /// Drop per index, per collection and shrink
        /// This steps will check/validate all file data
        /// </summary>
        private void CheckIntegrity()
        {
            using (var db = new UltraLiteEngine(this.Filename))
            {
                var cols = db.GetCollectionNames().ToArray();

                foreach (var col in cols)
                {
                    var indexes = db.GetIndexes(col).ToArray();

                    foreach (var idx in indexes)
                    {
                        var q = db.Find(col, Query.All(idx.Field));

                        foreach (var doc in q)
                        {
                            // document are ok!
                        }

                        // lets drop this index (if not _id)
                        if (idx.Field != "_id")
                        {
                            db.DropIndex(col, idx.Field);
                        }
                    }

                    // and drop collection
                    db.DropCollection(col);
                }

                // and now shrink
                db.Shrink();
            }
        }
Exemplo n.º 2
0
        public void Delete_Query()
        {
            using (var file = new TempFile())
            {
                var initial = new DateTime(2000, 01, 01);

                using (var db = new UltraLiteEngine(file.Filename))
                {
                    for (var i = 0; i < 5000; i++)
                    {
                        db.Insert("col", new BsonDocument {
                            { "dt", initial.AddDays(i) }
                        });
                    }

                    db.EnsureIndex("col", "dt");

                    Assert.AreEqual(5000, db.Count("col"));

                    Assert.AreEqual(0, db.Count("col", Query.GT("dd", initial)));

                    var del = db.Delete("col", Query.GT("dd", initial));

                    Assert.AreEqual(0, del);

                    Assert.AreEqual(0, db.Count("col", Query.GT("dd", initial)));
                }
            }
        }
Exemplo n.º 3
0
        public void Engine_QueryUpdate_Documents()
        {
            using (var file = new TempFile())
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    db.EnsureIndex("col", "name");

                    // insert 4 documents
                    db.Insert("col", new BsonDocument {
                        { "_id", 1 }
                    });
                    db.Insert("col", new BsonDocument {
                        { "_id", 2 }
                    });
                    db.Insert("col", new BsonDocument {
                        { "_id", 3 }
                    });
                    db.Insert("col", new BsonDocument {
                        { "_id", 4 }
                    });

                    // query all documents and update name
                    foreach (var d in db.Find("col", Query.All()))
                    {
                        d["name"] = "john";
                        db.Update("col", d);
                    }

                    // this simple test if same thread open a read mode and then open write lock mode
                    Assert.AreEqual(4, db.Count("col", Query.EQ("name", "john")));
                }
        }
Exemplo n.º 4
0
        public void Query_Using_First_Linq()
        {
            using (var file = new TempFile())
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    db.Insert("col", new BsonDocument[]
                    {
                        new BsonDocument {
                            { "_id", 1 }, { "name", "e" }
                        },
                        new BsonDocument {
                            { "_id", 2 }, { "name", "d" }
                        },
                        new BsonDocument {
                            { "_id", 3 }, { "name", "c" }
                        },
                        new BsonDocument {
                            { "_id", 4 }, { "name", "b" }
                        },
                        new BsonDocument {
                            { "_id", 5 }, { "name", "a" }
                        }
                    });

                    var first = db.Find("col", Query.All()).First();

                    Assert.AreEqual("e", first["name"].AsString);
                }
        }
Exemplo n.º 5
0
        public void Simple_Performance_Runner()
        {
            // just a simple example to test performance speed
            using (var file = new TempFile())
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    var ti = new Stopwatch();
                    var tx = new Stopwatch();
                    var tu = new Stopwatch();
                    var td = new Stopwatch();

                    ti.Start();
                    db.Insert("col", GetDocs(N1));
                    ti.Stop();

                    tx.Start();
                    db.EnsureIndex("col", "name");
                    tx.Stop();

                    tu.Start();
                    db.Update("col", GetDocs(N1));
                    tu.Stop();

                    db.EnsureIndex("col", "name");

                    td.Start();
                    db.Delete("col", Query.All());
                    td.Stop();

                    Debug.WriteLine("Insert time: " + ti.ElapsedMilliseconds);
                    Debug.WriteLine("EnsureIndex time: " + tx.ElapsedMilliseconds);
                    Debug.WriteLine("Update time: " + tu.ElapsedMilliseconds);
                    Debug.WriteLine("Delete time: " + td.ElapsedMilliseconds);
                }
        }
Exemplo n.º 6
0
 public void CreateDatafile()
 {
     using (var s = new FileStream(Filename, System.IO.FileMode.CreateNew))
     {
         UltraLiteEngine.CreateDatabase(s);
     }
 }
Exemplo n.º 7
0
        public void Engine_Upsert_Documents()
        {
            using (var file = new TempFile())
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    var doc1 = new BsonDocument {
                        { "_id", 1 }, { "name", "John" }
                    };

                    var u1 = db.Upsert("col", doc1); // true (insert)

                    doc1["name"] = "changed";

                    var u2 = db.Upsert("col", doc1); // false (update)

                    Assert.AreEqual(true, u1);
                    Assert.AreEqual(false, u2);

                    // get data from db
                    var r = db.Find("col", Query.EQ("_id", 1)).Single();

                    // test changed value
                    Assert.AreEqual(doc1["name"].AsString, r["name"].AsString);
                }
        }
Exemplo n.º 8
0
        public void Encrypted_Database()
        {
            using (var encrypt = new TempFile())
                using (var plain = new TempFile())
                {
                    // create a database with no password - plain data
                    using (var db = new UltraLiteEngine(plain.Filename))
                    {
                        db.Insert("col", new BsonDocument {
                            { "name", "Mauricio David" }
                        });
                    }

                    // read datafile to find "Mauricio" string
                    Assert.IsTrue(plain.ReadAsText().Contains("Mauricio David"));

                    // create a database with password
                    using (var db = new UltraLiteEngine(encrypt.Filename, "abc123"))
                    {
                        db.Insert("col", new BsonDocument {
                            { "name", "Mauricio David" }
                        });
                    }

                    // test if is possible find "Mauricio" string
                    Assert.IsFalse(encrypt.ReadAsText().Contains("Mauricio David"));

                    // try access using wrong password
                    try
                    {
                        using (var db = new UltraLiteEngine(encrypt.Filename, "abc1234"))
                        {
                            Assert.Fail(); // can't work
                        }
                    }
                    catch (UltraLiteException ex)
                    {
                        Assert.IsTrue(ex.ErrorCode == 123); // wrong password
                    }

                    // open encrypted db and read document
                    using (var db = new UltraLiteEngine(encrypt.Filename, "abc123"))
                    {
                        var doc = db.Find("col", Query.All()).First();

                        Assert.AreEqual("Mauricio David", doc["name"].AsString);

                        // let's remove password to work CheckIntegrety
                        db.Shrink(null, null);
                    }
                }
        }
Exemplo n.º 9
0
        public void Bulk_Insert_Engine()
        {
            using (var file = new TempFile())
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    // let's bulk 500.000 documents
                    db.InsertBulk("col", GetDocs(1, 60000));

                    // and assert if all are inserted (based on collection header only)
                    Assert.AreEqual(60000, db.Count("col"));

                    // and now count all
                    Assert.AreEqual(60000, db.Count("col", Query.All()));
                }
        }
Exemplo n.º 10
0
        public void UserVersion_Get_Set()
        {
            using (var file = new TempFile())
            {
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    Assert.AreEqual(0, db.UserVersion);
                    db.UserVersion = 5;
                }

                using (var db = new UltraLiteEngine(file.Filename))
                {
                    Assert.AreEqual(5, db.UserVersion);
                }
            }
        }
Exemplo n.º 11
0
        public void DropCollection()
        {
            using (var file = new TempFile())
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    Assert.IsFalse(db.GetCollectionNames().Any(x => x == "col"));

                    db.Insert("col", new BsonDocument {
                        { "a", 1 }
                    });
                    Assert.IsTrue(db.GetCollectionNames().Any(x => x == "col"));

                    db.DropCollection("col");

                    Assert.IsFalse(db.GetCollectionNames().Any(x => x == "col"));
                }
        }
Exemplo n.º 12
0
        public void Shrink_After_DropCollection()
        {
            using (var file = new TempFile())
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    db.InsertBulk("col", GetDocs(1, 10000));

                    db.DropCollection("col");

                    // full disk usage
                    var size = file.Size;

                    var r = db.Shrink();

                    // only header page + reserved lock page
                    Assert.AreEqual(8192, size - r);
                }
        }
Exemplo n.º 13
0
        public void Engine_Delete_Documents()
        {
            using (var file = new TempFile())
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    var doc1 = new BsonDocument {
                        { "_id", 1 }, { "name", "John" }
                    };
                    var doc2 = new BsonDocument {
                        { "_id", 2 }, { "name", "Doe" }
                    };

                    db.Insert("col", doc1);
                    db.Insert("col", doc2);

                    db.Delete("col", Query.GTE("_id", 1));

                    db.Insert("col", doc1);
                }
        }
Exemplo n.º 14
0
        public void Index_Order()
        {
            using (var tmp = new TempFile())
                using (var db = new UltraLiteEngine(tmp.Filename))
                {
                    db.Insert("col", new BsonDocument {
                        { "text", "D" }
                    });
                    db.Insert("col", new BsonDocument {
                        { "text", "A" }
                    });
                    db.Insert("col", new BsonDocument {
                        { "text", "E" }
                    });
                    db.Insert("col", new BsonDocument {
                        { "text", "C" }
                    });
                    db.Insert("col", new BsonDocument {
                        { "text", "B" }
                    });

                    db.EnsureIndex("col", "text");

                    var asc = string.Join("",
                                          db.Find("col", Query.All("text"))
                                          .Select(x => x["text"].AsString)
                                          .ToArray());

                    var desc = string.Join("",
                                           db.Find("col", Query.All("text", Query.Descending))
                                           .Select(x => x["text"].AsString)
                                           .ToArray());

                    Assert.AreEqual("ABCDE", asc);
                    Assert.AreEqual("EDCBA", desc);

                    var indexes = db.GetIndexes("col");

                    Assert.AreEqual(1, indexes.Count(x => x.Field == "text"));
                }
        }
Exemplo n.º 15
0
        public void Engine_Using_MemoryStream()
        {
            var mem = new MemoryStream();

            using (var db = new UltraLiteEngine(mem))
            {
                db.Insert("col", new BsonDocument {
                    { "_id", 1 }, { "name", "John" }
                });
                db.Insert("col", new BsonDocument {
                    { "_id", 2 }, { "name", "Doe" }
                });
            }

            using (var db = new UltraLiteEngine(mem))
            {
                var john = db.Find("col", Query.EQ("_id", 1)).FirstOrDefault();
                var doe  = db.Find("col", Query.EQ("_id", 2)).FirstOrDefault();

                Assert.AreEqual("John", john["name"].AsString);
                Assert.AreEqual("Doe", doe["name"].AsString);
            }
        }
Exemplo n.º 16
0
        public void Engine_Insert_Documents()
        {
            using (var file = new TempFile())
            {
                using (var db = new UltraLiteEngine(file.Filename))
                {
                    db.Insert("col", new BsonDocument {
                        { "_id", 1 }, { "name", "John" }
                    });
                    db.Insert("col", new BsonDocument {
                        { "_id", 2 }, { "name", "Doe" }
                    });
                }

                using (var db = new UltraLiteEngine(file.Filename))
                {
                    var john = db.Find("col", Query.EQ("_id", 1)).FirstOrDefault();
                    var doe  = db.Find("col", Query.EQ("_id", 2)).FirstOrDefault();

                    Assert.AreEqual("John", john["name"].AsString);
                    Assert.AreEqual("Doe", doe["name"].AsString);
                }
            }
        }
Exemplo n.º 17
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);
            }
        }
Exemplo n.º 18
0
        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);
                }
            }
        }
Exemplo n.º 19
0
        public void ExecuteQuery(bool createIndex)
        {
            using (var db = new UltraLiteEngine(new MemoryStream()))
            {
                db.Insert("col", new BsonDocument[]
                {
                    new BsonDocument {
                        ["age"] = 1, ["name"] = "a"
                    },
                    new BsonDocument {
                        ["age"] = 2, ["name"] = "b"
                    },
                    new BsonDocument {
                        ["age"] = 3, ["name"] = "c"
                    },
                    new BsonDocument {
                        ["age"] = 4, ["name"] = "d"
                    },
                    new BsonDocument {
                        ["age"] = 5, ["name"] = "e"
                    },
                    new BsonDocument {
                        ["age"] = 6, ["name"] = "f"
                    },
                    new BsonDocument {
                        ["age"] = 7, ["name"] = "g"
                    },
                    new BsonDocument {
                        ["age"] = 8, ["name"] = "h"
                    },
                    new BsonDocument {
                        ["age"] = 9, ["name"] = "i"
                    },
                    new BsonDocument {
                        ["age"] = 9, ["name"] = "j"
                    }
                });

                if (createIndex)
                {
                    db.EnsureIndex("col", "age");
                    db.EnsureIndex("col", "name");
                }

                Func <Query, string> result = (q) => string.Join(",", db.Find("col", q).Select(x => x["name"].AsString));

                Assert.AreEqual("a,b,c,d,e,f,g,h,i,j", result(Query.All()));

                Assert.AreEqual("a", result(Query.EQ("age", 1)));
                Assert.AreEqual("g", result(Query.EQ("age", 7)));

                Assert.AreEqual("h,i,j", result(Query.GT("age", 7)));
                Assert.AreEqual("g,h,i,j", result(Query.GTE("age", 7)));

                Assert.AreEqual("", result(Query.LT("age", 1)));
                Assert.AreEqual("a", result(Query.LTE("age", 1)));

                Assert.AreEqual("g,h,i,j", result(Query.Between("age", 7, 9)));

                Assert.AreEqual("a,b,c,d,e,f,g,h", result(Query.Not("age", 9)));
                Assert.AreEqual("a", result(Query.Not(Query.GTE("age", 2))));
                Assert.AreEqual("a,g,i,j", result(Query.In("age", 1, 7, 9)));
                Assert.AreEqual("a", result(Query.StartsWith("name", "a")));

                Assert.AreEqual("j", result(Query.And(Query.EQ("age", 9), Query.EQ("name", "j"))));

                Assert.AreEqual("j", result(Query.And(Query.GTE("age", 1), Query.And(Query.LTE("age", 9), Query.EQ("name", "j")))));

                Assert.AreEqual("j", result(Query.And(Query.GTE("age", 1), Query.LTE("age", 9), Query.EQ("name", "j"))));

                Assert.AreEqual("a,i,j", result(Query.Or(Query.EQ("age", 1), Query.EQ("age", 9))));

                Assert.AreEqual("b,d,f,h", result(Query.Where("age", (v) => v.AsInt32 % 2 == 0)));
            }
        }