public void Include_Test() { using (var db = new LiteEngine(DB.Path())) { var customer1 = new Customer { CustomerId = Guid.NewGuid(), Name = "Mauricio" }; var order1 = new Order { OrderKey = 1, Date = DateTime.Now, CustomerId = customer1.CustomerId }; var order2 = new Order { OrderKey = 2, Date = new DateTime(2000, 1, 1), CustomerId = customer1.CustomerId }; var customers = db.GetCollection <Customer>("Customer"); var orders = db.GetCollection <Order>("Order"); customers.EnsureIndex(x => x.Name, true); customers.Insert(customer1); orders.Insert(order1); orders.Insert(order2); var query = orders .Include((x) => x.Customer = customers.FindById(x.CustomerId)) .All() .Select(x => new { x.OrderKey, Cust = x.Customer.Name, CustomerInstance = x.Customer }) .FirstOrDefault(); Assert.AreEqual(customer1.Name, query.Cust); } }
/// <summary> /// Dump database converting to most recent version syntax /// </summary> public void Dump(TextWriter writer) { // do not include this collections now var specials = new string[] { "_master", "_files", "_chunks" }; foreach (var name in _db.GetCollections().Where(x => !specials.Contains(x))) { var col = _db.GetCollection(name); var indexes = col.GetIndexes().Where(x => x["field"] != "_id"); writer.WriteLine("-- Collection '{0}'", name); foreach (var index in indexes) { writer.WriteLine("db.{0}.ensureIndex {1} {2}", name, index["field"].AsString, JsonEx.Serialize(index)); } foreach (var doc in col.Find(Query.All())) { writer.WriteLine("db.{0}.insert {1}", name, JsonEx.Serialize(doc)); } } // convert FileStorage to new format var files = _db.GetCollection("_files"); var chunks = _db.GetCollection("_chunks"); if (files.Count() == 0) { return; } writer.WriteLine("-- FileStorage: _files"); foreach (var file in files.Find(Query.All())) { // adding missing values file["chunks"] = chunks.Count(Query.StartsWith("_id", file.Id + @"\")); writer.WriteLine("db._files.insert {0}", JsonEx.Serialize(file)); } writer.WriteLine("-- FileStorage: _chunks"); foreach (var chunk in chunks.Find(Query.All())) { // adding _id format 00000 chunk.Id = string.Format("{0}\\{1:00000}", chunk.Id.ToString().Substring(0, chunk.Id.ToString().IndexOf('\\')), Convert.ToInt32(chunk.Id.ToString().Substring(chunk.Id.ToString().IndexOf('\\') + 1))); writer.WriteLine("db._chunks.insert {0}", JsonEx.Serialize(chunk)); } }
public void Search_Perf() { Guid g; using (var db = new LiteEngine(dbpath)) { var c = db.GetCollection <PerfItem>("perf"); Debug.Print("Total rows in collection " + c.Count()); var i = c.FindById(7737); g = i.MyGuid; Debug.Print(i.MyGuid + " - " + i.Nome); } using (var db = new LiteEngine(dbpath)) { var c = db.GetCollection <PerfItem>("perf"); var i = c.FindOne(Query.EQ("MyGuid", g)); Debug.Print(i.MyGuid + " - " + i.Nome); } }
public void Find_Order() { using (var db = new LiteEngine(DB.Path())) { var col = db.GetCollection <OrderObj>("order"); col.EnsureIndex("Text"); col.Insert(new OrderObj { Id = 1, Text = "D" }); col.Insert(new OrderObj { Id = 2, Text = "A" }); col.Insert(new OrderObj { Id = 3, Text = "E" }); col.Insert(new OrderObj { Id = 4, Text = "C" }); col.Insert(new OrderObj { Id = 5, Text = "B" }); var asc = col.Find(Query.All("Text")); var result = ""; foreach (var i in asc) { result += i.Text.ToString(); } Assert.AreEqual("ABCDE", result); } }
public void AddUser(User Person) { using (var db = new LiteEngine(_location)) { var col = db.GetCollection <User>(_uniqueGuid); var allUsers = col.All(); foreach (User u in allUsers) { if (u.Username == Person.Username && u.Region == Person.Region) { throw new Exception("User already exists!"); } } Person.Id = GetIndex(col.All()); Person.Password = DPAPI.Encrypt(DPAPI.KeyType.UserKey, Person.Password, null, //Could possibly use as a password required to login "evidence.zip"); //just a random note if base64 is decoded col.Insert(Person); } }
public void Index_Insert() { using (var db = new LiteEngine(dbpath)) { var c = db.GetCollection("col1"); var d = new BsonDocument(); var id1 = c.NextVal(); var id2 = c.NextVal(); var id3 = c.NextVal(); d["Name"] = "John 1"; c.Insert(id1, d); d["Name"] = "John 2"; c.Insert(id2, d); d["Name"] = "John 3"; c.Insert(id3, d); d["Name"] = "John A"; c.Insert("A", d); var r = c.Find(Query.GTE("_id", 1)); foreach (var nd in r) { Debug.Print(nd["Name"].AsString); } } }
public void Create_100k_Rows_DB() { using (var db = new LiteEngine(dbpath)) { var c = db.GetCollection <PerfItem>("perf"); //c.EnsureIndex("MyGuid", true); var id = 0; for (var j = 0; j < 3; j++) { var d = DateTime.Now; db.BeginTrans(); for (var i = 0; i < 10000; i++) { id++; c.Insert(id, new PerfItem { Id = id, MyGuid = Guid.NewGuid(), Nome = "Jose Silva " + id }); } db.Commit(); Debug.Print("Commits " + j + " in " + DateTime.Now.Subtract(d).TotalMilliseconds); } } }
/// <summary> /// /// </summary> /// <param name="entName"></param> /// <returns></returns> public IList <LicenseEntry> Get(string entName) { var collection = _db.GetCollection <LicenseEntry>(_tableName); var filtered = collection.Find(i => i.EnterpriseName.Equals(entName)); return(filtered.ToList()); }
private void Verify(string name, LiteEngine db, int k) { var col = db.GetCollection(name); Assert.AreEqual(50 * k, col.Count()); Assert.AreEqual(0 * k, col.Count(Query.EQ("Updates", 0))); Assert.AreEqual(20 * k, col.Count(Query.EQ("Updates", 1))); Assert.AreEqual(30 * k, col.Count(Query.EQ("Updates", 2))); }
public void Perf_Test() { var path = DB.Path(true, "test.db"); using (var db = new LiteEngine("journal=true;filename=" + path)) { db.BeginTrans(); var col = db.GetCollection <Post>("posts"); col.Insert(Post.GetData(20000)); db.Commit(); } }
public void Files_Store() { using (var db = new LiteEngine(dbpath)) { var c = db.GetCollection("customer"); db.BeginTrans(); for (var i = 1; i <= 500; i++) { var d = new BsonDocument(); d["Name"] = "San Jose"; c.Insert(i, d); } for (var i = 1; i <= 500; i++) { c.Delete(i); } db.Commit(); Dump.Pages(db, "before"); var meta = new Dictionary <string, string>(); meta["my-data"] = "Google LiteDB"; db.Storage.Upload("my/foto1.jpg", new MemoryStream(new byte[5000]), meta); Dump.Pages(db, "after file"); var f = db.Storage.FindByKey("my/foto1.jpg"); Assert.AreEqual(5000, f.Length); Assert.AreEqual("Google LiteDB", f.Metadata["my-data"]); var mem = new MemoryStream(); f.OpenRead(db).CopyTo(mem); // file real size after read all bytes Assert.AreEqual(5000, mem.Length); // all bytes are 0 Assert.AreEqual(5000, mem.ToArray().Count(x => x == 0)); db.Storage.Delete("my/foto1.jpg"); Dump.Pages(db, "deleted file"); } }
/// <summary> /// Gets a list of all the users without passwords /// </summary> /// <returns>A list of users without passwords</returns> public List <User> GetUserList() { using (var db = new LiteEngine(_location)) { var col = db.GetCollection <User>(_uniqueGuid); var allUsers = col.All(); foreach (User u in allUsers) { u.Password = ""; } return(allUsers.ToList()); } }
/// <summary> /// Remove a user from the user file. /// </summary> /// <param name="Person">The user to remove.</param> public void RemoveUser(User Person) { using (var db = new LiteEngine(_location)) { var col = db.GetCollection <User>(_uniqueGuid); var allUsers = col.All(); foreach (User u in allUsers) { if (u.Username == Person.Username && u.Region == Person.Region) { col.Delete(u.Id); } } } }
private void PopulateCollection(string name, LiteEngine db, int k) { var col = db.GetCollection(name); col.EnsureIndex("Updates"); for (var j = 0; j < k; j++) { // create 100 documents with Update = 0 for (var i = 1; i <= 100; i++) { var doc = new BsonDocument(); doc.Id = Guid.NewGuid(); doc["Today"] = DateTime.Today; doc["Name"] = "John Doe"; doc["Updates"] = 0; col.Insert(doc); } // change 20 documents do Update = 1 for (var i = 1; i <= 20; i++) { var doc = col.FindOne(Query.EQ("Updates", 0)); doc["Name"] = doc["Name"].AsString.PadRight(rnd.Next(10, 500), '-'); doc["Updates"] = 1; col.Update(doc); } // change 30 documents (with Update = 0) to Update = 2 for (var i = 1; i <= 30; i++) { var doc = col.FindOne(Query.EQ("Updates", 0)); doc["Updates"] = 2; col.Update(doc); } // delete all documents with Update = 0 (50 documents) var deleted = col.Delete(Query.EQ("Updates", 0)); Assert.AreEqual(50, deleted); // balance: 50 (20 with Update = 1, 30 with Update = 2) } }
/// <summary> /// Gets a user based on their username & region /// </summary> /// <param name="Name">The username of the user</param> /// <param name="Region">The shard region of the user.</param> /// <returns>The requested user.</returns> public User GetUser(string Name, string Region) { using (var db = new LiteEngine(_location)) { var col = db.GetCollection <User>(_uniqueGuid); var allUsers = col.All(); foreach (User u in allUsers) { if (u.Username == Name && u.Region == Region) { return(u); } } } return(null); }
public void Linq_Test() { using (var db = new LiteEngine(DB.Path())) { var c1 = new Customer { CustomerId = Guid.NewGuid(), Name = "Mauricio", CreationDate = new DateTime(2015, 1, 1) }; var c2 = new Customer { CustomerId = Guid.NewGuid(), Name = "Malafaia", CreationDate = new DateTime(2015, 1, 1) }; var c3 = new Customer { CustomerId = Guid.NewGuid(), Name = "Chris", CreationDate = new DateTime(2000, 1, 1) }; var c4 = new Customer { CustomerId = Guid.NewGuid(), Name = "Juliane", CreationDate = new DateTime(2011, 8, 11) }; var col = db.GetCollection <Customer>("Customer"); col.EnsureIndex(x => x.Name, true); col.EnsureIndex(x => x.CreationDate); col.Insert(new Customer[] { c1, c2, c3, c4 }); var past = -30; // simple Assert.AreEqual(1, col.Count(x => x.Name == "Chris")); Assert.AreEqual(2, col.Count(x => x.CreationDate == new DateTime(2015, 1, 1))); Assert.AreEqual(1, col.Count(x => x.Name.StartsWith("mal"))); Assert.AreEqual(4, col.Count(x => x.CreationDate >= DateTime.Now.AddYears(past))); Assert.AreEqual(c3.CustomerId, col.FindOne(x => x.CreationDate <= new DateTime(2000, 1, 1)).CustomerId); Assert.AreEqual("Chris", col.FindOne(x => x.Name != "Mauricio").Name); Assert.AreEqual(1, col.Count(x => x.Name.Equals("Mauricio"))); // and/or Assert.AreEqual(1, col.Count(x => x.CreationDate == new DateTime(2015, 1, 1) && x.Name.StartsWith("Mal"))); Assert.AreEqual(2, col.Count(x => x.CreationDate == new DateTime(2015, 1, 1) && (x.Name.StartsWith("Mal") || x.Name == "Mauricio"))); } }
public void Page_PrevNext_Test() { using (var db = new LiteEngine(DB.Path())) { var k = 1; db.BeginTrans(); this.PopulateCollection("my_collection_1", db, k); //this.PopulateCollection("my_collection_2", db, k); //this.PopulateCollection("my_collection_3", db, k); //this.PopulateCollection("my_collection_4", db, k); //this.PopulateCollection("my_collection_3", db, 5); db.Commit(); this.Verify("my_collection_1", db, k); //this.Verify("my_collection_2", db, k); //this.Verify("my_collection_3", db, k); //this.Verify("my_collection_4", db, k); Dump.Pages(db); db.GetCollection("my_collection_1").Delete(Query.All()); //db.GetCollection("my_collection_2").Delete(Query.All()); //db.GetCollection("my_collection_3").Delete(Query.All()); //db.GetCollection("my_collection_4").Delete(Query.All()); Dump.Pages(db, "After clear"); db.FileStorage.Upload("my/foto1.jpg", new MemoryStream(new byte[1024 * 50])); } using (var db = new LiteEngine(DB.Path(false))) { Dump.Pages(db, "After File"); } }
public static Collection <T> GetCollection <T>(this LiteEngine engine) where T : new() { return(engine.GetCollection <T>(typeof(T).Name)); }
/// <summary> /// Read collection name from db.(colname).(command) /// </summary> public Collection <BsonDocument> ReadCollection(LiteEngine db, StringScanner s) { return(db.GetCollection(s.Scan(@"db\.(\w+)\.\w+\s*", 1))); }
//[TestMethod] public void Stress_Test() { // The most important test: // - Create 3 task read/write operations in same file. // - Insert, update, delete same documents // - Insert big files // - Delete all documents // - Insert a big file (use all pages) // - Read this big file and test md5 var file = DB.Path(true, "stress.db"); var rnd = new Random(DateTime.Now.Second); var N = 100; var a = new LiteEngine(file); var b = new LiteEngine(file); var c = new LiteEngine(file); var d = new LiteEngine(file); // Task A -> Insert 100 documents var ta = Task.Factory.StartNew(() => { var col = a.GetCollection("col1"); for (var i = 1; i <= N; i++) { col.Insert(CreateDoc(i, "My String Guided " + Guid.NewGuid().ToString("n"))); } }); // Task B -> Update 100 documents var tb = Task.Factory.StartNew(() => { var col = b.GetCollection("col1"); var i = 1; while (i < N) { //Task.Delay(rnd.Next(100, 500)); var success = col.Update(CreateDoc(i, "update value")); if (success) { i++; } } }); //// TasK C -> Delete 99 documents (keep only _id = 1) //var tc = Task.Factory.StartNew(() => //{ // var col = c.GetCollection("col1"); // var i = 2; // while (i < N) // { // Task.Delay(rnd.Next(100, 500)); // var success = col.Delete(i); // if (success) i++; // } //}); // Task D -> Upload 20 files // Now, test data Task.WaitAll(ta, tb); //, tb, tc); using (var db = new LiteEngine(file)) { var col = db.GetCollection("col1"); Assert.AreEqual(1, col.Count()); var doc = col.FindById(1); Assert.AreEqual("update value", doc["Name"].AsString); } a.Dispose(); b.Dispose(); c.Dispose(); }