* @param {Buffer} value * @return {Murmur3Token}
(value)
| 136 | * @return {Murmur3Token} |
| 137 | */ |
| 138 | hash(value) { |
| 139 | // This is an adapted version of the MurmurHash.hash3_x64_128 from Cassandra used |
| 140 | // for M3P. Compared to that methods, there's a few inlining of arguments and we |
| 141 | // only return the first 64-bits of the result since that's all M3 partitioner uses. |
| 142 | |
| 143 | const data = value; |
| 144 | let offset = 0; |
| 145 | const length = data.length; |
| 146 | |
| 147 | const nblocks = length >> 4; // Process as 128-bit blocks. |
| 148 | |
| 149 | const h1 = new MutableLong(); |
| 150 | const h2 = new MutableLong(); |
| 151 | let k1 = new MutableLong(); |
| 152 | let k2 = new MutableLong(); |
| 153 | |
| 154 | for (let i = 0; i < nblocks; i++) { |
| 155 | k1 = this.getBlock(data, offset, i * 2); |
| 156 | k2 = this.getBlock(data, offset, i * 2 + 1); |
| 157 | |
| 158 | k1.multiply(mconst1); |
| 159 | this.rotl64(k1, 31); |
| 160 | k1.multiply(mconst2); |
| 161 | |
| 162 | h1.xor(k1); |
| 163 | this.rotl64(h1, 27); |
| 164 | h1.add(h2); |
| 165 | h1.multiply(mlongFive).add(mconst5); |
| 166 | |
| 167 | k2.multiply(mconst2); |
| 168 | this.rotl64(k2, 33); |
| 169 | k2.multiply(mconst1); |
| 170 | h2.xor(k2); |
| 171 | this.rotl64(h2, 31); |
| 172 | h2.add(h1); |
| 173 | h2.multiply(mlongFive).add(mconst6); |
| 174 | } |
| 175 | //---------- |
| 176 | // tail |
| 177 | |
| 178 | // Advance offset to the unprocessed tail of the data. |
| 179 | offset += nblocks * 16; |
| 180 | |
| 181 | k1 = new MutableLong(); |
| 182 | k2 = new MutableLong(); |
| 183 | |
| 184 | /* eslint-disable no-fallthrough */ |
| 185 | switch(length & 15) { |
| 186 | case 15: |
| 187 | k2.xor(fromSignedByte(data[offset+14]).shiftLeft(48)); |
| 188 | case 14: |
| 189 | k2.xor(fromSignedByte(data[offset+13]).shiftLeft(40)); |
| 190 | case 13: |
| 191 | k2.xor(fromSignedByte(data[offset+12]).shiftLeft(32)); |
| 192 | case 12: |
| 193 | k2.xor(fromSignedByte(data[offset+11]).shiftLeft(24)); |
| 194 | case 11: |
| 195 | k2.xor(fromSignedByte(data[offset+10]).shiftLeft(16)); |
nothing calls this directly
no test coverage detected