Persistent object discussion

Sergei Degtiarev via Digitalmars-d digitalmars-d at puremagic.com
Tue May 10 18:25:39 PDT 2016


I want to suggest an addition to standard library - persistent 
object module. The idea is straightforward, if we use file mapped 
memory to store some data, the data may outlive the application 
and be kept until next application start, thus providing 
persistent and consistent storage between application runs. Two 
closest conceptions are obviously serialization and general 
shared memory, but there are several important moments which make 
a difference. It differs from serialization mainly by its 
efficiency and reliability, once the object in shared memory is 
created it might be accessed with almost same efficiency as any 
other in-memory value, moreover, it has all the chances to 
survive process crash or spontaneous stop as modification of any 
variable takes no time, unlike long serialization process. It 
also differs from general shared memory in its object-oriented 
approach, instead of separate allocation and initialization of 
the shared memory piece, we call ctor immediately returning 
initialized object with the difference, the object is placed into 
shared memory. The RAII concept is fully supported, the object is 
either created and initialized or created preserving its value if 
it did exist, (btw, this is why I had to create own shared memory 
functions instead of using existing std module). The module is 
also natural candidate for interprocess communications, but 
concurrency primitives are intentionally not included (except 
atomic creation), instead, various synchronizations may be built 
on top of it.
   Obviously, it might be used with value types only. The 
persistent object may contain either simple value or struct or 
static array, the file is either created or opened and extended 
if needed. In the last case, the object is initialized, or, in 
the case of array, only extended part is initialized. The 
existing part of the memory is kept as is, assuming it already 
contains initialized values. It might be also created as dynamic 
array, in this case the length is derived from the file size, and 
the file must exist.
The code is here: 
https://github.com/D-Programming-Language/phobos/pull/3625 , 
please see unittest section for numerous usage examples.
   I used the conception for several years (C++, fast and reliable 
transaction servers), and it proved to be very useful for such a 
kind of applications. The module is placed to review queue for 
voting and discussion. I will be happy to hear your opinion and 
suggestions.




More information about the Digitalmars-d mailing list