All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.zalando.sprocwrapper.sharding.VirtualShardMd5Strategy Maven / Gradle / Ivy

Go to download

Library to make PostgreSQL stored procedures available through simple Java "*SProcService" interfaces including automatic object serialization and deserialization (using typemapper and convention-over-configuration). Supports sharding, advisory locking, statement timeouts and PostgreSQL types such as enums and hstore.

There is a newer version: 2.0.0
Show newest version
package de.zalando.sprocwrapper.sharding;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.List;

/**
 * extract the last 3 bytes of the shard key's MD5 hash: the shard key must be either a string or a string list (the
 * first list item is used in this case)
 *
 * @author  jmussler
 * @author  hjacobs
 */
public class VirtualShardMd5Strategy extends VirtualShardKeyStrategy {
    @Override
    public int getShardId(final Object[] objs) {
        if (objs == null || objs.length == 0) {
            return 0;
        }

        String input = null;

        if (objs[0] == null) {
            return 0;
        }

        if (objs[0] instanceof List) {
            List stringList = (List) objs[0];
            if (stringList.isEmpty()) {
                return 0;
            }

            input = stringList.get(0);
        } else {
            input = (String) objs[0];
        }

        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException nsae) {
            throw new RuntimeException("Unable to use md5 algorithm", nsae);
        }

        final byte[] md5 = digest.digest(input.getBytes());
        return (md5[15] & 0xff) + ((md5[14] & 0xff) << 8) + ((md5[13] & 0xff) << 16);

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy