Ejemplo n.º 1
0
        /// <summary>
        /// NOTE: Code-generation-invoked method, method name and parameter order matters
        /// </summary>
        /// <param name="objectMayArray">value</param>
        /// <param name="granularity">granularity</param>
        /// <param name="serializers">serializers</param>
        /// <returns>hash</returns>
        public static int SerializeAndCRC32Hash(
            object objectMayArray,
            int granularity,
            Serializer[] serializers)
        {
            byte[] bytes;
            try {
                if (objectMayArray is object[]) {
                    bytes = SerializerFactory.Serialize(serializers, (object[]) objectMayArray);
                }
                else {
                    bytes = SerializerFactory.Serialize(serializers[0], objectMayArray);
                }
            }
            catch (IOException e) {
                Log.Error("Exception serializing parameters for computing consistent hash: " + e.Message, e);
                bytes = new byte[0];
            }

            long value = ByteExtensions.GetCrc32(bytes);
            int result = (int) value % granularity;
            if (result >= 0) {
                return result;
            }

            return -result;
        }