当前位置: 首页>>代码示例>>Scala>>正文


Scala Hashing类代码示例

本文整理汇总了Scala中com.google.common.hash.Hashing的典型用法代码示例。如果您正苦于以下问题:Scala Hashing类的具体用法?Scala Hashing怎么用?Scala Hashing使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Hashing类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: MurmurHash3Benchmark

//设置package包名称以及导入依赖的类
package bloomfilter.hashing

import java.nio.ByteBuffer

import sandbox.hashing.{YonikMurmurHash3, CassandraMurmurHash, AlgebirdMurmurHash128}
import sandbox.hashing.YonikMurmurHash3.LongPair
import com.yahoo.sketches.hash.{MurmurHash3 => yMurmurHash3}
import com.google.common.hash.Hashing
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}
import scala.util.hashing.{MurmurHash3 => ScalaMurmurHash3}
import com.clearspring.analytics.hash.{MurmurHash => StreamLibMurmurHash}

@State(Scope.Benchmark)
class MurmurHash3Benchmark {

  val key = Range(0, 64).map(_.toByte).toArray

  @Benchmark
  def javaVersion() = {
    YonikMurmurHash3.murmurhash3_x64_128(key, 0, key.length, 0, new LongPair)
  }

  @Benchmark
  def scalaVersion() = {
    MurmurHash3Generic.murmurhash3_x64_128(key, 0, key.length, 0)
  }

  val guavaMurmur = Hashing.murmur3_128()

  @Benchmark
  def guavaVersion() = {
    guavaMurmur.hashBytes(key, 0, key.length)
  }

  @Benchmark
  def cassandraVersion() = {
    CassandraMurmurHash.hash3_x64_128(ByteBuffer.wrap(key), 0, key.length, 0)
  }

  val algebirdMurmur = AlgebirdMurmurHash128(0)

  @Benchmark
  def algebirdVersion() = {
    algebirdMurmur.apply(key)
  }

  @Benchmark
  def yahooVersion() = {
    yMurmurHash3.hash(key, 0)
  }

  @Benchmark
  def scalaStdlibVersion() = {
    ScalaMurmurHash3.arrayHash(key, 0)
  }

  @Benchmark
  def streamLibVersion() = {
    StreamLibMurmurHash.hash(key)
  }
} 
开发者ID:alexandrnikitin,项目名称:bloom-filter-scala,代码行数:62,代码来源:MurmurHash3Benchmark.scala

示例2: read

//设置package包名称以及导入依赖的类
package services

import java.nio.charset.StandardCharsets
import javax.inject.Inject

import com.google.common.hash.Hashing
import com.google.inject.ImplementedBy
import daos.LinkDao
import models.Link


@ImplementedBy(classOf[LinkServiceImpl])
trait LinkService {
  def read(shortUrl: String): Option[Link]
  def read(id: Long): Option[Link]
  def incrementViews(link: Link): Link
  def save(link: Link): Link
  def simplify(link: Link): Link
  def checkOrSimplify(link: Link): Link
}

class LinkServiceImpl @Inject()(linkDao: LinkDao) extends LinkService {

  def read(shortUrl: String): Option[Link] =
    Option.apply(linkDao.read(shortUrl))

  def read(id: Long): Option[Link] =
    Option.apply(linkDao.read(id))

  def incrementViews(link: Link): Link = {
    val updatedLink = Link(link.id, link.url, link.shortUrl, link.views + 1)
    linkDao.update(updatedLink)
  }

  def save(link: Link): Link = {
    val simplified: Link = if (link.shortUrl == null) simplify(link) else checkOrSimplify(link)
    val linkFromDb: Option[Link] = read(simplified.shortUrl)
    if (linkFromDb.nonEmpty) linkFromDb.get
    else linkDao.save(simplified)
  }

  def simplify(link: Link): Link = {
    require(link.url != null)
    val builder = new StringBuilder
    builder append Hashing.murmur3_32().hashString(link.url, StandardCharsets.UTF_8).toString
    Link(link.id, link.url, builder.toString, link.views)
  }

  def checkOrSimplify(link: Link): Link = {
    if (linkDao.read(link.shortUrl) != null) simplify(link)
    else link
  }
} 
开发者ID:YashchenkoN,项目名称:simp.ly,代码行数:54,代码来源:LinkService.scala


注:本文中的com.google.common.hash.Hashing类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。