AMashenkov commented on a change in pull request #721: URL: https://github.com/apache/ignite-3/pull/721#discussion_r826122918
########## File path: modules/core/src/main/java/org/apache/ignite/internal/util/HashCalculator.java ########## @@ -0,0 +1,259 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.util; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.BitSet; +import java.util.UUID; +import org.apache.ignite.lang.IgniteInternalException; + +/** + * Implement hash calculator. + */ +public class HashCalculator { + int hash = 0; + + /** + * Append value to hash calculation. + * + * @param v Value to update hash. + */ + public void append(Object v) { + if (v == null) { + appendLong(0); + return; + } + + if (v.getClass() == Byte.class) { + appendByte((byte) v); + } else if (v.getClass() == Short.class) { + appendShort((short) v); + } else if (v.getClass() == Integer.class) { + appendInt((int) v); + } else if (v.getClass() == Long.class) { + appendLong((long) v); + } else if (v.getClass() == Float.class) { + appendFloat((float) v); + } else if (v.getClass() == Double.class) { + appendDouble((Double) v); + } else if (v.getClass() == BigInteger.class) { + appendNumber((BigInteger) v); + } else if (v.getClass() == BigDecimal.class) { + appendDecimal((BigDecimal) v); + } else if (v.getClass() == UUID.class) { + appendUuid((UUID) v); + } else if (v.getClass() == LocalDate.class) { + appendDate((LocalDate) v); + } else if (v.getClass() == LocalTime.class) { + appendTime((LocalTime) v); + } else if (v.getClass() == LocalDateTime.class) { + appendDateTime((LocalDateTime) v); + } else if (v.getClass() == Instant.class) { + appendTimestamp((Instant) v); + } else if (v.getClass() == byte[].class) { + appendBytes((byte[]) v); + } else if (v.getClass() == String.class) { + appendString((String) v); + } else if (v.getClass() == BitSet.class) { + appendBitmask((BitSet) v); + } else { + throw new IgniteInternalException("Unsupported value type: [cls=" + v.getClass() + ']'); + } + } + + /** + * Append byte to hash calculation. + * + * @param v Value to update hash. + */ + public void appendByte(byte v) { + appendLong(v); + } + + /** + * Append short to hash calculation. + * + * @param v Value to update hash. + */ + public void appendShort(short v) { + appendLong(v); + } + + /** + * Append integer to hash calculation. + * + * @param v Value to update hash. + */ + public void appendInt(int v) { + appendLong(v); + } + + /** + * Append long to hash calculation. + * + * @param v Value to update hash. + */ + public void appendLong(long v) { + ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); Review comment: Let's avoid allocations and reuse the buffer. You can see how DigestBase JDK implementation of MessageDigest interface do this. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
