mbdavid / LiteDB

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

IsDirty flag ... did I miss something or does it not exist ? #2147

Closed stephenhauck closed 2 years ago

stephenhauck commented 2 years ago

In the past I have used Fody with SQLite to implement INPC and it adds an IsDirty functionality as well. Does LiteDB have an IsDirty indicator of some kind that I can check to cut down on DB IO. ?

Thanks !

mbdavid commented 2 years ago

Hi @stephenhauck,

Every write command you execute in LiteDB change some (or all?) data pages from your datafile. All this changes go to log file (can be a physical or in-memory file). When CHECKPOINT commands run, all this dirty pages in log file override original datafile. CHECKPOINT occurs when exceed 1000 pages on log file or when dispose database.

There is no "IsDirty" per database, only a internal property in BasePage that track this changes.

https://github.com/mbdavid/LiteDB/blob/master/LiteDB/Engine/Pages/BasePage.cs#L128

Does this answer answer your question?

stephenhauck commented 2 years ago

That information is VERY helpful for my future question that I had related to this. What I was looking for was something like this ...

In my model classes I can implement INPC and add the event and an "IsChanged" flag like so..

image

Then I can tell PropertyChanged.Fody to implement the INPC code for me and it sets the "IsChanged" flag so in my code I know if an instance of an object has changed and needs to be updated back to the database but Fody weaves it in so I don't have to "manage" the state of the flag unless I want to.

I was wondering if there is a class I can inherit from that has that kind of functionality for models that I will use in LiteDB ?

I mean I have the ObjectId property for a class that will be stored in LiteDB but was wondering if there was a mechanism similar to the PropertyChanged.Fody .

kcsombrio commented 2 years ago

Hi @stephenhauck, so LiteDB doesn't implement this kind of control mechanism. I'm talking to Mauricio right now he's saying that this kind of control has to be implemented in a layer above LiteDB, like Entity Framework does over databases. Right now, there is no similar control to do this.

stephenhauck commented 2 years ago

OK, I just wanted to make sure there wasn't an object model in LiteDB that I could inherit from that offered this kind of functionality. It is definitely outside the bounds of what a database engine does or should do but having this when writing code is helpful.