fluree / db

Fluree database library
https://fluree.github.io/db/
Other
333 stars 21 forks source link

Add `with-meta` support to #Flake #753

Closed bplatz closed 3 months ago

bplatz commented 3 months ago

This adds support for Clojure's with-meta and meta to Fluree's Flake custom type. This was done as there was a need to store internal metadata about a Flake (specifically, originating reasoner rules that generated the Flake).

(with-meta flake {:some "metadata"})

Along with the complementary function: (meta flake)

While we already have the .-m Flake field, which we call meta, it is used in the equality comparisons (as intended). Therefore an otherwise identical flake but with different metadata will be deemed as different.

But this means we don't have a place to store metadata that is not part of equality. That would involve one of 4 solutions: 1) store this 'non-equality' metadata on a special key on the existing .-m. This would have conflated .-m, and also comparators would have to perform a dissoc frequently which I was concerned could hurt comparator performance 2) Add a new field on the Flake and implement custom functions 3) Add a new field on the Flake and implement the existing Clojure protocols that add more basic support for with-meta and meta 4) Move to defrecord instead of deftype for Flakes, which would automatically support with-meta, meta

I opted for #3. This will at a small amount of memory overhead per Flake, but will not require extra comparator logic.

Now that we have this, our current .-m field ideally would have been called something other than "meta", but not sure that is worth a more invasive change at the moment.