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 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); } } }
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 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"); } }
private byte[] GetChunkData(int index) { // avoid too many extend pages on memory _engine.Cache.RemoveExtendPages(); // check if there is no more chunks in this file var chunks = _engine.GetCollection("_chunks"); var chunk = chunks.FindById(_entry.Id + "\\" + index); // if chunk is null there is no more chunks return(chunk == null ? null : chunk["data"].AsByteArray); }
/// <summary> /// Bulk documents to a collection - use data chunks for most efficient insert /// </summary> public static int Bulk <T>(string connectionString, string collectionName, IEnumerable <T> docs, int buffer = 2000) where T : new() { if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentNullException("connectionString"); } if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (docs == null) { throw new ArgumentNullException("collectionName"); } if (buffer < 100) { throw new ArgumentException("buffer must be bigger than 100"); } var enumerator = docs.GetEnumerator(); var count = 0; while (true) { var buff = buffer; using (var db = new LiteEngine(connectionString)) { var col = db.GetCollection <T>(collectionName); var more = true; db.BeginTrans(); while ((more = enumerator.MoveNext()) && buff > 0) { col.Insert(enumerator.Current); buff--; count++; } db.Commit(); if (more == false) { return(count); } } } }
/// <summary> /// Create a empty database ready to be used using connectionString as parameters /// </summary> private static void CreateNewDatabase(ConnectionString connectionString) { using (var stream = File.Create(connectionString.Filename)) { using (var writer = new BinaryWriter(stream)) { // creating header + master collection DiskService.WritePage(writer, new HeaderPage { PageID = 0, LastPageID = 1 }); DiskService.WritePage(writer, new CollectionPage { PageID = 1, CollectionName = "_master" }); } } // create _id index on _master collection using (var db = new LiteEngine("filename=" + connectionString.Filename + ";journal=false")) { db.GetCollection("_master").EnsureIndex("_id", true); } }
internal FileStorage(LiteEngine engine) { _engine = engine; _files = _engine.GetCollection("_files"); _chunks = _engine.GetCollection("_chunks"); }
public override void Init() { File.Delete(new LiteDB.ConnectionString(this.ConnectionString).Filename); _db = new LiteDB.LiteEngine(this.ConnectionString); _col = _db.GetCollection <Customer>("customer"); }
public static string Index(LiteEngine db, string collection, string field, int size = 5) { var sbs = new StringBuilder[IndexNode.MAX_LEVEL_LENGTH + 1]; var first = true; var col = db.GetCollection(collection).GetCollectionPage(false); if (col == null) { throw new ArgumentException("Invalid collection name"); } var index = col.Indexes.FirstOrDefault(x => x.Field == field); if (index == null) { throw new ArgumentException("Invalid index field name"); } for (var i = 0; i < sbs.Length; i++) { sbs[i] = new StringBuilder(); } var cur = index.HeadNode; while (!cur.IsEmpty) { var page = db.Pager.GetPage <IndexPage>(cur.PageID); var node = page.Nodes[cur.Index]; sbs[0].Append((first ? "HEAD" : node.Key.Value == null ? "null" : Limit(node.Key.Value.ToString(), size)).PadBoth(1 + (2 * size))); first = false; for (var i = 0; i < IndexNode.MAX_LEVEL_LENGTH; i++) { var sb = sbs[i + 1]; var p = "-"; var n = "-"; if (i < node.Prev.Length) { if (!node.Prev[i].IsEmpty) { if (node.Prev[i].Equals(index.HeadNode)) { p = "<-H"; } else { var pprev = db.Pager.GetPage <IndexPage>(node.Prev[i].PageID); var pnode = pprev.Nodes[node.Prev[i].Index]; p = pnode.Key.Value == null ? "null" : pnode.Key.Value.ToString(); } } if (!node.Next[i].IsEmpty) { var pnext = db.Pager.GetPage <IndexPage>(node.Next[i].PageID); var pnode = pnext.Nodes[node.Next[i].Index]; n = pnode.Key.Value == null ? "null" : pnode.Key.Value.ToString(); } } sb.Append(Limit(p, size).PadLeft(size) + "|" + Limit(n, size).PadRight(size)); } cur = node.Next[0]; } var s = new StringBuilder(); for (var i = sbs.Length - 1; i >= 0; i--) { s.AppendLine(sbs[i].ToString()); } return(s.ToString()); }