ipfs / go-datastore

key-value datastore interfaces
MIT License
230 stars 65 forks source link

feat: add Features + datastore scoping #188

Closed guseggert closed 2 years ago

guseggert commented 2 years ago

This is ripped out of https://github.com/guseggert/go-ds-grpc, as I realized it solves a more general problem.

Commit

The motivation for this is to enable "dispatching" datastores that dynamically implement the type of the datastore they are dispatching to, so that type assertions behave equivalently on the dispatcher as on the dispatchee. We also want this to be backwards-compatible with existing code using type assertions.

At a high level, this works by generating a concrete implementation of every possible combination of "features", and then picking the right implementation at runtime. This is necessary due to language constraints in Go--it is currently impossible to create a concrete type dynamically with reflection that implements an interface.

"Features" are introduced here, which are supplemental, optional interfaces that datastores may implement. These are backwards-compatible with existing "features", which are:

New features can also be added in a backwards-compatible way. E.g. if datastore A is scoped down to datastore B, a new feature F is added, and then implemented on B, then A will continue to implement the same set of features since it hasn't implemented F yet (and vice versa if F is implemented on A but not B).

Examples of things this enables:

Some related issues:

guseggert commented 2 years ago

@aschmahmann pointed out yesterday that we could also use this, along with a new method on mount datastores to return a scoped datastore for a given key/key prefix/query, which effectively solves the problem with mixing multiple datastore types together with a mount datastore.

Jorropo commented 2 years ago

Minor changes should be needed.

I don't like routines that try to make up with degraded state silently.

If something is not happening as it should, fail loudly, so I (the programmer) can learn about it and fix it.