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); } } }
internal LiteFileStream(LiteEngine engine, LiteFileInfo file, FileAccess mode) { _engine = engine; _file = file; _mode = mode; if(mode == FileAccess.Read) { // initialize first data block _currentChunkData = this.GetChunkData(_currentChunkIndex); } else if(mode == FileAccess.Write) { _buffer = new MemoryStream(MAX_CHUNK_SIZE); // delete chunks content if needed if (file.Length > 0) { _engine.Delete(LiteStorage.CHUNKS, Query.StartsWith("_id", _file.Id + "\\")); } // clear size counters file.Length = 0; file.Chunks = 0; } }
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 Files_Store_My_Picture() { using (var db = new LiteEngine(dbpath)) { var files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Pictures", "*.jpg", SearchOption.AllDirectories); foreach (var f in files.Take(50)) { db.Storage.Upload(Path.GetFileName(f), f); } } using (var db = new LiteEngine(dbpath)) { Directory.CreateDirectory(@"C:\temp\pictures-50"); foreach (var f in db.Storage.All()) { f.SaveAs(db, @"C:\temp\pictures-50\" + f.Key, true); } var delete5 = db.Storage.All().Take(5); foreach(var f in delete5) db.Storage.Delete(f.Key); Directory.CreateDirectory(@"C:\temp\pictures-45"); foreach (var f in db.Storage.All()) { Debug.Print(f.Key); f.SaveAs(db, @"C:\temp\pictures-45\" + f.Key, true); } } }
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); } } }
/// <summary> /// Upgrade datafile from v6 to new v7 format used in LiteDB 3 /// </summary> public static bool Upgrade(string filename, string password = null, bool backup = true, int batchSize = 5000) { // if not exists, just exit if (!File.Exists(filename)) return false; // use a temp file to copy/convert data from var tempFile = FileHelper.GetTempFile(filename); // open fiel as stream and test if is V6 using(var stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { IDbReader reader = new LiteDB_V6.DbReader(); if (reader.Initialize(stream, password) == false) return false; // open new datafile to copy data from using (var engine = new LiteEngine(tempFile, false)) { foreach(var col in reader.GetCollections()) { // first, create all indexes var indexes = reader.GetIndexes(col); foreach(var index in indexes) { engine.EnsureIndex(col, index.Key, index.Value); } // now copy documents in 5000 groups var docs = reader.GetDocuments(col); foreach(var batch in docs.Batch(batchSize)) { engine.Insert(col, batch); // just clear pages engine.Rollback(); } } } } // if backup, move current file to new -bkp if (backup) { File.Move(filename, FileHelper.GetTempFile(filename, "-bkp")); } else { File.Delete(filename); } // move temp file to original filename File.Move(tempFile, filename); return true; }
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"); } }
internal LiteFileInfo(LiteEngine engine, BsonDocument doc) { _engine = engine; this.Id = doc["_id"].AsString; this.Filename = doc["filename"].AsString; this.MimeType = doc["mimeType"].AsString; this.Length = doc["length"].AsInt64; this.Chunks = doc["chunks"].AsInt32; this.UploadDate = doc["uploadDate"].AsDateTime; this.Metadata = doc["metadata"].AsDocument; }
/// <summary> /// Reduce disk size re-arranging unused spaces. Can change password. If temporary disk was not provided, use MemoryStream temp disk /// </summary> public long Shrink(string password = null, IDiskService temp = null) { var originalSize = _disk.FileLength; // if temp disk are not passed, use memory stream disk temp = temp ?? new StreamDiskService(new MemoryStream()); using(_locker.Write()) using (var engine = new LiteEngine(temp, password)) { // read all collection foreach (var collectionName in this.GetCollectionNames()) { // first create all user indexes (exclude _id index) foreach (var index in this.GetIndexes(collectionName).Where(x => x.Field != "_id")) { engine.EnsureIndex(collectionName, index.Field, index.Unique); } // copy all docs engine.Insert(collectionName, this.Find(collectionName, Query.All())); } // copy user version engine.UserVersion = this.UserVersion; // set current disk size to exact new disk usage _disk.SetLength(temp.FileLength); // read new header page to start copy var header = BasePage.ReadPage(temp.ReadPage(0)) as HeaderPage; // copy (as is) all pages from temp disk to original disk for (uint i = 0; i <= header.LastPageID; i++) { var page = temp.ReadPage(i); _disk.WritePage(i, page); } // create/destroy crypto class _crypto = password == null ? null : new AesEncryption(password, header.Salt); // initialize all services again (crypto can be changed) this.InitializeServices(); } // return how many bytes are reduced return originalSize - temp.FileLength; }
internal LiteFileInfo(LiteEngine engine, string id, string filename) { if (!IdPattern.IsMatch(id)) throw LiteException.InvalidFormat("FileId", id); _engine = engine; this.Id = id; this.Filename = Path.GetFileName(filename); this.MimeType = MimeTypeConverter.GetMimeType(this.Filename); this.Length = 0; this.Chunks = 0; this.UploadDate = DateTime.Now; this.Metadata = new BsonDocument(); }
public override void Init() { File.Delete(new LiteDB.ConnectionString(this.ConnectionString).Filename); _db = new LiteDB.LiteEngine(this.ConnectionString); _col = _db.GetCollection <Customer>("customer"); }
private void InitDb() { _db = new LiteEngine(this.ConnectionString); }