FilterCode v2
Filter encoding version 1 uses a randomly generated UUID with -
removed:
private String generateShortNameV1() {
for (int i = 0; i < 10; i++) {
final String code = "v1" + UUID.randomUUID().toString().replace("-", "");
// return shortName if unique
if (shortFilterRepository.findByCode(code) == null) {
return code;
}
}
throw new RuntimeException("Failed to generate a unique filters short name in several attempts");
}
This generates filter codes like v1a37ad370d9ca4f62b3d9840094aafae9
and they are fairly long.
Shorter filter codes
We can make use of https://hashids.org/java/ to generate shorter codes with a custom salt!
Initial idea is to use the current timestamp (long value, 4b), resulting in drastically smaller unique short filter codes.
private String generateShortNameV2() {
Hashids hashids = new Hashids("this is my salt");
for (int i = 0; i < 10; i++) {
String hash = hashids.encode(System.currentTimeMillis());
final String code = "v2" + hash;
// return shortName if unique
if (shortFilterRepository.findByCode(code) == null) {
return code;
}
}
throw new RuntimeException("Failed to generate a unique filters short name in several attempts");
}
We could extend this to our use of UUIDs in URLs, and see if that makes a difference in URL code
length.