mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.52k stars 1.24k forks source link

[BUG] Exception thrown dropping collection when database loaded from memory stream #2247

Closed TrevorDArcyEvans closed 1 year ago

TrevorDArcyEvans commented 1 year ago

Version LiteDB commit 6d9ac6237ff8cae104a3c57a8de4ec55b4506e87 (June 15, 2022)

Describe the bug Exception thrown dropping collection when database loaded from memory stream

Code to Reproduce

    const string DbName = "v5-test.db";

    var dbStr = File.ReadAllText(DbName);
    var strm = new MemoryStream(Encoding.ASCII.GetBytes(dbStr));
    var db = new LiteDatabase(strm);

    // This works as expected
    //var db = new LiteDatabase(DbName);

    db.DropCollection("col1");

v5-test.zip

Expected behavior

Screenshots/Stacktrace

Unhandled exception. System.Exception: LiteDB ENSURE: request page must be less or equals lastest page in data file
   at LiteDB.Constants.ENSURE(Boolean conditional, String message) in C:\dev\trevorde\LiteDB\LiteDB\Utils\Constants.cs:line 133
   at LiteDB.Engine.Snapshot.GetPage[T](UInt32 pageID, FileOrigin& origin, Int64& position, Int32& walVersion) in C:\dev\trevorde\LiteDB\LiteDB\Engine\Services\SnapShot.cs:line 151
   at LiteDB.Engine.Snapshot.GetPage[T](UInt32 pageID) in C:\dev\trevorde\LiteDB\LiteDB\Engine\Services\SnapShot.cs:line 142
   at LiteDB.Engine.Snapshot.DropCollection(Action safePoint) in C:\dev\trevorde\LiteDB\LiteDB\Engine\Services\SnapShot.cs:line 608
   at LiteDB.Engine.LiteEngine.<>c__DisplayClass1_0.<DropCollection>b__0(TransactionService transaction) in C:\dev\trevorde\LiteDB\LiteDB\Engine\Engine\Collection.cs:line 39
   at LiteDB.Engine.LiteEngine.AutoTransaction[T](Func`2 fn) in C:\dev\trevorde\LiteDB\LiteDB\Engine\Engine\Transaction.cs:line 79
   at LiteDB.Engine.LiteEngine.DropCollection(String name) in C:\dev\trevorde\LiteDB\LiteDB\Engine\Engine\Collection.cs:line 29
   at LiteDB.LiteDatabase.DropCollection(String name) in C:\dev\trevorde\LiteDB\LiteDB\Client\Database\LiteDatabase.cs:line 208
   at Program.Main(String[] args) in C:\dev\trevorde\LiteDB.Test\Program.cs:line 12

Additional context Are all db operations supported when db is loaded from a memory stream?

pjy612 commented 1 year ago

why not use File.ReadAllBytes ?

TrevorDArcyEvans commented 1 year ago

I'm trying to get LiteDB working in WebAssembly: https://github.com/TrevorDArcyEvans/LiteDB.Studio.WebAssembly https://github.com/TrevorDArcyEvans/LiteDB.Studio.WebAssembly/blob/855818616ae29bfb3510dfc5f23ee15cc90b0784/Pages/Index.razor.cs#L100

TrevorDArcyEvans commented 1 year ago

All streams are not the same. See #2248