Exemplo n.º 1
0
        public void Execute(LiteEngine db, StringScanner s, Display display)
        {
            var col   = this.ReadCollection(db, s);
            var query = s.Match("{") ? Query.All() : this.ReadQuery(s);
            var code  = DynamicCode.GetCode(s);

            var docs = col.Find(query).ToArray();

            try
            {
                db.BeginTrans();

                foreach (var doc in docs)
                {
                    code(doc["_id"].RawValue, doc, col, db);
                }

                db.Commit();

                display.WriteBson(docs.Length);
            }
            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }
Exemplo n.º 2
0
        public void Execute(ref LiteEngine db, StringScanner s, Display display)
        {
            var col      = this.ReadCollection(db, s);
            var filename = s.Scan(@".*");
            var json     = File.ReadAllText(filename, Encoding.UTF8);
            var docs     = JsonEx.Deserialize(json);
            var count    = 0;

            db.BeginTrans();

            if (docs.IsArray)
            {
                foreach (var doc in docs.AsArray)
                {
                    count++;
                    col.Insert(new BsonDocument(doc));
                }
            }
            else
            {
                count = 1;
                col.Insert(new BsonDocument(docs));
            }

            db.Commit();

            display.WriteBson(count);
        }
Exemplo n.º 3
0
        public void Execute(LiteEngine db, StringScanner s, Display display)
        {
            if (db == null)
            {
                throw new LiteException("No database");
            }

            var col      = this.ReadCollection(db, s);
            var filename = s.Scan(@".*");
            var json     = File.ReadAllText(filename, Encoding.UTF8);
            var docs     = JsonEx.DeserializeArray <BsonDocument>(json);
            var count    = 0;

            db.BeginTrans();

            foreach (var doc in docs)
            {
                count++;
                col.Insert(doc);
            }

            db.Commit();

            display.WriteBson(count);
        }
Exemplo n.º 4
0
        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);
                }
            }
        }
Exemplo n.º 5
0
        public void Execute(LiteEngine db, StringScanner s, Display display)
        {
            if (db == null)
            {
                throw new LiteException("No database");
            }

            db.BeginTrans();
        }
Exemplo n.º 6
0
        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();
            }
        }
Exemplo n.º 7
0
        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");
            }
        }
Exemplo n.º 8
0
        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");
            }
        }
Exemplo n.º 9
0
        public void FreeSlot_Insert()
        {
            using (var e = new LiteEngine())
            {
                e.BeginTrans();

                // get transaction/snapshot "col1"
                var t = e.GetMonitor().GetTransaction(false, out var isNew);
                var s = t.CreateSnapshot(LockMode.Write, "col1", true);

                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["n"] = new byte[200]
                                                      } }, BsonAutoId.Int32);

                // get pages
                var colPage   = s.CollectionPage;
                var dataPage  = s.LocalPages.FirstOrDefault(x => x.PageType == PageType.Data);
                var indexPage = s.LocalPages.FirstOrDefault(x => x.PageType == PageType.Index);

                // test dataPage free space
                dataPage.FreeBytes.Should().Be(7928);

                // page should be in Slot #0 (7344 - 8160 free bytes)
                colPage.FreeDataPageList.Should().Equal(dataPage.PageID, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue);

                // adding 1 more document into same page
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["n"] = new byte[600]
                                                      } }, BsonAutoId.Int32);

                dataPage.FreeBytes.Should().Be(7296);

                // page should me moved into Slot #1 (6120 - 7343 free bytes)
                colPage.FreeDataPageList.Should().Equal(uint.MaxValue, dataPage.PageID, uint.MaxValue, uint.MaxValue, uint.MaxValue);

                // adding 1 big document to move this page into last page
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["n"] = new byte[6000]
                                                      } }, BsonAutoId.Int32);

                dataPage.FreeBytes.Should().Be(1264);

                // now this page should me moved into last Slot (#4) - next document will use another data page (even a very small document)
                colPage.FreeDataPageList.Should().Equal(uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, dataPage.PageID);

                // adding a very small document to test adding new page
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["n"] = new byte[10]
                                                      } }, BsonAutoId.Int32);

                // no changes in dataPage... but new page as created
                dataPage.FreeBytes.Should().Be(1264);

                var dataPage2 = s.LocalPages.FirstOrDefault(x => x.PageType == PageType.Data && x.PageID != dataPage.PageID);

                dataPage2.FreeBytes.Should().Be(8118);

                // test slots (#0 for dataPage2 and #4 for dataPage1)
                colPage.FreeDataPageList.Should().Equal(dataPage2.PageID, uint.MaxValue, uint.MaxValue, uint.MaxValue, dataPage.PageID);

                // add another big document into dataPage2 do put both pages in same free Slot (#4)
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["n"] = new byte[7000]
                                                      } }, BsonAutoId.Int32);

                // now, both pages are linked in same slot #4 (starts with new dataPage2)
                colPage.FreeDataPageList.Should().Equal(uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, dataPage2.PageID);

                // dataPage2 link into dataPage1
                dataPage2.NextPageID.Should().Be(dataPage.PageID);
                dataPage.PrevPageID.Should().Be(dataPage2.PageID);

                // and both start/end points to null
                dataPage2.PrevPageID.Should().Be(uint.MaxValue);
                dataPage.NextPageID.Should().Be(uint.MaxValue);

                // do ColID tests
                dataPage.ColID.Should().Be(colPage.PageID);
                dataPage2.ColID.Should().Be(colPage.PageID);
                indexPage.ColID.Should().Be(colPage.PageID);
            }
        }
Exemplo n.º 10
0
        public void FreeSlot_Delete()
        {
            using (var e = new LiteEngine())
            {
                e.BeginTrans();

                // get transaction/snapshot "col1"
                var t = e.GetMonitor().GetTransaction(false, out var isNew);
                var s = t.CreateSnapshot(LockMode.Write, "col1", true);

                // first page
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["_id"] = 1, ["n"] = new byte[2000]
                                                      } }, BsonAutoId.Int32);
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["_id"] = 2, ["n"] = new byte[2000]
                                                      } }, BsonAutoId.Int32);
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["_id"] = 3, ["n"] = new byte[2000]
                                                      } }, BsonAutoId.Int32);

                // second page
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["_id"] = 4, ["n"] = new byte[2000]
                                                      } }, BsonAutoId.Int32);
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["_id"] = 5, ["n"] = new byte[2000]
                                                      } }, BsonAutoId.Int32);
                e.Insert("col1", new BsonDocument[] { new BsonDocument {
                                                          ["_id"] = 6, ["n"] = new byte[2000]
                                                      } }, BsonAutoId.Int32);

                // get pages
                var colPage   = s.CollectionPage;
                var indexPage = s.LocalPages.FirstOrDefault(x => x.PageType == PageType.Index);
                var dataPage1 = s.LocalPages.FirstOrDefault(x => x.PageType == PageType.Data);
                var dataPage2 = s.LocalPages.FirstOrDefault(x => x.PageType == PageType.Data && x.PageID != dataPage1.PageID);

                // test dataPage free space
                dataPage1.FreeBytes.Should().Be(2064);
                dataPage2.FreeBytes.Should().Be(2064);

                colPage.FreeDataPageList.Should().Equal(uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, dataPage2.PageID);

                // delete some data
                e.Delete("col1", new BsonValue[] { 2 });

                // test again dataPage
                dataPage1.FreeBytes.Should().Be(4092);

                colPage.FreeDataPageList.Should().Equal(uint.MaxValue, uint.MaxValue, uint.MaxValue, dataPage1.PageID, dataPage2.PageID);

                // clear first page
                e.Delete("col1", new BsonValue[] { 1, 3 });

                // page1 must be now a clean page
                var emptyPage = s.LocalPages.FirstOrDefault(x => x.PageID == dataPage1.PageID);

                emptyPage.PageType.Should().Be(PageType.Empty);
                emptyPage.ItemsCount.Should().Be(0);
                emptyPage.FreeBytes.Should().Be(8160);

                t.Pages.DeletedPages.Should().Be(1);
                t.Pages.FirstDeletedPageID.Should().Be(emptyPage.PageID);
                t.Pages.LastDeletedPageID.Should().Be(emptyPage.PageID);
            }
        }
Exemplo n.º 11
0
 public void Execute(LiteEngine db, StringScanner s, Display display)
 {
     db.BeginTrans();
 }
Exemplo n.º 12
0
 public void Execute(LiteEngine engine, StringScanner s, Display display, InputCommand input, Env env)
 {
     engine.BeginTrans();
 }