kv: support computation of index field#228
Conversation
fe6e39f to
a3d6cbf
Compare
nrc
left a comment
There was a problem hiding this comment.
Looks good, thanks! Couple of questions inline
| /// computed by the specified closure, which is expected to return | ||
| /// `impl IntoIterator<Item = I>` (where `I` is the index key type). Most commonly, this | ||
| /// will likely be `Option`: you might want to return `None` in some cases if the value has | ||
| /// an optional field or an index key can't be produced. |
There was a problem hiding this comment.
Is the use of IntoIterator future-proofing for multiple secondary keys per primary key (which I hadn't thought of, but I guess might be useful as well as multiple primary keys per secondary)? If not, then could we use Option rather than IntoIterator?
There was a problem hiding this comment.
The salient case in Peers is that there's an index on both the tailnet IPv4 and IPv6, which are both represented naturally as just IpAddr and are disjoint, so they can live in the same index. It's not like it's terribly onerous to just split them and use Option — if you'd prefer that I'm fine with it until we see a compelling need for a large or variable number of provably unique index keys
There was a problem hiding this comment.
So there's one index IpAddr -> Peer where there might be 0, 1, or 2 IpAddrs per peer?
There was a problem hiding this comment.
It's always 2 in this case
There was a problem hiding this comment.
We discussed this in a meeting last week. The use case is that the index is IpAddr -> Peer where each Peer has two IpAddr fields (4 and 6). The use of iterator supports this (or arbitrary numbers of foreign keys). Option is used for the 0 or 1 case and is simply converted into an iterator. In the future we want to support a foreign key mapping to multiple values as well as multiple keys mapping to one value, so we might need to revisit this design as part of that (for the peer use case, having separate indexes for ipv4 and 6 would also work but would be a bit less optimal).
39b98e6 to
0a2f293
Compare
0a2f293 to
df131ff
Compare
Signed-off-by: Nathan Perry <nathan@tailscale.com> Change-Id: I2f06ff801e32df800336175bc31d7b956a6a6964
df131ff to
091fd97
Compare
Allow specifying a closure to compute indices for a given value. By default, it just returns
Some(value.clone())(the current behavior), but you can returnNoneif the value doesn't have a key for that index (such asNode::disco_key, which is optional) or multiple values if they exist and you know they're unique (such as the peer's tailnet ipv4 and ipv6, which are necessarily disjoint).