CodeSOD: Duplication

This post was originally published on this site

The Daily WTF

NoSQL databases frequently are designed to shard or partition across many nodes. That, of course, makes enforcing unique IDs different than you might do in a SQL database. You can’t efficiently have an autoincrement sequence, and instead have to have something like a UUID.

But if you’ve designed your NoSQL database badly, or your input data isn’t well sanitized, you might find yourself in a situation where you can’t guarantee uniqueness without validating every row. That’s a bad place to be, but it’s probably how the code Remco found started its life.

The purpose of this Java code is to query all the customer IDs from a database and ensure that they’re fully unique.

private Completable validateUniqueCustomerIds(List<Container> rootContainers) { if (!validateUniqueIds) { log.trace(“validateUniqueCustomerIds validateUniqueIds == false -> skipping validation”); return Completable.complete(); } // use Flowable.share() to make sure we only call the repository once. final Flowable<String> nonEmptyCustomerIds = someMongoRepository.getCustomerIds()

To read the full article click on the 'post' link at the top.