-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18d64e4
commit 22da5e5
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/scala/com/github/mogproject/redismock/util/MurmurHash.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.mogproject.redismock.util | ||
|
||
import com.github.mogproject.redismock.util.Implicits._ | ||
|
||
|
||
object MurmurHash { | ||
|
||
private val defaultSeed = 0xadc83b19L | ||
|
||
def murmurHash64A(data: Seq[Byte], seed: Long = defaultSeed): Long = { | ||
val m = 0xc6a4a7935bd1e995L | ||
val r = 47 | ||
|
||
val f: Long => Long = m.* | ||
val g: Long => Long = x => x ^ (x >>> r) | ||
|
||
val h = data.grouped(8).foldLeft(seed ^ f(data.length)) { case (y, xs) => | ||
val k = xs.foldRight(0L)((b, x) => (x << 8) + b.toUnsignedLong) | ||
val j: Long => Long = if (xs.length == 8) f compose g compose f else identity | ||
f(y ^ j(k)) | ||
} | ||
(g compose f compose g)(h) | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/test/scala/com/github/mogproject/redismock/util/MurmurHashSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.github.mogproject.redismock.util | ||
|
||
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Matchers, FunSpec} | ||
|
||
|
||
class MurmurHashSpec extends FunSpec with Matchers with BeforeAndAfterEach with BeforeAndAfterAll { | ||
describe("HashUtil#murmurHash64A") { | ||
it("should return 64 bit value") { | ||
MurmurHash.murmurHash64A("A".getBytes) shouldBe 0xfc089b66b14af040L | ||
MurmurHash.murmurHash64A("AB".getBytes) shouldBe 0x24fb508dc42efb7fL | ||
MurmurHash.murmurHash64A("B".getBytes) shouldBe 0x130dd9603dcd32a4L | ||
MurmurHash.murmurHash64A("BA".getBytes) shouldBe 0x25cbc673105b365dL | ||
|
||
MurmurHash.murmurHash64A("".getBytes) shouldBe 0xd8dfea6585bc9732L | ||
MurmurHash.murmurHash64A("ABCDEFGHabcdefg".getBytes) shouldBe 0x3dfb8e09231eeb32L | ||
MurmurHash.murmurHash64A("ABCDEFGHabcdefgh".getBytes) shouldBe 0x40055074d92b389fL | ||
MurmurHash.murmurHash64A("ABCDEFGHabcdefghi".getBytes) shouldBe 0xd298ffe6aa77babeL | ||
|
||
MurmurHash.murmurHash64A(Seq.fill[Byte](100)(0)) shouldBe 0xc9450ffd5a7bf9ccL | ||
MurmurHash.murmurHash64A(Seq.fill[Byte](100)(-1)) shouldBe 0x78be0c4f11cdc6d5L | ||
} | ||
} | ||
} |