BHoM / BHoM_Engine

Internal manipulation of the BHoM
GNU Lesser General Public License v3.0
26 stars 15 forks source link

Diffing_Engine: refactor method for hash generation - exceptions when hashing #1927

Closed alelom closed 2 years ago

alelom commented 4 years ago

Description:

The biggest problem we currently have when generating the hash is allowing for exclusions: fields that should not be accounted for when a unique hash has to be generated.

This is complicated for two reasons:

  1. we have a property that HAS to be excluded every time when computing the hash: the BHoM_Guid.
    (The reason is obvious: we want the hash to be representative of the instance of the object, a combination of all its properties; the BHoM_Guid is randomly re-established every time, say, the Grasshopper definition is reopened, upon object instantiation).

  2. The BHoM_Guid is ubiquitous, at all levels: every object has it, and a single BHoM Object may contain other sub-objects that have other BHoM_Guids.

In general, the BHoM_Guid simply constitutes the worst-case for the hash generation mechanism.

Anyway, the approach is:

For the byte serialisation originally was to use Mongo's byte serialisation that allowed to exclude certain properties. This was actually suggested by @adecler a while ago. However, that did not work, as Mongo's exception where limited to the first level properties only. This means that BHoM_Guid of sub-objects (e.g. Nodes of a Bar) were not excluded, and the hash was incorrectly computed.

For this reason, given the impeding need of the hash in the Diffing Engine, I had to revert to another working solution. I am currently serialising the object into a string, then I discover and remove the unwanted properties. Obviously, this is slow, but it works reliably.

Another reason to tackle this: in order to solve the property search a bit more efficiently I used some methods fromNewtonsoft.Json, which is a reference we want to remove from the BHoM_Engine. Therefore, solving this issue would solve also https://github.com/BHoM/BHoM_Engine/issues/1885.

I spent quite some time researching for better alternatives, but I feel like I hit a wall, and I really would appreciate the help of @adecler to see what we can do with Mongo or other solutions.

alelom commented 2 years ago

This has been closed by the various refactorings to the Hash method.