Java: versioned data structures?

前端 未结 6 2507
别那么骄傲
别那么骄傲 2021-02-20 13:24

I have a data structure that is pretty simple (basically a structure containing some arrays and single values), but I need to record the history of the data structure so that I

6条回答
  •  醉酒成梦
    2021-02-20 13:51

    If you are only storing a little bit of data and don't have a lot of changes then storing each version is fine.

    If you don't need to access the old version of the data too often, I wouldn't cache each one, I'd just make it so you could rebuild to it.

    You could do this by saving mutations as transactions and replaying the transactions (with the ability to stop at any point.

    So you start with an empty data structure and you might get an "Add" instruction followed by a "Change" and another "add" and then maybe a "Delete". Each of these objects would contain a COPY (not a pointer to the same object) of the thing being added or changed.

    You would concatenate each operation into a list while at the same time mutating your collection.

    If you find that you need a version at an older timestamp, start with a new empty collection, replay until you hit that timestamp then stop and you have the collection as it would be at that time.

    If this was a very long-running application and you often needed to access items near the end, you could write an "Undo" for each add/change/delete operation object and actually mutate the data back and forth.

    So imagine you have your data object and this array of mutations, you could easily run up and down the mutation list changing the data object back and forth to any version you want.

    You could even contain multiple data objects, just create a new empty one and run it up the mutation array (think of it as a timeline--where each stored mutation would contain a timestamp or some version number) until you get it to the timestamp you want--this way you could have "Milestones" that you could reach instantly--for instance, if you allocated one milestone for each thread you could make the addMutation method synchronized and this data collection would become 100% threadsafe.

    Note that if you actually return the data object you should only return a copy of the data--otherwise the next time you mutated that milestone it would mutate the data object you returned.

    Hmm, you could also include "Rollup" functionality--if you ever decide that you will not need access to the tail (the first few transactions) you could apply them to a "Start" structure and then delete them--from then on you copy the start structure to begin from the start rather than always starting with an empty data structure.

    Man, this is an awesome pattern--now I want to implement it.

提交回复
热议问题