On 2022-10-29T18:38:19 +0700
Andreas Reichel <
and...@manticore-projects.com> wrote:
>
> Well, it depends what you want.
> But your MAP key should be corresponding with your Table PRIMARY KEY.
>
> If you have a compound Primary Key, then your Map also must have a
> compound Key (not just an Integer) and you would likely need to build a
> Key Class implementing Comparable, Equals and Hash.
I understand that, I think we may have gone down the wrong fork in the
road. :)
My question is about being able to declare one or more maps in a way
that will permit efficient (ie, not O(N)) to a subset of the keys in
a map.
Let's assume then that I have this as a composite primary key:
record GroupMember(int group, int member);
And I have a map:
MVMap<GroupMember, GroupMember> m;
This matches the definition of the `t` table I posted in the original
email; the primary key and the row are the same value.
How, then, do I efficiently ask for all rows that have a given group
value g?
I would have to do something like this:
var members = new ArrayList<Integer>();
for (var entry : m.entrySet()) {
if (entry.getKey().group() == g) {
members.add(entry.getKey().member());
}
}
return members;
This works, but is O(N) in the size of the map, whereas it _could_ be
O(1) (assuming that map access are O(1)) if I could declare the map
as being something analogous to:
MVMap<Integer, Set<Integer>> m;
I assume that I'm _not_ supposed to do this, given that MVStore doesn't
come with DataType definitions for any collection types.
Now obviously H2 is using MVStore internally for database tables, and
there it's obviously possible to have something like:
create table t (group integer not null, member integer not null);
create index t_groups on t (group);
... and then have "SELECT * FROM t where t.group = ?" be an indexed
scan instead of a seq scan over the entire t table... But what exactly
would an equivalent MVStore configuration look like?