oop - "Journaling" or "transactions" design pattern? -


I am trying to apply the object that has been reported, or there is a continuous transaction. That is, the data in the object (probably a map) is as if the changes are made in the data, those changes are organized separately, if you have sandboxed so that any external object base state (before changes) Or access to the latest data. Then there is another operation which changes the base state.

This reminds me to some extent the Linux journaling file system is written to File System Change Magazine, and is committed to permanent storage later.

This is probably the concept of "transaction" in the world of relational databases; That is, you have some data, you start a transaction and manipulate data in some way. With no changes in concurrent processes, you will see old figures. Then you can either do a "rollback" transaction, or "commit" your changes.

I am trying to implement it especially in Java, but obviously this is a normal object-oriented pattern, if it exists. I hope this can be made at least, but I am not the best way to implement it.

Also, assume that the object holds full ton data, a complete hierarchy (sub objects and so on). Therefore, no complete data can keep two copies of the tree; It will be a lot of hard work and copy operation will take a lot of time. I am looking to implement it in the context of a game, with one per operation per frame, so it should really be optimal.

The best way to get what you want to achieve is to make the object and all its sub-objects irreversible . Then two threads can work on them without any conflicts, and you do not have to maintain two copies of everything. Only two things are needed which are things that really change, and they can be very small.

Suppose the object is made of objects A and B Object B, Object B is made of Object D and E. And Object C is made of Object F and G. A, A, B, and C, each are just two pointers, and whatever is D, E, F, and G, whatever they are.

First you give your initial example and give it to both threads.

  Thread on - & gt; A1 {B1 {D1, E1} C1 {F1, G1}} Thread-2 - & gt; A1 {B1 {D1, E1} C1 {F1, G1}}  

Then both the threads are pointing to the same example, no extra memory consumption , And there are no threading issues, because objects never change.

ThreadOn needs to modify the object F now. To do this, it only creates a new F, to maintain it a new C, and the new A has to be new. The original B, D, E and G are unchanged, and they do not need to be copied.

  Thread on - & gt; A2 {B1 {D1, E1} C2 {F2, G1}} Thread-2 - & gt; A1 {B1 {D1, E1} C1 {F1, G1}}  

Two threads are sharing examples of B, D, E and G.

Now ThreadOn needs to modify item E.

  Thread on - & gt; A3 {B2 {D1, E2} C2 {F2, G1}} Thread-2 - & gt; A1 {B1 {D1, E1} C1 {F1, G1}} Now ThreadTwo requires the latest version, so Threadon only gives an indicator for its copy.  
  Threadon - & gt; A3 {B2 {D1, E2} C2 {F2, G1}} Thread-2 - & gt; Because A3 {B2 {D1, E2} C2 {F2, G1}} Since the objects are irreversible, there is no risk of a threading issue, and ThreadOn Correct it may be right, every time it's just making a new example of those parts that have changed, and their containers  
  Thread on - & gt; A4 {B3 {D2, E2} C2 {F2, G1}} Thread-2 - & gt; A3 {B2 {D1, E2} C2 {F2, G1}} Threadon - & gt; A5 {B3 {D2, E2} C3 {F3, G1}} Thread-2 - & gt; A3 {B2 {D1, E2} C2 {F2, G1}}  

It is fast, memory efficient and thread safe.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -