redis ZRANGEBYLEX and ipv6

462 Views Asked by At

To follow up on this post: redis store 128 bit number

How to do a lexicographical search with an ipv6 address.

If I use it like this:

ZADD index 0 8000::/24
ZADD index 0 2001:db8:1f89::/48
ZADD index 0 2001:db9:1f89::/48
ZADD index 0 1234:5678:abcd:effa:4200:1972::/112

ZRANGEBYLEX index [2001:0db8:0000:85a3:0000:0000:ac1f + LIMIT 0 1
1) "2001:db8:1f89::/48"

But:

ZRANGEBYLEX index [2001:1db8:0000:85a3:0000:0000:ac1f + LIMIT 0 1
1) "2001:db8:1f89::/48"

An idea ?

1

There are 1 best solutions below

0
On

Convert CIDR to IP range and add to ZSET start IP. Before adding IP fill its numbers by zero! Example: 2001:db8:cafe:1:0:0:0:100 -> 2001:0db8:cafe:0001:0000:0000:0000:0100.

And use "ZREVRANGEBYLEX key [2001:0db8:cafe:0001:0000:0000:0000:0200 - LIMIT 0 1" to get start IP of a range.