[erlang-questions] Selecting records from Mnesia using partial key

Mikael Pettersson mikpelinux@REDACTED
Sun Jul 8 16:45:59 CEST 2018


On Fri, Jul 6, 2018 at 1:34 PM, Frans Schneider <fchschneider@REDACTED> wrote:
> Dear list,
>
> Before trying to reinvent the wheel, maybe somebody can point me in the
> right direction.
>
> I have a request with a variable set of attributes [A, B, C...] and a Mnesia
> table with policies, each applicable for a also variable set of attributes
> [X, Y, Z...]. Policies may be applicable for the same set of attributes, in
> which case all these policies have to be evaluated.
>
> A policy should be selected if its attributes is the set _or a subset_ of
> attributes from the request: i.e. if the request has the set of attributes
> [A, B] than all policies with attributes [A, B], [A] and [B] should be
> selected.
>
> Attributes currently are records holding two strings but could be turned
> into atoms.
>
> Does anybody know a nice way to implement this in Mnesia?

Without further information about or restrictions to your problem, I guess the
simplest solution is to use a qlc to query the policies table, and in the qlc
filter by available attributes.

This is effectively going to traverse the entire table, so it won't be
very efficient,
but it may be enough for your case (we don't know your table size, number of
attributes, etc).

If you want something more efficient, you'll need one or more indices, i.e.
mappings from attributes (or sets of attributes) to applicable policies.  Since
your input may contain a superset of a policy's attributes, you'll have to
create it/them yourself rather than relying on mnesia's built-in
indexing support.



More information about the erlang-questions mailing list