leekeiabstraction commented on code in PR #117: URL: https://github.com/apache/fluss-rust/pull/117#discussion_r2649683577
########## crates/fluss/src/util/murmur_hash.rs: ########## @@ -0,0 +1,211 @@ +// 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. + +/* This file is based on source code of Apache Flink Project (https://flink.apache.org/), licensed by the Apache + * Software Foundation (ASF) under the Apache License, Version 2.0. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. */ + +pub const MURMUR3_DEFAULT_SEED: i32 = 0; +pub const FLINK_MURMUR3_DEFAULT_SEED: i32 = 42; + +const C1: i32 = 0xCC9E_2D51_u32 as i32; +const C2: i32 = 0x1B87_3593; +const R1: u32 = 15; +const R2: u32 = 13; +const M: i32 = 5; +const N: i32 = 0xE654_6B64_u32 as i32; +const CHUNK_SIZE: usize = 4; + +/// Hashes the data using 32-bit Murmur3 hash with 0 as seed +/// +/// # Arguments +/// * `data` - byte array containing data to be hashed +/// +/// # Returns +/// Returns hash value +pub fn hash_bytes(data: &[u8]) -> i32 { + hash_bytes_with_seed(data, MURMUR3_DEFAULT_SEED) +} + +#[inline(always)] +fn hash_bytes_with_seed(data: &[u8], seed: i32) -> i32 { + let length = data.len(); + let chunks = length / CHUNK_SIZE; + let length_aligned = chunks * CHUNK_SIZE; + + let mut h1 = hash_full_chunks(data, seed, length_aligned); + let mut k1 = 0i32; + + for (shift, &b) in data[length_aligned..].iter().enumerate() { + k1 |= (b as i32) << (8 * shift); + } + + h1 ^= k1.wrapping_mul(C1).rotate_left(R1).wrapping_mul(C2); + + fmix(h1, length) Review Comment: Updated the standard (non-fluss/flink) `hash_bytes/with_seed` to return u32. -- 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]
