I'm facing the following issue. One Thread is building and updating a Tree Object. To validate the Tree it is required to calculate the hash of that tree. So a second Thread calculates the hash of that tree continuously.
Now I got the following problem: That tree is about 300mb in size and I wanna ensure that the Tree doesn't change while the hash is calculated, like taking a snapshot and calculate the hash of it.
My guess is I have the following two options:
I think you should neither block while calculating a new hash nor copying or taking snapshots of the whole tree, especially if the tree takes about 300 mb of memory.
Instead, I'd take another approach. I'd use an incremental hash function. I'm not an expert in these matters, but the best one I know so far is Murmur3F from greenrobot common utility library. Please check their samples.
Murmur3F lets you call its
update() method many times. Then you call
getValue() to get the actual hash value. And you can do this many times.
So I wouldn't recalculate the hash for the whole tree in a separate thread each time it's modified. Using that Murmur3F hash implementation, I'd use the
update() method on every update to the tree and
getValue() on tree's
getHash(), for instance.